feat: unify draw function

This commit is contained in:
Krzysztof Rudnicki 2023-03-19 13:53:13 +01:00
parent 1228e5d618
commit e5a609d7f9
5 changed files with 80 additions and 130 deletions

View File

@ -6,9 +6,11 @@ layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor; out vec3 ourColor;
out vec2 TexCoord; out vec2 TexCoord;
uniform float xOffset;
void main() void main()
{ {
gl_Position = vec4(aPos, 1.0); gl_Position = vec4(aPos.x + xOffset, aPos.y, aPos.z, 1.0);
ourColor = aColor; ourColor = aColor;
TexCoord = vec2(aTexCoord.x, aTexCoord.y); TexCoord = vec2(aTexCoord.x, aTexCoord.y);
} }

View File

@ -98,11 +98,20 @@ inline const char* FRAGMENT_SHADER_TEXTURE_FILENAME{
// 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[]{-0.5f, -0.5f, 0.0f, 0.5f, -0.5f, inline constexpr float TRIANGLE_VERTICES[]{
0.0f, 0.0f, 0.5f, 0.0f}; -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 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[]{ inline constexpr float TRIANGLES_VERTICES[]{
// first triangle // first triangle
-0.9f, -0.5f, 0.0f, // left -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 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[]{ inline constexpr float TRIANGLE_ONE[]{
// first triangle // first triangle
-0.9f, -0.5f, 0.0f, // left -0.9f, -0.5f, 0.0f, // left

View File

@ -22,34 +22,37 @@
drawFigureReturn drawFigure(const int whatToDraw) { drawFigureReturn drawFigure(const int whatToDraw) {
switch (whatToDraw) { switch (whatToDraw) {
case 0: case 0:
return drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME, return draw(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE); constants::TRIANGLE_VERTICES_SIZE);
case 1: case 1:
return drawSquare( return draw(
constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE, constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE); constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE,
constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE);
case 2: case 2:
// Try to draw 2 triangles next to each other // Try to draw 2 triangles next to each other
// using glDrawArrays by adding more vertices to your data. // 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::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLES_VERTICES, constants::TRIANGLES_VERTICES,
constants::TRIANGLES_VERTICES_SIZE); constants::TRIANGLES_VERTICES_SIZE,
constants::TRIANGLES_INDICES,
constants::TRIANGLES_INDICES_SIZE);
case 3: case 3:
drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME, draw(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE); constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE);
// Now create the same 2 triangles // Now create the same 2 triangles
// using two different VAOs and VBOs for their data // 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::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_TWO, constants::TRIANGLE_TWO,
constants::TRIANGLE_TWO_SIZE); constants::TRIANGLE_TWO_SIZE);
case 4: case 4:
drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME, draw(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE); constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE);
/* /*
@ -58,31 +61,34 @@ drawFigureReturn drawFigure(const int whatToDraw) {
fragment shader that outputs the color yellow; fragment shader that outputs the color yellow;
draw both triangles again where one outputs the color yellow draw both triangles again where one outputs the color yellow
*/ */
return drawTriangleClass( return draw(
constants::VERTEX_SHADER_SOURCE_FILENAME, constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME, constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME,
constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE); constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE);
case 5: case 5:
// Get color from vertex shader to fragment shader // Get color from vertex shader to fragment shader
return drawTriangleClass( return draw(
constants::VERTEX_SHADER_COLOR_FILENAME, constants::VERTEX_SHADER_COLOR_FILENAME,
constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME, constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME,
constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE); constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE);
case 6: case 6:
// set color from opengl code to uniform value in fragment shader // 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::FRAGMENT_SHADER_UNIFORMS_FILENAME,
constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE); constants::TRIANGLE_VERTICES_SIZE);
case 7: case 7:
// set color from opengl code to uniform value in fragment shader // set color from opengl code to uniform value in fragment shader
return drawTriangleClass( return draw(
constants::VERTEX_SHADER_VERTICE_COLOR_FILENAME, constants::VERTEX_SHADER_VERTICE_COLOR_FILENAME,
constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_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: case 8:
// upside down triangle // 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::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE); constants::TRIANGLE_VERTICES_SIZE);
@ -90,23 +96,28 @@ drawFigureReturn drawFigure(const int whatToDraw) {
// offset triangle // offset triangle
offsetsStruct offsets = offsetsStruct(); offsetsStruct offsets = offsetsStruct();
offsets.xOffset = 0.5f; offsets.xOffset = 0.5f;
return drawTriangleClass(constants::VERTEX_SHADER_OFFSET_FILENAME, return draw(constants::VERTEX_SHADER_OFFSET_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE, false, false, constants::TRIANGLE_VERTICES_SIZE,
offsets); constants::TRIANGLE_INDICES,
constants::TRIANGLE_INDICES_SIZE,
false, false, offsets);
} }
case 10: 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::FRAGMENT_SHADER_TASK_THREE_FILENAME,
constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE); constants::TRIANGLE_VERTICES_SIZE);
case constants::MAX_DRAW_CALL: case constants::MAX_DRAW_CALL: {
return drawDebilMode( return draw(
constants::VERTEX_SHADER_TEXTURE_FILENAME, constants::VERTEX_SHADER_TEXTURE_FILENAME,
constants::FRAGMENT_SHADER_TEXTURE_FILENAME, constants::FRAGMENT_SHADER_TEXTURE_FILENAME,
constants::TEXTURE_VERTICES, constants::TEXTURE_VERTICES_SIZE, constants::TEXTURE_VERTICES, constants::TEXTURE_VERTICES_SIZE,
constants::TEXTURE_INDICES, constants::TEXTURE_INDICES_SIZE); constants::TEXTURE_INDICES,
constants::TEXTURE_INDICES_SIZE,
true, true);
}
default: default:
throw "No function for this draw call"; throw "No function for this draw call";
} }
@ -131,95 +142,44 @@ void setOffsets(Shader shader, const offsetsStruct offsets) {
shader.setFloat("zOffset", offsets.zOffset); shader.setFloat("zOffset", offsets.zOffset);
} }
drawFigureReturn drawTriangleClass(const char* vertexPath, drawFigureReturn draw(const char* vertexPath, const char* fragmentPath,
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,
const float vertices[], const float vertices[],
const size_t verticesSize, const size_t verticesSize,
const unsigned int indices[], 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 // https://stackoverflow.com/questions/33883609/opengl-linker-error-linking-with-uncompiled-shader
Shader ourShader(constants::VERTEX_SHADER_TEXTURE_FILENAME, Shader ourShader(vertexPath,
constants::FRAGMENT_SHADER_TEXTURE_FILENAME); 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 = 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 = unsigned int EBO =
copyVerticesMemory(indices, indicesSize, GL_ELEMENT_ARRAY_BUFFER); 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 texture1 = loadAndCreateTexture("assets/container.png", true);
unsigned int texture2 = loadAndCreateTexture("assets/awesomeface.png", false); 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("texture1", 0);
ourShader.setInt("texture2", 1); ourShader.setInt("texture2", 1);
activateAndBindTextures(texture1, GL_TEXTURE0); activateAndBindTextures(texture1, GL_TEXTURE0);
activateAndBindTextures(texture2, GL_TEXTURE1); activateAndBindTextures(texture2, GL_TEXTURE1);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, indicesSize, GL_UNSIGNED_INT, 0);
drawFigureReturn newReturn; drawFigureReturn newReturn;
newReturn.success = 0; newReturn.success = 0;
@ -229,16 +189,6 @@ drawFigureReturn drawDebilMode(const char* vertexPath, const char* fragmentPath,
return newReturn; 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, void updateUniformColor(const unsigned int shaderProgram,
const GLchar* uniformName) { const GLchar* uniformName) {
// update the uniform color // update the uniform color

View File

@ -23,27 +23,6 @@ struct drawFigureReturn {
drawFigureReturn drawFigure(const int whatToDraw); 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, void updateUniformColor(const unsigned int shaderProgram,
const GLchar* uniformName); const GLchar* uniformName);
@ -52,12 +31,15 @@ void doDrawArrays(const unsigned int shaderProgram,
const GLenum drawArrayMode, const int firstIndex, const GLenum drawArrayMode, const int firstIndex,
const unsigned int numberOfIndicesToBeRendered); const unsigned int numberOfIndicesToBeRendered);
drawFigureReturn drawDebilMode( drawFigureReturn draw(
const char* vertexPath = constants::VERTEX_SHADER_SOURCE_FILENAME, const char* vertexPath = constants::VERTEX_SHADER_SOURCE_FILENAME,
const char* fragmentPath = constants::FRAGMENT_SHADER_SOURCE_FILENAME, const char* fragmentPath = constants::FRAGMENT_SHADER_SOURCE_FILENAME,
const float vertices[] = constants::TRIANGLE_VERTICES, const float vertices[] = constants::TRIANGLE_VERTICES,
const size_t verticesSize = constants::TRIANGLE_VERTICES_SIZE, const size_t verticesSize = constants::TRIANGLE_VERTICES_SIZE,
const unsigned int indices[] = constants::TEXTURE_INDICES, const unsigned int indices[] = constants::TRIANGLE_INDICES,
const size_t indicesSize = constants::TEXTURE_INDICES_SIZE); 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_ #endif // ENGINE_ENGINE_DRAW_HPP_

Binary file not shown.