feat: make code compliant with cpplint

This commit is contained in:
Krzysztof Rudnicki 2023-03-12 17:32:42 +01:00
parent 553c5f7397
commit a2adca27e5
17 changed files with 526 additions and 280 deletions

View File

@ -1,7 +1,6 @@
{
"recommendations": [
"aaron-bond.better-comments",
"ms-vscode.cpptools",
"jbenden.c-cpp-flylint",
"ms-vscode.cpptools-themes",
"twxs.cmake",
@ -12,8 +11,14 @@
"ms-vscode.makefile-tools",
"pkief.material-icon-theme",
"ms-vscode-remote.vscode-remote-extensionpack",
"slevesque.shader",
"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"
]
}

View File

@ -99,6 +99,7 @@
"awesomeface",
"kuchy",
"multiplatform",
"RENDERLOOP",
"VERTICE"
]
}

View File

@ -1,11 +1,11 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef BEFORE_RENDER_CPP
#define BEFORE_RENDER_CPP
#include "Engine/engine/beforeRender.hpp"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "./beforeRender.hpp"
#include "./constants.hpp"
#include "./misc.hpp"

View File

@ -1,14 +1,17 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef BEFORE_RENDER_HPP
#define BEFORE_RENDER_HPP
#ifndef ENGINE_ENGINE_BEFORERENDER_HPP_
#define ENGINE_ENGINE_BEFORERENDER_HPP_
#include <GLFW/glfw3.h>
void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion);
void instantiateGLFWwindow();
GLFWwindow *createWindowObject();
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);
GLFWwindow *prepareForRender();
#endif
#endif // ENGINE_ENGINE_BEFORERENDER_HPP_

View File

