mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-07-04 13:03:07 +02:00
feat: toDo draw square class
This commit is contained in:
parent
36c0723878
commit
131e1f4ec9
7
Engine/engine/Shaders/fragmentShaderColorFromVertex.fs
Normal file
7
Engine/engine/Shaders/fragmentShaderColorFromVertex.fs
Normal file
@ -0,0 +1,7 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
in vec3 vertexColor; // from vertex
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(vertexColor, 1.0);
|
||||
}
|
||||
@ -2,5 +2,5 @@
|
||||
out vec4 FragColor;
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
||||
FragColor = vec4(0.9f, 0.5f, 0.2f, 1.0f);
|
||||
}
|
||||
6
Engine/engine/Shaders/fragmentShaderSourceYellow.fs
Normal file
6
Engine/engine/Shaders/fragmentShaderSourceYellow.fs
Normal file
@ -0,0 +1,6 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(1.0f, 1.0f, 0.0f, 1.0f);
|
||||
}
|
||||
7
Engine/engine/Shaders/fragmentShaderUniforms.fs
Normal file
7
Engine/engine/Shaders/fragmentShaderUniforms.fs
Normal file
@ -0,0 +1,7 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
uniform vec4 ourColor; // set in OGL code
|
||||
void main()
|
||||
{
|
||||
FragColor = ourColor;
|
||||
}
|
||||
8
Engine/engine/Shaders/vertexShaderColor.vs
Normal file
8
Engine/engine/Shaders/vertexShaderColor.vs
Normal file
@ -0,0 +1,8 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
out vec3 vertexColor;
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(aPos, 1.0);
|
||||
vertexColor = vec3(0.5, 0.0, 0.0);
|
||||
}
|
||||
10
Engine/engine/Shaders/vertexShaderVerticeColor.vs
Normal file
10
Engine/engine/Shaders/vertexShaderVerticeColor.vs
Normal file
@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
// position has attribute position 0
|
||||
layout (location = 1) in vec3 aColor; // color has attribute position 1
|
||||
out vec3 vertexColor; // output a color to fragment shader
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(aPos, 1.0);
|
||||
vertexColor = aColor;
|
||||
}
|
||||
@ -42,6 +42,12 @@ namespace constants
|
||||
" 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" } ;
|
||||
|
||||
inline const char* VERTEX_SHADER_COLOR_FILENAME {
|
||||
"./Shaders/vertexShaderColor.vs"
|
||||
};
|
||||
|
||||
|
||||
|
||||
// we write vertex shader
|
||||
// version of glsl (since ogl 3.3 same as ogl so we pick 330)
|
||||
@ -67,6 +73,10 @@ namespace constants
|
||||
" vertexColor = aColor;\n"
|
||||
"}\0" } ;
|
||||
|
||||
inline const char* VERTEX_SHADER_VERTICE_COLOR_FILENAME {
|
||||
"./Shaders/vertexShaderVerticeColor.vs"
|
||||
};
|
||||
|
||||
// ... to fragment shader (vertexColor)
|
||||
inline const char *FRAGMENT_SHADER_COLOR_FROM_VERTEX {
|
||||
"#version 330 core\n"
|
||||
@ -78,6 +88,10 @@ namespace constants
|
||||
"}\0"
|
||||
};
|
||||
|
||||
inline const char* FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME {
|
||||
"./Shaders/fragmentShaderColorFromVertex.fs"
|
||||
};
|
||||
|
||||
// uniforms
|
||||
inline const char *FRAGMENT_SHADER_UNIFORMS {
|
||||
"#version 330 core\n"
|
||||
@ -89,6 +103,10 @@ namespace constants
|
||||
"}\0"
|
||||
};
|
||||
|
||||
inline const char* FRAGMENT_SHADER_UNIFORMS_FILENAME {
|
||||
"./Shaders/fragmentShaderUniforms.fs"
|
||||
};
|
||||
|
||||
// write fragment shader
|
||||
// we set the color of each pixel to be orange
|
||||
inline const char *FRAGMENT_SHADER_SOURCE {
|
||||
@ -115,6 +133,10 @@ namespace constants
|
||||
"}\0"
|
||||
};
|
||||
|
||||
inline const char* FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME {
|
||||
"./Shaders/fragmentShaderSourceYellow.fs"
|
||||
};
|
||||
|
||||
// we specify three vertices
|
||||
// each of them with position in 3d space
|
||||
// x y z
|
||||
|
||||
@ -18,25 +18,25 @@ int drawFigure(const int whatToDraw)
|
||||
case 0:
|
||||
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, false);
|
||||
case 1:
|
||||
return drawSquare(constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE);
|
||||
return drawSquareClass(constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
||||
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, false);
|
||||
return drawTriangleClass(constants::TRIANGLES_VERTICES, constants::TRIANGLES_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, 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_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE, false) == -1 || drawTriangle(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE, false) == -1);
|
||||
return (drawTriangleClass(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, false) == -1 || drawTriangleClass(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, 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_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE, false) == -1 || drawTriangle(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE_YELLOW, false) == -1);
|
||||
return (drawTriangleClass(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, false) == -1 || drawTriangleClass(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME, false) == -1);
|
||||
case 5:
|
||||
// Get color from vertex shader to fragment shader
|
||||
return drawTriangle(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_COLOR, constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX, false);
|
||||
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_COLOR_FILENAME, constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME, 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_SHADER_COLOR, constants::FRAGMENT_SHADER_UNIFORMS, false);
|
||||
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_COLOR_FILENAME, constants::FRAGMENT_SHADER_UNIFORMS_FILENAME, false);
|
||||
case 7:
|
||||
// set color from opengl code to uniform value in fragment shader
|
||||
return drawTriangle(constants::TRIANGLE_COLORS, constants::TRIANGLE_COLORS_SIZE, constants::VERTEX_SHADER_VERTICE_COLOR, constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX, true);
|
||||
return drawTriangleClass(constants::TRIANGLE_COLORS, constants::TRIANGLE_COLORS_SIZE, constants::VERTEX_SHADER_VERTICE_COLOR_FILENAME, constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME, true);
|
||||
case constants::MAX_DRAW_CALL:
|
||||
// set color from opengl code to uniform value in fragment shader
|
||||
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, false);
|
||||
@ -104,6 +104,20 @@ int drawSquare(const char* vertexShaderSource, const char* fragmentShaderSource)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int drawSquareClass(const char* vertexPath, const char* fragmentPath)
|
||||
{
|
||||
Shader ourShader(vertexPath, fragmentPath);
|
||||
ourShader.use();
|
||||
const unsigned int vertexBufferObject = copyVerticesMemory(constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE, GL_ARRAY_BUFFER);
|
||||
const unsigned int vertexArrayObject = generateBindVAO();
|
||||
copyVerticesArray(vertexBufferObject, constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE, GL_ARRAY_BUFFER);
|
||||
|
||||
// set vertex attribute pointers
|
||||
configureVertexAttribute(false);
|
||||
doDrawArrays(ourShader.ID, vertexArrayObject, GL_TRIANGLES, 0, constants::SQUARE_VERTICES_SIZE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const GLenum drawType, const int numberOfElementsToDraw)
|
||||
{
|
||||
glUseProgram(shaderProgram);
|
||||
@ -112,8 +126,6 @@ void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexA
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void updateUniformColor(const unsigned int shaderProgram, const GLchar* uniformName)
|
||||
{
|
||||
// update the uniform color
|
||||
@ -134,7 +146,6 @@ void updateUniformColor(const unsigned int shaderProgram, const GLchar* uniformN
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered)
|
||||
{
|
||||
// use shader program to render an object
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
|
||||
int drawFigure(const int whatToDraw);
|
||||
int drawSquare(const char* vertexShaderSource, const char* fragmentShaderSource);
|
||||
int drawSquareClass(const char* vertexPath, const char* fragmentPath);
|
||||
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, const bool colorIncluded);
|
||||
int drawTriangleClass(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexPath, const char* fragmentPath, const bool colorIncluded);
|
||||
|
||||
Binary file not shown.
Loading…
Reference in New Issue
Block a user