From 28661dd9f1c5d9e7e40c3769207c8efa943f87ff Mon Sep 17 00:00:00 2001 From: 4VRDriver <44267643+4VRDriver@users.noreply.github.com> Date: Tue, 1 Sep 2020 01:18:48 +0200 Subject: [PATCH] Triangle with colors --- Controller.cpp | 39 ++++++++++++++++----------------------- Controller.h | 13 ++++++++++++- VertexBuffer.cpp | 6 ++++-- res/shaders/old.fs | 7 ------- res/shaders/old.vs | 7 ------- 5 files changed, 32 insertions(+), 40 deletions(-) delete mode 100644 res/shaders/old.fs delete mode 100644 res/shaders/old.vs diff --git a/Controller.cpp b/Controller.cpp index 1e6e235..85bb71c 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -10,9 +10,6 @@ #endif #include "Controller.h" -#include "ShaderProgram.h" -#include "VertexBuffer.h" -#include "defines.h" Controller::Controller() { if(!glfwInit()) exit(-1); @@ -34,39 +31,34 @@ Controller::~Controller() { } void Controller::run() { - glClearColor(0.241f, 0.578f, 0.308f, 1.0f); + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); - ShaderProgram shaderProgram("res/shaders/old.vs", "res/shaders/old.fs"); - shaderProgram.bind(); + shaderProgram = new ShaderProgram("res/shaders/basic.vs", "res/shaders/basic.fs"); + shaderProgram->bind(); - Vertex vertices[] = { - Vertex{-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, - Vertex{0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, - Vertex{0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f} - }; uint32_t numVertices = sizeof(vertices) / sizeof(Vertex); - VertexBuffer vertexBuffer(vertices, numVertices); - vertexBuffer.unbind(); + vertexBuffer = new VertexBuffer(vertices, numVertices); // This is the game loop while(!glfwWindowShouldClose(gameWindow->getGLFWwindow())) { // Timing limit_framerate(); - static int slowdown = 0; - if(slowdown++ == 60) { + static int frameCount = 55; + if(frameCount++ == 60) { std::cout << "FPS: " << 1/deltaTime << std::endl; - slowdown = 0; + frameCount = 0; } // Update game + // ... // Render and buffer swap glClear(GL_COLOR_BUFFER_BIT); - vertexBuffer.bind(); + vertexBuffer->bind(); glDrawArrays(GL_TRIANGLES, 0, 3); - vertexBuffer.unbind(); + vertexBuffer->unbind(); glfwSwapBuffers(gameWindow->getGLFWwindow()); @@ -76,11 +68,6 @@ void Controller::run() { } -void Controller::error_callback(int error, const char* description) { - (void)error; - fprintf(stderr, "Error: %s\n", description); -} - void Controller::limit_framerate() { static double startingTime = 0.0; static double lastTime = 0.0; @@ -101,3 +88,9 @@ void Controller::limit_framerate() { startingTime = glfwGetTime(); } + +// GLFW error function +void Controller::error_callback(int error, const char* description) { + (void)error; + fprintf(stderr, "Error: %s\n", description); +} diff --git a/Controller.h b/Controller.h index 525083f..ef3a889 100644 --- a/Controller.h +++ b/Controller.h @@ -4,6 +4,9 @@ #include "Window.h" #include "EventHandler.h" +#include "ShaderProgram.h" +#include "VertexBuffer.h" +#include "defines.h" class Controller { @@ -23,7 +26,15 @@ private: EventHandler *gameEventHandler; const uint16_t MAX_FPS = 60; - double deltaTime; + ShaderProgram *shaderProgram; + VertexBuffer *vertexBuffer; + + Vertex vertices[3] = { + Vertex{-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f}, + Vertex{0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f}, + Vertex{0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f} + }; + }; diff --git a/VertexBuffer.cpp b/VertexBuffer.cpp index bb35623..1fe0ed8 100644 --- a/VertexBuffer.cpp +++ b/VertexBuffer.cpp @@ -4,18 +4,20 @@ #include "defines.h" VertexBuffer::VertexBuffer(void* data, uint32_t numVertices) { + glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); - + glGenBuffers(1, &bufferId); glBindBuffer(GL_ARRAY_BUFFER, bufferId); glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(Vertex), data, GL_STATIC_DRAW); - + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, x)); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, r)); + // This will also unbind the vertex buffer glBindVertexArray(0); } diff --git a/res/shaders/old.fs b/res/shaders/old.fs deleted file mode 100644 index 59085e3..0000000 --- a/res/shaders/old.fs +++ /dev/null @@ -1,7 +0,0 @@ -#version 330 core - -out vec4 FragColor; - -void main() { - FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); -} \ No newline at end of file diff --git a/res/shaders/old.vs b/res/shaders/old.vs deleted file mode 100644 index a50d935..0000000 --- a/res/shaders/old.vs +++ /dev/null @@ -1,7 +0,0 @@ -#version 330 core - -layout(location = 0) in vec3 aPos; - -void main() { - gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); -} \ No newline at end of file