diff --git a/Engine/engine/Shaders/vertexShaderTexture.vs b/Engine/engine/Shaders/vertexShaderTexture.vs index ef04f60..bfbe611 100644 --- a/Engine/engine/Shaders/vertexShaderTexture.vs +++ b/Engine/engine/Shaders/vertexShaderTexture.vs @@ -6,9 +6,11 @@ layout (location = 2) in vec2 aTexCoord; out vec3 ourColor; out vec2 TexCoord; +uniform float xOffset; + void main() { - gl_Position = vec4(aPos, 1.0); + gl_Position = vec4(aPos.x + xOffset, aPos.y, aPos.z, 1.0); ourColor = aColor; TexCoord = vec2(aTexCoord.x, aTexCoord.y); } \ No newline at end of file diff --git a/Engine/engine/constants.hpp b/Engine/engine/constants.hpp index bc79463..0977213 100644 --- a/Engine/engine/constants.hpp +++ b/Engine/engine/constants.hpp @@ -98,11 +98,20 @@ inline const char* FRAGMENT_SHADER_TEXTURE_FILENAME{ // we specify three vertices // each of them with position in 3d space // x y z -inline constexpr float TRIANGLE_VERTICES[]{-0.5f, -0.5f, 0.0f, 0.5f, -0.5f, - 0.0f, 0.0f, 0.5f, 0.0f}; +inline constexpr float TRIANGLE_VERTICES[]{ + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + 0.0f, 0.5f, 0.0f + }; inline constexpr size_t TRIANGLE_VERTICES_SIZE = {sizeof(TRIANGLE_VERTICES)}; +inline constexpr unsigned int TRIANGLE_INDICES[]{ + 0, 1, 2, // first triangle +}; + +inline constexpr size_t TRIANGLE_INDICES_SIZE = {sizeof(TRIANGLE_INDICES)}; + inline constexpr float TRIANGLES_VERTICES[]{ // first triangle -0.9f, -0.5f, 0.0f, // left @@ -116,6 +125,13 @@ inline constexpr float TRIANGLES_VERTICES[]{ inline constexpr size_t TRIANGLES_VERTICES_SIZE = {sizeof(TRIANGLES_VERTICES)}; +inline constexpr unsigned int TRIANGLES_INDICES[]{ + 0, 1, 2, // first triangle + 0, 1, 2 // second triangle +}; + +inline constexpr size_t TRIANGLES_INDICES_SIZE = {sizeof(TRIANGLES_INDICES)}; + inline constexpr float TRIANGLE_ONE[]{ // first triangle -0.9f, -0.5f, 0.0f, // left diff --git a/Engine/engine/draw.cpp b/Engine/engine/draw.cpp index 2702cfd..81ff193 100644 --- a/Engine/engine/draw.cpp +++ b/Engine/engine/draw.cpp @@ -22,34 +22,37 @@ drawFigureReturn drawFigure(const int whatToDraw) { switch (whatToDraw) { case 0: - return drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME, + return draw(constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE); case 1: - return drawSquare( - constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE, - constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE, - constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE); + return draw( + constants::VERTEX_SHADER_SOURCE_FILENAME, + constants::FRAGMENT_SHADER_SOURCE_FILENAME, + constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE, + constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE); case 2: // Try to draw 2 triangles next to each other // using glDrawArrays by adding more vertices to your data. - return drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME, + return draw(constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, constants::TRIANGLES_VERTICES, - constants::TRIANGLES_VERTICES_SIZE); + constants::TRIANGLES_VERTICES_SIZE, + constants::TRIANGLES_INDICES, + constants::TRIANGLES_INDICES_SIZE); case 3: - drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME, + draw(constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE); // Now create the same 2 triangles // using two different VAOs and VBOs for their data - return drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME, + return draw(constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE); case 4: - drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME, + draw(constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE); /* @@ -58,31 +61,34 @@ drawFigureReturn drawFigure(const int whatToDraw) { fragment shader that outputs the color yellow; draw both triangles again where one outputs the color yellow */ - return drawTriangleClass( + return draw( constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME, constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE); case 5: // Get color from vertex shader to fragment shader - return drawTriangleClass( + return draw( constants::VERTEX_SHADER_COLOR_FILENAME, constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME, constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE); case 6: // set color from opengl code to uniform value in fragment shader - return drawTriangleClass(constants::VERTEX_SHADER_COLOR_FILENAME, + // toDo: fix + return draw(constants::VERTEX_SHADER_COLOR_FILENAME, constants::FRAGMENT_SHADER_UNIFORMS_FILENAME, constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE); case 7: // set color from opengl code to uniform value in fragment shader - return drawTriangleClass( + return draw( constants::VERTEX_SHADER_VERTICE_COLOR_FILENAME, constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME, - constants::TRIANGLE_COLORS, constants::TRIANGLE_COLORS_SIZE, true); + constants::TRIANGLE_COLORS, constants::TRIANGLE_COLORS_SIZE, + constants::TRIANGLE_INDICES, + constants::TRIANGLE_INDICES_SIZE, true); case 8: // upside down triangle - return drawTriangleClass(constants::VERTEX_SHADER_UPSIDE_DOWN_FILENAME, + return draw(constants::VERTEX_SHADER_UPSIDE_DOWN_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE); @@ -90,23 +96,28 @@ drawFigureReturn drawFigure(const int whatToDraw) { // offset triangle offsetsStruct offsets = offsetsStruct(); offsets.xOffset = 0.5f; - return drawTriangleClass(constants::VERTEX_SHADER_OFFSET_FILENAME, + return draw(constants::VERTEX_SHADER_OFFSET_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, constants::TRIANGLE_VERTICES, - constants::TRIANGLE_VERTICES_SIZE, false, false, - offsets); + constants::TRIANGLE_VERTICES_SIZE, + constants::TRIANGLE_INDICES, + constants::TRIANGLE_INDICES_SIZE, + false, false, offsets); } case 10: - return drawTriangleClass(constants::VERTEX_SHADER_TASK_THREE_FILENAME, + return draw(constants::VERTEX_SHADER_TASK_THREE_FILENAME, constants::FRAGMENT_SHADER_TASK_THREE_FILENAME, constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE); - case constants::MAX_DRAW_CALL: - return drawDebilMode( + case constants::MAX_DRAW_CALL: { + return draw( constants::VERTEX_SHADER_TEXTURE_FILENAME, constants::FRAGMENT_SHADER_TEXTURE_FILENAME, constants::TEXTURE_VERTICES, constants::TEXTURE_VERTICES_SIZE, - constants::TEXTURE_INDICES, constants::TEXTURE_INDICES_SIZE); + constants::TEXTURE_INDICES, + constants::TEXTURE_INDICES_SIZE, + true, true); + } default: throw "No function for this draw call"; } @@ -131,95 +142,44 @@ void setOffsets(Shader shader, const offsetsStruct offsets) { shader.setFloat("zOffset", offsets.zOffset); } -drawFigureReturn drawTriangleClass(const char* vertexPath, - const char* fragmentPath, - const float triangleVertices[], - const size_t triangleVerticesSize, - const bool colorIncluded, - const bool textureIncluded, - const offsetsStruct offsets) { - // In your CPP file: - // ====================== - // float offset = 0.5f; - - Shader ourShader(vertexPath, fragmentPath); - ourShader.use(); - setOffsets(ourShader, offsets); - - const unsigned int vertexBufferObject = copyVerticesMemory( - triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER); - const unsigned int vertexArrayObject = generateBindVAO(); - copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize, - GL_ARRAY_BUFFER); - // set vertex attribute pointers - configureVertexAttribute(colorIncluded, textureIncluded); - doDrawArrays(ourShader.ID, vertexArrayObject, GL_TRIANGLES, 0, - triangleVerticesSize); - drawFigureReturn newReturn; - newReturn.success = 0; - newReturn.VAO = vertexArrayObject; - newReturn.VBO = vertexBufferObject; - newReturn.EBO = 0; - return newReturn; -} - -drawFigureReturn drawSquare( - const float squareVertices[], const size_t squareVerticesSize, - const unsigned int squareIndices[], const size_t squareIndicesSize, - const char* vertexShaderSource, const char* fragmentShaderSource, - const bool colorIncluded, const bool textureIncluded, - const offsetsStruct offsets) { - const unsigned int shaderProgram = - getShaderProgram(vertexShaderSource, fragmentShaderSource); - drawFigureReturn newReturn = drawFigureReturn(); - if (shaderProgram == 0) { - newReturn.success = -1; - return newReturn; - } - const unsigned int VAO = generateBindVAO(); - copyVerticesMemory(squareVertices, squareVerticesSize, GL_ARRAY_BUFFER); - copyVerticesMemory(squareIndices, squareIndicesSize, GL_ELEMENT_ARRAY_BUFFER); - - // set vertex attribute pointers - configureVertexAttribute(colorIncluded, textureIncluded); - - doDrawElements(shaderProgram, VAO, GL_TRIANGLES, GL_UNSIGNED_INT, - squareIndicesSize); - newReturn.success = 0; - newReturn.VAO = VAO; - return newReturn; -} - -drawFigureReturn drawDebilMode(const char* vertexPath, const char* fragmentPath, +drawFigureReturn draw(const char* vertexPath, const char* fragmentPath, const float vertices[], const size_t verticesSize, const unsigned int indices[], - const size_t indicesSize) { + const size_t indicesSize, + const bool colorIncluded, + const bool textureIncluded, + const offsetsStruct offsets) { // https://stackoverflow.com/questions/33883609/opengl-linker-error-linking-with-uncompiled-shader - Shader ourShader(constants::VERTEX_SHADER_TEXTURE_FILENAME, - constants::FRAGMENT_SHADER_TEXTURE_FILENAME); + Shader ourShader(vertexPath, + fragmentPath); + ourShader.use(); // don't forget to activate/use + // the shader before setting uniforms! + // set it via the texture class + setOffsets(ourShader, offsets); - unsigned int VAO = generateBindVAO(); unsigned int VBO = - copyVerticesMemory(vertices, verticesSize, GL_ARRAY_BUFFER); + copyVerticesMemory(vertices, verticesSize, GL_ARRAY_BUFFER); + unsigned int VAO = generateBindVAO(); + copyVerticesArray(VBO, vertices, verticesSize, + GL_ARRAY_BUFFER); unsigned int EBO = copyVerticesMemory(indices, indicesSize, GL_ELEMENT_ARRAY_BUFFER); - configureVertexAttribute(true, true); + configureVertexAttribute(colorIncluded, textureIncluded); + doDrawArrays(ourShader.ID, VAO, GL_TRIANGLES, 0, + verticesSize); unsigned int texture1 = loadAndCreateTexture("assets/container.png", true); unsigned int texture2 = loadAndCreateTexture("assets/awesomeface.png", false); - ourShader.use(); // don't forget to activate/use - // the shader before setting uniforms! - // set it via the texture class ourShader.setInt("texture1", 0); ourShader.setInt("texture2", 1); activateAndBindTextures(texture1, GL_TEXTURE0); activateAndBindTextures(texture2, GL_TEXTURE1); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glDrawElements(GL_TRIANGLES, indicesSize, GL_UNSIGNED_INT, 0); drawFigureReturn newReturn; newReturn.success = 0; @@ -229,16 +189,6 @@ drawFigureReturn drawDebilMode(const char* vertexPath, const char* fragmentPath, return newReturn; } -void doDrawElements(const unsigned int shaderProgram, - const unsigned int vertexArrayObject, - const GLenum drawArrayMode, const GLenum drawType, - const int numberOfElementsToDraw) { - glUseProgram(shaderProgram); - glBindVertexArray(vertexArrayObject); - glDrawElements(drawArrayMode, numberOfElementsToDraw, drawType, 0); - glBindVertexArray(0); -} - void updateUniformColor(const unsigned int shaderProgram, const GLchar* uniformName) { // update the uniform color diff --git a/Engine/engine/draw.hpp b/Engine/engine/draw.hpp index 7bf7a6c..b340935 100644 --- a/Engine/engine/draw.hpp +++ b/Engine/engine/draw.hpp @@ -23,27 +23,6 @@ struct drawFigureReturn { drawFigureReturn drawFigure(const int whatToDraw); -drawFigureReturn drawSquare( - const float squareVertices[] = constants::SQUARE_VERTICES, - const size_t squareVerticesSize = constants::SQUARE_VERTICES_SIZE, - const unsigned int squareIndices[] = constants::SQUARE_INDICES, - const size_t squareIndicesSize = constants::SQUARE_INDICES_SIZE, - const char* vertexShaderSource = constants::VERTEX_SHADER_SOURCE, - const char* fragmentShaderSource = constants::FRAGMENT_SHADER_SOURCE, - const bool colorIncluded = false, const bool textureIncluded = false, - const offsetsStruct offsets = offsetsStruct()); - -void doDrawElements(const unsigned int shaderProgram, - const unsigned int vertexArrayObject, - const GLenum drawArrayMode, const GLenum drawType, - const int numberOfElementsToDraw); - -drawFigureReturn drawTriangleClass( - const char* vertexPath, const char* fragmentPath, - const float triangleVertices[], const size_t triangleVerticesSize, - const bool colorIncluded = false, const bool textureIncluded = false, - const offsetsStruct offsets = offsetsStruct()); - void updateUniformColor(const unsigned int shaderProgram, const GLchar* uniformName); @@ -52,12 +31,15 @@ void doDrawArrays(const unsigned int shaderProgram, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered); -drawFigureReturn drawDebilMode( +drawFigureReturn draw( const char* vertexPath = constants::VERTEX_SHADER_SOURCE_FILENAME, const char* fragmentPath = constants::FRAGMENT_SHADER_SOURCE_FILENAME, const float vertices[] = constants::TRIANGLE_VERTICES, const size_t verticesSize = constants::TRIANGLE_VERTICES_SIZE, - const unsigned int indices[] = constants::TEXTURE_INDICES, - const size_t indicesSize = constants::TEXTURE_INDICES_SIZE); + const unsigned int indices[] = constants::TRIANGLE_INDICES, + const size_t indicesSize = constants::TRIANGLE_INDICES_SIZE, + const bool colorIncluded = false, + const bool textureIncluded = false, + const offsetsStruct offsets = offsetsStruct()); #endif // ENGINE_ENGINE_DRAW_HPP_ diff --git a/Engine/engine/match b/Engine/engine/match index 241abb8..a5c8dbb 100755 Binary files a/Engine/engine/match and b/Engine/engine/match differ