diff --git a/framework/include/util.h b/framework/include/util.h index 86a3078..b534058 100644 --- a/framework/include/util.h +++ b/framework/include/util.h @@ -7,8 +7,14 @@ #include #include +#include namespace util { + template + void bufferData(GLenum target, std::vector data, GLenum usage) { + glBufferData(target, data.size() * sizeof(T), &data.front(), usage); + } + void shaderFile(GLuint shader, const std::string &path) { const std::string str = readFile(path); const char *c_str = str.c_str(); diff --git a/glapp/shaders/main.frag b/glapp/shaders/main.frag index 6271a69..ee7c459 100644 --- a/glapp/shaders/main.frag +++ b/glapp/shaders/main.frag @@ -1,6 +1,6 @@ #version 330 -in vec3 color; +smooth in vec3 color; out vec4 fcolor; diff --git a/glapp/shaders/main.vert b/glapp/shaders/main.vert index 11c0ab5..e53c687 100644 --- a/glapp/shaders/main.vert +++ b/glapp/shaders/main.vert @@ -5,7 +5,7 @@ uniform mat4 pvm; in vec3 vCol; in vec2 vPos; -out vec3 color; +smooth out vec3 color; void main() { gl_Position = pvm * vec4(vPos, 0, 1); diff --git a/glapp/src/main.cpp b/glapp/src/main.cpp index 9f30b0b..45b6f97 100644 --- a/glapp/src/main.cpp +++ b/glapp/src/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -19,12 +20,7 @@ struct Vertex { class GLApp : public App { private: - std::vector vertices = { - {glm::vec2(+0.5f, +0.5f), glm::vec3(1, 0, 0)}, - {glm::vec2(+0.5f, -0.5f), glm::vec3(0, 1, 0)}, - {glm::vec2(-0.5f, -0.5f), glm::vec3(0, 0, 1)}, - {glm::vec2(-0.5f, +0.5f), glm::vec3(1, 1, 1)}, - }; + std::vector vertices; GLuint vertex_buffer = 0; GLuint vertex_shader = 0, fragment_shader = 0, program = 0; @@ -37,11 +33,29 @@ protected: } void init() override { + vertices.push_back({ + glm::vec2(0), glm::vec3(1) + }); + + const int N = 16; + const float TAU = 6.28318f; + + for (int i = 0; i <= N; ++i) { + float t = (float) i / N; + + auto p = glm::vec2(t) + glm::vec2(0, 1) / 4.f; + auto c = glm::vec3(t) + glm::vec3(0, 1, 2) / 3.f; + + vertices.push_back({ + glm::cos(TAU * p), glm::cos(TAU * c) + }); + } + setTitle("Hello Square!"); glGenBuffers(1, &vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); - glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices.front(), GL_STATIC_DRAW); + util::bufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW); program = util::buildProgram({ vertex_shader = util::buildShader("shaders/main.vert"), @@ -52,11 +66,13 @@ protected: vpos_location = (GLuint) glGetAttribLocation(program, "vPos"); vcol_location = (GLuint) glGetAttribLocation(program, "vCol"); + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glEnableVertexAttribArray(vpos_location); glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void *) nullptr); - glEnableVertexAttribArray(vcol_location); glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void *) (sizeof(float) * 2)); + + glBindBuffer(GL_ARRAY_BUFFER, 0); } void display() override { @@ -69,7 +85,6 @@ protected: glClear(GL_COLOR_BUFFER_BIT); - auto pvm = glm::ortho(-ratio, ratio, -1.f, 1.f); pvm = glm::rotate(pvm, (float) glfwGetTime(), glm::vec3(0.f, 0.f, 1.f)); pvm = glm::scale(pvm, glm::vec3(0.9f)); @@ -77,7 +92,7 @@ protected: glUseProgram(program); glUniformMatrix4fv(pvm_location, 1, GL_FALSE, glm::value_ptr(pvm)); - glDrawArrays(GL_QUADS, 0, (GLsizei) vertices.size()); + glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) vertices.size()); swapBuffers(); }