chore: ran clang-formatter on root folder

This commit is contained in:
Krzysztof Rudnicki 2023-03-14 01:56:29 +01:00
parent a2adca27e5
commit d79b634e84
18 changed files with 1005 additions and 1092 deletions

View File

@ -1,24 +1,20 @@
{ {
"recommendations": [ "recommendations": [
"aaron-bond.better-comments", "aaron-bond.better-comments",
"jbenden.c-cpp-flylint", "ms-vscode.cpptools",
"ms-vscode.cpptools-themes", "ms-vscode.cpptools-themes",
"twxs.cmake",
"ms-vscode.cmake-tools",
"stackbreak.comment-divider",
"donjayamanne.githistory",
"james-yu.latex-workshop",
"ms-vscode.makefile-tools",
"pkief.material-icon-theme",
"ms-vscode-remote.vscode-remote-extensionpack",
"henriiik.vscode-sort",
"hbenl.test-adapter-converter",
"streetsidesoftware.code-spell-checker",
"wmaurer.change-case", "wmaurer.change-case",
"xaver.clang-format",
"streetsidesoftware.code-spell-checker",
"vadimcn.vscode-lldb",
"usernamehw.errorlens", "usernamehw.errorlens",
"donjayamanne.githistory",
"dtoplak.vscode-glsllint",
"kisstkondoros.vscode-gutter-preview", "kisstkondoros.vscode-gutter-preview",
"james-yu.latex-workshop",
"davidanson.vscode-markdownlint", "davidanson.vscode-markdownlint",
"pkief.material-icon-theme",
"streetsidesoftware.code-spell-checker-polish", "streetsidesoftware.code-spell-checker-polish",
"tyriar.sort-lines" "slevesque.shader"
] ]
} }

View File

@ -0,0 +1 @@
BasedOnStyle: Google

View File

@ -1,11 +1,13 @@
// "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 <glad/glad.h> #include "./beforeRender.hpp"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <iostream> #include <iostream>
#include "./beforeRender.hpp"
#include "./constants.hpp" #include "./constants.hpp"
#include "./misc.hpp" #include "./misc.hpp"
@ -37,11 +39,8 @@ GLFWwindow *createWindowObject() {
// Third is the name of the window // Third is the name of the window
// We ignore last two // We ignore last two
GLFWwindow *window = glfwCreateWindow( GLFWwindow *window = glfwCreateWindow(
constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT,
constants::MAIN_WINDOW_HEIGHT, constants::MAIN_WINDOW_NAME, nullptr, nullptr);
constants::MAIN_WINDOW_NAME,
nullptr,
nullptr);
return window; return window;
} }
@ -55,8 +54,7 @@ int initializeGLAD() {
} }
// resizes viewport when user resizes window // resizes viewport when user resizes window
void framebuffer_size_callback(GLFWwindow *window, void framebuffer_size_callback(GLFWwindow *window, const int width,
const int width,
const int height) { const int height) {
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
} }
@ -67,10 +65,7 @@ void viewPort(GLFWwindow *window) {
// 3th and 4th width and height of rendering window // 3th and 4th width and height of rendering window
// we could set them to be smaller than window dimension, // we could set them to be smaller than window dimension,
// ogl rendering will be then displayed in smaller window // ogl rendering will be then displayed in smaller window
glViewport(0, glViewport(0, 0, constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT);
0,
constants::MAIN_WINDOW_WIDTH,
constants::MAIN_WINDOW_HEIGHT);
// processed coordinates are between -1 and 1 so here we map: // processed coordinates are between -1 and 1 so here we map:
// (-1 to 1) to (0, constants::MAIN_WINDOW_WIDTH) // (-1 to 1) to (0, constants::MAIN_WINDOW_WIDTH)
// and (0, constants::MAIN_WINDOW_HEIGHT) // and (0, constants::MAIN_WINDOW_HEIGHT)
@ -92,8 +87,7 @@ GLFWwindow *prepareForRender() {
// we make context of this window main context of current thread // we make context of this window main context of current thread
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
if (initializeGLAD() == -1) if (initializeGLAD() == -1) return nullptr;
return nullptr;
viewPort(window); viewPort(window);
return window; return window;
} }

View File

@ -7,9 +7,7 @@ void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion);
void instantiateGLFWwindow(); void instantiateGLFWwindow();
GLFWwindow *createWindowObject(); GLFWwindow *createWindowObject();
int initializeGLAD(); int initializeGLAD();
void framebuffer_size_callback( void framebuffer_size_callback(GLFWwindow *window, const int width,
GLFWwindow *window,
const int width,
const int height); const int height);
void viewPort(GLFWwindow *window); void viewPort(GLFWwindow *window);
GLFWwindow *prepareForRender(); GLFWwindow *prepareForRender();

View File

