mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-11-22 07:12:25 +01: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;
|
out vec4 FragColor;
|
||||||
void main()
|
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;
|
||||||
|
}
|
||||||
@ -43,6 +43,12 @@ namespace constants
|
|||||||
" vertexColor = vec3(0.5, 0.0, 0.0);\n" // dark red color
|
" vertexColor = vec3(0.5, 0.0, 0.0);\n" // dark red color
|
||||||
"}\0" } ;
|
"}\0" } ;
|
||||||
|
|
||||||
|
inline const char* VERTEX_SHADER_COLOR_FILENAME {
|
||||||
|
"./Shaders/vertexShaderColor.vs"
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// we write vertex shader
|
// 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
|
||||||
@ -67,6 +73,10 @@ namespace constants
|
|||||||
" vertexColor = aColor;\n"
|
" vertexColor = aColor;\n"
|
||||||
"}\0" } ;
|
"}\0" } ;
|
||||||
|
|
||||||
|
inline const char* VERTEX_SHADER_VERTICE_COLOR_FILENAME {
|
||||||
|
"./Shaders/vertexShaderVerticeColor.vs"
|
||||||
|
};
|
||||||
|
|
||||||
// ... to fragment shader (vertexColor)
|
// ... to fragment shader (vertexColor)
|
||||||
inline const char *FRAGMENT_SHADER_COLOR_FROM_VERTEX {
|
inline const char *FRAGMENT_SHADER_COLOR_FROM_VERTEX {
|
||||||
"#version 330 core\n"
|
"#version 330 core\n"
|
||||||
@ -78,6 +88,10 @@ namespace constants
|
|||||||
"}\0"
|
"}\0"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline const char* FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME {
|
||||||
|
"./Shaders/fragmentShaderColorFromVertex.fs"
|
||||||
|
};
|
||||||
|
|
||||||
// uniforms
|
// uniforms
|
||||||
inline const char *FRAGMENT_SHADER_UNIFORMS {
|
inline const char *FRAGMENT_SHADER_UNIFORMS {
|
||||||
"#version 330 core\n"
|
"#version 330 core\n"
|
||||||
@ -89,6 +103,10 @@ namespace constants
|
|||||||
"}\0"
|
"}\0"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline const char* FRAGMENT_SHADER_UNIFORMS_FILENAME {
|
||||||
|
"./Shaders/fragmentShaderUniforms.fs"
|
||||||
|
};
|
||||||
|
|
||||||
// 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 {
|
||||||
@ -115,6 +133,10 @@ namespace constants
|
|||||||
"}\0"
|
"}\0"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline const char* FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME {
|
||||||
|
"./Shaders/fragmentShaderSourceYellow.fs"
|
||||||
|
};
|
||||||
|
|
||||||
// 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
|
||||||
|
|||||||
@ -18,25 +18,25 @@ int drawFigure(const int whatToDraw)
|
|||||||
case 0:
|
case 0:
|
||||||
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, false);
|
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, false);
|
||||||
case 1:
|
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:
|
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, false);
|
return drawTriangleClass(constants::TRIANGLES_VERTICES, constants::TRIANGLES_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, 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, 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:
|
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, 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:
|
case 5:
|
||||||
// Get color from vertex shader to fragment shader
|
// 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:
|
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 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:
|
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 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:
|
case constants::MAX_DRAW_CALL:
|
||||||
// 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::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, false);
|
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;
|
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)
|
void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const GLenum drawType, const int numberOfElementsToDraw)
|
||||||
{
|
{
|
||||||
glUseProgram(shaderProgram);
|
glUseProgram(shaderProgram);
|
||||||
@ -112,8 +126,6 @@ void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexA
|
|||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void updateUniformColor(const unsigned int shaderProgram, const GLchar* uniformName)
|
void updateUniformColor(const unsigned int shaderProgram, const GLchar* uniformName)
|
||||||
{
|
{
|
||||||
// update the uniform color
|
// 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)
|
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
|
// use shader program to render an object
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
int drawFigure(const int whatToDraw);
|
int drawFigure(const int whatToDraw);
|
||||||
int drawSquare(const char* vertexShaderSource, const char* fragmentShaderSource);
|
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);
|
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 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);
|
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