diff --git a/shaders/shader.frag b/shaders/shader.frag index 1ce7c9f..965830b 100644 --- a/shaders/shader.frag +++ b/shaders/shader.frag @@ -1,6 +1,9 @@ #version 330 core +in vec3 frag_col; out vec4 FragColor; + +uniform vec4 MyColor; void main() { - FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); + FragColor = vec4(frag_col, 1.0f);//vec4(posInWorld.x, posInWorld.y, posInWorld.z, 1.0f); } \ No newline at end of file diff --git a/shaders/shader.vert b/shaders/shader.vert index 5d16282..a287f1b 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -1,6 +1,10 @@ #version 330 core layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 col; +out vec3 frag_col; + void main() { + frag_col = col; gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 02fbf2f..6745967 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,10 @@ #include #include #include +#include +#include + +#define RAND_0_1 ((float)rand()) / RAND_MAX void framebufferSizeCallback(GLFWwindow *window, int width, int height) { glViewport(10, 10, width-10, height-10); @@ -36,11 +40,23 @@ std::string readFile(std::string path) { } content += '\0'; - std::cout << content << std::endl; file.close(); return content; } +void updateVertices(float (&vertices)[24]) { + // if (RAND_0_1 < 0.99f) return; + float a[] = { + -1.0f, 1.0f, 0.0f, RAND_0_1, RAND_0_1, RAND_0_1, // upper left + -1.0f, -1.0f, 0.0f, RAND_0_1, RAND_0_1, RAND_0_1, // bottom left + 1.0f, 1.0f, 0.0f, RAND_0_1, RAND_0_1, RAND_0_1, // upper right + 1.0f, -1.0f, 0.0f, RAND_0_1, RAND_0_1, RAND_0_1 // bottom right + }; + + // vertices = a; + memcpy(vertices, a, sizeof(a)); +} + unsigned int prepareShaders() { unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); @@ -91,6 +107,7 @@ unsigned int prepareShaders() { } int main () { + srand(time(0)); glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); @@ -116,22 +133,34 @@ int main () { unsigned int shaderProgram = prepareShaders(); float vertices[] = { - -0.5, 0.0, 0.0, - 0.5, 0.5, 0.0, - 0.5, -0.5, 0.0 + -1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, // upper left + -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left + 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, // upper right + 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f // bottom right }; - - unsigned int VBO, VAO; + unsigned int indices[] = { + 1, 2, 3, + 1, 2, 0 + }; + unsigned int VBO, VAO, EBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); + glGenBuffers(1, &EBO); + glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); - glBindBuffer(GL_ARRAY_BUFFER, 0); + + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3 * sizeof(float))); + glEnableVertexAttribArray(1); + glBindVertexArray(0); while(!glfwWindowShouldClose(window)) { @@ -141,9 +170,20 @@ int main () { glClear(GL_COLOR_BUFFER_BIT); glUseProgram(shaderProgram); - glBindVertexArray(VAO); - glDrawArrays(GL_TRIANGLES, 0, 3); + // float time = glfwGetTime(); + // float greenValue = sin(time); + + + // int vertexColorLocation = glGetUniformLocation(shaderProgram, "MyColor"); + // glUniform4f(vertexColorLocation, 0, greenValue, 0, 1); + glBindVertexArray(VAO); + + updateVertices(vertices); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + + glBindVertexArray(0); glfwSwapBuffers(window); glfwPollEvents(); } diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp new file mode 100644 index 0000000..45e46a5 --- /dev/null +++ b/src/ui/ui.cpp @@ -0,0 +1,5 @@ +#include "ui.hpp" + +void UI::draw() { + +}; \ No newline at end of file diff --git a/src/ui/ui.hpp b/src/ui/ui.hpp new file mode 100644 index 0000000..23302f0 --- /dev/null +++ b/src/ui/ui.hpp @@ -0,0 +1,4 @@ +class UI { + void draw(); + +}; \ No newline at end of file