Revert "Revert "feat: shaders cooperation, uniform and colors from vertice""

This reverts commit 4fed1960da.
This commit is contained in:
Krzysztof Rudnicki 2022-10-19 15:51:07 +02:00
parent 4fed1960da
commit 41331f90fe
7 changed files with 228 additions and 102 deletions

View File

@ -77,5 +77,8 @@
"ctime": "cpp", "ctime": "cpp",
"ratio": "cpp" "ratio": "cpp"
}, },
"C_Cpp.errorSquiggles": "Disabled" "C_Cpp.errorSquiggles": "Disabled",
"cSpell.words": [
"VERTICE"
]
} }

View File

@ -4,106 +4,187 @@
#include <iostream> #include <iostream>
#include <string_view> #include <string_view>
namespace constants namespace constants
{ {
inline constexpr int GLFW_MAJOR_VERSION{3}; inline constexpr int GLFW_MAJOR_VERSION { 3 };
inline constexpr int GLFW_MINOR_VERSION{3}; inline constexpr int GLFW_MINOR_VERSION { 3 };
// best practice is to use inline constexpr std::string_view but glfwCreateWindow takes only char* as input // best practice is to use inline constexpr std::string_view but glfwCreateWindow takes only char* as input
inline const char *MAIN_WINDOW_NAME{"Match"}; inline const char* MAIN_WINDOW_NAME { "Match" };
inline constexpr int MAIN_WINDOW_WIDTH{800}; inline constexpr int MAIN_WINDOW_WIDTH { 800 };
inline constexpr int MAIN_WINDOW_HEIGHT{600}; inline constexpr int MAIN_WINDOW_HEIGHT { 600 };
inline constexpr struct inline constexpr struct {
{
GLfloat red = 1.0f; GLfloat red = 1.0f;
GLfloat green = 0.0f; GLfloat green = 0.0f;
GLfloat blue = 0.0f; GLfloat blue = 0.0f;
GLfloat alpha = 1.0f; GLfloat alpha = 1.0f;
} RED; } RED;
inline constexpr struct inline constexpr struct {
{
GLfloat red = 1.0f; GLfloat red = 1.0f;
GLfloat green = 1.0f; GLfloat green = 1.0f;
GLfloat blue = 1.0f; GLfloat blue = 1.0f;
GLfloat alpha = 1.0f; GLfloat alpha = 1.0f;
} WHITE; } WHITE;
inline constexpr struct inline constexpr struct {
{
GLfloat red = 0.2f; GLfloat red = 0.2f;
GLfloat green = 0.3f; GLfloat green = 0.3f;
GLfloat blue = 0.3f; GLfloat blue = 0.3f;
GLfloat alpha = 1.0f; GLfloat alpha = 1.0f;
} LEARN_OPEN_GL_COLOR; } LEARN_OPEN_GL_COLOR;
// we write vertex shader inline const std::string_view VERTEX_SHADER_OUTPUT_FILE {
"./shaders/vertexShaderOutput.vs"
};
inline const std::string_view VERTEX_FIRST_SHADER {
"./shaders/firstShader.vs"
};
inline const std::string_view VERTEX_WITH_COLOR {
"./shaders/vertexWithColor.vs"
};
inline const std::string_view FRAGMENT_YELLOW {
"./shaders/yellow.fs"
};
inline const std::string_view FRAGMENT_ORANGE {
"./shaders/orange.fs"
};
inline const std::string_view FRAGMENT_COLOR_INPUT {
"./shaders/fragmentColorInput.fs"
};
inline const std::string_view FRAGMENT_UNIFORMS {
"./shaders/fragmentUniforms.fs"
};
// we send output from vertex shader (vertexColor)...
inline const char *VERTEX_SHADER_COLOR { "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"out vec3 vertexColor;\n" // color output to fragment shader
"void main()\n"
"{\n"
" gl_Position = vec4(aPos, 1.0);\n" // we directly give xyz of aPos as an argument
" vertexColor = vec3(0.5, 0.0, 0.0);\n" // dark red color
"}\0" } ;
// we write vertex shader
// version of glsl (since ogl 3.3 same as ogl so we pick 330) // 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 // in this shader we just forward input data to shader output
inline const char *VERTEX_SHADER_SOURCE{"#version 330 core\n" inline const char *VERTEX_SHADER_SOURCE { "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n" "layout (location = 0) in vec3 aPos;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0"}; "}\0" } ;
inline const char *VERTEX_SHADER_VERTICE_COLOR { "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n" // position has attribute position 0
"layout (location = 1) in vec3 aColor;\n" // color has attribute position 1
"out vec3 vertexColor;\n" // output a color to fragment shader
"void main()\n"
"{\n"
" gl_Position = vec4(aPos, 1.0);\n"
" vertexColor = aColor;\n"
"}\0" } ;
// ... to fragment shader (vertexColor)
inline const char *FRAGMENT_SHADER_COLOR_FROM_VERTEX {
"#version 330 core\n"
"out vec4 FragColor;\n"
"in vec3 vertexColor;\n" // from vertex
"void main()\n"
"{\n"
"FragColor = vec4(vertexColor, 1.0);\n"
"}\0"
};
// uniforms
inline const char *FRAGMENT_SHADER_UNIFORMS {
"#version 330 core\n"
"out vec4 FragColor;\n"
"uniform vec4 ourColor;\n" // set in OGL code
"void main()\n"
"{\n"
"FragColor = ourColor;\n"
"}\0"
};
// write fragment shader // write fragment shader
// we set the color of each pixel to be orange // we set the color of each pixel to be orange
inline const char *FRAGMENT_SHADER_SOURCE{ inline const char *FRAGMENT_SHADER_SOURCE {
"#version 330 core\n" "#version 330 core\n"
"out vec4 FragColor;\n" "out vec4 FragColor;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
"FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" "FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\0"}; "}\0"
};
inline const char *FRAGMENT_SHADER_SOURCE_YELLOW{ inline const char *FRAGMENT_SHADER_SOURCE_YELLOW {
"#version 330 core\n" "#version 330 core\n"
"out vec4 FragColor;\n" "out vec4 FragColor;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
"FragColor = vec4(1.0f, 1.0f, 0.0f, 1.0f);\n" "FragColor = vec4(1.0f, 1.0f, 0.0f, 1.0f);\n"
"}\0"}; "}\0"
};
// we specify three vertices // we specify three vertices
// each of them with position in 3d space // each of them with position in 3d space
// x y z // x y z
inline constexpr float TRIANGLE_VERTICES[]{ inline constexpr float TRIANGLE_VERTICES[] {
-0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f}; 0.0f, 0.5f, 0.0f
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 size_t TRIANGLE_VERTICES_SIZE = { sizeof(TRIANGLE_VERTICES) };
inline constexpr float TRIANGLE_ONE[]{ inline constexpr float TRIANGLES_VERTICES[] {
// first triangle // first triangle
-0.9f, -0.5f, 0.0f, // left -0.9f, -0.5f, 0.0f, // left
-0.0f, -0.5f, 0.0f, // right -0.0f, -0.5f, 0.0f, // right
-0.45f, 0.5f, 0.0f, // top -0.45f, 0.5f, 0.0f, // top
};
inline constexpr size_t TRIANGLE_ONE_SIZE = {sizeof(TRIANGLE_ONE)};
inline constexpr float TRIANGLE_TWO[]{
// second triangle // second triangle
0.0f, -0.5f, 0.0f, // left 0.0f, -0.5f, 0.0f, // left
0.9f, -0.5f, 0.0f, // right 0.9f, -0.5f, 0.0f, // right
0.45f, 0.5f, 0.0f // top 0.45f, 0.5f, 0.0f // top
}; };
inline constexpr size_t TRIANGLE_TWO_SIZE = {sizeof(TRIANGLE_TWO)}; 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) };
inline constexpr float TRIANGLE_COLORS[] {
// positions // colors
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top
};
inline constexpr size_t TRIANGLE_COLORS_SIZE = { sizeof(TRIANGLE_COLORS) };
// compare with square done with only vertices: // compare with square done with only vertices:
/* /*
@ -111,31 +192,31 @@ namespace constants
// first triangle // first triangle
0.5f, 0.5f, 0.0f, // top right 0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right 0.5f, -0.5f, 0.0f, // bottom right
-0.5f, 0.5f, 0.0f, // top left -0.5f, 0.5f, 0.0f, // top left
// second triangle // second triangle
0.5f, -0.5f, 0.0f, // bottom right 0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left -0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left -0.5f, 0.5f, 0.0f // top left
}; };
bottom right and top left is specified twice ! bottom right and top left is specified twice !
*/ */
inline constexpr float SQUARE_VERTICES[]{ inline constexpr float SQUARE_VERTICES[] {
0.5f, 0.5f, 0.0f, // top right 0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right 0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left -0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left -0.5f, 0.5f, 0.0f // top left
}; };
inline constexpr unsigned int SQUARE_INDICES[]{ inline constexpr unsigned int SQUARE_INDICES[] {
0, 1, 3, // first triangle 0, 1, 3, // first triangle
1, 2, 3 // second triangle 1, 2, 3 // second triangle
}; };
inline constexpr size_t SQUARE_INDICES_SIZE = {sizeof(SQUARE_INDICES)}; inline constexpr size_t SQUARE_INDICES_SIZE = { sizeof(SQUARE_INDICES) };
inline constexpr size_t SQUARE_VERTICES_SIZE = {sizeof(SQUARE_VERTICES)}; inline constexpr size_t SQUARE_VERTICES_SIZE = { sizeof(SQUARE_VERTICES) };
inline constexpr int MAX_DRAW_CALL = {4}; inline constexpr int MAX_DRAW_CALL = { 7 };
} }

