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": [
"aaron-bond.better-comments",
"jbenden.c-cpp-flylint",
"ms-vscode.cpptools",
"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",
"xaver.clang-format",
"streetsidesoftware.code-spell-checker",
"vadimcn.vscode-lldb",
"usernamehw.errorlens",
"donjayamanne.githistory",
"dtoplak.vscode-glsllint",
"kisstkondoros.vscode-gutter-preview",
"james-yu.latex-workshop",
"davidanson.vscode-markdownlint",
"pkief.material-icon-theme",
"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>"
#ifndef BEFORE_RENDER_CPP
#define BEFORE_RENDER_CPP
#include <glad/glad.h>
#include "./beforeRender.hpp"
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <iostream>
#include "./beforeRender.hpp"
#include "./constants.hpp"
#include "./misc.hpp"
@ -37,11 +39,8 @@ GLFWwindow *createWindowObject() {
// Third is the name of the window
// We ignore last two
GLFWwindow *window = glfwCreateWindow(
constants::MAIN_WINDOW_WIDTH,
constants::MAIN_WINDOW_HEIGHT,
constants::MAIN_WINDOW_NAME,
nullptr,
nullptr);
constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT,
constants::MAIN_WINDOW_NAME, nullptr, nullptr);
return window;
}
@ -55,8 +54,7 @@ int initializeGLAD() {
}
// resizes viewport when user resizes window
void framebuffer_size_callback(GLFWwindow *window,
const int width,
void framebuffer_size_callback(GLFWwindow *window, const int width,
const int height) {
glViewport(0, 0, width, height);
}
@ -67,10 +65,7 @@ void viewPort(GLFWwindow *window) {
// 3th and 4th width and height of rendering window
// we could set them to be smaller than window dimension,
// ogl rendering will be then displayed in smaller window
glViewport(0,
0,
constants::MAIN_WINDOW_WIDTH,
constants::MAIN_WINDOW_HEIGHT);
glViewport(0, 0, constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT);
// processed coordinates are between -1 and 1 so here we map:
// (-1 to 1) to (0, constants::MAIN_WINDOW_WIDTH)
// and (0, constants::MAIN_WINDOW_HEIGHT)
@ -92,8 +87,7 @@ GLFWwindow *prepareForRender() {
// we make context of this window main context of current thread
glfwMakeContextCurrent(window);
if (initializeGLAD() == -1)
return nullptr;
if (initializeGLAD() == -1) return nullptr;
viewPort(window);
return window;
}

View File

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

View File

@ -2,10 +2,10 @@
#ifndef ENGINE_ENGINE_CONSTANTS_HPP_
#define ENGINE_ENGINE_CONSTANTS_HPP_
#include <GLFW/glfw3.h>
#include <iostream>
#include <string_view>
namespace constants {
inline constexpr int GLFW_MAJOR_VERSION{3};
inline constexpr int GLFW_MINOR_VERSION{3};
@ -36,53 +36,44 @@ namespace constants {
} LEARN_OPEN_GL_COLOR;
inline const char* VERTEX_SHADER_COLOR_FILENAME{
"./Shaders/vertexShaderColor.vs"
};
"./Shaders/vertexShaderColor.vs"};
inline const char* VERTEX_SHADER_UPSIDE_DOWN_FILENAME{
"./Shaders/vertexShaderUpsideDown.vs"
};
"./Shaders/vertexShaderUpsideDown.vs"};
inline const char* VERTEX_SHADER_OFFSET_FILENAME{
"./Shaders/vertexShaderOffset.vs"
};
"./Shaders/vertexShaderOffset.vs"};
// 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
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"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0"};
inline const char* VERTEX_SHADER_SOURCE_FILENAME{
"./Shaders/vertexShaderSource.vs"
};
"./Shaders/vertexShaderSource.vs"};
inline const char* VERTEX_SHADER_VERTICE_COLOR_FILENAME{
"./Shaders/vertexShaderVerticeColor.vs"
};
"./Shaders/vertexShaderVerticeColor.vs"};
inline const char* VERTEX_SHADER_TASK_THREE_FILENAME{
"./Shaders/vertexShaderTaskThree.vs"
};
"./Shaders/vertexShaderTaskThree.vs"};
inline const char* VERTEX_SHADER_TEXTURE_FILENAME{
"./Shaders/vertexShaderTexture.vs"
};
"./Shaders/vertexShaderTexture.vs"};
inline const char* FRAGMENT_SHADER_TASK_THREE_FILENAME{
"./Shaders/fragmentShaderTaskThree.fs"
};
"./Shaders/fragmentShaderTaskThree.fs"};
inline const char* FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME{
"./Shaders/fragmentShaderColorFromVertex.fs"
};
"./Shaders/fragmentShaderColorFromVertex.fs"};
inline const char* FRAGMENT_SHADER_UNIFORMS_FILENAME{
"./Shaders/fragmentShaderUniforms.fs"
};
"./Shaders/fragmentShaderUniforms.fs"};
// write fragment shader
// we set the color of each pixel to be orange
@ -93,32 +84,23 @@ namespace constants {
"void main()\n"
"{\n"
"FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\0"
};
"}\0"};
inline const char* FRAGMENT_SHADER_SOURCE_FILENAME{
"./Shaders/fragmentShaderSource.fs"
};
"./Shaders/fragmentShaderSource.fs"};
inline const char* FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME{
"./Shaders/fragmentShaderSourceYellow.fs"
};
"./Shaders/fragmentShaderSourceYellow.fs"};
inline const char* FRAGMENT_SHADER_TEXTURE_FILENAME{
"./Shaders/fragmentShaderTexture.fs"
};
"./Shaders/fragmentShaderTexture.fs"};
// we specify three vertices
// each of them with position in 3d space
// x y z
inline constexpr float TRIANGLE_VERTICES[] {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
inline constexpr float TRIANGLE_VERTICES[]{-0.5f, -0.5f, 0.0f, 0.5f, -0.5f,
0.0f, 0.0f, 0.5f, 0.0f};
inline constexpr size_t TRIANGLE_VERTICES_SIZE = {
sizeof(TRIANGLE_VERTICES)
};
inline constexpr size_t TRIANGLE_VERTICES_SIZE = {sizeof(TRIANGLE_VERTICES)};
inline constexpr float TRIANGLES_VERTICES[]{
// first triangle
@ -131,8 +113,7 @@ namespace constants {
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
@ -202,10 +183,7 @@ namespace constants {
-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

View File

@ -1,61 +1,57 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef DRAW_CPP
#define DRAW_CPP
#include <glad/glad.h>
#include "./draw.hpp"
#include <GLFW/glfw3.h>
#include <iostream>
#include <glad/glad.h>
#include <cmath>
#include <filesystem>
#include <iostream>
#include <utility>
#include "./draw.hpp"
#include "./renderLoop.hpp"
#include "./shaders.hpp"
#include "./constants.hpp"
#include "./misc.hpp"
#include "./renderLoop.hpp"
#include "./shader.hpp"
#include "./textures.hpp"
#include "./shaders.hpp"
#include "./stb_image.h"
#include "./textures.hpp"
drawFigureReturn drawFigure(const int whatToDraw) {
switch (whatToDraw) {
case 0:
return drawTriangleClass(
return drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE,
constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
constants::TRIANGLE_VERTICES_SIZE);
case 1:
return drawSquare(
constants::SQUARE_VERTICES,
constants::SQUARE_VERTICES_SIZE,
constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE,
constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE,
constants::VERTEX_SHADER_SOURCE,
constants::FRAGMENT_SHADER_SOURCE);
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(
return drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLES_VERTICES,
constants::TRIANGLES_VERTICES_SIZE,
constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
constants::TRIANGLES_VERTICES_SIZE);
case 3:
drawTriangleClass(constants::TRIANGLE_ONE,
constants::TRIANGLE_ONE_SIZE,
constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE);
// Now create the same 2 triangles
// 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_SIZE,
constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
constants::TRIANGLE_TWO_SIZE);
case 4:
drawTriangleClass(constants::TRIANGLE_ONE,
constants::TRIANGLE_ONE_SIZE,
constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE);
/*
Create two shader programs where
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
*/
return drawTriangleClass(
constants::TRIANGLE_TWO,
constants::TRIANGLE_TWO_SIZE,
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:
// Get color from vertex shader to fragment shader
return drawTriangleClass(constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE,
return drawTriangleClass(
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:
// 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::VERTEX_SHADER_COLOR_FILENAME,
constants::FRAGMENT_SHADER_UNIFORMS_FILENAME,
constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE);
case 7:
// set color from opengl code to uniform value in fragment shader
return drawTriangleClass(constants::TRIANGLE_COLORS,
constants::TRIANGLE_COLORS_SIZE,
return drawTriangleClass(
constants::VERTEX_SHADER_VERTICE_COLOR_FILENAME,
constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME,
true);
constants::TRIANGLE_COLORS, constants::TRIANGLE_COLORS_SIZE, 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);
case 9:
{
return drawTriangleClass(constants::VERTEX_SHADER_UPSIDE_DOWN_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE);
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,
return drawTriangleClass(constants::VERTEX_SHADER_OFFSET_FILENAME,
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
false,
false,
constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE, 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);
return drawTriangleClass(constants::VERTEX_SHADER_TASK_THREE_FILENAME,
constants::FRAGMENT_SHADER_TASK_THREE_FILENAME,
constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE);
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);
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,
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;
if (shaders.first == 0 || shaders.second == 0) return 0;
const unsigned int shaderProgram =
linkShaderObjectsShaderProgram(shaders.first, shaders.second);
if (shaderProgram == 0)
return 0;
if (shaderProgram == 0) return 0;
return shaderProgram;
}
@ -145,11 +131,10 @@ void setOffsets(Shader shader, const offsetsStruct offsets) {
shader.setFloat("zOffset", offsets.zOffset);
}
drawFigureReturn drawTriangleClass(
drawFigureReturn drawTriangleClass(const char* vertexPath,
const char* fragmentPath,
const float triangleVertices[],
const size_t triangleVerticesSize,
const char* vertexPath,
const char* fragmentPath,
const bool colorIncluded,
const bool textureIncluded,
const offsetsStruct offsets) {
@ -161,24 +146,14 @@ drawFigureReturn drawTriangleClass(
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,
copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize,
GL_ARRAY_BUFFER);
// set vertex attribute pointers
configureVertexAttribute(colorIncluded, textureIncluded);
doDrawArrays(
ourShader.ID,
vertexArrayObject,
GL_TRIANGLES,
0,
doDrawArrays(ourShader.ID, vertexArrayObject, GL_TRIANGLES, 0,
triangleVerticesSize);
drawFigureReturn newReturn;
newReturn.success = 0;
@ -189,14 +164,10 @@ drawFigureReturn drawTriangleClass(
}
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 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);
@ -207,52 +178,37 @@ drawFigureReturn drawSquare(
}
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,
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,
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,
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!
@ -260,7 +216,6 @@ drawFigureReturn drawDebilMode(
ourShader.setInt("texture1", 0);
ourShader.setInt("texture2", 1);
activateAndBindTextures(texture1, GL_TEXTURE0);
activateAndBindTextures(texture2, GL_TEXTURE1);
@ -274,11 +229,9 @@ drawFigureReturn drawDebilMode(
return newReturn;
}
void doDrawElements(
const unsigned int shaderProgram,
void doDrawElements(const unsigned int shaderProgram,
const unsigned int vertexArrayObject,
const GLenum drawArrayMode,
const GLenum drawType,
const GLenum drawArrayMode, const GLenum drawType,
const int numberOfElementsToDraw) {
glUseProgram(shaderProgram);
glBindVertexArray(vertexArrayObject);
@ -286,13 +239,11 @@ void doDrawElements(
glBindVertexArray(0);
}
void updateUniformColor(
const unsigned int shaderProgram,
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;
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 =
@ -312,11 +263,9 @@ void updateUniformColor(
}
}
void doDrawArrays(
const unsigned int shaderProgram,
void doDrawArrays(const unsigned int shaderProgram,
const unsigned int vertexArrayObject,
const GLenum drawArrayMode,
const int firstIndex,
const GLenum drawArrayMode, const int firstIndex,
const unsigned int numberOfIndicesToBeRendered) {
// use shader program to render an object
glUseProgram(shaderProgram);

View File

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

Binary file not shown.

View File

@ -1,8 +1,8 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef MAIN_CPP
#define MAIN_CPP
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <iostream>
#include <random>
@ -11,16 +11,15 @@
// 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 "./constants.hpp"
#include "./misc.hpp"
#include "./renderLoop.hpp"
int main() {
// changed in glfwWindowShouldClose
GLFWwindow *window = prepareForRender();
if (window == nullptr)
return -1;
if (window == nullptr) return -1;
renderLoop(window);
// clean GLFW resources

View File

@ -1,10 +1,9 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef MISC_CPP
#define MISC_CPP
#include <iostream>
#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

View File

@ -1,14 +1,17 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef RENDER_LOOP_CPP
#define RENDER_LOOP_CPP
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "./renderLoop.hpp"
#include "./draw.hpp"
#include "./shaders.hpp"
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <iostream>
#include "./constants.hpp"
#include "./draw.hpp"
#include "./misc.hpp"
#include "./shaders.hpp"
int processInput(GLFWwindow *window, const int whatToDraw) {
static bool lockedLeft = false;
@ -25,10 +28,8 @@ int processInput(GLFWwindow *window, const int whatToDraw) {
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if ( !PRESSED_CHANGE_LEFT )
lockedLeft = 0;
if ( !PRESSED_CHANGE_RIGHT )
lockedRight = 0;
if (!PRESSED_CHANGE_LEFT) lockedLeft = 0;
if (!PRESSED_CHANGE_RIGHT) lockedRight = 0;
if (PRESSED_CHANGE_RIGHT && lockedRight == 0) {
lockedRight = 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
unsigned int copyVerticesMemory(
const float vertices[],
unsigned int copyVerticesMemory(const float vertices[],
const size_t sizeOfVertices,
const GLenum boundBufferTarget) {
// stores vertices in gpu memory
@ -56,16 +56,15 @@ unsigned int copyVerticesMemory(
// 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.
GL_STATIC_DRAW: the data is set only once and used many times.
GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few
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.
*/
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
return vertexBufferObject;
}
unsigned int copyVerticesMemory(
const unsigned int vertices[],
unsigned int copyVerticesMemory(const unsigned int vertices[],
const size_t sizeOfVertices,
const GLenum boundBufferTarget) {
// stores vertices in gpu memory
@ -74,58 +73,53 @@ unsigned int copyVerticesMemory(
// 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);
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.
GL_STATIC_DRAW: the data is set only once and used many times.
GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few
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.
*/
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
return vertexBufferObject;
}
void configureVertexAttribute(
const bool colorIncluded,
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))
size of vertex attribute (we use vec3 so it contains 3 values)
type of data of which vec consists of
should data be normalized, (useful when we use integer data)
space between vertex attributes, each position data is 3 times the size of float
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 */
which vertex attribute we configure (from shader source code layout (location
= 0)) size of vertex attribute (we use vec3 so it contains 3 values) type of
data of which vec consists of should data be normalized, (useful when we use
integer data) space between vertex attributes, each position data is 3 times
the size of float 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), reinterpret_cast<void *>(0));
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float),
reinterpret_cast<void *>(0));
// enable vertex attribute
glEnableVertexAttribArray(0);
return;
}
/* 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 */
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), reinterpret_cast<void *>(0));
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), reinterpret_cast<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;
@ -136,21 +130,18 @@ void configureVertexAttribute(
// 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));
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), reinterpret_cast<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), reinterpret_cast<void*>(6 * sizeof(float)));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
reinterpret_cast<void *>(6 * sizeof(float)));
glEnableVertexAttribArray(2);
return;
}
@ -166,10 +157,8 @@ unsigned int generateBindVAO() {
return vertexArrayObject;
}
void copyVerticesArray(
const unsigned int vertexBufferObject,
const float vertices[],
const size_t sizeOfVertices,
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);
@ -181,8 +170,7 @@ int renderLoopInside(GLFWwindow *window, int whatToDraw) {
whatToDraw = processInput(window, whatToDraw);
// We specify the color to clear the screen with
// RGB and alpha value
glClearColor(
constants::LEARN_OPEN_GL_COLOR.red,
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);
@ -190,7 +178,6 @@ int renderLoopInside(GLFWwindow *window, int whatToDraw) {
// 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) {
print("error with drawing!");
@ -214,7 +201,6 @@ int renderLoopInside(GLFWwindow *window, int whatToDraw) {
glDeleteBuffers(1, &successAndVao.VBO);
glDeleteBuffers(1, &successAndVao.EBO);
return whatToDraw;
}

View File

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

View File

@ -2,13 +2,14 @@
#ifndef SHADER_CPP
#define SHADER_CPP
#include "./shader.hpp"
#include <glad/glad.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include "./shader.hpp"
#include <sstream>
#include <string>
const std::string Shader::fileToShader(const std::string shaderPath) {
// 1. retrieve the vertex/fragment source code from filePath
@ -26,20 +27,16 @@ const std::string Shader::fileToShader(const std::string shaderPath) {
shaderFile.close();
// convert stream into string
shaderCode = shaderStream.str();
}
catch (std::ifstream::failure& e) {
std::cout <<
"ERROR::SHADER::FILE_NOT_SUCCESSFULLY_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) {
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();
@ -67,14 +64,10 @@ Shader::Shader(const std::string vertexPath, const std::string fragmentPath) {
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()),
reinterpret_cast<int>(value));
glUniform1i(glGetUniformLocation(ID, name.c_str()), int(value));
}
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);
if (!success) {
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout <<
"ERROR::SHADER_COMPILATION_ERROR of type: "
<< type << "\n" << infoLog <<
"\n -- --------------------------------------------------- -- "
std::cout
<< "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n"
<< infoLog
<< "\n -- --------------------------------------------------- -- "
<< std::endl;
}
} else {
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
std::cout <<
"ERROR::PROGRAM_LINKING_ERROR of type: "
<< type << "\n" << infoLog <<
"\n -- --------------------------------------------------- -- "
std::cout
<< "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n"
<< infoLog
<< "\n -- --------------------------------------------------- -- "
<< std::endl;
}
}

View File

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

View File

@ -1,14 +1,15 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef SHADERS_CPP
#define SHADERS_CPP
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "./shaders.hpp"
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include "./constants.hpp"
#include "./misc.hpp"
unsigned int linkShaderObjectsShaderProgram(
const unsigned int vertexShaders,
unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders,
const unsigned int fragmentShader) {
// link shader objects into shader program
// will store shader program id
@ -21,8 +22,7 @@ unsigned int linkShaderObjectsShaderProgram(
// link shaders
glLinkProgram(shaderProgram);
if (!shaderSuccessful(shaderProgram, false))
return 0;
if (!shaderSuccessful(shaderProgram, false)) return 0;
// activate program
// 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)
glDeleteShader(vertexShaders);
glDeleteShader(fragmentShader);
if (shaderProgram == 0)
print("Shader Program Linking Failed");
if (shaderProgram == 0) print("Shader Program Linking Failed");
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);
@ -50,14 +47,12 @@ unsigned int compileShader(
glShaderSource(shaderID, 1, &shaderSource, nullptr);
// compile shader
glCompileShader(shaderID);
if (!shaderSuccessful(shaderID, true))
return 0;
if (!shaderSuccessful(shaderID, true)) return 0;
return shaderID;
}
std::pair<unsigned int, unsigned int> compileShaders(
const char* vertexShaderSource,
const char* fragmentShaderSource) {
const char* vertexShaderSource, const char* fragmentShaderSource) {
const unsigned int vertexShader =
compileShader(GL_VERTEX_SHADER, vertexShaderSource);

View File

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

View File

@ -1,120 +1,141 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef TEXTURES_CPP
#define TEXTURES_CPP
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "stb_image.h"
#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
// first argument is texture target (by default we work with 2D textures so it will be GL_TEXTURE_2D)
// second argument is what option we want to set and for which texture axis (s, t or r)
// Third argument is texture wrapping mode
// first argument is texture target (by default we work with 2D textures so it
// will be GL_TEXTURE_2D) second argument is what option we want to set and
// for which texture axis (s, t or r) Third argument is texture wrapping mode
glTexParameteri(textureTarget, GL_TEXTURE_WRAP_S, sCoordinateOption);
if(sCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL)
{
// if we use GL_CLAMP_TO_BORDER all of the space that is not occupied by texture will be occupied by color
// we pass the color we want to use in float array (RGB + transparency)
if (sCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) {
// if we use GL_CLAMP_TO_BORDER all of the space that is not occupied by
// texture will be occupied by color we pass the color we want to use in
// float array (RGB + transparency)
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
}
}
void setTextureTCoordinate(const GLenum textureTarget, const GLint tCoordinateOption, const float* borderColor)
{
// for comments concerning glTexParameteri and glTexParameterfv go to setTextureSCoordinate function
if(textureTarget == GL_TEXTURE_2D || textureTarget == GL_TEXTURE_3D)
{
void setTextureTCoordinate(const GLenum textureTarget,
const GLint tCoordinateOption,
const float* borderColor) {
// 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);
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);
}
}
}
void setTextureRCoordinate(const GLenum textureTarget, const GLint rCoordinateOption, const float* borderColor)
{
// for comments concerning glTexParameteri and glTexParameterfv go to setTextureSCoordinate function
if(textureTarget == GL_TEXTURE_3D)
{
void setTextureRCoordinate(const GLenum textureTarget,
const GLint rCoordinateOption,
const float* borderColor) {
// for comments concerning glTexParameteri and glTexParameterfv go to
// setTextureSCoordinate function
if (textureTarget == GL_TEXTURE_3D) {
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);
}
}
}
// Coordinate options can be: GL_REPEAT, GL_MIRRORED_REPEAT, GL_CLAMP_TO_EDGE, 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)
{
// Coordinate options can be: GL_REPEAT, GL_MIRRORED_REPEAT, GL_CLAMP_TO_EDGE,
// 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) {
setTextureSCoordinate(textureTarget, sCoordinateOption, borderColor);
setTextureTCoordinate(textureTarget, tCoordinateOption, borderColor);
setTextureRCoordinate(textureTarget, rCoordinateOption, borderColor);
}
void setTextureFilteringAndMipMap(const GLenum textureTarget, const GLenum filterType, 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
void setTextureFilteringAndMipMap(const GLenum textureTarget,
const GLenum filterType,
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
// LINEAR_MIPMAP - interpolate between two mipmaps that match the size of a pixel
// NEAREST (at the end) - samples texture based on nearest neighbor interpolation
// LINEAR (at the end) - samples texture using linear interpolation
// setting anything else than GL_TEXTURE_MIN_FILTER as a second parameter results in GL_INVALID_ENUM error
// LINEAR_MIPMAP - interpolate between two mipmaps that match the size of a
// pixel NEAREST (at the end) - samples texture based on nearest neighbor
// interpolation LINEAR (at the end) - samples texture using linear
// 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);
// 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)
// second argument is specifying whether this filter works for magnifying textures GL_TEXTURE_MAG_FILTER or minifying 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
// first argument is texture target (by default we work with 2D textures so it
// will be GL_TEXTURE_2D) second argument is specifying whether this filter
// works for magnifying textures GL_TEXTURE_MAG_FILTER or minifying
// 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);
}
unsigned int generateAndBindTexture(const GLenum textureTarget, const GLsizei numberOfTextures ) {
unsigned int generateAndBindTexture(const GLenum textureTarget,
const GLsizei numberOfTextures) {
unsigned int texture;
// 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);
// then we bind texture
glBindTexture(textureTarget, 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;
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) {
std::cout << "textureWidth " << textureWidth << " textureHeight " << textureHeight << " colorChannels " << colorChannels << std::endl;
std::cout << "textureWidth " << textureWidth << " textureHeight "
<< textureHeight << " colorChannels " << colorChannels
<< std::endl;
// generate texture
/*
first argument is texture target, setting it to GL_TEXTURE_2D will only affect 2D targets and not 1D or 3D
2nd is mipmap level for which to create texture, base level is 0
3rd is in what format we want to store texture (our image has only rgb values so rgb)
4th and 5th are the width and height of texture
6th argument is always 0 (legacy, its called border parameter)
7th and 8th are the format and datatype of source image, we store the image data as chars (bytes) so we pass that
9th last argument is actual data of the texture
first argument is texture target, setting it to GL_TEXTURE_2D will only
affect 2D targets and not 1D or 3D 2nd is mipmap level for which to
create texture, base level is 0 3rd is in what format we want to store
texture (our image has only rgb values so rgb) 4th and 5th are the width
and height of texture 6th argument is always 0 (legacy, its called border
parameter) 7th and 8th are the format and datatype of source image, we
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);
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);
// after generating texture, lets free the memory from image
stbi_image_free(data);
return texture;
}
else
{
} else {
std::cout << "Failed to load texture! " << std::endl;
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) {
// load and create a texture
// -------------------------
@ -129,8 +150,7 @@ unsigned int loadAndCreateTexture(const char* texturePath, bool flipImage) {
return texture;
}
void activateAndBindTextures(unsigned int texture, GLenum textureNumber)
{
void activateAndBindTextures(unsigned int texture, GLenum textureNumber) {
glActiveTexture(textureNumber);
glBindTexture(GL_TEXTURE_2D, texture);
}

View File

@ -1,20 +1,39 @@
// "Copyright [2023] <Krzysztof Rudnicki>"
#ifndef TEXTURES_HPP
#define TEXTURES_HPP
#include <iostream>
#include <glad/glad.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);
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);
#include <iostream>
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);
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);
// 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);
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