mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-11-24 19:46:26 +01:00
feat: make code compliant with cpplint
This commit is contained in:
parent
553c5f7397
commit
a2adca27e5
11
.vscode/extensions.json
vendored
11
.vscode/extensions.json
vendored
@ -1,7 +1,6 @@
|
|||||||
{
|
{
|
||||||
"recommendations": [
|
"recommendations": [
|
||||||
"aaron-bond.better-comments",
|
"aaron-bond.better-comments",
|
||||||
"ms-vscode.cpptools",
|
|
||||||
"jbenden.c-cpp-flylint",
|
"jbenden.c-cpp-flylint",
|
||||||
"ms-vscode.cpptools-themes",
|
"ms-vscode.cpptools-themes",
|
||||||
"twxs.cmake",
|
"twxs.cmake",
|
||||||
@ -12,8 +11,14 @@
|
|||||||
"ms-vscode.makefile-tools",
|
"ms-vscode.makefile-tools",
|
||||||
"pkief.material-icon-theme",
|
"pkief.material-icon-theme",
|
||||||
"ms-vscode-remote.vscode-remote-extensionpack",
|
"ms-vscode-remote.vscode-remote-extensionpack",
|
||||||
"slevesque.shader",
|
|
||||||
"henriiik.vscode-sort",
|
"henriiik.vscode-sort",
|
||||||
"hbenl.test-adapter-converter"
|
"hbenl.test-adapter-converter",
|
||||||
|
"streetsidesoftware.code-spell-checker",
|
||||||
|
"wmaurer.change-case",
|
||||||
|
"usernamehw.errorlens",
|
||||||
|
"kisstkondoros.vscode-gutter-preview",
|
||||||
|
"davidanson.vscode-markdownlint",
|
||||||
|
"streetsidesoftware.code-spell-checker-polish",
|
||||||
|
"tyriar.sort-lines"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@ -99,6 +99,7 @@
|
|||||||
"awesomeface",
|
"awesomeface",
|
||||||
"kuchy",
|
"kuchy",
|
||||||
"multiplatform",
|
"multiplatform",
|
||||||
|
"RENDERLOOP",
|
||||||
"VERTICE"
|
"VERTICE"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -1,11 +1,11 @@
|
|||||||
// "Copyright [2023] <Krzysztof Rudnicki>"
|
// "Copyright [2023] <Krzysztof Rudnicki>"
|
||||||
#ifndef BEFORE_RENDER_CPP
|
#ifndef BEFORE_RENDER_CPP
|
||||||
#define BEFORE_RENDER_CPP
|
#define BEFORE_RENDER_CPP
|
||||||
#include "Engine/engine/beforeRender.hpp"
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "./beforeRender.hpp"
|
||||||
#include "./constants.hpp"
|
#include "./constants.hpp"
|
||||||
#include "./misc.hpp"
|
#include "./misc.hpp"
|
||||||
|
|
||||||
|
|||||||
@ -1,14 +1,17 @@
|
|||||||
// "Copyright [2023] <Krzysztof Rudnicki>"
|
// "Copyright [2023] <Krzysztof Rudnicki>"
|
||||||
#ifndef BEFORE_RENDER_HPP
|
#ifndef ENGINE_ENGINE_BEFORERENDER_HPP_
|
||||||
#define BEFORE_RENDER_HPP
|
#define ENGINE_ENGINE_BEFORERENDER_HPP_
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion);
|
void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion);
|
||||||
void instantiateGLFWwindow();
|
void instantiateGLFWwindow();
|
||||||
GLFWwindow *createWindowObject();
|
GLFWwindow *createWindowObject();
|
||||||
int initializeGLAD();
|
int initializeGLAD();
|
||||||
void framebuffer_size_callback(GLFWwindow *window, const int width, const int height);
|
void framebuffer_size_callback(
|
||||||
|
GLFWwindow *window,
|
||||||
|
const int width,
|
||||||
|
const int height);
|
||||||
void viewPort(GLFWwindow *window);
|
void viewPort(GLFWwindow *window);
|
||||||
GLFWwindow *prepareForRender();
|
GLFWwindow *prepareForRender();
|
||||||
|
|
||||||
#endif
|
#endif // ENGINE_ENGINE_BEFORERENDER_HPP_
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
// "Copyright [2023] <Krzysztof Rudnicki>"
|
// "Copyright [2023] <Krzysztof Rudnicki>"
|
||||||
#ifndef CONSTANTS_HPP
|
#ifndef ENGINE_ENGINE_CONSTANTS_HPP_
|
||||||
#define CONSTANTS_HPP
|
#define ENGINE_ENGINE_CONSTANTS_HPP_
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#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 };
|
||||||
@ -46,7 +46,6 @@ namespace constants
|
|||||||
inline const char* VERTEX_SHADER_OFFSET_FILENAME {
|
inline const char* VERTEX_SHADER_OFFSET_FILENAME {
|
||||||
"./Shaders/vertexShaderOffset.vs"
|
"./Shaders/vertexShaderOffset.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
|
||||||
@ -56,8 +55,7 @@ namespace constants
|
|||||||
"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_SOURCE_FILENAME {
|
inline const char* VERTEX_SHADER_SOURCE_FILENAME {
|
||||||
"./Shaders/vertexShaderSource.vs"
|
"./Shaders/vertexShaderSource.vs"
|
||||||
};
|
};
|
||||||
@ -101,7 +99,6 @@ namespace constants
|
|||||||
inline const char* FRAGMENT_SHADER_SOURCE_FILENAME {
|
inline const char* FRAGMENT_SHADER_SOURCE_FILENAME {
|
||||||
"./Shaders/fragmentShaderSource.fs"
|
"./Shaders/fragmentShaderSource.fs"
|
||||||
};
|
};
|
||||||
|
|
||||||
inline const char* FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME {
|
inline const char* FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME {
|
||||||
"./Shaders/fragmentShaderSourceYellow.fs"
|
"./Shaders/fragmentShaderSourceYellow.fs"
|
||||||
};
|
};
|
||||||
@ -119,7 +116,9 @@ namespace constants
|
|||||||
0.0f, 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 float TRIANGLES_VERTICES[] {
|
inline constexpr float TRIANGLES_VERTICES[] {
|
||||||
// first triangle
|
// first triangle
|
||||||
@ -132,7 +131,8 @@ namespace constants
|
|||||||
0.45f, 0.5f, 0.0f // top
|
0.45f, 0.5f, 0.0f // top
|
||||||
};
|
};
|
||||||
|
|
||||||
inline constexpr size_t TRIANGLES_VERTICES_SIZE = { sizeof(TRIANGLES_VERTICES) };
|
inline constexpr size_t TRIANGLES_VERTICES_SIZE = {
|
||||||
|
sizeof(TRIANGLES_VERTICES) };
|
||||||
|
|
||||||
inline constexpr float TRIANGLE_ONE[] {
|
inline constexpr float TRIANGLE_ONE[] {
|
||||||
// first triangle
|
// first triangle
|
||||||
@ -202,7 +202,9 @@ namespace constants
|
|||||||
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
|
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
|
||||||
};
|
};
|
||||||
|
|
||||||
inline constexpr size_t TEXTURE_VERTICES_SIZE = { sizeof(TEXTURE_VERTICES) };
|
inline constexpr size_t TEXTURE_VERTICES_SIZE = {
|
||||||
|
sizeof(TEXTURE_VERTICES)
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
inline constexpr unsigned int TEXTURE_INDICES[] {
|
inline constexpr unsigned int TEXTURE_INDICES[] {
|
||||||
@ -211,8 +213,6 @@ namespace constants
|
|||||||
};
|
};
|
||||||
|
|
||||||
inline constexpr size_t TEXTURE_INDICES_SIZE = { sizeof(TEXTURE_INDICES) };
|
inline constexpr size_t TEXTURE_INDICES_SIZE = { sizeof(TEXTURE_INDICES) };
|
||||||
|
} // namespace constants
|
||||||
|
|
||||||
|
#endif // ENGINE_ENGINE_CONSTANTS_HPP_
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
@ -6,69 +6,134 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include "draw.hpp"
|
#include <utility>
|
||||||
#include "renderLoop.hpp"
|
#include "./draw.hpp"
|
||||||
#include "shaders.hpp"
|
#include "./renderLoop.hpp"
|
||||||
#include "constants.hpp"
|
#include "./shaders.hpp"
|
||||||
#include "misc.hpp"
|
#include "./constants.hpp"
|
||||||
#include "shader.hpp"
|
#include "./misc.hpp"
|
||||||
#include "textures.hpp"
|
#include "./shader.hpp"
|
||||||
#include "stb_image.h"
|
#include "./textures.hpp"
|
||||||
|
#include "./stb_image.h"
|
||||||
|
|
||||||
drawFigureReturn drawFigure(const int whatToDraw)
|
drawFigureReturn drawFigure(const int whatToDraw) {
|
||||||
{
|
switch (whatToDraw) {
|
||||||
switch (whatToDraw)
|
|
||||||
{
|
|
||||||
case 0:
|
case 0:
|
||||||
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
return drawTriangleClass(
|
||||||
|
constants::TRIANGLE_VERTICES,
|
||||||
|
constants::TRIANGLE_VERTICES_SIZE,
|
||||||
|
constants::VERTEX_SHADER_SOURCE_FILENAME,
|
||||||
|
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
||||||
case 1:
|
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 drawSquare(
|
||||||
|
constants::SQUARE_VERTICES,
|
||||||
|
constants::SQUARE_VERTICES_SIZE,
|
||||||
|
constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE,
|
||||||
|
constants::VERTEX_SHADER_SOURCE,
|
||||||
|
constants::FRAGMENT_SHADER_SOURCE);
|
||||||
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
|
||||||
return drawTriangleClass(constants::TRIANGLES_VERTICES, constants::TRIANGLES_VERTICES_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
// using glDrawArrays by adding more vertices to your data.
|
||||||
|
return drawTriangleClass(
|
||||||
|
constants::TRIANGLES_VERTICES,
|
||||||
|
constants::TRIANGLES_VERTICES_SIZE,
|
||||||
|
constants::VERTEX_SHADER_SOURCE_FILENAME,
|
||||||
|
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
||||||
case 3:
|
case 3:
|
||||||
drawTriangleClass(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
drawTriangleClass(constants::TRIANGLE_ONE,
|
||||||
// Now create the same 2 triangles using two different VAOs and VBOs for their data
|
constants::TRIANGLE_ONE_SIZE,
|
||||||
return drawTriangleClass(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
constants::VERTEX_SHADER_SOURCE_FILENAME,
|
||||||
|
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
||||||
|
// Now create the same 2 triangles
|
||||||
|
// using two different VAOs and VBOs for their data
|
||||||
|
return drawTriangleClass(
|
||||||
|
constants::TRIANGLE_TWO,
|
||||||
|
constants::TRIANGLE_TWO_SIZE,
|
||||||
|
constants::VERTEX_SHADER_SOURCE_FILENAME,
|
||||||
|
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
||||||
case 4:
|
case 4:
|
||||||
drawTriangleClass(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
drawTriangleClass(constants::TRIANGLE_ONE,
|
||||||
// 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
|
constants::TRIANGLE_ONE_SIZE,
|
||||||
return drawTriangleClass(constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE, constants::VERTEX_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME);
|
constants::VERTEX_SHADER_SOURCE_FILENAME,
|
||||||
|
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
||||||
|
/*
|
||||||
|
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 drawTriangleClass(
|
||||||
|
constants::TRIANGLE_TWO,
|
||||||
|
constants::TRIANGLE_TWO_SIZE,
|
||||||
|
constants::VERTEX_SHADER_SOURCE_FILENAME,
|
||||||
|
constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME);
|
||||||
case 5:
|
case 5:
|
||||||
// Get color from vertex shader to fragment shader
|
// Get color from vertex shader to fragment shader
|
||||||
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_COLOR_FILENAME, constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME);
|
return drawTriangleClass(constants::TRIANGLE_VERTICES,
|
||||||
|
constants::TRIANGLE_VERTICES_SIZE,
|
||||||
|
constants::VERTEX_SHADER_COLOR_FILENAME,
|
||||||
|
constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME);
|
||||||
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::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_COLOR_FILENAME, constants::FRAGMENT_SHADER_UNIFORMS_FILENAME);
|
return drawTriangleClass(constants::TRIANGLE_VERTICES,
|
||||||
|
constants::TRIANGLE_VERTICES_SIZE,
|
||||||
|
constants::VERTEX_SHADER_COLOR_FILENAME,
|
||||||
|
constants::FRAGMENT_SHADER_UNIFORMS_FILENAME);
|
||||||
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(constants::TRIANGLE_COLORS, constants::TRIANGLE_COLORS_SIZE, constants::VERTEX_SHADER_VERTICE_COLOR_FILENAME, constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME, 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 8:
|
case 8:
|
||||||
// upside down triangle
|
// upside down triangle
|
||||||
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_UPSIDE_DOWN_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
return drawTriangleClass(constants::TRIANGLE_VERTICES,
|
||||||
|
constants::TRIANGLE_VERTICES_SIZE,
|
||||||
|
constants::VERTEX_SHADER_UPSIDE_DOWN_FILENAME,
|
||||||
|
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
||||||
case 9:
|
case 9:
|
||||||
{
|
{
|
||||||
// offset triangle
|
// offset triangle
|
||||||
offsetsStruct offsets = offsetsStruct();
|
offsetsStruct offsets = offsetsStruct();
|
||||||
offsets.xOffset = 0.5f;
|
offsets.xOffset = 0.5f;
|
||||||
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_OFFSET_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME, false, false, offsets);
|
return drawTriangleClass(constants::TRIANGLE_VERTICES,
|
||||||
|
constants::TRIANGLE_VERTICES_SIZE,
|
||||||
|
constants::VERTEX_SHADER_OFFSET_FILENAME,
|
||||||
|
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
offsets);
|
||||||
}
|
}
|
||||||
case 10:
|
case 10:
|
||||||
return drawTriangleClass(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, constants::VERTEX_SHADER_TASK_THREE_FILENAME, constants::FRAGMENT_SHADER_TASK_THREE_FILENAME);
|
return drawTriangleClass(constants::TRIANGLE_VERTICES,
|
||||||
|
constants::TRIANGLE_VERTICES_SIZE,
|
||||||
|
constants::VERTEX_SHADER_TASK_THREE_FILENAME,
|
||||||
|
constants::FRAGMENT_SHADER_TASK_THREE_FILENAME);
|
||||||
case constants::MAX_DRAW_CALL:
|
case constants::MAX_DRAW_CALL:
|
||||||
return drawDebilMode(constants::VERTEX_SHADER_TEXTURE_FILENAME, constants::FRAGMENT_SHADER_TEXTURE_FILENAME, constants::TEXTURE_VERTICES, constants::TEXTURE_VERTICES_SIZE, constants::TEXTURE_INDICES, constants::TEXTURE_INDICES_SIZE);
|
return drawDebilMode(
|
||||||
|
constants::VERTEX_SHADER_TEXTURE_FILENAME,
|
||||||
|
constants::FRAGMENT_SHADER_TEXTURE_FILENAME,
|
||||||
|
constants::TEXTURE_VERTICES,
|
||||||
|
constants::TEXTURE_VERTICES_SIZE,
|
||||||
|
constants::TEXTURE_INDICES,
|
||||||
|
constants::TEXTURE_INDICES_SIZE);
|
||||||
default:
|
default:
|
||||||
throw "No function for this draw call";
|
throw "No function for this draw call";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int getShaderProgram(const char* vertexShaderSource, const char* fragmentShaderSource)
|
unsigned int getShaderProgram(
|
||||||
{
|
const char* vertexShaderSource,
|
||||||
const std::pair<unsigned int, unsigned int> shaders = compileShaders(vertexShaderSource, fragmentShaderSource);
|
const char* fragmentShaderSource) {
|
||||||
|
const std::pair<unsigned int, unsigned int> shaders =
|
||||||
|
compileShaders(vertexShaderSource, fragmentShaderSource);
|
||||||
if (shaders.first == 0 || shaders.second == 0)
|
if (shaders.first == 0 || shaders.second == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
const unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second);
|
const unsigned int shaderProgram =
|
||||||
|
linkShaderObjectsShaderProgram(shaders.first, shaders.second);
|
||||||
|
|
||||||
if (shaderProgram == 0)
|
if (shaderProgram == 0)
|
||||||
return 0;
|
return 0;
|
||||||
return shaderProgram;
|
return shaderProgram;
|
||||||
@ -80,8 +145,14 @@ void setOffsets(Shader shader, const offsetsStruct offsets) {
|
|||||||
shader.setFloat("zOffset", offsets.zOffset);
|
shader.setFloat("zOffset", offsets.zOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
drawFigureReturn drawTriangleClass(const float triangleVertices[], const size_t triangleVerticesSize, const char* vertexPath, const char* fragmentPath, const bool colorIncluded, const bool textureIncluded, const offsetsStruct offsets)
|
drawFigureReturn drawTriangleClass(
|
||||||
{
|
const float triangleVertices[],
|
||||||
|
const size_t triangleVerticesSize,
|
||||||
|
const char* vertexPath,
|
||||||
|
const char* fragmentPath,
|
||||||
|
const bool colorIncluded,
|
||||||
|
const bool textureIncluded,
|
||||||
|
const offsetsStruct offsets) {
|
||||||
// In your CPP file:
|
// In your CPP file:
|
||||||
// ======================
|
// ======================
|
||||||
// float offset = 0.5f;
|
// float offset = 0.5f;
|
||||||
@ -90,12 +161,25 @@ drawFigureReturn drawTriangleClass(const float triangleVertices[], const size_t
|
|||||||
ourShader.use();
|
ourShader.use();
|
||||||
setOffsets(ourShader, offsets);
|
setOffsets(ourShader, offsets);
|
||||||
|
|
||||||
const unsigned int vertexBufferObject = copyVerticesMemory(triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
|
const unsigned int vertexBufferObject =
|
||||||
|
copyVerticesMemory(
|
||||||
|
triangleVertices,
|
||||||
|
triangleVerticesSize,
|
||||||
|
GL_ARRAY_BUFFER);
|
||||||
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
|
||||||
configureVertexAttribute(colorIncluded, textureIncluded);
|
configureVertexAttribute(colorIncluded, textureIncluded);
|
||||||
doDrawArrays(ourShader.ID, vertexArrayObject, GL_TRIANGLES, 0, triangleVerticesSize);
|
doDrawArrays(
|
||||||
|
ourShader.ID,
|
||||||
|
vertexArrayObject,
|
||||||
|
GL_TRIANGLES,
|
||||||
|
0,
|
||||||
|
triangleVerticesSize);
|
||||||
drawFigureReturn newReturn;
|
drawFigureReturn newReturn;
|
||||||
newReturn.success = 0;
|
newReturn.success = 0;
|
||||||
newReturn.VAO = vertexArrayObject;
|
newReturn.VAO = vertexArrayObject;
|
||||||
@ -104,9 +188,18 @@ drawFigureReturn drawTriangleClass(const float triangleVertices[], const size_t
|
|||||||
return newReturn;
|
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)
|
drawFigureReturn drawSquare(
|
||||||
{
|
const float squareVertices[],
|
||||||
const unsigned int shaderProgram = getShaderProgram(vertexShaderSource, fragmentShaderSource);
|
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();
|
drawFigureReturn newReturn = drawFigureReturn();
|
||||||
if (shaderProgram == 0) {
|
if (shaderProgram == 0) {
|
||||||
newReturn.success = -1;
|
newReturn.success = -1;
|
||||||
@ -114,34 +207,57 @@ drawFigureReturn drawSquare(const float squareVertices[], const size_t squareVer
|
|||||||
}
|
}
|
||||||
const unsigned int VAO = generateBindVAO();
|
const unsigned int VAO = generateBindVAO();
|
||||||
copyVerticesMemory(squareVertices, squareVerticesSize, GL_ARRAY_BUFFER);
|
copyVerticesMemory(squareVertices, squareVerticesSize, GL_ARRAY_BUFFER);
|
||||||
copyVerticesMemory(squareIndices, squareIndicesSize, GL_ELEMENT_ARRAY_BUFFER);
|
copyVerticesMemory(
|
||||||
|
squareIndices,
|
||||||
|
squareIndicesSize,
|
||||||
|
GL_ELEMENT_ARRAY_BUFFER);
|
||||||
|
|
||||||
// set vertex attribute pointers
|
// set vertex attribute pointers
|
||||||
configureVertexAttribute(colorIncluded, textureIncluded);
|
configureVertexAttribute(colorIncluded, textureIncluded);
|
||||||
|
|
||||||
doDrawElements(shaderProgram, VAO, GL_TRIANGLES, GL_UNSIGNED_INT, squareIndicesSize);
|
doDrawElements(
|
||||||
|
shaderProgram,
|
||||||
|
VAO,
|
||||||
|
GL_TRIANGLES,
|
||||||
|
GL_UNSIGNED_INT,
|
||||||
|
squareIndicesSize);
|
||||||
newReturn.success = 0;
|
newReturn.success = 0;
|
||||||
newReturn.VAO = VAO;
|
newReturn.VAO = VAO;
|
||||||
return newReturn;
|
return newReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
drawFigureReturn drawDebilMode(const char* vertexPath, const char* fragmentPath, const float vertices[], const size_t verticesSize, const unsigned int indices[], const size_t indicesSize) {
|
drawFigureReturn drawDebilMode(
|
||||||
|
const char* vertexPath,
|
||||||
|
const char* fragmentPath,
|
||||||
|
const float vertices[],
|
||||||
|
const size_t verticesSize,
|
||||||
|
const unsigned int indices[],
|
||||||
|
const size_t indicesSize) {
|
||||||
// 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, constants::FRAGMENT_SHADER_TEXTURE_FILENAME);
|
Shader ourShader(
|
||||||
|
constants::VERTEX_SHADER_TEXTURE_FILENAME,
|
||||||
|
constants::FRAGMENT_SHADER_TEXTURE_FILENAME);
|
||||||
|
|
||||||
unsigned int VAO = generateBindVAO();
|
unsigned int VAO = generateBindVAO();
|
||||||
unsigned int VBO = copyVerticesMemory(vertices, verticesSize, GL_ARRAY_BUFFER);
|
unsigned int VBO = copyVerticesMemory(
|
||||||
unsigned int EBO = copyVerticesMemory(indices, indicesSize, GL_ELEMENT_ARRAY_BUFFER);
|
vertices,
|
||||||
|
verticesSize,
|
||||||
|
GL_ARRAY_BUFFER);
|
||||||
|
unsigned int EBO = copyVerticesMemory(indices,
|
||||||
|
indicesSize,
|
||||||
|
GL_ELEMENT_ARRAY_BUFFER);
|
||||||
|
|
||||||
configureVertexAttribute(true, true);
|
configureVertexAttribute(true, true);
|
||||||
|
|
||||||
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!
|
ourShader.use(); // don't forget to activate/use
|
||||||
// either set it manually like so:
|
// the shader before setting uniforms!
|
||||||
|
// set it via the texture class
|
||||||
ourShader.setInt("texture1", 0);
|
ourShader.setInt("texture1", 0);
|
||||||
// or set it via the texture class
|
|
||||||
ourShader.setInt("texture2", 1);
|
ourShader.setInt("texture2", 1);
|
||||||
|
|
||||||
|
|
||||||
@ -158,23 +274,33 @@ 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)
|
void doDrawElements(
|
||||||
{
|
const unsigned int shaderProgram,
|
||||||
|
const unsigned int vertexArrayObject,
|
||||||
|
const GLenum drawArrayMode,
|
||||||
|
const GLenum drawType,
|
||||||
|
const int numberOfElementsToDraw) {
|
||||||
glUseProgram(shaderProgram);
|
glUseProgram(shaderProgram);
|
||||||
glBindVertexArray(vertexArrayObject);
|
glBindVertexArray(vertexArrayObject);
|
||||||
glDrawElements(drawArrayMode, numberOfElementsToDraw, drawType, 0);
|
glDrawElements(drawArrayMode, numberOfElementsToDraw, drawType, 0);
|
||||||
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
|
||||||
const float timeValue = glfwGetTime(); // retrieve running time
|
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
|
const float greenValue =
|
||||||
const int vertexColorLocation = glGetUniformLocation(shaderProgram, uniformName); // query the location of our uniform
|
sin(timeValue) / 2.0f + 0.5f;
|
||||||
if(vertexColorLocation != -1) // if glGetUniformLocation returns -1 it could not find the location
|
// vary the color from 0.0 to 1.0 using sin
|
||||||
{
|
// query the location of our uniform
|
||||||
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f); /* we set the uniform value using glUniform4f
|
const int vertexColorLocation =
|
||||||
|
glGetUniformLocation(shaderProgram, uniformName);
|
||||||
|
// if glGetUniformLocation returns -1 it could not find the location
|
||||||
|
if (vertexColorLocation != -1) {
|
||||||
|
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
|
||||||
|
/* we set the uniform value using glUniform4f
|
||||||
4f means that it expects 4 floats
|
4f means that it expects 4 floats
|
||||||
few of possible postfixes:
|
few of possible postfixes:
|
||||||
f: the function expects a float as its value.
|
f: the function expects a float as its value.
|
||||||
@ -186,8 +312,12 @@ 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
|
||||||
glUseProgram(shaderProgram);
|
glUseProgram(shaderProgram);
|
||||||
updateUniformColor(shaderProgram, "ourColor");
|
updateUniformColor(shaderProgram, "ourColor");
|
||||||
@ -199,4 +329,4 @@ void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArr
|
|||||||
glDrawArrays(drawArrayMode, firstIndex, numberOfIndicesToBeRendered);
|
glDrawArrays(drawArrayMode, firstIndex, numberOfIndicesToBeRendered);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif // #ifndef DRAW_CPP
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
// "Copyright [2023] <Krzysztof Rudnicki>"
|
// "Copyright [2023] <Krzysztof Rudnicki>"
|
||||||
#ifndef DRAW_HPP
|
#ifndef ENGINE_ENGINE_DRAW_HPP_
|
||||||
#define DRAW_HPP
|
#define ENGINE_ENGINE_DRAW_HPP_
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "constants.hpp"
|
#include "./constants.hpp"
|
||||||
|
|
||||||
struct offsetsStruct {
|
struct offsetsStruct {
|
||||||
offsetsStruct(): xOffset(0), yOffset(0), zOffset(0) { }
|
offsetsStruct(): xOffset(0), yOffset(0), zOffset(0) { }
|
||||||
@ -12,7 +12,7 @@ struct offsetsStruct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct drawFigureReturn {
|
struct drawFigureReturn {
|
||||||
drawFigureReturn(): success(-1), VAO(0), VBO(0), EBO(0){ }
|
drawFigureReturn(): success(-1), VAO(0), VBO(0), EBO(0) { }
|
||||||
int success;
|
int success;
|
||||||
unsigned int VAO;
|
unsigned int VAO;
|
||||||
unsigned int VBO;
|
unsigned int VBO;
|
||||||
@ -20,11 +20,51 @@ 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 float triangleVertices[], const size_t triangleVerticesSize, const char* vertexPath, const char* fragmentPath, const bool colorIncluded = false, const bool textureIncluded = false, const offsetsStruct offsets = offsetsStruct());
|
|
||||||
void updateUniformColor(const unsigned int shaderProgram, const GLchar* uniformName);
|
|
||||||
void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArrayObject, const GLenum drawArrayMode, const int firstIndex, const unsigned int numberOfIndicesToBeRendered );
|
|
||||||
drawFigureReturn drawDebilMode(const char* vertexPath, const char* fragmentPath, const float vertices[], const size_t verticesSize, const unsigned int indices[], const size_t indicesSize);
|
|
||||||
|
|
||||||
#endif
|
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 float triangleVertices[],
|
||||||
|
const size_t triangleVerticesSize,
|
||||||
|
const char* vertexPath,
|
||||||
|
const char* fragmentPath,
|
||||||
|
const bool colorIncluded = false,
|
||||||
|
const bool textureIncluded = false,
|
||||||
|
const offsetsStruct offsets = offsetsStruct());
|
||||||
|
|
||||||
|
void updateUniformColor(
|
||||||
|
const unsigned int shaderProgram,
|
||||||
|
const GLchar* uniformName);
|
||||||
|
|
||||||
|
void doDrawArrays(
|
||||||
|
const unsigned int shaderProgram,
|
||||||
|
const unsigned int vertexArrayObject,
|
||||||
|
const GLenum drawArrayMode,
|
||||||
|
const int firstIndex,
|
||||||
|
const unsigned int numberOfIndicesToBeRendered);
|
||||||
|
|
||||||
|
drawFigureReturn drawDebilMode(
|
||||||
|
const char* vertexPath,
|
||||||
|
const char* fragmentPath,
|
||||||
|
const float vertices[],
|
||||||
|
const size_t verticesSize,
|
||||||
|
const unsigned int indices[],
|
||||||
|
const size_t indicesSize);
|
||||||
|
|
||||||
|
#endif // ENGINE_ENGINE_DRAW_HPP_
|
||||||
|
|||||||
Binary file not shown.
@ -5,17 +5,18 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <random> // I am using standart library RNG because I am lazy and wanted to create quick code snippet
|
#include <random>
|
||||||
// upgrade to this: https://arvid.io/2018/06/30/on-cxx-random-number-generator-quality/ whenever, if ever I feel like it
|
// I am using standard library RNG because
|
||||||
#include <chrono> // for std::chrono
|
// I am lazy and wanted to create quick code snippet upgrade to this:
|
||||||
|
// https://arvid.io/2018/06/30/on-cxx-random-number-generator-quality/
|
||||||
|
// whenever, if ever I feel like it
|
||||||
|
|
||||||
#include "constants.hpp"
|
#include "./constants.hpp"
|
||||||
#include "beforeRender.hpp"
|
#include "./beforeRender.hpp"
|
||||||
#include "misc.hpp"
|
#include "./misc.hpp"
|
||||||
#include "renderLoop.hpp"
|
#include "./renderLoop.hpp"
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
// changed in glfwWindowShouldClose
|
// changed in glfwWindowShouldClose
|
||||||
GLFWwindow *window = prepareForRender();
|
GLFWwindow *window = prepareForRender();
|
||||||
if (window == nullptr)
|
if (window == nullptr)
|
||||||
@ -27,4 +28,4 @@ int main()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif // #ifndef MAIN_CPP
|
||||||
|
|||||||
@ -2,9 +2,8 @@
|
|||||||
#ifndef MISC_CPP
|
#ifndef MISC_CPP
|
||||||
#define MISC_CPP
|
#define MISC_CPP
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "misc.hpp"
|
#include "./misc.hpp"
|
||||||
void print(const std::string s)
|
void print(const std::string s) {
|
||||||
{
|
|
||||||
std::cout << s << std::endl;
|
std::cout << s << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
// "Copyright [2023] <Krzysztof Rudnicki>"
|
// "Copyright [2023] <Krzysztof Rudnicki>"
|
||||||
#ifndef MISC_HPP
|
#ifndef ENGINE_ENGINE_MISC_HPP_
|
||||||
#define MISC_HPP
|
#define ENGINE_ENGINE_MISC_HPP_
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
void print(const std::string s);
|
void print(const std::string s);
|
||||||
#endif
|
#endif // ENGINE_ENGINE_MISC_HPP_
|
||||||
|
|||||||
@ -4,20 +4,22 @@
|
|||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "renderLoop.hpp"
|
#include "./renderLoop.hpp"
|
||||||
#include "draw.hpp"
|
#include "./draw.hpp"
|
||||||
#include "shaders.hpp"
|
#include "./shaders.hpp"
|
||||||
#include "constants.hpp"
|
#include "./constants.hpp"
|
||||||
#include "misc.hpp"
|
#include "./misc.hpp"
|
||||||
|
|
||||||
int processInput(GLFWwindow *window, const int whatToDraw)
|
int processInput(GLFWwindow *window, const int whatToDraw) {
|
||||||
{
|
|
||||||
static bool lockedLeft = false;
|
static bool lockedLeft = false;
|
||||||
static bool lockedRight = false;
|
static bool lockedRight = false;
|
||||||
const bool PRESSED_CHANGE_LEFT = (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS);
|
const bool PRESSED_CHANGE_LEFT =
|
||||||
const bool PRESSED_CHANGE_RIGHT = (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS);
|
(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
|
||||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
@ -27,13 +29,11 @@ int processInput(GLFWwindow *window, const int whatToDraw)
|
|||||||
lockedLeft = 0;
|
lockedLeft = 0;
|
||||||
if ( !PRESSED_CHANGE_RIGHT )
|
if ( !PRESSED_CHANGE_RIGHT )
|
||||||
lockedRight = 0;
|
lockedRight = 0;
|
||||||
if ( PRESSED_CHANGE_RIGHT && lockedRight == 0 )
|
if ( PRESSED_CHANGE_RIGHT && lockedRight == 0 ) {
|
||||||
{
|
|
||||||
lockedRight = 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 )
|
if ( PRESSED_CHANGE_LEFT && lockedLeft == 0 ) {
|
||||||
{
|
|
||||||
lockedLeft = 1;
|
lockedLeft = 1;
|
||||||
return (whatToDraw == 0 ? constants::MAX_DRAW_CALL : whatToDraw - 1);
|
return (whatToDraw == 0 ? constants::MAX_DRAW_CALL : whatToDraw - 1);
|
||||||
}
|
}
|
||||||
@ -41,15 +41,19 @@ int processInput(GLFWwindow *window, const int whatToDraw)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// https://stackoverflow.com/a/25680092
|
// https://stackoverflow.com/a/25680092
|
||||||
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) {
|
||||||
// stores vertices in gpu memory
|
// stores vertices in gpu memory
|
||||||
unsigned int vertexBufferObject;
|
unsigned int vertexBufferObject;
|
||||||
// this is open gl object so we refer it by its ID generated here and stored in vertexBufferObject variable
|
// this is open gl object so we refer
|
||||||
|
// it by its ID generated here and stored in vertexBufferObject variable
|
||||||
glGenBuffers(1, &vertexBufferObject);
|
glGenBuffers(1, &vertexBufferObject);
|
||||||
// buffer type of vertex buffer object is GL_ARRAY_BUFFER
|
// buffer type of vertex buffer object is GL_ARRAY_BUFFER
|
||||||
glBindBuffer(boundBufferTarget, vertexBufferObject);
|
glBindBuffer(boundBufferTarget, vertexBufferObject);
|
||||||
// now whenever we change GL_ARRAY_BUFFER we change bound buffer vertexBufferObject
|
// now whenever we change GL_ARRAY_BUFFER
|
||||||
|
// we change bound buffer vertexBufferObject
|
||||||
|
|
||||||
/* we copy vertex data into buffer memory
|
/* we copy vertex data into buffer memory
|
||||||
GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few times.
|
GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few times.
|
||||||
@ -60,15 +64,21 @@ unsigned int copyVerticesMemory(const float vertices[], const size_t sizeOfVerti
|
|||||||
return vertexBufferObject;
|
return vertexBufferObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int copyVerticesMemory(const unsigned int vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget)
|
unsigned int copyVerticesMemory(
|
||||||
{
|
const unsigned int vertices[],
|
||||||
|
const size_t sizeOfVertices,
|
||||||
|
const GLenum boundBufferTarget) {
|
||||||
// stores vertices in gpu memory
|
// stores vertices in gpu memory
|
||||||
unsigned int vertexBufferObject;
|
unsigned int vertexBufferObject;
|
||||||
// this is open gl object so we refer it by its ID generated here and stored in vertexBufferObject variable
|
// this is open gl object so we refer it
|
||||||
|
// by its ID generated here and stored in vertexBufferObject variable
|
||||||
glGenBuffers(1, &vertexBufferObject);
|
glGenBuffers(1, &vertexBufferObject);
|
||||||
// buffer type of vertex buffer object is GL_ARRAY_BUFFER
|
// buffer type of vertex buffer object is GL_ARRAY_BUFFER
|
||||||
glBindBuffer(boundBufferTarget, vertexBufferObject);
|
glBindBuffer(
|
||||||
// now whenever we change GL_ARRAY_BUFFER we change bound buffer vertexBufferObject
|
boundBufferTarget,
|
||||||
|
vertexBufferObject);
|
||||||
|
// now whenever we change GL_ARRAY_BUFFER
|
||||||
|
// we change bound buffer vertexBufferObject
|
||||||
|
|
||||||
/* we copy vertex data into buffer memory
|
/* we copy vertex data into buffer memory
|
||||||
GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few times.
|
GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few times.
|
||||||
@ -79,8 +89,9 @@ unsigned int copyVerticesMemory(const unsigned int vertices[], const size_t size
|
|||||||
return vertexBufferObject;
|
return vertexBufferObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
void configureVertexAttribute(const bool colorIncluded, const bool textureIncluded)
|
void configureVertexAttribute(
|
||||||
{
|
const bool colorIncluded,
|
||||||
|
const bool textureIncluded) {
|
||||||
/* 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))
|
||||||
@ -91,8 +102,10 @@ void configureVertexAttribute(const bool colorIncluded, const bool textureInclud
|
|||||||
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 */
|
||||||
if(!colorIncluded) {
|
if (!colorIncluded) {
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
|
glVertexAttribPointer(
|
||||||
|
0, 3, GL_FLOAT, GL_FALSE,
|
||||||
|
3 * sizeof(float), reinterpret_cast<void *>(0));
|
||||||
// enable vertex attribute
|
// enable vertex attribute
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
return;
|
return;
|
||||||
@ -103,37 +116,47 @@ void configureVertexAttribute(const bool colorIncluded, const bool textureInclud
|
|||||||
/* we change attribute location, color values have size of 3 floats
|
/* 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
|
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 */
|
https://learnopengl.com/img/getting-started/vertex_attribute_pointer_interleaved.png */
|
||||||
|
if (colorIncluded && !textureIncluded) {
|
||||||
if(colorIncluded && !textureIncluded) {
|
glVertexAttribPointer(
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
|
0, 3, GL_FLOAT, GL_FALSE,
|
||||||
|
6 * sizeof(float), reinterpret_cast<void *>(0));
|
||||||
// enable vertex attribute
|
// enable vertex attribute
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3* sizeof(float)));
|
glVertexAttribPointer(
|
||||||
|
1, 3, GL_FLOAT, GL_FALSE,
|
||||||
|
6 * sizeof(float), reinterpret_cast<void *>(3* sizeof(float)));
|
||||||
// enable vertex attribute
|
// enable vertex attribute
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Since we've added an extra vertex attribute we again have to notify OpenGL of the new vertex format
|
// Since we've added an extra vertex
|
||||||
// this time for textures we add just 2 more attributes and not 3 as with color
|
// attribute we again have to notify OpenGL of the new vertex format
|
||||||
if(textureIncluded) {
|
// this time for textures we add just
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
|
// 2 more attributes and not 3 as with color
|
||||||
|
if (textureIncluded) {
|
||||||
|
glVertexAttribPointer(
|
||||||
|
0, 3, GL_FLOAT, GL_FALSE,
|
||||||
|
8 * sizeof(float), reinterpret_cast<void *>(0));
|
||||||
// enable vertex attribute
|
// enable vertex attribute
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)(3* sizeof(float)));
|
glVertexAttribPointer(
|
||||||
|
1, 3, GL_FLOAT, GL_FALSE,
|
||||||
|
8 * sizeof(float), reinterpret_cast<void *>(3* sizeof(float)));
|
||||||
// enable vertex attribute
|
// enable vertex attribute
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
|
glVertexAttribPointer(
|
||||||
|
2, 2, GL_FLOAT, GL_FALSE,
|
||||||
|
8 * sizeof(float), reinterpret_cast<void*>(6 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(2);
|
glEnableVertexAttribArray(2);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int generateBindVAO()
|
unsigned int generateBindVAO() {
|
||||||
{
|
|
||||||
// vertex array object is used to draw objects by binding them to vao
|
// vertex array object is used to draw objects by binding them to vao
|
||||||
// generate vao
|
// generate vao
|
||||||
unsigned int vertexArrayObject;
|
unsigned int vertexArrayObject;
|
||||||
@ -143,40 +166,50 @@ unsigned int generateBindVAO()
|
|||||||
return vertexArrayObject;
|
return vertexArrayObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
void copyVerticesArray(const unsigned int vertexBufferObject, const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget)
|
void copyVerticesArray(
|
||||||
{
|
const unsigned int vertexBufferObject,
|
||||||
|
const float vertices[],
|
||||||
|
const size_t sizeOfVertices,
|
||||||
|
const GLenum boundBufferTarget) {
|
||||||
// copy vertices array in array useful for OGL
|
// copy vertices array in array useful for OGL
|
||||||
glBindBuffer(boundBufferTarget, vertexBufferObject);
|
glBindBuffer(boundBufferTarget, vertexBufferObject);
|
||||||
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
|
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
|
||||||
}
|
}
|
||||||
|
|
||||||
int renderLoopInside(GLFWwindow *window, int whatToDraw)
|
int renderLoopInside(GLFWwindow *window, int whatToDraw) {
|
||||||
{
|
|
||||||
// input
|
// input
|
||||||
whatToDraw = processInput(window, whatToDraw);
|
whatToDraw = processInput(window, whatToDraw);
|
||||||
// We specify the color to clear the screen with
|
// We specify the color to clear the screen with
|
||||||
// RGB and alpha value
|
// RGB and alpha value
|
||||||
glClearColor(constants::LEARN_OPEN_GL_COLOR.red, constants::LEARN_OPEN_GL_COLOR.green, constants::LEARN_OPEN_GL_COLOR.blue, constants::LEARN_OPEN_GL_COLOR.alpha);
|
glClearColor(
|
||||||
// There is GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT
|
constants::LEARN_OPEN_GL_COLOR.red,
|
||||||
|
constants::LEARN_OPEN_GL_COLOR.green,
|
||||||
|
constants::LEARN_OPEN_GL_COLOR.blue,
|
||||||
|
constants::LEARN_OPEN_GL_COLOR.alpha);
|
||||||
|
// There is
|
||||||
|
// GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
|
||||||
drawFigureReturn successAndVao = drawFigure(whatToDraw);
|
drawFigureReturn successAndVao = drawFigure(whatToDraw);
|
||||||
if (successAndVao.success == -1)
|
if (successAndVao.success == -1) {
|
||||||
{
|
|
||||||
print("error with drawing!");
|
print("error with drawing!");
|
||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
};
|
}
|
||||||
|
|
||||||
// swaps buffer containing color values of each pixel in window
|
// swaps buffer containing color values of each pixel in window
|
||||||
// there is front buffer (final image) and back buffer (where all rendering commands draw to)
|
// there is front buffer (final image)
|
||||||
// when back buffer is ready we swap it with front buffer to eliminate flickering
|
// and back buffer (where all rendering commands draw to)
|
||||||
|
// when back buffer is ready we swap it
|
||||||
|
// with front buffer to eliminate flickering
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
|
||||||
// glfwPollEvents checks if any event (like mouse/keyboard input was triggered), updates window state and calls functions (which we register via callback methods)
|
// glfwPollEvents checks if any event
|
||||||
|
// (like mouse/keyboard input was triggered),
|
||||||
|
// updates window state and calls functions
|
||||||
|
// (which we register via callback methods)
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
// std::cout << "successAndVao.VAO" << successAndVao.VAO << "successAndVao.VBO" << successAndVao.VBO << "successAndVao.EBO" << successAndVao.EBO << std::endl;
|
|
||||||
glDeleteVertexArrays(1, &successAndVao.VAO);
|
glDeleteVertexArrays(1, &successAndVao.VAO);
|
||||||
glDeleteBuffers(1, &successAndVao.VBO);
|
glDeleteBuffers(1, &successAndVao.VBO);
|
||||||
glDeleteBuffers(1, &successAndVao.EBO);
|
glDeleteBuffers(1, &successAndVao.EBO);
|
||||||
@ -185,12 +218,10 @@ int renderLoopInside(GLFWwindow *window, int whatToDraw)
|
|||||||
return whatToDraw;
|
return whatToDraw;
|
||||||
}
|
}
|
||||||
|
|
||||||
void renderLoop(GLFWwindow *window)
|
void renderLoop(GLFWwindow *window) {
|
||||||
{
|
|
||||||
int whatToDraw = 0;
|
int whatToDraw = 0;
|
||||||
// glfwWindowShouldClose checks if GLFW was instructed to close
|
// glfwWindowShouldClose checks if GLFW was instructed to close
|
||||||
while (!glfwWindowShouldClose(window))
|
while (!glfwWindowShouldClose(window)) {
|
||||||
{
|
|
||||||
whatToDraw = renderLoopInside(window, whatToDraw);
|
whatToDraw = renderLoopInside(window, whatToDraw);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,17 +1,34 @@
|
|||||||
// "Copyright [2023] <Krzysztof Rudnicki>"
|
// "Copyright [2023] <Krzysztof Rudnicki>"
|
||||||
#ifndef RENDER_LOOP_HPP
|
#ifndef ENGINE_ENGINE_RENDERLOOP_HPP_
|
||||||
#define RENDER_LOOP_HPP
|
#define ENGINE_ENGINE_RENDERLOOP_HPP_
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void renderLoop(GLFWwindow *window);
|
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(const bool colorIncluded, const bool textureIncluded);
|
|
||||||
|
void configureVertexAttribute(
|
||||||
|
const bool colorIncluded,
|
||||||
|
const bool textureIncluded);
|
||||||
|
|
||||||
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(
|
||||||
unsigned int copyVerticesMemory(const unsigned int vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);
|
const float vertices[],
|
||||||
|
const size_t sizeOfVertices,
|
||||||
|
const GLenum boundBufferTarget);
|
||||||
|
|
||||||
#endif
|
unsigned int copyVerticesMemory(
|
||||||
|
const unsigned int vertices[],
|
||||||
|
const size_t sizeOfVertices,
|
||||||
|
const GLenum boundBufferTarget);
|
||||||
|
|
||||||
|
#endif // ENGINE_ENGINE_RENDERLOOP_HPP_
|
||||||
|
|||||||
@ -8,17 +8,15 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "shader.hpp"
|
#include "./shader.hpp"
|
||||||
|
|
||||||
const std::string Shader::fileToShader(const std::string shaderPath)
|
const std::string Shader::fileToShader(const std::string shaderPath) {
|
||||||
{
|
|
||||||
// 1. retrieve the vertex/fragment source code from filePath
|
// 1. retrieve the vertex/fragment source code from filePath
|
||||||
std::string shaderCode;
|
std::string shaderCode;
|
||||||
std::ifstream shaderFile;
|
std::ifstream shaderFile;
|
||||||
// ensure ifstream objects can throw exceptions:
|
// ensure ifstream objects can throw exceptions:
|
||||||
shaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
// open files
|
// open files
|
||||||
shaderFile.open(shaderPath);
|
shaderFile.open(shaderPath);
|
||||||
std::stringstream shaderStream;
|
std::stringstream shaderStream;
|
||||||
@ -29,17 +27,20 @@ const std::string Shader::fileToShader(const std::string shaderPath)
|
|||||||
// convert stream into string
|
// convert stream into string
|
||||||
shaderCode = shaderStream.str();
|
shaderCode = shaderStream.str();
|
||||||
}
|
}
|
||||||
catch (std::ifstream::failure& e)
|
catch (std::ifstream::failure& e) {
|
||||||
{
|
std::cout <<
|
||||||
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl;
|
"ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: "
|
||||||
|
<< e.what() << std::endl;
|
||||||
}
|
}
|
||||||
return shaderCode;
|
return shaderCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
Shader::Shader(const std::string vertexPath, const std::string fragmentPath)
|
Shader::Shader(const std::string vertexPath, const std::string fragmentPath) {
|
||||||
{
|
|
||||||
const std::string vshaderCodeString = this -> fileToShader(vertexPath);
|
const std::string vshaderCodeString = this -> fileToShader(vertexPath);
|
||||||
const std::string fShaderCodeString = this -> fileToShader(fragmentPath);
|
|
||||||
|
const std::string fShaderCodeString =
|
||||||
|
this -> fileToShader(fragmentPath);
|
||||||
|
|
||||||
const char* vShaderCode = vshaderCodeString.c_str();
|
const char* vShaderCode = vshaderCodeString.c_str();
|
||||||
const char * fShaderCode = fShaderCodeString.c_str();
|
const char * fShaderCode = fShaderCodeString.c_str();
|
||||||
// 2. compile shaders
|
// 2. compile shaders
|
||||||
@ -60,51 +61,52 @@ Shader::Shader(const std::string vertexPath, const std::string fragmentPath)
|
|||||||
glAttachShader(ID, fragment);
|
glAttachShader(ID, fragment);
|
||||||
glLinkProgram(ID);
|
glLinkProgram(ID);
|
||||||
checkCompileErrors(ID, "PROGRAM");
|
checkCompileErrors(ID, "PROGRAM");
|
||||||
// delete the shaders as they're linked into our program now and no longer necessary
|
// delete the shaders as they're linked
|
||||||
|
// into our program now and no longer necessary
|
||||||
glDeleteShader(vertex);
|
glDeleteShader(vertex);
|
||||||
glDeleteShader(fragment);
|
glDeleteShader(fragment);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::use()
|
void Shader::use() {
|
||||||
{
|
|
||||||
glUseProgram(ID);
|
glUseProgram(ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setBool(const std::string &name, bool value) const
|
void Shader::setBool(const std::string &name, bool value) const {
|
||||||
{
|
glUniform1i(
|
||||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
|
glGetUniformLocation(ID, name.c_str()),
|
||||||
|
reinterpret_cast<int>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setInt(const std::string &name, int value) const
|
void Shader::setInt(const std::string &name, int value) const {
|
||||||
{
|
|
||||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setFloat(const std::string &name, float value) const
|
void Shader::setFloat(const std::string &name, float value) const {
|
||||||
{
|
|
||||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::checkCompileErrors(unsigned int shader, std::string type)
|
void Shader::checkCompileErrors(unsigned int shader, std::string type) {
|
||||||
{
|
|
||||||
int success;
|
int success;
|
||||||
char infoLog[1024];
|
char infoLog[1024];
|
||||||
if (type != "PROGRAM")
|
if (type != "PROGRAM") {
|
||||||
{
|
|
||||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||||
if (!success)
|
if (!success) {
|
||||||
{
|
|
||||||
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
|
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
|
||||||
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
|
std::cout <<
|
||||||
|
"ERROR::SHADER_COMPILATION_ERROR of type: "
|
||||||
|
<< type << "\n" << infoLog <<
|
||||||
|
"\n -- --------------------------------------------------- -- "
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
||||||
if (!success)
|
if (!success) {
|
||||||
{
|
|
||||||
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
|
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
|
||||||
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
|
std::cout <<
|
||||||
|
"ERROR::PROGRAM_LINKING_ERROR of type: "
|
||||||
|
<< type << "\n" << infoLog <<
|
||||||
|
"\n -- --------------------------------------------------- -- "
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
// "Copyright [2023] <Krzysztof Rudnicki>"
|
// "Copyright [2023] <Krzysztof Rudnicki>"
|
||||||
#ifndef SHADER_HPP
|
#ifndef ENGINE_ENGINE_SHADER_HPP_
|
||||||
#define SHADER_HPP
|
#define ENGINE_ENGINE_SHADER_HPP_
|
||||||
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
|
||||||
@ -9,9 +9,8 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
class Shader
|
class Shader {
|
||||||
{
|
public:
|
||||||
public:
|
|
||||||
unsigned int ID;
|
unsigned int ID;
|
||||||
// constructor generates the shader on the fly
|
// constructor generates the shader on the fly
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
@ -28,10 +27,15 @@ public:
|
|||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
void setFloat(const std::string &name, float value) const;
|
void setFloat(const std::string &name, float value) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// utility function for checking shader compilation/linking errors.
|
// utility function for checking shader compilation/linking errors.
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
void checkCompileErrors(unsigned int shader, std::string type);
|
void checkCompileErrors(unsigned int shader, std::string type);
|
||||||
void compileErrorsMessage(const unsigned int shader, const bool compilation, const std::string type, const std::string errorMessage);
|
|
||||||
|
void compileErrorsMessage(
|
||||||
|
const unsigned int shader,
|
||||||
|
const bool compilation,
|
||||||
|
const std::string type,
|
||||||
|
const std::string errorMessage);
|
||||||
};
|
};
|
||||||
#endif
|
#endif // ENGINE_ENGINE_SHADER_HPP_
|
||||||
|
|||||||
@ -3,12 +3,13 @@
|
|||||||
#define SHADERS_CPP
|
#define SHADERS_CPP
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include "shaders.hpp"
|
#include "./shaders.hpp"
|
||||||
#include "constants.hpp"
|
#include "./constants.hpp"
|
||||||
#include "misc.hpp"
|
#include "./misc.hpp"
|
||||||
|
|
||||||
unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders, const unsigned int fragmentShader)
|
unsigned int linkShaderObjectsShaderProgram(
|
||||||
{
|
const unsigned int vertexShaders,
|
||||||
|
const unsigned int fragmentShader) {
|
||||||
// link shader objects into shader program
|
// link shader objects into shader program
|
||||||
// will store shader program id
|
// will store shader program id
|
||||||
// creates program
|
// creates program
|
||||||
@ -27,7 +28,8 @@ unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders, co
|
|||||||
// after that every shader and rendering call will use this program object
|
// after that every shader and rendering call will use this program object
|
||||||
glUseProgram(shaderProgram);
|
glUseProgram(shaderProgram);
|
||||||
|
|
||||||
// delete shaders (they are linked into shaderProgram and we do not need them anymore)
|
// delete shaders
|
||||||
|
// (they are linked into shaderProgram and we do not need them anymore)
|
||||||
glDeleteShader(vertexShaders);
|
glDeleteShader(vertexShaders);
|
||||||
glDeleteShader(fragmentShader);
|
glDeleteShader(fragmentShader);
|
||||||
if (shaderProgram == 0)
|
if (shaderProgram == 0)
|
||||||
@ -35,13 +37,16 @@ unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders, co
|
|||||||
return shaderProgram;
|
return shaderProgram;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int compileShader(const GLenum shaderType, const char *shaderSource)
|
unsigned int compileShader(
|
||||||
{
|
const GLenum shaderType,
|
||||||
|
const char *shaderSource) {
|
||||||
// we create vertex shader and assign its id to shader variable
|
// we create vertex shader and assign its id to shader variable
|
||||||
const unsigned int shaderID = glCreateShader(shaderType);
|
const unsigned int shaderID = glCreateShader(shaderType);
|
||||||
|
|
||||||
// attach shader source code to shader object
|
// attach shader source code to shader object
|
||||||
// from left: shader object to compile, how many strings as source code, actual source code (we leave the 4th as nullptr)
|
// from left: shader object to compile,
|
||||||
|
// how many strings as source code, actual source code
|
||||||
|
// (we leave the 4th as nullptr)
|
||||||
glShaderSource(shaderID, 1, &shaderSource, nullptr);
|
glShaderSource(shaderID, 1, &shaderSource, nullptr);
|
||||||
// compile shader
|
// compile shader
|
||||||
glCompileShader(shaderID);
|
glCompileShader(shaderID);
|
||||||
@ -50,26 +55,28 @@ unsigned int compileShader(const GLenum shaderType, const char *shaderSource)
|
|||||||
return shaderID;
|
return shaderID;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<unsigned int, unsigned int> compileShaders(const char* vertexShaderSource, const char* fragmentShaderSource)
|
std::pair<unsigned int, unsigned int> compileShaders(
|
||||||
{
|
const char* vertexShaderSource,
|
||||||
const unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
|
const char* fragmentShaderSource) {
|
||||||
if (vertexShader == 0)
|
const unsigned int vertexShader =
|
||||||
{
|
compileShader(GL_VERTEX_SHADER, vertexShaderSource);
|
||||||
|
|
||||||
|
if (vertexShader == 0) {
|
||||||
print("Vertex Shader Compilation Failed");
|
print("Vertex Shader Compilation Failed");
|
||||||
return std::make_pair(0, 0);
|
return std::make_pair(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
|
const unsigned int fragmentShader =
|
||||||
if (fragmentShader == 0)
|
compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
|
||||||
{
|
|
||||||
|
if (fragmentShader == 0) {
|
||||||
print("Fragment Shader Compilation Failed");
|
print("Fragment Shader Compilation Failed");
|
||||||
return std::make_pair(0, 0);
|
return std::make_pair(0, 0);
|
||||||
}
|
}
|
||||||
return std::make_pair(vertexShader, fragmentShader);
|
return std::make_pair(vertexShader, fragmentShader);
|
||||||
}
|
}
|
||||||
|
|
||||||
void shaderFailedMessage(const unsigned int shader, const bool compilation)
|
void shaderFailedMessage(const unsigned int shader, const bool compilation) {
|
||||||
{
|
|
||||||
char infoLog[512];
|
char infoLog[512];
|
||||||
const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]);
|
const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]);
|
||||||
compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, nullptr, infoLog)
|
compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, nullptr, infoLog)
|
||||||
@ -78,8 +85,7 @@ void shaderFailedMessage(const unsigned int shader, const bool compilation)
|
|||||||
<< infoLog << std::endl;
|
<< infoLog << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int shaderSuccessful(const unsigned int shader, const bool compilation)
|
int shaderSuccessful(const unsigned int shader, const bool compilation) {
|
||||||
{
|
|
||||||
// check if compilation was successful
|
// check if compilation was successful
|
||||||
// int because glGetShaderiv requires int
|
// int because glGetShaderiv requires int
|
||||||
int success;
|
int success;
|
||||||
@ -88,8 +94,7 @@ int shaderSuccessful(const unsigned int shader, const bool compilation)
|
|||||||
compilation ? glGetShaderiv(shader, GL_COMPILE_STATUS, &success)
|
compilation ? glGetShaderiv(shader, GL_COMPILE_STATUS, &success)
|
||||||
: glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
: glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
||||||
// if not display compilation log
|
// if not display compilation log
|
||||||
if (!success)
|
if (!success) {
|
||||||
{
|
|
||||||
shaderFailedMessage(shader, compilation);
|
shaderFailedMessage(shader, compilation);
|
||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
|
|||||||
@ -1,16 +1,23 @@
|
|||||||
// "Copyright [2023] <Krzysztof Rudnicki>"
|
// "Copyright [2023] <Krzysztof Rudnicki>"
|
||||||
#ifndef SHADERS_HPP
|
#ifndef ENGINE_ENGINE_SHADERS_HPP_
|
||||||
#define SHADERS_HPP
|
#define ENGINE_ENGINE_SHADERS_HPP_
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
unsigned int linkShaderObjectsShaderProgram(
|
||||||
|
const unsigned int vertexShaders,
|
||||||
|
const unsigned int fragmentShader);
|
||||||
|
|
||||||
|
std::pair<unsigned int, unsigned int> compileShaders(
|
||||||
|
const char* vertexShaderSource,
|
||||||
|
const char* fragmentShaderSource);
|
||||||
|
|
||||||
unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders, const unsigned int fragmentShader);
|
|
||||||
std::pair<unsigned int, unsigned int> compileShaders(const char* vertexShaderSource, const char* fragmentShaderSource);
|
|
||||||
unsigned int compileShader(const GLenum shaderType, const char *shaderSource);
|
unsigned int compileShader(const GLenum shaderType, const char *shaderSource);
|
||||||
int shaderCompilationSuccessful(const unsigned int shader);
|
int shaderCompilationSuccessful(const unsigned int shader);
|
||||||
int shaderProgramLinkingSuccessful(const unsigned int shaderProgram);
|
int shaderProgramLinkingSuccessful(const unsigned int shaderProgram);
|
||||||
int shaderSuccessful(const unsigned int shader, const bool compilation);
|
int shaderSuccessful(const unsigned int shader, const bool compilation);
|
||||||
void shaderFailedMessage(const unsigned int shader, const bool compilation);
|
void shaderFailedMessage(const unsigned int shader, const bool compilation);
|
||||||
|
|
||||||
#endif
|
#endif // ENGINE_ENGINE_SHADERS_HPP_
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user