View File

@ -3,6 +3,7 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <iostream> #include <iostream>
#include <cmath>
#include "draw.hpp" #include "draw.hpp"
#include "renderLoop.hpp" #include "renderLoop.hpp"
#include "shaders.hpp" #include "shaders.hpp"
@ -14,18 +15,27 @@ int drawFigure(const int whatToDraw)
switch (whatToDraw) switch (whatToDraw)
{ {
case 0: case 0:
return drawTriangle(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE); return drawTriangle(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_FIRST_SHADER, constants::FRAGMENT_ORANGE, false);
case 1: case 1:
return drawSquare(constants::VERTEX_FIRST_SHADER, constants::FRAGMENT_ORANGE); return drawSquare(constants::VERTEX_FIRST_SHADER, constants::FRAGMENT_ORANGE);
case 2: case 2:
// Try to draw 2 triangles next to each other using glDrawArrays by adding more vertices to your data. // 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); return drawTriangle(constants::TRIANGLES_VERTICES, constants::TRIANGLES_VERTICES_SIZE, constants::VERTEX_FIRST_SHADER, constants::FRAGMENT_ORANGE, false);
case 3: case 3:
// Now create the same 2 triangles using two different VAOs and VBOs for their data // 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); 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 constants::MAX_DRAW_CALL: 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 // 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); 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: default:
throw "No function for this draw call"; throw "No function for this draw call";
} }
@ -40,8 +50,7 @@ int drawSquare(const std::string_view vertexShaderSource, const std::string_view
copyVerticesMemory(constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE, GL_ELEMENT_ARRAY_BUFFER); copyVerticesMemory(constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE, GL_ELEMENT_ARRAY_BUFFER);
// set vertex attribute pointers // set vertex attribute pointers
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); configureVertexAttribute(false);
glEnableVertexAttribArray(0);
doDrawElements(ourShader, VAO, GL_TRIANGLES, GL_UNSIGNED_INT, std::size(constants::SQUARE_INDICES)); doDrawElements(ourShader, VAO, GL_TRIANGLES, GL_UNSIGNED_INT, std::size(constants::SQUARE_INDICES));
return 0; return 0;
@ -55,27 +64,36 @@ void doDrawElements(const Shader Shader, const unsigned int vertexArrayObject, c
glBindVertexArray(0); glBindVertexArray(0);
} }
int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char *vertexShaderSource, const char *fragmentShaderSource) 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); const unsigned int vertexBufferObject = copyVerticesMemory(triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
Shader ourShader(vertexShaderSource, fragmentShaderSource); Shader ourShader(vertexShaderSource, fragmentShaderSource);
configureVertexAttribute();
const unsigned int vertexArrayObject = generateBindVAO(); const unsigned int vertexArrayObject = generateBindVAO();
copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER); copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
// set vertex attribute pointers // set vertex attribute pointers
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); configureVertexAttribute(colorIncluded);
glEnableVertexAttribArray(0); doDrawArrays(ourShader, vertexArrayObject, GL_TRIANGLES, 0, triangleVerticesSize);
doDrawArrays(shaderProgram, vertexArrayObject, GL_TRIANGLES, 0, triangleVerticesSize);
return 0; return 0;
} }
void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered) 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 // use shader program to render an object
glUseProgram(shaderProgram); Shader.use();
Shader.setFloat("ourColor", updateUniformColor());
glBindVertexArray(vertexArrayObject); glBindVertexArray(vertexArrayObject);
// From left: // From left:
// primitive type we want to draw // primitive type we want to draw