@ -2,10 +2,10 @@
#ifndef ENGINE_ENGINE_CONSTANTS_HPP_ #ifndef ENGINE_ENGINE_CONSTANTS_HPP_
#define ENGINE_ENGINE_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};
@ -36,53 +36,44 @@ namespace constants {
} LEARN_OPEN_GL_COLOR; } LEARN_OPEN_GL_COLOR;
inline const char* VERTEX_SHADER_COLOR_FILENAME{ inline const char* VERTEX_SHADER_COLOR_FILENAME{
"./Shaders/vertexShaderColor.vs" "./Shaders/vertexShaderColor.vs"};
};
inline const char* VERTEX_SHADER_UPSIDE_DOWN_FILENAME{ inline const char* VERTEX_SHADER_UPSIDE_DOWN_FILENAME{
"./Shaders/vertexShaderUpsideDown.vs" "./Shaders/vertexShaderUpsideDown.vs"};
};
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
// DO NOT REMOVE UNTIL CAN DRAW SQUARE USING CLASS // DO NOT REMOVE UNTIL CAN DRAW SQUARE USING CLASS
inline const char *VERTEX_SHADER_SOURCE { "#version 330 core\n" inline const char* VERTEX_SHADER_SOURCE{
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n" "layout (location = 0) in vec3 aPos;\n"
"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"};
};
inline const char* VERTEX_SHADER_VERTICE_COLOR_FILENAME{ inline const char* VERTEX_SHADER_VERTICE_COLOR_FILENAME{
"./Shaders/vertexShaderVerticeColor.vs" "./Shaders/vertexShaderVerticeColor.vs"};
};
inline const char* VERTEX_SHADER_TASK_THREE_FILENAME{ inline const char* VERTEX_SHADER_TASK_THREE_FILENAME{
"./Shaders/vertexShaderTaskThree.vs" "./Shaders/vertexShaderTaskThree.vs"};
};
inline const char* VERTEX_SHADER_TEXTURE_FILENAME{ inline const char* VERTEX_SHADER_TEXTURE_FILENAME{
"./Shaders/vertexShaderTexture.vs" "./Shaders/vertexShaderTexture.vs"};
};
inline const char* FRAGMENT_SHADER_TASK_THREE_FILENAME{ inline const char* FRAGMENT_SHADER_TASK_THREE_FILENAME{
"./Shaders/fragmentShaderTaskThree.fs" "./Shaders/fragmentShaderTaskThree.fs"};
};
inline const char* FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME{ inline const char* FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME{
"./Shaders/fragmentShaderColorFromVertex.fs" "./Shaders/fragmentShaderColorFromVertex.fs"};
};
inline const char* FRAGMENT_SHADER_UNIFORMS_FILENAME{ inline const char* FRAGMENT_SHADER_UNIFORMS_FILENAME{
"./Shaders/fragmentShaderUniforms.fs" "./Shaders/fragmentShaderUniforms.fs"};
};
// write fragment shader // write fragment shader
// we set the color of each pixel to be orange // we set the color of each pixel to be orange
@ -93,32 +84,23 @@ namespace constants {
"void main()\n" "void main()\n"
"{\n" "{\n"
"FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" "FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\0" "}\0"};
};
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"};
};
inline const char* FRAGMENT_SHADER_TEXTURE_FILENAME{ inline const char* FRAGMENT_SHADER_TEXTURE_FILENAME{
"./Shaders/fragmentShaderTexture.fs" "./Shaders/fragmentShaderTexture.fs"};
};
// we specify three vertices // we specify three vertices
// each of them with position in 3d space // each of them with position in 3d space
// x y z // x y z
inline constexpr float TRIANGLE_VERTICES[] { inline constexpr float TRIANGLE_VERTICES[]{-0.5f, -0.5f, 0.0f, 0.5f, -0.5f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f};
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
inline constexpr size_t TRIANGLE_VERTICES_SIZE = { inline constexpr size_t TRIANGLE_VERTICES_SIZE = {sizeof(TRIANGLE_VERTICES)};
sizeof(TRIANGLE_VERTICES)
};
inline constexpr float TRIANGLES_VERTICES[]{ inline constexpr float TRIANGLES_VERTICES[]{
// first triangle // first triangle
@ -131,8 +113,7 @@ namespace constants {
0.45f, 0.5f, 0.0f // top 0.45f, 0.5f, 0.0f // top
}; };
inline constexpr size_t TRIANGLES_VERTICES_SIZE = { inline constexpr size_t TRIANGLES_VERTICES_SIZE = {sizeof(TRIANGLES_VERTICES)};
sizeof(TRIANGLES_VERTICES) };
inline constexpr float TRIANGLE_ONE[]{ inline constexpr float TRIANGLE_ONE[]{
// first triangle // first triangle
@ -202,10 +183,7 @@ 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 = { inline constexpr size_t TEXTURE_VERTICES_SIZE = {sizeof(TEXTURE_VERTICES)};
sizeof(TEXTURE_VERTICES)
};
inline constexpr unsigned int TEXTURE_INDICES[]{ inline constexpr unsigned int TEXTURE_INDICES[]{
0, 1, 3, // first triangle 0, 1, 3, // first triangle

View File

@ -1,61 +1,57 @@
// "Copyright [2023] <Krzysztof Rudnicki>" // "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef DRAW_CPP #ifndef DRAW_CPP
#define DRAW_CPP #define DRAW_CPP
#include <glad/glad.h> #include "./draw.hpp"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <iostream> #include <glad/glad.h>
#include <cmath> #include <cmath>
#include <filesystem> #include <filesystem>
#include <iostream>
#include <utility> #include <utility>
#include "./draw.hpp"
#include "./renderLoop.hpp"
#include "./shaders.hpp"
#include "./constants.hpp" #include "./constants.hpp"
#include "./misc.hpp" #include "./misc.hpp"
#include "./renderLoop.hpp"
#include "./shader.hpp" #include "./shader.hpp"
#include "./textures.hpp" #include "./shaders.hpp"
#include "./stb_image.h" #include "./stb_image.h"
#include "./textures.hpp"
drawFigureReturn drawFigure(const int whatToDraw) { drawFigureReturn drawFigure(const int whatToDraw) {
switch (whatToDraw) { switch (whatToDraw) {
case 0: case 0:
return drawTriangleClass( return drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE, constants::TRIANGLE_VERTICES_SIZE);
constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
case 1: case 1:
return drawSquare( return drawSquare(
constants::SQUARE_VERTICES, constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE,
constants::SQUARE_VERTICES_SIZE,
constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE, constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE,
constants::VERTEX_SHADER_SOURCE, constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE);
constants::FRAGMENT_SHADER_SOURCE);
case 2: case 2:
// Try to draw 2 triangles next to each other // Try to draw 2 triangles next to each other
// using glDrawArrays by adding more vertices to your data. // using glDrawArrays by adding more vertices to your data.
return drawTriangleClass( return drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLES_VERTICES, constants::TRIANGLES_VERTICES,
constants::TRIANGLES_VERTICES_SIZE, constants::TRIANGLES_VERTICES_SIZE);
constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
case 3: case 3:
drawTriangleClass(constants::TRIANGLE_ONE, drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_ONE_SIZE, constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::VERTEX_SHADER_SOURCE_FILENAME, constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE);
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
// Now create the same 2 triangles // Now create the same 2 triangles
// using two different VAOs and VBOs for their data // using two different VAOs and VBOs for their data
return drawTriangleClass( return drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_TWO, constants::TRIANGLE_TWO,
constants::TRIANGLE_TWO_SIZE, constants::TRIANGLE_TWO_SIZE);
constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
case 4: case 4:
drawTriangleClass(constants::TRIANGLE_ONE, drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_ONE_SIZE, constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::VERTEX_SHADER_SOURCE_FILENAME, constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE);
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
/* /*
Create two shader programs where Create two shader programs where
the second program uses a different the second program uses a different
@ -63,79 +59,69 @@ drawFigureReturn drawFigure(const int whatToDraw) {
draw both triangles again where one outputs the color yellow draw both triangles again where one outputs the color yellow
*/ */
return drawTriangleClass( return drawTriangleClass(
constants::TRIANGLE_TWO,
constants::TRIANGLE_TWO_SIZE,
constants::VERTEX_SHADER_SOURCE_FILENAME, constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME); constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME,
constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE);
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, return drawTriangleClass(
constants::TRIANGLE_VERTICES_SIZE,
constants::VERTEX_SHADER_COLOR_FILENAME, constants::VERTEX_SHADER_COLOR_FILENAME,
constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME); constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME,
constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE);
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, return drawTriangleClass(constants::VERTEX_SHADER_COLOR_FILENAME,
constants::TRIANGLE_VERTICES_SIZE, constants::FRAGMENT_SHADER_UNIFORMS_FILENAME,
constants::VERTEX_SHADER_COLOR_FILENAME, constants::TRIANGLE_VERTICES,
constants::FRAGMENT_SHADER_UNIFORMS_FILENAME); constants::TRIANGLE_VERTICES_SIZE);
case 7: case 7:
// set color from opengl code to uniform value in fragment shader // set color from opengl code to uniform value in fragment shader
return drawTriangleClass(constants::TRIANGLE_COLORS, return drawTriangleClass(
constants::TRIANGLE_COLORS_SIZE,
constants::VERTEX_SHADER_VERTICE_COLOR_FILENAME, constants::VERTEX_SHADER_VERTICE_COLOR_FILENAME,
constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME, constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME,
true); constants::TRIANGLE_COLORS, constants::TRIANGLE_COLORS_SIZE, true);
case 8: case 8:
// upside down triangle // upside down triangle
return drawTriangleClass(constants::TRIANGLE_VERTICES, return drawTriangleClass(constants::VERTEX_SHADER_UPSIDE_DOWN_FILENAME,
constants::TRIANGLE_VERTICES_SIZE, constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::VERTEX_SHADER_UPSIDE_DOWN_FILENAME, constants::TRIANGLE_VERTICES,
constants::FRAGMENT_SHADER_SOURCE_FILENAME); constants::TRIANGLE_VERTICES_SIZE);
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, return drawTriangleClass(constants::VERTEX_SHADER_OFFSET_FILENAME,
constants::TRIANGLE_VERTICES_SIZE,
constants::VERTEX_SHADER_OFFSET_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME, constants::FRAGMENT_SHADER_SOURCE_FILENAME,
false, constants::TRIANGLE_VERTICES,
false, constants::TRIANGLE_VERTICES_SIZE, false, false,
offsets); offsets);
} }
case 10: case 10:
return drawTriangleClass(constants::TRIANGLE_VERTICES, return drawTriangleClass(constants::VERTEX_SHADER_TASK_THREE_FILENAME,
constants::TRIANGLE_VERTICES_SIZE, constants::FRAGMENT_SHADER_TASK_THREE_FILENAME,
constants::VERTEX_SHADER_TASK_THREE_FILENAME, constants::TRIANGLE_VERTICES,
constants::FRAGMENT_SHADER_TASK_THREE_FILENAME); constants::TRIANGLE_VERTICES_SIZE);
case constants::MAX_DRAW_CALL: case constants::MAX_DRAW_CALL:
return drawDebilMode( return drawDebilMode(
constants::VERTEX_SHADER_TEXTURE_FILENAME, constants::VERTEX_SHADER_TEXTURE_FILENAME,
constants::FRAGMENT_SHADER_TEXTURE_FILENAME, constants::FRAGMENT_SHADER_TEXTURE_FILENAME,
constants::TEXTURE_VERTICES, constants::TEXTURE_VERTICES, constants::TEXTURE_VERTICES_SIZE,
constants::TEXTURE_VERTICES_SIZE, constants::TEXTURE_INDICES, constants::TEXTURE_INDICES_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( unsigned int getShaderProgram(const char* vertexShaderSource,
const char* vertexShaderSource,
const char* fragmentShaderSource) { const char* fragmentShaderSource) {
const std::pair<unsigned int, unsigned int> shaders = const std::pair<unsigned int, unsigned int> shaders =
compileShaders(vertexShaderSource, fragmentShaderSource); 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 = const unsigned int shaderProgram =
linkShaderObjectsShaderProgram(shaders.first, shaders.second); linkShaderObjectsShaderProgram(shaders.first, shaders.second);
if (shaderProgram == 0) if (shaderProgram == 0) return 0;
return 0;
return shaderProgram; return shaderProgram;
} }
@ -145,11 +131,10 @@ void setOffsets(Shader shader, const offsetsStruct offsets) {
shader.setFloat("zOffset", offsets.zOffset); shader.setFloat("zOffset", offsets.zOffset);
} }
drawFigureReturn drawTriangleClass( drawFigureReturn drawTriangleClass(const char* vertexPath,
const char* fragmentPath,
const float triangleVertices[], const float triangleVertices[],
const size_t triangleVerticesSize, const size_t triangleVerticesSize,
const char* vertexPath,
const char* fragmentPath,
const bool colorIncluded, const bool colorIncluded,
const bool textureIncluded, const bool textureIncluded,
const offsetsStruct offsets) { const offsetsStruct offsets) {
@ -161,24 +146,14 @@ drawFigureReturn drawTriangleClass(
ourShader.use(); ourShader.use();
setOffsets(ourShader, offsets); setOffsets(ourShader, offsets);
const unsigned int vertexBufferObject = const unsigned int vertexBufferObject = copyVerticesMemory(
copyVerticesMemory( triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER);
triangleVertices,
triangleVerticesSize,
GL_ARRAY_BUFFER);
const unsigned int vertexArrayObject = generateBindVAO(); const unsigned int vertexArrayObject = generateBindVAO();
copyVerticesArray( copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize,
vertexBufferObject,
triangleVertices,
triangleVerticesSize,
GL_ARRAY_BUFFER); GL_ARRAY_BUFFER);
// set vertex attribute pointers // set vertex attribute pointers
configureVertexAttribute(colorIncluded, textureIncluded); configureVertexAttribute(colorIncluded, textureIncluded);
doDrawArrays( doDrawArrays(ourShader.ID, vertexArrayObject, GL_TRIANGLES, 0,
ourShader.ID,
vertexArrayObject,
GL_TRIANGLES,
0,
triangleVerticesSize); triangleVerticesSize);
drawFigureReturn newReturn; drawFigureReturn newReturn;
newReturn.success = 0; newReturn.success = 0;
@ -189,14 +164,10 @@ drawFigureReturn drawTriangleClass(
} }
drawFigureReturn drawSquare( drawFigureReturn drawSquare(
const float squareVertices[], const float squareVertices[], const size_t squareVerticesSize,
const size_t squareVerticesSize, const unsigned int squareIndices[], const size_t squareIndicesSize,
const unsigned int squareIndices[], const char* vertexShaderSource, const char* fragmentShaderSource,
const size_t squareIndicesSize, const bool colorIncluded, const bool textureIncluded,
const char* vertexShaderSource,
const char* fragmentShaderSource,
const bool colorIncluded,
const bool textureIncluded,
const offsetsStruct offsets) { const offsetsStruct offsets) {
const unsigned int shaderProgram = const unsigned int shaderProgram =
getShaderProgram(vertexShaderSource, fragmentShaderSource); getShaderProgram(vertexShaderSource, fragmentShaderSource);
@ -207,52 +178,37 @@ drawFigureReturn drawSquare(
} }
const unsigned int VAO = generateBindVAO(); const unsigned int VAO = generateBindVAO();
copyVerticesMemory(squareVertices, squareVerticesSize, GL_ARRAY_BUFFER); copyVerticesMemory(squareVertices, squareVerticesSize, GL_ARRAY_BUFFER);
copyVerticesMemory( copyVerticesMemory(squareIndices, squareIndicesSize, GL_ELEMENT_ARRAY_BUFFER);
squareIndices,
squareIndicesSize,
GL_ELEMENT_ARRAY_BUFFER);
// set vertex attribute pointers // set vertex attribute pointers
configureVertexAttribute(colorIncluded, textureIncluded); configureVertexAttribute(colorIncluded, textureIncluded);
doDrawElements( doDrawElements(shaderProgram, VAO, GL_TRIANGLES, GL_UNSIGNED_INT,
shaderProgram,
VAO,
GL_TRIANGLES,
GL_UNSIGNED_INT,
squareIndicesSize); squareIndicesSize);
newReturn.success = 0; newReturn.success = 0;
newReturn.VAO = VAO; newReturn.VAO = VAO;
return newReturn; return newReturn;
} }
drawFigureReturn drawDebilMode( drawFigureReturn drawDebilMode(const char* vertexPath, const char* fragmentPath,
const char* vertexPath,
const char* fragmentPath,
const float vertices[], const float vertices[],
const size_t verticesSize, const size_t verticesSize,
const unsigned int indices[], const unsigned int indices[],
const size_t indicesSize) { const size_t indicesSize) {
// 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( Shader ourShader(constants::VERTEX_SHADER_TEXTURE_FILENAME,
constants::VERTEX_SHADER_TEXTURE_FILENAME,
constants::FRAGMENT_SHADER_TEXTURE_FILENAME); constants::FRAGMENT_SHADER_TEXTURE_FILENAME);
unsigned int VAO = generateBindVAO(); unsigned int VAO = generateBindVAO();
unsigned int VBO = copyVerticesMemory( unsigned int VBO =
vertices, copyVerticesMemory(vertices, verticesSize, GL_ARRAY_BUFFER);
verticesSize, unsigned int EBO =
GL_ARRAY_BUFFER); copyVerticesMemory(indices, indicesSize, GL_ELEMENT_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( unsigned int texture2 = loadAndCreateTexture("assets/awesomeface.png", false);
"assets/awesomeface.png",
false);
ourShader.use(); // don't forget to activate/use ourShader.use(); // don't forget to activate/use
// the shader before setting uniforms! // the shader before setting uniforms!
@ -260,7 +216,6 @@ drawFigureReturn drawDebilMode(
ourShader.setInt("texture1", 0); ourShader.setInt("texture1", 0);
ourShader.setInt("texture2", 1); ourShader.setInt("texture2", 1);
activateAndBindTextures(texture1, GL_TEXTURE0); activateAndBindTextures(texture1, GL_TEXTURE0);
activateAndBindTextures(texture2, GL_TEXTURE1); activateAndBindTextures(texture2, GL_TEXTURE1);
@ -274,11 +229,9 @@ drawFigureReturn drawDebilMode(
return newReturn; return newReturn;
} }
void doDrawElements( void doDrawElements(const unsigned int shaderProgram,
const unsigned int shaderProgram,
const unsigned int vertexArrayObject, const unsigned int vertexArrayObject,
const GLenum drawArrayMode, const GLenum drawArrayMode, const GLenum drawType,
const GLenum drawType,
const int numberOfElementsToDraw) { const int numberOfElementsToDraw) {
glUseProgram(shaderProgram); glUseProgram(shaderProgram);
glBindVertexArray(vertexArrayObject); glBindVertexArray(vertexArrayObject);
@ -286,13 +239,11 @@ void doDrawElements(
glBindVertexArray(0); glBindVertexArray(0);
} }
void updateUniformColor( void updateUniformColor(const unsigned int shaderProgram,
const unsigned int shaderProgram,
const GLchar* uniformName) { 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 = const float greenValue = sin(timeValue) / 2.0f + 0.5f;
sin(timeValue) / 2.0f + 0.5f;
// vary the color from 0.0 to 1.0 using sin // vary the color from 0.0 to 1.0 using sin
// query the location of our uniform // query the location of our uniform
const int vertexColorLocation = const int vertexColorLocation =
@ -312,11 +263,9 @@ void updateUniformColor(
} }
} }
void doDrawArrays( void doDrawArrays(const unsigned int shaderProgram,
const unsigned int shaderProgram,
const unsigned int vertexArrayObject, const unsigned int vertexArrayObject,
const GLenum drawArrayMode, const GLenum drawArrayMode, const int firstIndex,
const int firstIndex,
const unsigned int numberOfIndicesToBeRendered) { const unsigned int numberOfIndicesToBeRendered) {
// use shader program to render an object // use shader program to render an object
glUseProgram(shaderProgram); glUseProgram(shaderProgram);

View File

@ -1,9 +1,11 @@
// "Copyright [2023] <Krzysztof Rudnicki>" // "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef ENGINE_ENGINE_DRAW_HPP_ #ifndef ENGINE_ENGINE_DRAW_HPP_
#define ENGINE_ENGINE_DRAW_HPP_ #define ENGINE_ENGINE_DRAW_HPP_
#include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <string> #include <string>
#include "./constants.hpp" #include "./constants.hpp"
struct offsetsStruct { struct offsetsStruct {
@ -28,43 +30,34 @@ drawFigureReturn drawSquare(
const size_t squareIndicesSize = constants::SQUARE_INDICES_SIZE, const size_t squareIndicesSize = constants::SQUARE_INDICES_SIZE,
const char* vertexShaderSource = constants::VERTEX_SHADER_SOURCE, const char* vertexShaderSource = constants::VERTEX_SHADER_SOURCE,
const char* fragmentShaderSource = constants::FRAGMENT_SHADER_SOURCE, const char* fragmentShaderSource = constants::FRAGMENT_SHADER_SOURCE,
const bool colorIncluded = false, const bool colorIncluded = false, const bool textureIncluded = false,
const bool textureIncluded = false,
const offsetsStruct offsets = offsetsStruct()); const offsetsStruct offsets = offsetsStruct());
void doDrawElements( void doDrawElements(const unsigned int shaderProgram,
const unsigned int shaderProgram,
const unsigned int vertexArrayObject, const unsigned int vertexArrayObject,
const GLenum drawArrayMode, const GLenum drawArrayMode, const GLenum drawType,
const GLenum drawType,
const int numberOfElementsToDraw); const int numberOfElementsToDraw);
drawFigureReturn drawTriangleClass( drawFigureReturn drawTriangleClass(
const float triangleVertices[], const char* vertexPath, const char* fragmentPath,
const size_t triangleVerticesSize, const float triangleVertices[], const size_t triangleVerticesSize,
const char* vertexPath, const bool colorIncluded = false, const bool textureIncluded = false,
const char* fragmentPath,
const bool colorIncluded = false,
const bool textureIncluded = false,
const offsetsStruct offsets = offsetsStruct()); const offsetsStruct offsets = offsetsStruct());
void updateUniformColor( void updateUniformColor(const unsigned int shaderProgram,
const unsigned int shaderProgram,
const GLchar* uniformName); const GLchar* uniformName);
void doDrawArrays( void doDrawArrays(const unsigned int shaderProgram,
const unsigned int shaderProgram,
const unsigned int vertexArrayObject, const unsigned int vertexArrayObject,
const GLenum drawArrayMode, const GLenum drawArrayMode, const int firstIndex,
const int firstIndex,
const unsigned int numberOfIndicesToBeRendered); const unsigned int numberOfIndicesToBeRendered);
drawFigureReturn drawDebilMode( drawFigureReturn drawDebilMode(
const char* vertexPath, const char* vertexPath = constants::VERTEX_SHADER_SOURCE_FILENAME,
const char* fragmentPath, const char* fragmentPath = constants::FRAGMENT_SHADER_SOURCE_FILENAME,
const float vertices[], const float vertices[] = constants::TRIANGLE_VERTICES,
const size_t verticesSize, const size_t verticesSize = constants::TRIANGLE_VERTICES_SIZE,
const unsigned int indices[], const unsigned int indices[] = constants::TEXTURE_INDICES,
const size_t indicesSize); const size_t indicesSize = constants::TEXTURE_INDICES_SIZE);
#endif // ENGINE_ENGINE_DRAW_HPP_ #endif // ENGINE_ENGINE_DRAW_HPP_

Binary file not shown.

View File

@ -1,8 +1,8 @@
// "Copyright [2023] <Krzysztof Rudnicki>" // "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef MAIN_CPP #ifndef MAIN_CPP
#define MAIN_CPP #define MAIN_CPP
#include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <iostream> #include <iostream>
#include <random> #include <random>
@ -11,16 +11,15 @@
// https://arvid.io/2018/06/30/on-cxx-random-number-generator-quality/ // https://arvid.io/2018/06/30/on-cxx-random-number-generator-quality/
// whenever, if ever I feel like it // whenever, if ever I feel like it
#include "./constants.hpp"
#include "./beforeRender.hpp" #include "./beforeRender.hpp"
#include "./constants.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) return -1;
return -1;
renderLoop(window); renderLoop(window);
// clean GLFW resources // clean GLFW resources

View File

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

View File

@ -1,14 +1,17 @@
// "Copyright [2023] <Krzysztof Rudnicki>" // "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef RENDER_LOOP_CPP #ifndef RENDER_LOOP_CPP
#define RENDER_LOOP_CPP #define RENDER_LOOP_CPP
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "./renderLoop.hpp" #include "./renderLoop.hpp"
#include "./draw.hpp"
#include "./shaders.hpp" #include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <iostream>
#include "./constants.hpp" #include "./constants.hpp"
#include "./draw.hpp"
#include "./misc.hpp" #include "./misc.hpp"
#include "./shaders.hpp"
int processInput(GLFWwindow *window, const int whatToDraw) { int processInput(GLFWwindow *window, const int whatToDraw) {
static bool lockedLeft = false; static bool lockedLeft = false;
@ -25,10 +28,8 @@ int processInput(GLFWwindow *window, const int whatToDraw) {
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
if ( !PRESSED_CHANGE_LEFT ) if (!PRESSED_CHANGE_LEFT) lockedLeft = 0;
lockedLeft = 0; if (!PRESSED_CHANGE_RIGHT) lockedRight = 0;
if ( !PRESSED_CHANGE_RIGHT )
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);
@ -41,8 +42,7 @@ int processInput(GLFWwindow *window, const int whatToDraw) {
} }
// https://stackoverflow.com/a/25680092 // https://stackoverflow.com/a/25680092
unsigned int copyVerticesMemory( unsigned int copyVerticesMemory(const float vertices[],
const float vertices[],
const size_t sizeOfVertices, const size_t sizeOfVertices,
const GLenum boundBufferTarget) { const GLenum boundBufferTarget) {
// stores vertices in gpu memory // stores vertices in gpu memory
@ -56,16 +56,15 @@ unsigned int copyVerticesMemory(
// we change bound buffer vertexBufferObject // 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
GL_STATIC_DRAW: the data is set only once and used many times. times. GL_STATIC_DRAW: the data is set only once and used many times.
GL_DYNAMIC_DRAW: the data is changed a lot and used many times. GL_DYNAMIC_DRAW: the data is changed a lot and used many times.
*/ */
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW); glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
return vertexBufferObject; return vertexBufferObject;
} }
unsigned int copyVerticesMemory( unsigned int copyVerticesMemory(const unsigned int vertices[],
const unsigned int vertices[],
const size_t sizeOfVertices, const size_t sizeOfVertices,
const GLenum boundBufferTarget) { const GLenum boundBufferTarget) {
// stores vertices in gpu memory // stores vertices in gpu memory
@ -74,58 +73,53 @@ unsigned int copyVerticesMemory(
// by its ID generated here and stored in vertexBufferObject variable // 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( glBindBuffer(boundBufferTarget, vertexBufferObject);
boundBufferTarget,
vertexBufferObject);
// now whenever we change GL_ARRAY_BUFFER // now whenever we change GL_ARRAY_BUFFER
// we change bound buffer vertexBufferObject // 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
GL_STATIC_DRAW: the data is set only once and used many times. times. GL_STATIC_DRAW: the data is set only once and used many times.
GL_DYNAMIC_DRAW: the data is changed a lot and used many times. GL_DYNAMIC_DRAW: the data is changed a lot and used many times.
*/ */
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW); glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
return vertexBufferObject; return vertexBufferObject;
} }
void configureVertexAttribute( void configureVertexAttribute(const bool colorIncluded,
const bool colorIncluded,
const bool textureIncluded) { 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
size of vertex attribute (we use vec3 so it contains 3 values) = 0)) size of vertex attribute (we use vec3 so it contains 3 values) type of
type of data of which vec consists of data of which vec consists of should data be normalized, (useful when we use
should data be normalized, (useful when we use integer data) integer data) space between vertex attributes, each position data is 3 times
space between vertex attributes, each position data is 3 times the size of float the size of float offset of where position data begins in buffer see:
offset of where position data begins in buffer 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
vertex attribute data take data from memory managed by VBO bound to GL_ARRAY_BUFFER */ GL_ARRAY_BUFFER */
if (!colorIncluded) { if (!colorIncluded) {
glVertexAttribPointer( glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float),
0, 3, GL_FLOAT, GL_FALSE, reinterpret_cast<void *>(0));
3 * sizeof(float), reinterpret_cast<void *>(0));
// enable vertex attribute // enable vertex attribute
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
return; return;
} }
/* 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
https://learnopengl.com/img/getting-started/vertex_attribute_pointer_interleaved.png */ 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) { if (colorIncluded && !textureIncluded) {
glVertexAttribPointer( glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
0, 3, GL_FLOAT, GL_FALSE, reinterpret_cast<void *>(0));
6 * sizeof(float), reinterpret_cast<void *>(0));
// enable vertex attribute // enable vertex attribute
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer( glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
1, 3, GL_FLOAT, GL_FALSE, reinterpret_cast<void *>(3 * sizeof(float)));
6 * sizeof(float), reinterpret_cast<void *>(3* sizeof(float)));
// enable vertex attribute // enable vertex attribute
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
return; return;
@ -136,21 +130,18 @@ void configureVertexAttribute(
// this time for textures we add just // this time for textures we add just
// 2 more attributes and not 3 as with color // 2 more attributes and not 3 as with color
if (textureIncluded) { if (textureIncluded) {
glVertexAttribPointer( glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
0, 3, GL_FLOAT, GL_FALSE, reinterpret_cast<void *>(0));
8 * sizeof(float), reinterpret_cast<void *>(0));
// enable vertex attribute // enable vertex attribute
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer( glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
1, 3, GL_FLOAT, GL_FALSE, reinterpret_cast<void *>(3 * sizeof(float)));
8 * sizeof(float), reinterpret_cast<void *>(3* sizeof(float)));
// enable vertex attribute // enable vertex attribute
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer( glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
2, 2, GL_FLOAT, GL_FALSE, reinterpret_cast<void *>(6 * sizeof(float)));
8 * sizeof(float), reinterpret_cast<void*>(6 * sizeof(float)));
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
return; return;
} }
@ -166,10 +157,8 @@ unsigned int generateBindVAO() {
return vertexArrayObject; return vertexArrayObject;
} }
void copyVerticesArray( void copyVerticesArray(const unsigned int vertexBufferObject,
const unsigned int vertexBufferObject, const float vertices[], const size_t sizeOfVertices,
const float vertices[],
const size_t sizeOfVertices,
const GLenum boundBufferTarget) { 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);
@ -181,8 +170,7 @@ int renderLoopInside(GLFWwindow *window, int whatToDraw) {
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( glClearColor(constants::LEARN_OPEN_GL_COLOR.red,
constants::LEARN_OPEN_GL_COLOR.red,
constants::LEARN_OPEN_GL_COLOR.green, constants::LEARN_OPEN_GL_COLOR.green,
constants::LEARN_OPEN_GL_COLOR.blue, constants::LEARN_OPEN_GL_COLOR.blue,
constants::LEARN_OPEN_GL_COLOR.alpha); constants::LEARN_OPEN_GL_COLOR.alpha);
@ -190,7 +178,6 @@ int renderLoopInside(GLFWwindow *window, int whatToDraw) {
// GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT // 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!");
@ -214,7 +201,6 @@ int renderLoopInside(GLFWwindow *window, int whatToDraw) {
glDeleteBuffers(1, &successAndVao.VBO); glDeleteBuffers(1, &successAndVao.VBO);
glDeleteBuffers(1, &successAndVao.EBO); glDeleteBuffers(1, &successAndVao.EBO);
return whatToDraw; return whatToDraw;
} }

View File

@ -2,32 +2,28 @@
#ifndef ENGINE_ENGINE_RENDERLOOP_HPP_ #ifndef ENGINE_ENGINE_RENDERLOOP_HPP_
#define ENGINE_ENGINE_RENDERLOOP_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( void copyVerticesArray(unsigned int vertexBufferObject, const float vertices[],
unsigned int vertexBufferObject,
const float vertices[],
const size_t sizeOfVertices, const size_t sizeOfVertices,
const GLenum boundBufferTarget); const GLenum boundBufferTarget);
unsigned int generateBindVAO(); unsigned int generateBindVAO();
void configureVertexAttribute( void configureVertexAttribute(const bool colorIncluded,
const bool colorIncluded,
const bool textureIncluded); const bool textureIncluded);
int processInput(GLFWwindow *window, int whatToDraw); int processInput(GLFWwindow *window, int whatToDraw);
unsigned int copyVerticesMemory( unsigned int copyVerticesMemory(const float vertices[],
const float vertices[],
const size_t sizeOfVertices, const size_t sizeOfVertices,
const GLenum boundBufferTarget); const GLenum boundBufferTarget);
unsigned int copyVerticesMemory( unsigned int copyVerticesMemory(const unsigned int vertices[],
const unsigned int vertices[],
const size_t sizeOfVertices, const size_t sizeOfVertices,
const GLenum boundBufferTarget); const GLenum boundBufferTarget);

View File

@ -2,13 +2,14 @@
#ifndef SHADER_CPP #ifndef SHADER_CPP
#define SHADER_CPP #define SHADER_CPP
#include "./shader.hpp"
#include <glad/glad.h> #include <glad/glad.h>
#include <string>
#include <fstream> #include <fstream>
#include <sstream>
#include <iostream> #include <iostream>
#include "./shader.hpp" #include <sstream>
#include <string>
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
@ -26,20 +27,16 @@ const std::string Shader::fileToShader(const std::string shaderPath) {
shaderFile.close(); shaderFile.close();
// 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 << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what()
std::cout << << 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();
@ -67,14 +64,10 @@ Shader::Shader(const std::string vertexPath, const std::string fragmentPath) {
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 {
@ -92,20 +85,20 @@ void Shader::checkCompileErrors(unsigned int shader, std::string type) {
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 << std::cout
"ERROR::SHADER_COMPILATION_ERROR of type: " << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n"
<< type << "\n" << infoLog << << infoLog
"\n -- --------------------------------------------------- -- " << "\n -- --------------------------------------------------- -- "
<< std::endl; << 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 << std::cout
"ERROR::PROGRAM_LINKING_ERROR of type: " << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n"
<< type << "\n" << infoLog << << infoLog
"\n -- --------------------------------------------------- -- " << "\n -- --------------------------------------------------- -- "
<< std::endl; << std::endl;
} }
} }

View File

@ -4,10 +4,10 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <string>
#include <fstream> #include <fstream>
#include <sstream>
#include <iostream> #include <iostream>
#include <sstream>
#include <string>
class Shader { class Shader {
public: public:
@ -32,9 +32,7 @@ class Shader {
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void checkCompileErrors(unsigned int shader, std::string type); void checkCompileErrors(unsigned int shader, std::string type);
void compileErrorsMessage( void compileErrorsMessage(const unsigned int shader, const bool compilation,
const unsigned int shader,
const bool compilation,
const std::string type, const std::string type,
const std::string errorMessage); const std::string errorMessage);
}; };

View File

@ -1,14 +1,15 @@
// "Copyright [2023] <Krzysztof Rudnicki>" // "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef SHADERS_CPP #ifndef SHADERS_CPP
#define SHADERS_CPP #define SHADERS_CPP
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "./shaders.hpp" #include "./shaders.hpp"
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include "./constants.hpp" #include "./constants.hpp"
#include "./misc.hpp" #include "./misc.hpp"
unsigned int linkShaderObjectsShaderProgram( unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders,
const unsigned int vertexShaders,
const unsigned int fragmentShader) { 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
@ -21,8 +22,7 @@ unsigned int linkShaderObjectsShaderProgram(
// link shaders // link shaders
glLinkProgram(shaderProgram); glLinkProgram(shaderProgram);
if (!shaderSuccessful(shaderProgram, false)) if (!shaderSuccessful(shaderProgram, false)) return 0;
return 0;
// activate program // activate program
// after that every shader and rendering call will use this program object // after that every shader and rendering call will use this program object
@ -32,14 +32,11 @@ unsigned int linkShaderObjectsShaderProgram(
// (they are linked into shaderProgram and we do not need them anymore) // (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) print("Shader Program Linking Failed");
print("Shader Program Linking Failed");
return shaderProgram; return shaderProgram;
} }
unsigned int compileShader( unsigned int compileShader(const GLenum shaderType, const char* shaderSource) {
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);
@ -50,14 +47,12 @@ unsigned int compileShader(
glShaderSource(shaderID, 1, &shaderSource, nullptr); glShaderSource(shaderID, 1, &shaderSource, nullptr);
// compile shader // compile shader
glCompileShader(shaderID); glCompileShader(shaderID);
if (!shaderSuccessful(shaderID, true)) if (!shaderSuccessful(shaderID, true)) return 0;
return 0;
return shaderID; return shaderID;
} }
std::pair<unsigned int, unsigned int> compileShaders( std::pair<unsigned int, unsigned int> compileShaders(
const char* vertexShaderSource, const char* vertexShaderSource, const char* fragmentShaderSource) {
const char* fragmentShaderSource) {
const unsigned int vertexShader = const unsigned int vertexShader =
compileShader(GL_VERTEX_SHADER, vertexShaderSource); compileShader(GL_VERTEX_SHADER, vertexShaderSource);

View File

@ -1,18 +1,17 @@
// "Copyright [2023] <Krzysztof Rudnicki>" // "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef ENGINE_ENGINE_SHADERS_HPP_ #ifndef ENGINE_ENGINE_SHADERS_HPP_
#define ENGINE_ENGINE_SHADERS_HPP_ #define ENGINE_ENGINE_SHADERS_HPP_
#include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <iostream> #include <iostream>
#include <utility> #include <utility>
unsigned int linkShaderObjectsShaderProgram( unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders,
const unsigned int vertexShaders,
const unsigned int fragmentShader); const unsigned int fragmentShader);
std::pair<unsigned int, unsigned int> compileShaders( std::pair<unsigned int, unsigned int> compileShaders(
const char* vertexShaderSource, const char* vertexShaderSource, const char* fragmentShaderSource);
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);

View File

@ -1,120 +1,141 @@
// "Copyright [2023] <Krzysztof Rudnicki>" // "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef TEXTURES_CPP #ifndef TEXTURES_CPP
#define TEXTURES_CPP #define TEXTURES_CPP
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "stb_image.h"
#include "textures.hpp" #include "textures.hpp"
void setTextureSCoordinate(const GLenum textureTarget, const GLint sCoordinateOption, const float* borderColor) #include <GLFW/glfw3.h>
{ #include <glad/glad.h>
#include "stb_image.h"
void setTextureSCoordinate(const GLenum textureTarget,
const GLint sCoordinateOption,
const float* borderColor) {
// https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexParameter.xhtml // https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexParameter.xhtml
// first argument is texture target (by default we work with 2D textures so it will be GL_TEXTURE_2D) // first argument is texture target (by default we work with 2D textures so it
// second argument is what option we want to set and for which texture axis (s, t or r) // will be GL_TEXTURE_2D) second argument is what option we want to set and
// Third argument is texture wrapping mode // for which texture axis (s, t or r) Third argument is texture wrapping mode
glTexParameteri(textureTarget, GL_TEXTURE_WRAP_S, sCoordinateOption); glTexParameteri(textureTarget, GL_TEXTURE_WRAP_S, sCoordinateOption);
if(sCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) if (sCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) {
{ // if we use GL_CLAMP_TO_BORDER all of the space that is not occupied by
// if we use GL_CLAMP_TO_BORDER all of the space that is not occupied by texture will be occupied by color // texture will be occupied by color we pass the color we want to use in
// we pass the color we want to use in float array (RGB + transparency) // float array (RGB + transparency)
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
} }
} }
void setTextureTCoordinate(const GLenum textureTarget, const GLint tCoordinateOption, const float* borderColor) void setTextureTCoordinate(const GLenum textureTarget,
{ const GLint tCoordinateOption,
// for comments concerning glTexParameteri and glTexParameterfv go to setTextureSCoordinate function const float* borderColor) {
if(textureTarget == GL_TEXTURE_2D || textureTarget == GL_TEXTURE_3D) // for comments concerning glTexParameteri and glTexParameterfv go to
{ // setTextureSCoordinate function
if (textureTarget == GL_TEXTURE_2D || textureTarget == GL_TEXTURE_3D) {
glTexParameteri(textureTarget, GL_TEXTURE_WRAP_T, tCoordinateOption); glTexParameteri(textureTarget, GL_TEXTURE_WRAP_T, tCoordinateOption);
if(tCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) if (tCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) {
{
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
} }
} }
} }
void setTextureRCoordinate(const GLenum textureTarget, const GLint rCoordinateOption, const float* borderColor) void setTextureRCoordinate(const GLenum textureTarget,
{ const GLint rCoordinateOption,
// for comments concerning glTexParameteri and glTexParameterfv go to setTextureSCoordinate function const float* borderColor) {
if(textureTarget == GL_TEXTURE_3D) // for comments concerning glTexParameteri and glTexParameterfv go to
{ // setTextureSCoordinate function
if (textureTarget == GL_TEXTURE_3D) {
glTexParameteri(textureTarget, GL_TEXTURE_WRAP_R, rCoordinateOption); glTexParameteri(textureTarget, GL_TEXTURE_WRAP_R, rCoordinateOption);
if(rCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) if (rCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) {
{
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
} }
} }
} }
// Coordinate options can be: GL_REPEAT, GL_MIRRORED_REPEAT, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER // Coordinate options can be: GL_REPEAT, GL_MIRRORED_REPEAT, GL_CLAMP_TO_EDGE,
// s t r are equivalent to x y z // GL_CLAMP_TO_BORDER s t r are equivalent to x y z
void setTextureParametersINT(const GLenum textureTarget, const GLint sCoordinateOption, const GLint tCoordinateOption, const GLint rCoordinateOption, const float* borderColor) void setTextureParametersINT(const GLenum textureTarget,
{ const GLint sCoordinateOption,
const GLint tCoordinateOption,
const GLint rCoordinateOption,
const float* borderColor) {
setTextureSCoordinate(textureTarget, sCoordinateOption, borderColor); setTextureSCoordinate(textureTarget, sCoordinateOption, borderColor);
setTextureTCoordinate(textureTarget, tCoordinateOption, borderColor); setTextureTCoordinate(textureTarget, tCoordinateOption, borderColor);
setTextureRCoordinate(textureTarget, rCoordinateOption, borderColor); setTextureRCoordinate(textureTarget, rCoordinateOption, borderColor);
} }
void setTextureFilteringAndMipMap(const GLenum textureTarget, const GLenum filterType, const GLint textureFilteringMethod, const GLint mipMapFilteringMethod) void setTextureFilteringAndMipMap(const GLenum textureTarget,
{ const GLenum filterType,
// mipMapFilteringMethod can be equal to: GL_NEAREST_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR const GLint textureFilteringMethod,
const GLint mipMapFilteringMethod) {
// mipMapFilteringMethod can be equal to: GL_NEAREST_MIPMAP_NEAREST,
// GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR
// NEAREST_MIPMAP - take nearest mipmap to match pixel size // NEAREST_MIPMAP - take nearest mipmap to match pixel size
// LINEAR_MIPMAP - interpolate between two mipmaps that match the size of a pixel // LINEAR_MIPMAP - interpolate between two mipmaps that match the size of a
// NEAREST (at the end) - samples texture based on nearest neighbor interpolation // pixel NEAREST (at the end) - samples texture based on nearest neighbor
// LINEAR (at the end) - samples texture using linear interpolation // interpolation LINEAR (at the end) - samples texture using linear
// setting anything else than GL_TEXTURE_MIN_FILTER as a second parameter results in GL_INVALID_ENUM error // interpolation setting anything else than GL_TEXTURE_MIN_FILTER as a second
// parameter results in GL_INVALID_ENUM error
glTexParameteri(textureTarget, GL_TEXTURE_MIN_FILTER, mipMapFilteringMethod); glTexParameteri(textureTarget, GL_TEXTURE_MIN_FILTER, mipMapFilteringMethod);
// https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexParameter.xhtml // https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexParameter.xhtml
// first argument is texture target (by default we work with 2D textures so it will be GL_TEXTURE_2D) // first argument is texture target (by default we work with 2D textures so it
// second argument is specifying whether this filter works for magnifying textures GL_TEXTURE_MAG_FILTER or minifying GL_TEXTURE_MIN_FILTER // will be GL_TEXTURE_2D) second argument is specifying whether this filter
// third argument is how we should magnify/minify textures, whether we should choose the color of pixel closest to the coordinates GL_NEAREST (default) // works for magnifying textures GL_TEXTURE_MAG_FILTER or minifying
// or should we blend the neighboring pixels and choose blended color GL_LINEAR // GL_TEXTURE_MIN_FILTER third argument is how we should magnify/minify
// textures, whether we should choose the color of pixel closest to the
// coordinates GL_NEAREST (default) or should we blend the neighboring pixels
// and choose blended color GL_LINEAR
glTexParameteri(textureTarget, filterType, textureFilteringMethod); glTexParameteri(textureTarget, filterType, textureFilteringMethod);
} }
unsigned int generateAndBindTexture(const GLenum textureTarget,
unsigned int generateAndBindTexture(const GLenum textureTarget, const GLsizei numberOfTextures ) { const GLsizei numberOfTextures) {
unsigned int texture; unsigned int texture;
// first argument is how many textures we want to generate // first argument is how many textures we want to generate
// second is an array of unsigned int in which we will store id of those textures // second is an array of unsigned int in which we will store id of those
// textures
glGenTextures(numberOfTextures, &texture); glGenTextures(numberOfTextures, &texture);
// then we bind texture // then we bind texture
glBindTexture(textureTarget, texture); glBindTexture(textureTarget, texture);
return texture; return texture;
} }
unsigned int loadAndBindTextureFile(unsigned int texture, const char* texturePath, bool flipImage) { unsigned int loadAndBindTextureFile(unsigned int texture,
const char* texturePath, bool flipImage) {
int textureWidth, textureHeight, colorChannels; int textureWidth, textureHeight, colorChannels;
stbi_set_flip_vertically_on_load(flipImage); stbi_set_flip_vertically_on_load(flipImage);
unsigned char *data = stbi_load(texturePath, &textureWidth, &textureHeight, &colorChannels, STBI_rgb_alpha); unsigned char* data = stbi_load(texturePath, &textureWidth, &textureHeight,
&colorChannels, STBI_rgb_alpha);
if (data) { if (data) {
std::cout << "textureWidth " << textureWidth << " textureHeight " << textureHeight << " colorChannels " << colorChannels << std::endl; std::cout << "textureWidth " << textureWidth << " textureHeight "
<< textureHeight << " colorChannels " << colorChannels
<< std::endl;
// generate texture // generate texture
/* /*
first argument is texture target, setting it to GL_TEXTURE_2D will only affect 2D targets and not 1D or 3D first argument is texture target, setting it to GL_TEXTURE_2D will only
2nd is mipmap level for which to create texture, base level is 0 affect 2D targets and not 1D or 3D 2nd is mipmap level for which to
3rd is in what format we want to store texture (our image has only rgb values so rgb) create texture, base level is 0 3rd is in what format we want to store
4th and 5th are the width and height of texture texture (our image has only rgb values so rgb) 4th and 5th are the width
6th argument is always 0 (legacy, its called border parameter) and height of texture 6th argument is always 0 (legacy, its called border
7th and 8th are the format and datatype of source image, we store the image data as chars (bytes) so we pass that parameter) 7th and 8th are the format and datatype of source image, we
9th last argument is actual data of the texture store the image data as chars (bytes) so we pass that 9th last argument
is actual data of the texture
*/ */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0,
GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
// after generating texture, lets free the memory from image // after generating texture, lets free the memory from image
stbi_image_free(data); stbi_image_free(data);
return texture; return texture;
} } else {
else
{
std::cout << "Failed to load texture! " << std::endl; std::cout << "Failed to load texture! " << std::endl;
return -1; return -1;
} }
} }
// const char* texturePath, int textureWidth, int textureHeight, int colorChannels, const GLenum textureTarget, const GLint sCoordinateOption, const GLint tCoordinateOption, const GLint rCoordinateOption, const float* borderColor // const char* texturePath, int textureWidth, int textureHeight, int
// colorChannels, const GLenum textureTarget, const GLint sCoordinateOption,
// const GLint tCoordinateOption, const GLint rCoordinateOption, const float*
// borderColor
unsigned int loadAndCreateTexture(const char* texturePath, bool flipImage) { unsigned int loadAndCreateTexture(const char* texturePath, bool flipImage) {
// load and create a texture // load and create a texture
// ------------------------- // -------------------------
@ -129,8 +150,7 @@ unsigned int loadAndCreateTexture(const char* texturePath, bool flipImage) {
return texture; return texture;
} }
void activateAndBindTextures(unsigned int texture, GLenum textureNumber) void activateAndBindTextures(unsigned int texture, GLenum textureNumber) {
{
glActiveTexture(textureNumber); glActiveTexture(textureNumber);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
} }

View File

@ -1,20 +1,39 @@
// "Copyright [2023] <Krzysztof Rudnicki>" // "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef TEXTURES_HPP #ifndef TEXTURES_HPP
#define TEXTURES_HPP #define TEXTURES_HPP
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glad/glad.h>
void setTextureParametersINT(const GLenum textureTarget = GL_TEXTURE_2D, const GLint sCoordinateOption = GL_REPEAT, const GLint tCoordinateOption = GL_REPEAT, const GLint rCoordinateOption = GL_REPEAT, const float* borderColor = NULL); #include <iostream>
void setTextureSCoordinate(const GLenum textureTarget = GL_TEXTURE_2D, const GLint sCoordinateOption = GL_REPEAT, const float* borderColor = NULL);
void setTextureTCoordinate(const GLenum textureTarget = GL_TEXTURE_2D, const GLint tCoordinateOption = GL_REPEAT, const float* borderColor = NULL); void setTextureParametersINT(const GLenum textureTarget = GL_TEXTURE_2D,
void setTextureRCoordinate(const GLenum textureTarget = GL_TEXTURE_3D, const GLint rCoordinateOption = GL_REPEAT, const float* borderColor = NULL); const GLint sCoordinateOption = GL_REPEAT,
void setTextureFilteringAndMipMap(const GLenum textureTarget = GL_TEXTURE_2D, const GLenum filterType = GL_TEXTURE_MAG_FILTER, const GLint textureFilteringMethod = GL_NEAREST, const GLint mipMapFilteringMethod = GL_NEAREST_MIPMAP_NEAREST); const GLint tCoordinateOption = GL_REPEAT,
const GLint rCoordinateOption = GL_REPEAT,
const float* borderColor = NULL);
void setTextureSCoordinate(const GLenum textureTarget = GL_TEXTURE_2D,
const GLint sCoordinateOption = GL_REPEAT,
const float* borderColor = NULL);
void setTextureTCoordinate(const GLenum textureTarget = GL_TEXTURE_2D,
const GLint tCoordinateOption = GL_REPEAT,
const float* borderColor = NULL);
void setTextureRCoordinate(const GLenum textureTarget = GL_TEXTURE_3D,
const GLint rCoordinateOption = GL_REPEAT,
const float* borderColor = NULL);
void setTextureFilteringAndMipMap(
const GLenum textureTarget = GL_TEXTURE_2D,
const GLenum filterType = GL_TEXTURE_MAG_FILTER,
const GLint textureFilteringMethod = GL_NEAREST,
const GLint mipMapFilteringMethod = GL_NEAREST_MIPMAP_NEAREST);
unsigned int generateAndBindTexture(const GLenum textureTarget); unsigned int generateAndBindTexture(const GLenum textureTarget);
// const char* texturePath, int textureWidth, int textureHeight, int colorChannels, const GLenum textureTarget, const GLint sCoordinateOption, const GLint tCoordinateOption, const GLint rCoordinateOption, const float* borderColor // const char* texturePath, int textureWidth, int textureHeight, int
// colorChannels, const GLenum textureTarget, const GLint sCoordinateOption,
// const GLint tCoordinateOption, const GLint rCoordinateOption, const float*
// borderColor
unsigned int loadAndCreateTexture(const char* texturePath, bool flipImage); unsigned int loadAndCreateTexture(const char* texturePath, bool flipImage);
void activateAndBindTextures(unsigned int texture, GLenum textureNumber); void activateAndBindTextures(unsigned int texture, GLenum textureNumber);
unsigned int loadAndBindTextureFile(unsigned int texture, const char* texturePath, bool flipImage); unsigned int loadAndBindTextureFile(unsigned int texture,
const char* texturePath, bool flipImage);
#endif // TEXTURES_HPP #endif // TEXTURES_HPP