#ifndef DRAW_CPP #define DRAW_CPP #include #include #include #include #include "draw.hpp" #include "renderLoop.hpp" #include "shaders.hpp" #include "constants.hpp" #include "misc.hpp" int drawFigure(const int whatToDraw) { switch (whatToDraw) { case 0: return drawTriangle(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_FIRST_SHADER, constants::FRAGMENT_ORANGE, false); case 1: return drawSquare(constants::VERTEX_FIRST_SHADER, constants::FRAGMENT_ORANGE); 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_FIRST_SHADER, constants::FRAGMENT_ORANGE, false); 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_FIRST_SHADER, constants::FRAGMENT_ORANGE, false) == -1 || drawTriangle(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_FIRST_SHADER, constants::FRAGMENT_ORANGE, false) == -1); case 4: // 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_FIRST_SHADER, constants::FRAGMENT_ORANGE, false) == -1 || drawTriangle(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_FIRST_SHADER, constants::FRAGMENT_YELLOW, false) == -1); case 5: // Get color from vertex shader to fragment shader return drawTriangle(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_WITH_COLOR, constants::FRAGMENT_COLOR_INPUT, false); case 6: // set color from opengl code to uniform value in fragment shader return drawTriangle(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_WITH_COLOR, constants::FRAGMENT_UNIFORMS, false); case constants::MAX_DRAW_CALL: // set color from opengl code to uniform value in fragment shader return drawTriangle(constants::TRIANGLE_COLORS, constants::TRIANGLE_COLORS_SIZE, constants::VERTEX_WITH_COLOR, constants::FRAGMENT_COLOR_INPUT, true); default: throw "No function for this draw call"; } } int drawSquare(const std::string_view vertexShaderSource, const std::string_view fragmentShaderSource) { Shader ourShader(vertexShaderSource, fragmentShaderSource); const unsigned int VAO = generateBindVAO(); copyVerticesMemory(constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE, GL_ARRAY_BUFFER); copyVerticesMemory(constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE, GL_ELEMENT_ARRAY_BUFFER); // set vertex attribute pointers configureVertexAttribute(false); doDrawElements(ourShader, VAO, GL_TRIANGLES, GL_UNSIGNED_INT, std::size(constants::SQUARE_INDICES)); return 0; } void doDrawElements(const Shader Shader, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const GLenum drawType, const int numberOfElementsToDraw) { Shader.use(); glBindVertexArray(vertexArrayObject); glDrawElements(drawArrayMode, numberOfElementsToDraw, drawType, 0); glBindVertexArray(0); } int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const std::string_view vertexShaderSource, const std::string_view fragmentShaderSource, const bool colorIncluded) { const unsigned int vertexBufferObject = copyVerticesMemory(triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER); Shader ourShader(vertexShaderSource, fragmentShaderSource); const unsigned int vertexArrayObject = generateBindVAO(); copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER); // set vertex attribute pointers configureVertexAttribute(colorIncluded); doDrawArrays(ourShader, vertexArrayObject, GL_TRIANGLES, 0, triangleVerticesSize); return 0; } GLfloat updateUniformColor() { // update the uniform color const float timeValue = glfwGetTime(); // retrieve running time const float greenValue = sin(timeValue) / 2.0f + 0.5f; // vary the color from 0.0 to 1.0 using sin return greenValue; } void doDrawArrays(const Shader Shader, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered) { // use shader program to render an object Shader.use(); Shader.setFloat("ourColor", updateUniformColor()); glBindVertexArray(vertexArrayObject); // From left: // primitive type we want to draw // starting index of vertex array // how many vertices we want to draw glDrawArrays(drawArrayMode, firstIndex, numberOfIndicesToBeRendered); } #endif