View File

@ -6,7 +6,8 @@
int drawFigure(const int whatToDraw); int drawFigure(const int whatToDraw);
int drawSquare(const std::string_view vertexShaderSource, const std::string_view fragmentShaderSource); int drawSquare(const std::string_view vertexShaderSource, const std::string_view fragmentShaderSource);
void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const GLenum drawType, const int numberOfElementsToDraw); void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const GLenum drawType, const int numberOfElementsToDraw);
int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const char *vertexShaderSource, const char *fragmentShaderSource); int drawTriangle(const float triangleVertices[], const size_t triangleVerticesSize, const std::string_view vertexShaderSource, const std::string_view fragmentShaderSource, const bool colorIncluded);
void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered); GLfloat updateUniformColor();
void doDrawArrays(const Shader Shader, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered );
#endif #endif

Binary file not shown.

View File

@ -11,8 +11,10 @@
int processInput(GLFWwindow *window, const int whatToDraw) int processInput(GLFWwindow *window, const int whatToDraw)
{ {
static bool locked = false; static bool lockedLeft = false;
const bool PRESSED_CHANGE_DRAW = (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS); static bool lockedRight = false;
const bool PRESSED_CHANGE_LEFT = (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS);
const bool PRESSED_CHANGE_RIGHT = (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS);
// glfwGetKey takes window and key as an input and checks is currently being pressed // glfwGetKey takes window and key as an input and checks is currently being pressed
// if the user pressed escape we close window // if the user pressed escape we close window
@ -20,12 +22,19 @@ int processInput(GLFWwindow *window, const int whatToDraw)
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
if ( !PRESSED_CHANGE_DRAW ) if ( !PRESSED_CHANGE_LEFT )
locked = 0; lockedLeft = 0;
if ( PRESSED_CHANGE_DRAW && locked == 0 ) if ( !PRESSED_CHANGE_RIGHT )
lockedRight = 0;
if ( PRESSED_CHANGE_RIGHT && lockedRight == 0 )
{ {
locked = 1; lockedRight = 1;
return (whatToDraw == constants::MAX_DRAW_CALL ? 0 : whatToDraw + 1); return (whatToDraw == constants::MAX_DRAW_CALL ? 0 : whatToDraw + 1);
}
if ( PRESSED_CHANGE_LEFT && lockedLeft == 0 )
{
lockedLeft = 1;
return (whatToDraw == 0 ? constants::MAX_DRAW_CALL : whatToDraw - 1);
} }
return whatToDraw; return whatToDraw;
} }
@ -69,21 +78,35 @@ unsigned int copyVerticesMemory(const unsigned int vertices[], const size_t size
return vertexBufferObject; return vertexBufferObject;
} }
void configureVertexAttribute() void configureVertexAttribute(const bool colorIncluded)
{ {
// specify how OGL interprets vertex data /* specify how OGL interprets vertex data
// From left: From left:
// which vertex attribute we configure (from shader source code layout (location = 0)) which vertex attribute we configure (from shader source code layout (location = 0))
// size of vertex attribute (we use vec3 so it contains 3 values) size of vertex attribute (we use vec3 so it contains 3 values)
// type of data of which vec consists of type of data of which vec consists of
// should data be normalized, (useful when we use integer data) should data be normalized, (useful when we use integer data)
// space between vertex attributes, each position data is 3 times the size of float space between vertex attributes, each position data is 3 times the size of float
// offset of where position data begins in buffer offset of where position data begins in buffer
// see: https://learnopengl.com/img/getting-started/vertex_attribute_pointer.png see: https://learnopengl.com/img/getting-started/vertex_attribute_pointer.png
// vertex attribute data take data from memory managed by VBO bound to GL_ARRAY_BUFFER vertex attribute data take data from memory managed by VBO bound to GL_ARRAY_BUFFER */
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); if(!colorIncluded) {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
// enable vertex attribute
glEnableVertexAttribArray(0);
return;
}
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
// enable vertex attribute // enable vertex attribute
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
/* we change attribute location, color values have size of 3 floats
we do not normalize values, in order to get the next attribute value in data array we need to move 6 floats, (3 for position and 3 for color), we also need to specify an offset, first we have position then after 3 floats we have color
https://learnopengl.com/img/getting-started/vertex_attribute_pointer_interleaved.png */
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3* sizeof(float)));
// enable vertex attribute
glEnableVertexAttribArray(1);
} }
unsigned int generateBindVAO() unsigned int generateBindVAO()

View File

@ -7,7 +7,7 @@ void renderLoop(GLFWwindow *window);
int 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); void copyVerticesArray(unsigned int vertexBufferObject, const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);
unsigned int generateBindVAO(); unsigned int generateBindVAO();
void configureVertexAttribute(); void configureVertexAttribute(const bool colorIncluded);
int 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 float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);