diff --git a/Engine/engine/constants.hpp b/Engine/engine/constants.hpp index 4285abf..c40e10f 100644 --- a/Engine/engine/constants.hpp +++ b/Engine/engine/constants.hpp @@ -36,7 +36,7 @@ namespace constants // we write vertex shader // version of glsl (since ogl 3.3 same as ogl so we pick 330) // in this shader we just forward input data to shader output - inline const char *vertexShaderSource { "#version 330 core\n" + inline const char *VERTEX_SHADER_SOURCE { "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" "void main()\n" "{\n" @@ -45,7 +45,7 @@ namespace constants // write fragment shader // we set the color of each pixel to be orange - inline const char *fragmentShaderSource { + inline const char *FRAGMENT_SHADER_SOURCE { "#version 330 core\n" "out vec4 FragColor;\n" "void main()\n" @@ -54,6 +54,15 @@ namespace constants "}\0" }; + inline const char *FRAGMENT_SHADER_SOURCE_YELLOW { + "#version 330 core\n" + "out vec4 FragColor;\n" + "void main()\n" + "{\n" + "FragColor = vec4(1.0f, 1.0f, 0.0f, 1.0f);\n" + "}\0" + }; + // we specify three vertices // each of them with position in 3d space // x y z @@ -65,6 +74,38 @@ namespace constants inline constexpr size_t TRIANGLE_VERTICES_SIZE = { sizeof(TRIANGLE_VERTICES) }; + inline constexpr float TRIANGLES_VERTICES[] { + // first triangle + -0.9f, -0.5f, 0.0f, // left + -0.0f, -0.5f, 0.0f, // right + -0.45f, 0.5f, 0.0f, // top + // second triangle + 0.0f, -0.5f, 0.0f, // left + 0.9f, -0.5f, 0.0f, // right + 0.45f, 0.5f, 0.0f // top + }; + + inline constexpr size_t TRIANGLES_VERTICES_SIZE = { sizeof(TRIANGLES_VERTICES) }; + + inline constexpr float TRIANGLE_ONE[] { + // first triangle + -0.9f, -0.5f, 0.0f, // left + -0.0f, -0.5f, 0.0f, // right + -0.45f, 0.5f, 0.0f, // top + }; + + inline constexpr size_t TRIANGLE_ONE_SIZE = { sizeof(TRIANGLE_ONE) }; + + inline constexpr float TRIANGLE_TWO[] { + // second triangle + 0.0f, -0.5f, 0.0f, // left + 0.9f, -0.5f, 0.0f, // right + 0.45f, 0.5f, 0.0f // top + }; + + inline constexpr size_t TRIANGLE_TWO_SIZE = { sizeof(TRIANGLE_TWO) }; + + // compare with square done with only vertices: /* float vertices[] = { @@ -95,7 +136,7 @@ namespace constants inline constexpr size_t SQUARE_VERTICES_SIZE = { sizeof(SQUARE_VERTICES) }; - inline constexpr int MAX_DRAW_CALL = { 2 }; + inline constexpr int MAX_DRAW_CALL = { 4 }; } diff --git a/Engine/engine/draw.cpp b/Engine/engine/draw.cpp index f81c27f..563aaf9 100644 --- a/Engine/engine/draw.cpp +++ b/Engine/engine/draw.cpp @@ -14,19 +14,26 @@ int drawFigure(const int whatToDraw) switch (whatToDraw) { case 0: - return drawTriangle(); + return drawTriangle(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE); case 1: - return drawSquare(); + return drawSquare(constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE); + case 2: + // Try to draw 2 triangles next to each other using glDrawArrays by adding more vertices to your data. + return drawTriangle(constants::TRIANGLES_VERTICES, constants::TRIANGLES_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE); + case 3: + // Now create the same 2 triangles using two different VAOs and VBOs for their data + return (drawTriangle(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE) == -1 || drawTriangle(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE) == -1); case constants::MAX_DRAW_CALL: - return drawTriangle(); + // Create two shader programs where the second program uses a different fragment shader that outputs the color yellow; draw both triangles again where one outputs the color yellow + return (drawTriangle(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE) == -1 || drawTriangle(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE_YELLOW) == -1); default: throw "No function for this draw call"; } } -int drawSquare() +int drawSquare(const char* vertexShaderSource, const char* fragmentShaderSource) { - const std::pair shaders = compileShaders(); + const std::pair shaders = compileShaders(vertexShaderSource, fragmentShaderSource); if (shaders.first == 0 || shaders.second == 0) return -1; @@ -54,11 +61,11 @@ void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexA glBindVertexArray(0); } -int drawTriangle() +int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexShaderSource, const char* fragmentShaderSource) { - const unsigned int vertexBufferObject = copyVerticesMemory(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, GL_ARRAY_BUFFER); + const unsigned int vertexBufferObject = copyVerticesMemory(triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER); - const std::pair shaders = compileShaders(); + const std::pair shaders = compileShaders(vertexShaderSource, fragmentShaderSource); if (shaders.first == 0 || shaders.second == 0) return -1; @@ -68,12 +75,12 @@ int drawTriangle() configureVertexAttribute(); const unsigned int vertexArrayObject = generateBindVAO(); - copyVerticesArray(vertexBufferObject, constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, GL_ARRAY_BUFFER); + copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER); // set vertex attribute pointers glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); glEnableVertexAttribArray(0); - doDrawArrays(shaderProgram, vertexArrayObject, GL_TRIANGLES, 0, 3); + doDrawArrays(shaderProgram, vertexArrayObject, GL_TRIANGLES, 0, triangleVerticesSize); return 0; } diff --git a/Engine/engine/draw.hpp b/Engine/engine/draw.hpp index f28bb3b..b58ddb1 100644 --- a/Engine/engine/draw.hpp +++ b/Engine/engine/draw.hpp @@ -4,9 +4,9 @@ #include int drawFigure(const int whatToDraw); -int drawSquare(); +int drawSquare(const char* vertexShaderSource, const char* fragmentShaderSource); void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const GLenum drawType, const int numberOfElementsToDraw); -int drawTriangle(); +int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexShaderSource, const char* fragmentShaderSource); void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered ); #endif \ No newline at end of file diff --git a/Engine/engine/match b/Engine/engine/match index 8dabe42..2a6c3d6 100755 Binary files a/Engine/engine/match and b/Engine/engine/match differ diff --git a/Engine/engine/renderLoop.cpp b/Engine/engine/renderLoop.cpp index 72371b8..1d03f34 100644 --- a/Engine/engine/renderLoop.cpp +++ b/Engine/engine/renderLoop.cpp @@ -9,16 +9,24 @@ #include "constants.hpp" #include "misc.hpp" -bool processInput(GLFWwindow *window, const int whatToDraw) +int processInput(GLFWwindow *window, const int whatToDraw) { + static bool locked = false; + const bool PRESSED_CHANGE_DRAW = (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS); + // glfwGetKey takes window and key as an input and checks is currently being pressed // if the user pressed escape we close window if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) + if ( !PRESSED_CHANGE_DRAW ) + locked = 0; + if ( PRESSED_CHANGE_DRAW && locked == 0 ) + { + locked = 1; return (whatToDraw == constants::MAX_DRAW_CALL ? 0 : whatToDraw + 1); + } return whatToDraw; } @@ -96,7 +104,7 @@ void copyVerticesArray(const unsigned int vertexBufferObject, const float vertic glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW); } -bool renderLoopInside(GLFWwindow *window, int whatToDraw) +int renderLoopInside(GLFWwindow *window, int whatToDraw) { // input whatToDraw = processInput(window, whatToDraw); @@ -119,6 +127,7 @@ bool renderLoopInside(GLFWwindow *window, int whatToDraw) // glfwPollEvents checks if any event (like mouse/keyboard input was triggered), updates window state and calls functions (which we register via callback methods) glfwPollEvents(); + return whatToDraw; } diff --git a/Engine/engine/renderLoop.hpp b/Engine/engine/renderLoop.hpp index 39112c4..6537df0 100644 --- a/Engine/engine/renderLoop.hpp +++ b/Engine/engine/renderLoop.hpp @@ -4,11 +4,11 @@ #include void renderLoop(GLFWwindow *window); -bool renderLoopInside(GLFWwindow *window, int whatToDraw); +int renderLoopInside(GLFWwindow *window, int whatToDraw); void copyVerticesArray(unsigned int vertexBufferObject, const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget); unsigned int generateBindVAO(); void configureVertexAttribute(); -bool processInput(GLFWwindow *window, int whatToDraw); +int processInput(GLFWwindow *window, int whatToDraw); unsigned int copyVerticesMemory(const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget); unsigned int copyVerticesMemory(const unsigned int vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget); diff --git a/Engine/engine/shaders.cpp b/Engine/engine/shaders.cpp index ad1db86..1e71541 100644 --- a/Engine/engine/shaders.cpp +++ b/Engine/engine/shaders.cpp @@ -49,16 +49,16 @@ unsigned int compileShader(const GLenum shaderType, const char *shaderSource) return shaderID; } -std::pair compileShaders() +std::pair compileShaders(const char* vertexShaderSource, const char* fragmentShaderSource) { - const unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource); + const unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource); if (vertexShader == 0) { print("Vertex Shader Compilation Failed"); return std::make_pair(0, 0); } - const unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, constants::fragmentShaderSource); + const unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource); if (fragmentShader == 0) { print("Fragment Shader Compilation Failed"); diff --git a/Engine/engine/shaders.hpp b/Engine/engine/shaders.hpp index 7eee842..29b7d46 100644 --- a/Engine/engine/shaders.hpp +++ b/Engine/engine/shaders.hpp @@ -5,7 +5,7 @@ #include unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders, const unsigned int fragmentShader); -std::pair compileShaders(); +std::pair compileShaders(const char* vertexShaderSource, const char* fragmentShaderSource); unsigned int compileShader(const GLenum shaderType, const char *shaderSource); int shaderCompilationSuccessful(const unsigned int shader); int shaderProgramLinkingSuccessful(const unsigned int shaderProgram);