diff --git a/include/VertexBuffer.hpp b/include/VertexBuffer.hpp index bee5f98..6a717e8 100644 --- a/include/VertexBuffer.hpp +++ b/include/VertexBuffer.hpp @@ -12,8 +12,12 @@ struct VertexBuffer { glBindBuffer(GL_ARRAY_BUFFER, bufferId); glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(Vertex), data, GL_STATIC_DRAW); + // POSITION glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), offsetof(struct Vertex,x)); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,x)); + // COLOR + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex,r)); glBindVertexArray(0); }; diff --git a/include/defines.hpp b/include/defines.hpp index 4b364ba..b6be7dc 100644 --- a/include/defines.hpp +++ b/include/defines.hpp @@ -15,5 +15,6 @@ typedef float float32; typedef double float64; struct Vertex { - float32 x, y, z; + float32 x, y, z; // position + float32 r, g, b, a; // color }; \ No newline at end of file diff --git a/shaders/README.md b/shaders/README.md index f6e2d6c..32a4ae4 100644 --- a/shaders/README.md +++ b/shaders/README.md @@ -1,4 +1,5 @@ # Attributes |Index|Description| |---|---| -|0|Position| \ No newline at end of file +|0|Position| +|1|Color| \ No newline at end of file diff --git a/shaders/basic.frag b/shaders/basic.frag index 998d91e..3957c96 100644 --- a/shaders/basic.frag +++ b/shaders/basic.frag @@ -1,7 +1,9 @@ #version 330 core -layout(location = 0) out vec4 color; +layout(location = 0) out vec4 f_color; + +in vec4 v_color; void main() { - color = vec4(1.0f, 1.0f, 0.0f, 1.0f); + f_color = v_color; } \ No newline at end of file diff --git a/shaders/basic.vert b/shaders/basic.vert index 2649cde..c63154f 100644 --- a/shaders/basic.vert +++ b/shaders/basic.vert @@ -1,7 +1,11 @@ #version 330 core -layout(location = 0) in vec3 position; +layout(location = 0) in vec3 a_position; +layout(location = 1) in vec4 a_color; + +out vec4 v_color; void main() { - gl_Position = vec4(position, 1.0f); + gl_Position = vec4(a_position, 1.0f); + v_color = a_color; } \ No newline at end of file diff --git a/src/Shader.cpp b/src/Shader.cpp index 96b7a1a..75285e9 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -31,7 +31,7 @@ GLuint Shader::compile(std::string shaderSource, GLenum shaderType) { glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length); char* message = new char[length]; glGetShaderInfoLog(id, length, &length, message); - std::cout << "Shader Compilation Error" << message << std::endl; + std::cout << "Shader Compilation Error [" << "None" << "] " << message << std::endl; delete[] message; return 0; } diff --git a/src/main.cpp b/src/main.cpp index 38b2b4e..abb00af 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,10 +19,12 @@ int main() { SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + uint32 flags = SDL_WINDOW_OPENGL; //| SDL_WINDOW_FULLSCREEN; + SDL_Window* window = SDL_CreateWindow( "Cinecraft", 800, 600, - SDL_WINDOW_OPENGL + flags ); SDL_GLContext context = SDL_GL_CreateContext(window); @@ -35,9 +37,9 @@ int main() { std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl; Vertex vertices[] = { - Vertex{-0.5f, -0.5f, 0.0f}, - Vertex{0.0f, 0.5f, 0.0f}, - Vertex{0.5f, -0.5f, 0.0f}, + Vertex{-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f}, + Vertex{0.0f, 0.5f, 0.0f, 0.0, 1.0f, 0.0f, 1.0f}, + Vertex{0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f}, }; uint32_t numVertices = 3; @@ -47,15 +49,16 @@ int main() { Shader shader("shaders/basic.vert", "shaders/basic.frag"); shader.bind(); + const uint64 perfCounterFrequency = SDL_GetPerformanceFrequency(); + uint64 lastCounter = SDL_GetPerformanceCounter(); + float32 delta = 0.0f; + + // WIREFRAME + //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + bool running = true; SDL_Event e; while (running) { - while (SDL_PollEvent(&e)) { - if (e.type == SDL_EVENT_QUIT) { - running = false; - } - } - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); @@ -65,6 +68,19 @@ int main() { vertexBuffer.unbind(); SDL_GL_SwapWindow(window); + + while (SDL_PollEvent(&e)) { + if (e.type == SDL_EVENT_QUIT) { + running = false; + } + } + + const uint64 endCounter = SDL_GetPerformanceCounter(); + const uint64 counterElapsed = endCounter - lastCounter; + delta = (static_cast(counterElapsed) / static_cast(perfCounterFrequency)); + const auto FPS = static_cast(static_cast(perfCounterFrequency) / static_cast(counterElapsed)); + std::cout << "FPS: " << FPS << std::endl; + lastCounter = endCounter; } SDL_GL_DestroyContext(context);