@ -1,34 +1,34 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef CONSTANTS_HPP
#define CONSTANTS_HPP
#ifndef ENGINE_ENGINE_CONSTANTS_HPP_
#define ENGINE_ENGINE_CONSTANTS_HPP_
#include <GLFW/glfw3.h>
#include <iostream>
#include <string_view>
namespace constants
{
namespace constants {
inline constexpr int GLFW_MAJOR_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
inline const char* MAIN_WINDOW_NAME { "Match" };
// 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 constexpr int MAIN_WINDOW_WIDTH { 800 };
inline constexpr int MAIN_WINDOW_HEIGHT { 600 };
inline constexpr struct {
inline constexpr struct {
GLfloat red = 1.0f;
GLfloat green = 0.0f;
GLfloat blue = 0.0f;
GLfloat alpha = 1.0f;
} RED;
inline constexpr struct {
inline constexpr struct {
GLfloat red = 1.0f;
GLfloat green = 1.0f;
GLfloat blue = 1.0f;
GLfloat alpha = 1.0f;
} WHITE;
inline constexpr struct {
inline constexpr struct {
GLfloat red = 0.2f;
GLfloat green = 0.3f;
GLfloat blue = 0.3f;
@ -46,8 +46,7 @@ namespace constants
inline const char* VERTEX_SHADER_OFFSET_FILENAME {
"./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)
// in this shader we just forward input data to shader output
// DO NOT REMOVE UNTIL CAN DRAW SQUARE USING CLASS
@ -56,8 +55,7 @@ namespace constants
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0" } ;
"}\0" };
inline const char* VERTEX_SHADER_SOURCE_FILENAME {
"./Shaders/vertexShaderSource.vs"
};
@ -101,7 +99,6 @@ namespace constants
inline const char* FRAGMENT_SHADER_SOURCE_FILENAME {
"./Shaders/fragmentShaderSource.fs"
};
inline const char* FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME {
"./Shaders/fragmentShaderSourceYellow.fs"
};
@ -119,26 +116,29 @@ namespace constants
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[] {
// first triangle
-0.9f, -0.5f, 0.0f, // left
-0.9f, -0.5f, 0.0f, // left
-0.0f, -0.5f, 0.0f, // right
-0.45f, 0.5f, 0.0f, // top
-0.45f, 0.5f, 0.0f, // top
// second triangle
0.0f, -0.5f, 0.0f, // left
0.9f, -0.5f, 0.0f, // right
0.45f, 0.5f, 0.0f // top
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[] {
// first triangle
-0.9f, -0.5f, 0.0f, // left
-0.9f, -0.5f, 0.0f, // left
-0.0f, -0.5f, 0.0f, // right
-0.45f, 0.5f, 0.0f, // top
-0.45f, 0.5f, 0.0f, // top
};
inline constexpr size_t TRIANGLE_ONE_SIZE = { sizeof(TRIANGLE_ONE) };
@ -147,7 +147,7 @@ namespace constants
// second triangle
0.0f, -0.5f, 0.0f, // left
0.9f, -0.5f, 0.0f, // right
0.45f, 0.5f, 0.0f // top
0.45f, 0.5f, 0.0f // top
};
inline constexpr size_t TRIANGLE_TWO_SIZE = { sizeof(TRIANGLE_TWO) };
@ -156,7 +156,7 @@ namespace constants
// positions // colors
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top
};
inline constexpr size_t TRIANGLE_COLORS_SIZE = { sizeof(TRIANGLE_COLORS) };
@ -179,7 +179,7 @@ namespace constants
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.25f, -0.5f, 0.0f, // bottom left
-0.25f, 0.5f, 0.0f // top left
-0.25f, 0.5f, 0.0f // top left
};
inline constexpr unsigned int SQUARE_INDICES[] {
@ -196,23 +196,23 @@ namespace constants
// https://learnopengl.com/img/getting-started/tex_coords.png
inline constexpr float TEXTURE_VERTICES[] {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-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, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom 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[] {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
inline constexpr size_t TEXTURE_INDICES_SIZE = { sizeof(TEXTURE_INDICES) };
} // namespace constants
}
#endif
#endif // ENGINE_ENGINE_CONSTANTS_HPP_

View File

@ -6,69 +6,134 @@
#include <iostream>
#include <cmath>
#include <filesystem>
#include "draw.hpp"
#include "renderLoop.hpp"
#include "shaders.hpp"
#include "constants.hpp"
#include "misc.hpp"
#include "shader.hpp"
#include "textures.hpp"
#include "stb_image.h"
#include <utility>
#include "./draw.hpp"
#include "./renderLoop.hpp"
#include "./shaders.hpp"
#include "./constants.hpp"
#include "./misc.hpp"
#include "./shader.hpp"
#include "./textures.hpp"
#include "./stb_image.h"
drawFigureReturn drawFigure(const int whatToDraw)
{
switch (whatToDraw)
{
drawFigureReturn drawFigure(const int whatToDraw) {
switch (whatToDraw) {
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:
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:
// Try to draw 2 triangles next to each other 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);
// Try to draw 2 triangles next to each other
// 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:
drawTriangleClass(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, 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);
drawTriangleClass(constants::TRIANGLE_ONE,
constants::TRIANGLE_ONE_SIZE,
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:
drawTriangleClass(constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE, 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);
drawTriangleClass(constants::TRIANGLE_ONE,
constants::TRIANGLE_ONE_SIZE,
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:
// 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:
// 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:
// 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:
// 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:
{
// offset triangle
offsetsStruct offsets = offsetsStruct();
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:
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:
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 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:
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:
throw "No function for this draw call";
}
}
unsigned int getShaderProgram(const char* vertexShaderSource, const char* fragmentShaderSource)
{
const std::pair<unsigned int, unsigned int> shaders = compileShaders(vertexShaderSource, fragmentShaderSource);
unsigned int getShaderProgram(
const char* vertexShaderSource,
const char* fragmentShaderSource) {
const std::pair<unsigned int, unsigned int> shaders =
compileShaders(vertexShaderSource, fragmentShaderSource);
if (shaders.first == 0 || shaders.second == 0)
return 0;
const unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second);
const unsigned int shaderProgram =
linkShaderObjectsShaderProgram(shaders.first, shaders.second);
if (shaderProgram == 0)
return 0;
return shaderProgram;
@ -80,8 +145,14 @@ void setOffsets(Shader shader, const offsetsStruct offsets) {
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:
// ======================
// float offset = 0.5f;
@ -90,12 +161,25 @@ drawFigureReturn drawTriangleClass(const float triangleVertices[], const size_t
ourShader.use();
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();
copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
copyVerticesArray(
vertexBufferObject,
triangleVertices,
triangleVerticesSize,
GL_ARRAY_BUFFER);
// set vertex attribute pointers
configureVertexAttribute(colorIncluded, textureIncluded);
doDrawArrays(ourShader.ID, vertexArrayObject, GL_TRIANGLES, 0, triangleVerticesSize);
doDrawArrays(
ourShader.ID,
vertexArrayObject,
GL_TRIANGLES,
0,
triangleVerticesSize);
drawFigureReturn newReturn;
newReturn.success = 0;
newReturn.VAO = vertexArrayObject;
@ -104,9 +188,18 @@ drawFigureReturn drawTriangleClass(const float triangleVertices[], const size_t
return newReturn;
}
drawFigureReturn drawSquare(const float squareVertices[], const size_t squareVerticesSize, const unsigned int squareIndices[], const size_t squareIndicesSize, const char* vertexShaderSource, const char* fragmentShaderSource, const bool colorIncluded, const bool textureIncluded, const offsetsStruct offsets)
{
const unsigned int shaderProgram = getShaderProgram(vertexShaderSource, fragmentShaderSource);
drawFigureReturn drawSquare(
const float squareVertices[],
const size_t squareVerticesSize,
const unsigned int squareIndices[],
const size_t squareIndicesSize,
const char* vertexShaderSource,
const char* fragmentShaderSource,
const bool colorIncluded,
const bool textureIncluded,
const offsetsStruct offsets) {
const unsigned int shaderProgram =
getShaderProgram(vertexShaderSource, fragmentShaderSource);
drawFigureReturn newReturn = drawFigureReturn();
if (shaderProgram == 0) {
newReturn.success = -1;
@ -114,34 +207,57 @@ drawFigureReturn drawSquare(const float squareVertices[], const size_t squareVer
}
const unsigned int VAO = generateBindVAO();
copyVerticesMemory(squareVertices, squareVerticesSize, GL_ARRAY_BUFFER);
copyVerticesMemory(squareIndices, squareIndicesSize, GL_ELEMENT_ARRAY_BUFFER);
copyVerticesMemory(
squareIndices,
squareIndicesSize,
GL_ELEMENT_ARRAY_BUFFER);
// set vertex attribute pointers
configureVertexAttribute(colorIncluded, textureIncluded);
doDrawElements(shaderProgram, VAO, GL_TRIANGLES, GL_UNSIGNED_INT, squareIndicesSize);
doDrawElements(
shaderProgram,
VAO,
GL_TRIANGLES,
GL_UNSIGNED_INT,
squareIndicesSize);
newReturn.success = 0;
newReturn.VAO = VAO;
return newReturn;
}
drawFigureReturn drawDebilMode(const char* vertexPath, const char* fragmentPath, const float vertices[], const 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
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 VBO = copyVerticesMemory(vertices, verticesSize, GL_ARRAY_BUFFER);
unsigned int EBO = copyVerticesMemory(indices, indicesSize, GL_ELEMENT_ARRAY_BUFFER);
unsigned int VBO = copyVerticesMemory(
vertices,
verticesSize,
GL_ARRAY_BUFFER);
unsigned int EBO = copyVerticesMemory(indices,
indicesSize,
GL_ELEMENT_ARRAY_BUFFER);
configureVertexAttribute(true, 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!
// either set it manually like so:
ourShader.use(); // don't forget to activate/use
// the shader before setting uniforms!
// set it via the texture class
ourShader.setInt("texture1", 0);
// or set it via the texture class
ourShader.setInt("texture2", 1);
@ -158,23 +274,33 @@ drawFigureReturn drawDebilMode(const char* vertexPath, const char* fragmentPath,
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);
glBindVertexArray(vertexArrayObject);
glDrawElements(drawArrayMode, numberOfElementsToDraw, drawType, 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
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 int vertexColorLocation = glGetUniformLocation(shaderProgram, uniformName); // query the location of our uniform
if(vertexColorLocation != -1) // if glGetUniformLocation returns -1 it could not find the location
{
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f); /* we set the uniform value using glUniform4f
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
// query the location of our uniform
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
few of possible postfixes:
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
glUseProgram(shaderProgram);
updateUniformColor(shaderProgram, "ourColor");
@ -199,4 +329,4 @@ void doDrawArrays(const unsigned int shaderProgram, const unsigned int vertexArr
glDrawArrays(drawArrayMode, firstIndex, numberOfIndicesToBeRendered);
}
#endif
#endif // #ifndef DRAW_CPP

View File

@ -1,10 +1,10 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef DRAW_HPP
#define DRAW_HPP
#ifndef ENGINE_ENGINE_DRAW_HPP_
#define ENGINE_ENGINE_DRAW_HPP_
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <string>
#include "constants.hpp"
#include "./constants.hpp"
struct offsetsStruct {
offsetsStruct(): xOffset(0), yOffset(0), zOffset(0) { }
@ -12,7 +12,7 @@ struct offsetsStruct {
};
struct drawFigureReturn {
drawFigureReturn(): success(-1), VAO(0), VBO(0), EBO(0){ }
drawFigureReturn(): success(-1), VAO(0), VBO(0), EBO(0) { }
int success;
unsigned int VAO;
unsigned int VBO;
@ -20,11 +20,51 @@ struct drawFigureReturn {
};
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.

View File

@ -5,17 +5,18 @@
#include <GLFW/glfw3.h>
#include <iostream>
#include <random> // I am using standart library RNG because 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 <chrono> // for std::chrono
#include <random>
// I am using standard library RNG because
// 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 "beforeRender.hpp"
#include "misc.hpp"
#include "renderLoop.hpp"
#include "./constants.hpp"
#include "./beforeRender.hpp"
#include "./misc.hpp"
#include "./renderLoop.hpp"
int main()
{
int main() {
// changed in glfwWindowShouldClose
GLFWwindow *window = prepareForRender();
if (window == nullptr)
@ -27,4 +28,4 @@ int main()
return 0;
}
#endif
#endif // #ifndef MAIN_CPP

View File

@ -2,10 +2,9 @@
#ifndef MISC_CPP
#define MISC_CPP
#include <iostream>
#include "misc.hpp"
void print(const std::string s)
{
#include "./misc.hpp"
void print(const std::string s) {
std::cout << s << std::endl;
}
#endif
#endif

View File

@ -1,7 +1,8 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef MISC_HPP
#define MISC_HPP
#ifndef ENGINE_ENGINE_MISC_HPP_
#define ENGINE_ENGINE_MISC_HPP_
#include <iostream>
#include <string>
void print(const std::string s);
#endif
#endif // ENGINE_ENGINE_MISC_HPP_

View File

@ -4,36 +4,36 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "renderLoop.hpp"
#include "draw.hpp"
#include "shaders.hpp"
#include "constants.hpp"
#include "misc.hpp"
#include "./renderLoop.hpp"
#include "./draw.hpp"
#include "./shaders.hpp"
#include "./constants.hpp"
#include "./misc.hpp"
int processInput(GLFWwindow *window, const int whatToDraw)
{
int processInput(GLFWwindow *window, const int whatToDraw) {
static bool lockedLeft = false;
static bool lockedRight = false;
const bool PRESSED_CHANGE_LEFT = (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS);
const bool PRESSED_CHANGE_RIGHT = (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS);
const bool PRESSED_CHANGE_LEFT =
(glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS);
const bool PRESSED_CHANGE_RIGHT =
(glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS);
// glfwGetKey takes window and key as an input and checks is currently being pressed
// glfwGetKey takes window and key as
// an input and checks is currently being pressed
// if the user pressed escape we close window
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if ( !PRESSED_CHANGE_LEFT )
if ( !PRESSED_CHANGE_LEFT )
lockedLeft = 0;
if ( !PRESSED_CHANGE_RIGHT )
if ( !PRESSED_CHANGE_RIGHT )
lockedRight = 0;
if ( PRESSED_CHANGE_RIGHT && lockedRight == 0 )
{
if ( PRESSED_CHANGE_RIGHT && lockedRight == 0 ) {
lockedRight = 1;
return (whatToDraw == constants::MAX_DRAW_CALL ? 0 : whatToDraw + 1);
}
if ( PRESSED_CHANGE_LEFT && lockedLeft == 0 )
{
if ( PRESSED_CHANGE_LEFT && lockedLeft == 0 ) {
lockedLeft = 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
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
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);
// buffer type of vertex buffer object is GL_ARRAY_BUFFER
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
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;
}
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
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);
// buffer type of vertex buffer object is GL_ARRAY_BUFFER
glBindBuffer(boundBufferTarget, vertexBufferObject);
// now whenever we change GL_ARRAY_BUFFER we change bound buffer vertexBufferObject
glBindBuffer(
boundBufferTarget,
vertexBufferObject);
// now whenever we change GL_ARRAY_BUFFER
// we change bound buffer vertexBufferObject
/* 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.
@ -79,8 +89,9 @@ unsigned int copyVerticesMemory(const unsigned int vertices[], const size_t size
return vertexBufferObject;
}
void configureVertexAttribute(const bool colorIncluded, const bool textureIncluded)
{
void configureVertexAttribute(
const bool colorIncluded,
const bool textureIncluded) {
/* specify how OGL interprets vertex data
From left:
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
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 */
if(!colorIncluded) {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
if (!colorIncluded) {
glVertexAttribPointer(
0, 3, GL_FLOAT, GL_FALSE,
3 * sizeof(float), reinterpret_cast<void *>(0));
// enable vertex attribute
glEnableVertexAttribArray(0);
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 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 */
if(colorIncluded && !textureIncluded) {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
if (colorIncluded && !textureIncluded) {
glVertexAttribPointer(
0, 3, GL_FLOAT, GL_FALSE,
6 * sizeof(float), reinterpret_cast<void *>(0));
// enable vertex attribute
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
glEnableVertexAttribArray(1);
return;
}
// Since we've added an extra vertex attribute we again have to notify OpenGL of the new vertex format
// this time for textures we add just 2 more attributes and not 3 as with color
if(textureIncluded) {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
// Since we've added an extra vertex
// attribute we again have to notify OpenGL of the new vertex format
// this time for textures we add just
// 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
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
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
return;
glVertexAttribPointer(
2, 2, GL_FLOAT, GL_FALSE,
8 * sizeof(float), reinterpret_cast<void*>(6 * sizeof(float)));
glEnableVertexAttribArray(2);
return;
}
}
unsigned int generateBindVAO()
{
unsigned int generateBindVAO() {
// vertex array object is used to draw objects by binding them to vao
// generate vao
unsigned int vertexArrayObject;
@ -143,40 +166,50 @@ unsigned int generateBindVAO()
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
glBindBuffer(boundBufferTarget, vertexBufferObject);
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
}
int renderLoopInside(GLFWwindow *window, int whatToDraw)
{
int renderLoopInside(GLFWwindow *window, int whatToDraw) {
// input
whatToDraw = processInput(window, whatToDraw);
// We specify the color to clear the screen with
// 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);
// There is GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT
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);
// There is
// GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT
glClear(GL_COLOR_BUFFER_BIT);
drawFigureReturn successAndVao = drawFigure(whatToDraw);
if (successAndVao.success == -1)
{
if (successAndVao.success == -1) {
print("error with drawing!");
glfwSetWindowShouldClose(window, true);
};
}
// 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)
// when back buffer is ready we swap it with front buffer to eliminate flickering
// there is front buffer (final image)
// 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);
// 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();
// std::cout << "successAndVao.VAO" << successAndVao.VAO << "successAndVao.VBO" << successAndVao.VBO << "successAndVao.EBO" << successAndVao.EBO << std::endl;
glDeleteVertexArrays(1, &successAndVao.VAO);
glDeleteBuffers(1, &successAndVao.VBO);
glDeleteBuffers(1, &successAndVao.EBO);
@ -185,14 +218,12 @@ int renderLoopInside(GLFWwindow *window, int whatToDraw)
return whatToDraw;
}
void renderLoop(GLFWwindow *window)
{
void renderLoop(GLFWwindow *window) {
int whatToDraw = 0;
// glfwWindowShouldClose checks if GLFW was instructed to close
while (!glfwWindowShouldClose(window))
{
while (!glfwWindowShouldClose(window)) {
whatToDraw = renderLoopInside(window, whatToDraw);
}
}
#endif
#endif

View File

@ -1,17 +1,34 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef RENDER_LOOP_HPP
#define RENDER_LOOP_HPP
#ifndef ENGINE_ENGINE_RENDERLOOP_HPP_
#define ENGINE_ENGINE_RENDERLOOP_HPP_
#include <GLFW/glfw3.h>
#include <iostream>
void renderLoop(GLFWwindow *window);
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();
void configureVertexAttribute(const bool colorIncluded, const bool textureIncluded);
void configureVertexAttribute(
const bool colorIncluded,
const bool textureIncluded);
int processInput(GLFWwindow *window, int whatToDraw);
unsigned int copyVerticesMemory(const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);
unsigned int copyVerticesMemory(const unsigned int vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget);
unsigned int copyVerticesMemory(
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_

View File

@ -8,17 +8,15 @@
#include <fstream>
#include <sstream>
#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
std::string shaderCode;
std::ifstream shaderFile;
// ensure ifstream objects can throw exceptions:
shaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try
{
shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
// open files
shaderFile.open(shaderPath);
std::stringstream shaderStream;
@ -29,17 +27,20 @@ const std::string Shader::fileToShader(const std::string shaderPath)
// convert stream into string
shaderCode = shaderStream.str();
}
catch (std::ifstream::failure& e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl;
catch (std::ifstream::failure& e) {
std::cout <<
"ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: "
<< e.what() << std::endl;
}
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 fShaderCodeString = this -> fileToShader(fragmentPath);
const std::string fShaderCodeString =
this -> fileToShader(fragmentPath);
const char* vShaderCode = vshaderCodeString.c_str();
const char * fShaderCode = fShaderCodeString.c_str();
// 2. compile shaders
@ -60,53 +61,54 @@ Shader::Shader(const std::string vertexPath, const std::string fragmentPath)
glAttachShader(ID, fragment);
glLinkProgram(ID);
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(fragment);
}
void Shader::use()
{
glUseProgram(ID);
void Shader::use() {
glUseProgram(ID);
}
void Shader::setBool(const std::string &name, bool value) const
{
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value);
void Shader::setBool(const std::string &name, bool value) const {
glUniform1i(
glGetUniformLocation(ID, name.c_str()),
reinterpret_cast<int>(value));
}
void Shader::setInt(const std::string &name, int value) const
{
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
void Shader::setInt(const std::string &name, int value) const {
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
}
void Shader::setFloat(const std::string &name, float value) const
{
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
void Shader::setFloat(const std::string &name, float value) const {
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;
char infoLog[1024];
if (type != "PROGRAM")
{
if (type != "PROGRAM") {
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
if (!success) {
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);
if (!success)
{
if (!success) {
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;
}
}
}
#endif // SHADER_CPP
#endif // SHADER_CPP

View File

@ -1,6 +1,6 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef SHADER_HPP
#define SHADER_HPP
#ifndef ENGINE_ENGINE_SHADER_HPP_
#define ENGINE_ENGINE_SHADER_HPP_
#include <glad/glad.h>
@ -9,9 +9,8 @@
#include <sstream>
#include <iostream>
class Shader
{
public:
class Shader {
public:
unsigned int ID;
// constructor generates the shader on the fly
// ------------------------------------------------------------------------
@ -28,10 +27,15 @@ public:
// ------------------------------------------------------------------------
void setFloat(const std::string &name, float value) const;
private:
private:
// utility function for checking shader compilation/linking errors.
// ------------------------------------------------------------------------
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_

View File

@ -3,12 +3,13 @@
#define SHADERS_CPP
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "shaders.hpp"
#include "constants.hpp"
#include "misc.hpp"
#include "./shaders.hpp"
#include "./constants.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
// will store shader program id
// 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
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(fragmentShader);
if (shaderProgram == 0)
@ -35,13 +37,16 @@ unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders, co
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
const unsigned int shaderID = glCreateShader(shaderType);
// 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);
// compile shader
glCompileShader(shaderID);
@ -50,26 +55,28 @@ unsigned int compileShader(const GLenum shaderType, const char *shaderSource)
return shaderID;
}
std::pair<unsigned int, unsigned int> compileShaders(const char* vertexShaderSource, const char* fragmentShaderSource)
{
const unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
if (vertexShader == 0)
{
std::pair<unsigned int, unsigned int> compileShaders(
const char* vertexShaderSource,
const char* fragmentShaderSource) {
const unsigned int vertexShader =
compileShader(GL_VERTEX_SHADER, vertexShaderSource);
if (vertexShader == 0) {
print("Vertex Shader Compilation Failed");
return std::make_pair(0, 0);
}
const unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
if (fragmentShader == 0)
{
const unsigned int fragmentShader =
compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
if (fragmentShader == 0) {
print("Fragment Shader Compilation Failed");
return std::make_pair(0, 0);
}
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];
const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]);
compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, nullptr, infoLog)
@ -78,8 +85,7 @@ void shaderFailedMessage(const unsigned int shader, const bool compilation)
<< 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
// int because glGetShaderiv requires int
int success;
@ -88,11 +94,10 @@ int shaderSuccessful(const unsigned int shader, const bool compilation)
compilation ? glGetShaderiv(shader, GL_COMPILE_STATUS, &success)
: glGetProgramiv(shader, GL_LINK_STATUS, &success);
// if not display compilation log
if (!success)
{
if (!success) {
shaderFailedMessage(shader, compilation);
}
return success;
}
#endif
#endif

View File

@ -1,16 +1,23 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef SHADERS_HPP
#define SHADERS_HPP
#ifndef ENGINE_ENGINE_SHADERS_HPP_
#define ENGINE_ENGINE_SHADERS_HPP_
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#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);
int shaderCompilationSuccessful(const unsigned int shader);
int shaderProgramLinkingSuccessful(const unsigned int shaderProgram);
int shaderSuccessful(const unsigned int shader, const bool compilation);
void shaderFailedMessage(const unsigned int shader, const bool compilation);
#endif
#endif // ENGINE_ENGINE_SHADERS_HPP_