mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-11-26 18:07:20 +01:00
chore: ran clang-formatter on root folder
This commit is contained in:
parent
a2adca27e5
commit
d79b634e84
22
.vscode/extensions.json
vendored
22
.vscode/extensions.json
vendored
@ -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"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
1
Engine/engine/.clang-format
Normal file
1
Engine/engine/.clang-format
Normal file
@ -0,0 +1 @@
|
|||||||
|
BasedOnStyle: Google
|
||||||
@ -1,101 +1,95 @@
|
|||||||
// "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"
|
||||||
|
|
||||||
void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion) {
|
void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion) {
|
||||||
// first argument tells us what option to configure
|
// first argument tells us what option to configure
|
||||||
// second is to what we set the value of this option
|
// second is to what we set the value of this option
|
||||||
// see: https://www.glfw.org/docs/latest/window.html#window_hints
|
// see: https://www.glfw.org/docs/latest/window.html#window_hints
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, GLFWMajorVersion);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, GLFWMajorVersion);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, GLFWMinorVersion);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, GLFWMinorVersion);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
// we set GLFW to 3.3 CORE
|
// we set GLFW to 3.3 CORE
|
||||||
// core profile gives us access to smaller subset of
|
// core profile gives us access to smaller subset of
|
||||||
// OGL without backwards compatible features
|
// OGL without backwards compatible features
|
||||||
|
|
||||||
// if we are on Mac OS X we need this for our code to work
|
// if we are on Mac OS X we need this for our code to work
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void instantiateGLFWwindow() {
|
void instantiateGLFWwindow() {
|
||||||
// Initialize GLFW
|
// Initialize GLFW
|
||||||
glfwInit();
|
glfwInit();
|
||||||
configureGLFW(constants::GLFW_MAJOR_VERSION, constants::GLFW_MINOR_VERSION);
|
configureGLFW(constants::GLFW_MAJOR_VERSION, constants::GLFW_MINOR_VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
GLFWwindow *createWindowObject() {
|
GLFWwindow *createWindowObject() {
|
||||||
// First two arguments are width and height
|
// First two arguments are width and height
|
||||||
// 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,
|
return window;
|
||||||
nullptr,
|
|
||||||
nullptr);
|
|
||||||
return window;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int initializeGLAD() {
|
int initializeGLAD() {
|
||||||
// we load address of OGL OS-specific function pointers
|
// we load address of OGL OS-specific function pointers
|
||||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||||
print("Failed to initialize GLAD");
|
print("Failed to initialize GLAD");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void viewPort(GLFWwindow *window) {
|
void viewPort(GLFWwindow *window) {
|
||||||
// We tell OGL size of rendering window
|
// We tell OGL size of rendering window
|
||||||
// First two define left corner of window
|
// First two define left corner of 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,
|
// processed coordinates are between -1 and 1 so here we map:
|
||||||
constants::MAIN_WINDOW_WIDTH,
|
// (-1 to 1) to (0, constants::MAIN_WINDOW_WIDTH)
|
||||||
constants::MAIN_WINDOW_HEIGHT);
|
// and (0, constants::MAIN_WINDOW_HEIGHT)
|
||||||
// processed coordinates are between -1 and 1 so here we map:
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||||
// (-1 to 1) to (0, constants::MAIN_WINDOW_WIDTH)
|
// we call framebuffer_size_callback on every window resize
|
||||||
// and (0, constants::MAIN_WINDOW_HEIGHT)
|
|
||||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
|
||||||
// we call framebuffer_size_callback on every window resize
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GLFWwindow *prepareForRender() {
|
GLFWwindow *prepareForRender() {
|
||||||
instantiateGLFWwindow();
|
instantiateGLFWwindow();
|
||||||
|
|
||||||
GLFWwindow *window = createWindowObject();
|
GLFWwindow *window = createWindowObject();
|
||||||
// function returns GLFWWindow object
|
// function returns GLFWWindow object
|
||||||
|
|
||||||
if (window == nullptr) {
|
if (window == nullptr) {
|
||||||
print("Failed to create GLFW window");
|
print("Failed to create GLFW window");
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return window;
|
|
||||||
}
|
|
||||||
// we make context of this window main context of current thread
|
|
||||||
glfwMakeContextCurrent(window);
|
|
||||||
|
|
||||||
if (initializeGLAD() == -1)
|
|
||||||
return nullptr;
|
|
||||||
viewPort(window);
|
|
||||||
return window;
|
return window;
|
||||||
|
}
|
||||||
|
// we make context of this window main context of current thread
|
||||||
|
glfwMakeContextCurrent(window);
|
||||||
|
|
||||||
|
if (initializeGLAD() == -1) return nullptr;
|
||||||
|
viewPort(window);
|
||||||
|
return window;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -7,10 +7,8 @@ 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 height);
|
||||||
const int width,
|
|
||||||
const int height);
|
|
||||||
void viewPort(GLFWwindow *window);
|
void viewPort(GLFWwindow *window);
|
||||||
GLFWwindow *prepareForRender();
|
GLFWwindow *prepareForRender();
|
||||||
|
|
||||||
|
|||||||
@ -2,217 +2,195 @@
|
|||||||
#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};
|
||||||
// best practice is to use inline constexpr
|
// best practice is to use inline constexpr
|
||||||
// std::string_view but glfwCreateWindow takes only char* as input
|
// std::string_view but glfwCreateWindow takes only char* as input
|
||||||
inline const char* MAIN_WINDOW_NAME { "Match" };
|
inline const char* MAIN_WINDOW_NAME{"Match"};
|
||||||
inline constexpr int MAIN_WINDOW_WIDTH { 800 };
|
inline constexpr int MAIN_WINDOW_WIDTH{800};
|
||||||
inline constexpr int MAIN_WINDOW_HEIGHT { 600 };
|
inline constexpr int MAIN_WINDOW_HEIGHT{600};
|
||||||
inline constexpr struct {
|
inline constexpr struct {
|
||||||
GLfloat red = 1.0f;
|
GLfloat red = 1.0f;
|
||||||
GLfloat green = 0.0f;
|
GLfloat green = 0.0f;
|
||||||
GLfloat blue = 0.0f;
|
GLfloat blue = 0.0f;
|
||||||
GLfloat alpha = 1.0f;
|
GLfloat alpha = 1.0f;
|
||||||
} RED;
|
} RED;
|
||||||
|
|
||||||
inline constexpr struct {
|
inline constexpr struct {
|
||||||
GLfloat red = 1.0f;
|
GLfloat red = 1.0f;
|
||||||
GLfloat green = 1.0f;
|
GLfloat green = 1.0f;
|
||||||
GLfloat blue = 1.0f;
|
GLfloat blue = 1.0f;
|
||||||
GLfloat alpha = 1.0f;
|
GLfloat alpha = 1.0f;
|
||||||
} WHITE;
|
} WHITE;
|
||||||
|
|
||||||
inline constexpr struct {
|
inline constexpr struct {
|
||||||
GLfloat red = 0.2f;
|
GLfloat red = 0.2f;
|
||||||
GLfloat green = 0.3f;
|
GLfloat green = 0.3f;
|
||||||
GLfloat blue = 0.3f;
|
GLfloat blue = 0.3f;
|
||||||
GLfloat alpha = 1.0f;
|
GLfloat alpha = 1.0f;
|
||||||
} 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{
|
||||||
inline const char *VERTEX_SHADER_SOURCE { "#version 330 core\n"
|
"#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
|
||||||
// DO NOT REMOVE UNTIL CAN DRAW SQUARE USING CLASS
|
// DO NOT REMOVE UNTIL CAN DRAW SQUARE USING CLASS
|
||||||
inline const char *FRAGMENT_SHADER_SOURCE {
|
inline const char* FRAGMENT_SHADER_SOURCE{
|
||||||
"#version 330 core\n"
|
"#version 330 core\n"
|
||||||
"out vec4 FragColor;\n"
|
"out vec4 FragColor;\n"
|
||||||
"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
|
|
||||||
-0.9f, -0.5f, 0.0f, // left
|
|
||||||
-0.0f, -0.5f, 0.0f, // right
|
|
||||||
-0.45f, 0.5f, 0.0f, // top
|
|
||||||
// second triangle
|
|
||||||
0.0f, -0.5f, 0.0f, // left
|
|
||||||
0.9f, -0.5f, 0.0f, // right
|
|
||||||
0.45f, 0.5f, 0.0f // top
|
|
||||||
};
|
|
||||||
|
|
||||||
inline constexpr size_t TRIANGLES_VERTICES_SIZE = {
|
|
||||||
sizeof(TRIANGLES_VERTICES) };
|
|
||||||
|
|
||||||
inline constexpr float TRIANGLE_ONE[] {
|
|
||||||
// first triangle
|
|
||||||
-0.9f, -0.5f, 0.0f, // left
|
|
||||||
-0.0f, -0.5f, 0.0f, // right
|
|
||||||
-0.45f, 0.5f, 0.0f, // top
|
|
||||||
};
|
|
||||||
|
|
||||||
inline constexpr size_t TRIANGLE_ONE_SIZE = { sizeof(TRIANGLE_ONE) };
|
|
||||||
|
|
||||||
inline constexpr float TRIANGLE_TWO[] {
|
|
||||||
// second triangle
|
|
||||||
0.0f, -0.5f, 0.0f, // left
|
|
||||||
0.9f, -0.5f, 0.0f, // right
|
|
||||||
0.45f, 0.5f, 0.0f // top
|
|
||||||
};
|
|
||||||
|
|
||||||
inline constexpr size_t TRIANGLE_TWO_SIZE = { sizeof(TRIANGLE_TWO) };
|
|
||||||
|
|
||||||
inline constexpr float TRIANGLE_COLORS[] {
|
|
||||||
// positions // colors
|
|
||||||
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
|
|
||||||
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
|
|
||||||
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top
|
|
||||||
};
|
|
||||||
|
|
||||||
inline constexpr size_t TRIANGLE_COLORS_SIZE = { sizeof(TRIANGLE_COLORS) };
|
|
||||||
|
|
||||||
// compare with square done with only vertices:
|
|
||||||
/*
|
|
||||||
float vertices[] = {
|
|
||||||
// first triangle
|
// first triangle
|
||||||
0.5f, 0.5f, 0.0f, // top right
|
-0.9f, -0.5f, 0.0f, // left
|
||||||
0.5f, -0.5f, 0.0f, // bottom right
|
-0.0f, -0.5f, 0.0f, // right
|
||||||
-0.5f, 0.5f, 0.0f, // top left
|
-0.45f, 0.5f, 0.0f, // top
|
||||||
|
// second triangle
|
||||||
|
0.0f, -0.5f, 0.0f, // left
|
||||||
|
0.9f, -0.5f, 0.0f, // right
|
||||||
|
0.45f, 0.5f, 0.0f // top
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr size_t TRIANGLES_VERTICES_SIZE = {sizeof(TRIANGLES_VERTICES)};
|
||||||
|
|
||||||
|
inline constexpr float TRIANGLE_ONE[]{
|
||||||
|
// first triangle
|
||||||
|
-0.9f, -0.5f, 0.0f, // left
|
||||||
|
-0.0f, -0.5f, 0.0f, // right
|
||||||
|
-0.45f, 0.5f, 0.0f, // top
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr size_t TRIANGLE_ONE_SIZE = {sizeof(TRIANGLE_ONE)};
|
||||||
|
|
||||||
|
inline constexpr float TRIANGLE_TWO[]{
|
||||||
// second triangle
|
// second triangle
|
||||||
0.5f, -0.5f, 0.0f, // bottom right
|
0.0f, -0.5f, 0.0f, // left
|
||||||
-0.5f, -0.5f, 0.0f, // bottom left
|
0.9f, -0.5f, 0.0f, // right
|
||||||
-0.5f, 0.5f, 0.0f // top left
|
0.45f, 0.5f, 0.0f // top
|
||||||
};
|
};
|
||||||
bottom right and top left is specified twice !
|
|
||||||
*/
|
|
||||||
inline constexpr float SQUARE_VERTICES[] {
|
|
||||||
0.5f, 0.5f, 0.0f, // top right
|
|
||||||
0.5f, -0.5f, 0.0f, // bottom right
|
|
||||||
-0.25f, -0.5f, 0.0f, // bottom left
|
|
||||||
-0.25f, 0.5f, 0.0f // top left
|
|
||||||
};
|
|
||||||
|
|
||||||
inline constexpr unsigned int SQUARE_INDICES[] {
|
inline constexpr size_t TRIANGLE_TWO_SIZE = {sizeof(TRIANGLE_TWO)};
|
||||||
0, 1, 3, // first triangle
|
|
||||||
1, 2, 3, // second triangle
|
|
||||||
};
|
|
||||||
|
|
||||||
inline constexpr size_t SQUARE_INDICES_SIZE = { sizeof(SQUARE_INDICES) };
|
inline constexpr float TRIANGLE_COLORS[]{
|
||||||
|
// positions // colors
|
||||||
|
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
|
||||||
|
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
|
||||||
|
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top
|
||||||
|
};
|
||||||
|
|
||||||
inline constexpr size_t SQUARE_VERTICES_SIZE = { sizeof(SQUARE_VERTICES) };
|
inline constexpr size_t TRIANGLE_COLORS_SIZE = {sizeof(TRIANGLE_COLORS)};
|
||||||
|
|
||||||
inline constexpr int MAX_DRAW_CALL = { 11 };
|
// compare with square done with only vertices:
|
||||||
|
/*
|
||||||
|
float vertices[] = {
|
||||||
|
// first triangle
|
||||||
|
0.5f, 0.5f, 0.0f, // top right
|
||||||
|
0.5f, -0.5f, 0.0f, // bottom right
|
||||||
|
-0.5f, 0.5f, 0.0f, // top left
|
||||||
|
// second triangle
|
||||||
|
0.5f, -0.5f, 0.0f, // bottom right
|
||||||
|
-0.5f, -0.5f, 0.0f, // bottom left
|
||||||
|
-0.5f, 0.5f, 0.0f // top left
|
||||||
|
};
|
||||||
|
bottom right and top left is specified twice !
|
||||||
|
*/
|
||||||
|
inline constexpr float SQUARE_VERTICES[]{
|
||||||
|
0.5f, 0.5f, 0.0f, // top right
|
||||||
|
0.5f, -0.5f, 0.0f, // bottom right
|
||||||
|
-0.25f, -0.5f, 0.0f, // bottom left
|
||||||
|
-0.25f, 0.5f, 0.0f // top left
|
||||||
|
};
|
||||||
|
|
||||||
// https://learnopengl.com/img/getting-started/tex_coords.png
|
inline constexpr unsigned int SQUARE_INDICES[]{
|
||||||
inline constexpr float TEXTURE_VERTICES[] {
|
0, 1, 3, // first triangle
|
||||||
// positions // colors // texture coords
|
1, 2, 3, // second triangle
|
||||||
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
|
};
|
||||||
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
|
|
||||||
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
|
|
||||||
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
|
|
||||||
};
|
|
||||||
|
|
||||||
inline constexpr size_t TEXTURE_VERTICES_SIZE = {
|
inline constexpr size_t SQUARE_INDICES_SIZE = {sizeof(SQUARE_INDICES)};
|
||||||
sizeof(TEXTURE_VERTICES)
|
|
||||||
};
|
|
||||||
|
|
||||||
|
inline constexpr size_t SQUARE_VERTICES_SIZE = {sizeof(SQUARE_VERTICES)};
|
||||||
|
|
||||||
inline constexpr unsigned int TEXTURE_INDICES[] {
|
inline constexpr int MAX_DRAW_CALL = {11};
|
||||||
0, 1, 3, // first triangle
|
|
||||||
1, 2, 3 // second triangle
|
|
||||||
};
|
|
||||||
|
|
||||||
inline constexpr size_t TEXTURE_INDICES_SIZE = { sizeof(TEXTURE_INDICES) };
|
// https://learnopengl.com/img/getting-started/tex_coords.png
|
||||||
|
inline constexpr float TEXTURE_VERTICES[]{
|
||||||
|
// positions // colors // texture coords
|
||||||
|
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
|
||||||
|
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
|
||||||
|
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
|
||||||
|
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr size_t TEXTURE_VERTICES_SIZE = {sizeof(TEXTURE_VERTICES)};
|
||||||
|
|
||||||
|
inline constexpr unsigned int TEXTURE_INDICES[]{
|
||||||
|
0, 1, 3, // first triangle
|
||||||
|
1, 2, 3 // second triangle
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr size_t TEXTURE_INDICES_SIZE = {sizeof(TEXTURE_INDICES)};
|
||||||
} // namespace constants
|
} // namespace constants
|
||||||
|
|
||||||
#endif // ENGINE_ENGINE_CONSTANTS_HPP_
|
#endif // ENGINE_ENGINE_CONSTANTS_HPP_
|
||||||
|
|||||||
@ -1,332 +1,281 @@
|
|||||||
// "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::TRIANGLE_VERTICES,
|
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
|
||||||
constants::TRIANGLE_VERTICES_SIZE,
|
constants::TRIANGLE_VERTICES,
|
||||||
constants::VERTEX_SHADER_SOURCE_FILENAME,
|
constants::TRIANGLE_VERTICES_SIZE);
|
||||||
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::FRAGMENT_SHADER_SOURCE);
|
||||||
constants::VERTEX_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::TRIANGLES_VERTICES,
|
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
|
||||||
constants::TRIANGLES_VERTICES_SIZE,
|
constants::TRIANGLES_VERTICES,
|
||||||
constants::VERTEX_SHADER_SOURCE_FILENAME,
|
constants::TRIANGLES_VERTICES_SIZE);
|
||||||
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(constants::VERTEX_SHADER_SOURCE_FILENAME,
|
||||||
return drawTriangleClass(
|
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::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE);
|
||||||
|
/*
|
||||||
|
Create two shader programs where
|
||||||
|
the second program uses a different
|
||||||
|
fragment shader that outputs the color yellow;
|
||||||
|
draw both triangles again where one outputs the color yellow
|
||||||
|
*/
|
||||||
|
return drawTriangleClass(
|
||||||
constants::VERTEX_SHADER_SOURCE_FILENAME,
|
constants::VERTEX_SHADER_SOURCE_FILENAME,
|
||||||
constants::FRAGMENT_SHADER_SOURCE_FILENAME);
|
constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME,
|
||||||
/*
|
constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE);
|
||||||
Create two shader programs where
|
|
||||||
the second program uses a different
|
|
||||||
fragment shader that outputs the color yellow;
|
|
||||||
draw both triangles again where one outputs the color yellow
|
|
||||||
*/
|
|
||||||
return drawTriangleClass(
|
|
||||||
constants::TRIANGLE_TWO,
|
|
||||||
constants::TRIANGLE_TWO_SIZE,
|
|
||||||
constants::VERTEX_SHADER_SOURCE_FILENAME,
|
|
||||||
constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME);
|
|
||||||
case 5:
|
case 5:
|
||||||
// Get color from vertex shader to fragment shader
|
// Get color from vertex shader to fragment shader
|
||||||
return drawTriangleClass(constants::TRIANGLE_VERTICES,
|
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,
|
constants::TRIANGLE_COLORS, constants::TRIANGLE_COLORS_SIZE, true);
|
||||||
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::VERTEX_SHADER_OFFSET_FILENAME,
|
||||||
return drawTriangleClass(constants::TRIANGLE_VERTICES,
|
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
|
||||||
constants::TRIANGLE_VERTICES_SIZE,
|
constants::TRIANGLE_VERTICES,
|
||||||
constants::VERTEX_SHADER_OFFSET_FILENAME,
|
constants::TRIANGLE_VERTICES_SIZE, false, false,
|
||||||
constants::FRAGMENT_SHADER_SOURCE_FILENAME,
|
offsets);
|
||||||
false,
|
|
||||||
false,
|
|
||||||
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) return 0;
|
||||||
if (shaders.first == 0 || shaders.second == 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOffsets(Shader shader, const offsetsStruct offsets) {
|
void setOffsets(Shader shader, const offsetsStruct offsets) {
|
||||||
shader.setFloat("xOffset", offsets.xOffset);
|
shader.setFloat("xOffset", offsets.xOffset);
|
||||||
shader.setFloat("yOffset", offsets.yOffset);
|
shader.setFloat("yOffset", offsets.yOffset);
|
||||||
shader.setFloat("zOffset", offsets.zOffset);
|
shader.setFloat("zOffset", offsets.zOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
drawFigureReturn drawTriangleClass(
|
drawFigureReturn drawTriangleClass(const char* vertexPath,
|
||||||
const float triangleVertices[],
|
const char* fragmentPath,
|
||||||
const size_t triangleVerticesSize,
|
const float triangleVertices[],
|
||||||
const char* vertexPath,
|
const size_t triangleVerticesSize,
|
||||||
const char* fragmentPath,
|
const bool colorIncluded,
|
||||||
const bool colorIncluded,
|
const bool textureIncluded,
|
||||||
const bool textureIncluded,
|
const offsetsStruct offsets) {
|
||||||
const offsetsStruct offsets) {
|
// In your CPP file:
|
||||||
// In your CPP file:
|
// ======================
|
||||||
// ======================
|
// float offset = 0.5f;
|
||||||
// float offset = 0.5f;
|
|
||||||
|
|
||||||
Shader ourShader(vertexPath, fragmentPath);
|
Shader ourShader(vertexPath, fragmentPath);
|
||||||
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,
|
const unsigned int vertexArrayObject = generateBindVAO();
|
||||||
triangleVerticesSize,
|
copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize,
|
||||||
GL_ARRAY_BUFFER);
|
GL_ARRAY_BUFFER);
|
||||||
const unsigned int vertexArrayObject = generateBindVAO();
|
// set vertex attribute pointers
|
||||||
copyVerticesArray(
|
configureVertexAttribute(colorIncluded, textureIncluded);
|
||||||
vertexBufferObject,
|
doDrawArrays(ourShader.ID, vertexArrayObject, GL_TRIANGLES, 0,
|
||||||
triangleVertices,
|
triangleVerticesSize);
|
||||||
triangleVerticesSize,
|
drawFigureReturn newReturn;
|
||||||
GL_ARRAY_BUFFER);
|
newReturn.success = 0;
|
||||||
// set vertex attribute pointers
|
newReturn.VAO = vertexArrayObject;
|
||||||
configureVertexAttribute(colorIncluded, textureIncluded);
|
newReturn.VBO = vertexBufferObject;
|
||||||
doDrawArrays(
|
newReturn.EBO = 0;
|
||||||
ourShader.ID,
|
return newReturn;
|
||||||
vertexArrayObject,
|
|
||||||
GL_TRIANGLES,
|
|
||||||
0,
|
|
||||||
triangleVerticesSize);
|
|
||||||
drawFigureReturn newReturn;
|
|
||||||
newReturn.success = 0;
|
|
||||||
newReturn.VAO = vertexArrayObject;
|
|
||||||
newReturn.VBO = vertexBufferObject;
|
|
||||||
newReturn.EBO = 0;
|
|
||||||
return newReturn;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
drawFigureReturn newReturn = drawFigureReturn();
|
drawFigureReturn newReturn = drawFigureReturn();
|
||||||
if (shaderProgram == 0) {
|
if (shaderProgram == 0) {
|
||||||
newReturn.success = -1;
|
newReturn.success = -1;
|
||||||
return newReturn;
|
|
||||||
}
|
|
||||||
const unsigned int VAO = generateBindVAO();
|
|
||||||
copyVerticesMemory(squareVertices, squareVerticesSize, GL_ARRAY_BUFFER);
|
|
||||||
copyVerticesMemory(
|
|
||||||
squareIndices,
|
|
||||||
squareIndicesSize,
|
|
||||||
GL_ELEMENT_ARRAY_BUFFER);
|
|
||||||
|
|
||||||
// set vertex attribute pointers
|
|
||||||
configureVertexAttribute(colorIncluded, textureIncluded);
|
|
||||||
|
|
||||||
doDrawElements(
|
|
||||||
shaderProgram,
|
|
||||||
VAO,
|
|
||||||
GL_TRIANGLES,
|
|
||||||
GL_UNSIGNED_INT,
|
|
||||||
squareIndicesSize);
|
|
||||||
newReturn.success = 0;
|
|
||||||
newReturn.VAO = VAO;
|
|
||||||
return newReturn;
|
return newReturn;
|
||||||
|
}
|
||||||
|
const unsigned int VAO = generateBindVAO();
|
||||||
|
copyVerticesMemory(squareVertices, squareVerticesSize, GL_ARRAY_BUFFER);
|
||||||
|
copyVerticesMemory(squareIndices, squareIndicesSize, GL_ELEMENT_ARRAY_BUFFER);
|
||||||
|
|
||||||
|
// set vertex attribute pointers
|
||||||
|
configureVertexAttribute(colorIncluded, textureIncluded);
|
||||||
|
|
||||||
|
doDrawElements(shaderProgram, VAO, GL_TRIANGLES, GL_UNSIGNED_INT,
|
||||||
|
squareIndicesSize);
|
||||||
|
newReturn.success = 0;
|
||||||
|
newReturn.VAO = VAO;
|
||||||
|
return newReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
drawFigureReturn drawDebilMode(
|
drawFigureReturn drawDebilMode(const char* vertexPath, const char* fragmentPath,
|
||||||
const char* vertexPath,
|
const float vertices[],
|
||||||
const char* fragmentPath,
|
const size_t verticesSize,
|
||||||
const float vertices[],
|
const unsigned int indices[],
|
||||||
const size_t verticesSize,
|
const size_t indicesSize) {
|
||||||
const unsigned int indices[],
|
// https://stackoverflow.com/questions/33883609/opengl-linker-error-linking-with-uncompiled-shader
|
||||||
const size_t indicesSize) {
|
Shader ourShader(constants::VERTEX_SHADER_TEXTURE_FILENAME,
|
||||||
// https://stackoverflow.com/questions/33883609/opengl-linker-error-linking-with-uncompiled-shader
|
constants::FRAGMENT_SHADER_TEXTURE_FILENAME);
|
||||||
Shader ourShader(
|
|
||||||
constants::VERTEX_SHADER_TEXTURE_FILENAME,
|
|
||||||
constants::FRAGMENT_SHADER_TEXTURE_FILENAME);
|
|
||||||
|
|
||||||
unsigned int VAO = generateBindVAO();
|
unsigned int VAO = generateBindVAO();
|
||||||
unsigned int VBO = copyVerticesMemory(
|
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!
|
||||||
// set it via the texture class
|
// set it via the texture class
|
||||||
ourShader.setInt("texture1", 0);
|
ourShader.setInt("texture1", 0);
|
||||||
ourShader.setInt("texture2", 1);
|
ourShader.setInt("texture2", 1);
|
||||||
|
|
||||||
|
activateAndBindTextures(texture1, GL_TEXTURE0);
|
||||||
|
activateAndBindTextures(texture2, GL_TEXTURE1);
|
||||||
|
|
||||||
activateAndBindTextures(texture1, GL_TEXTURE0);
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||||
activateAndBindTextures(texture2, GL_TEXTURE1);
|
|
||||||
|
|
||||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
drawFigureReturn newReturn;
|
||||||
|
newReturn.success = 0;
|
||||||
drawFigureReturn newReturn;
|
newReturn.VAO = VAO;
|
||||||
newReturn.success = 0;
|
newReturn.VBO = VBO;
|
||||||
newReturn.VAO = VAO;
|
newReturn.EBO = EBO;
|
||||||
newReturn.VBO = VBO;
|
return newReturn;
|
||||||
newReturn.EBO = EBO;
|
|
||||||
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 drawType,
|
||||||
const GLenum drawArrayMode,
|
const int numberOfElementsToDraw) {
|
||||||
const GLenum drawType,
|
glUseProgram(shaderProgram);
|
||||||
const int numberOfElementsToDraw) {
|
glBindVertexArray(vertexArrayObject);
|
||||||
glUseProgram(shaderProgram);
|
glDrawElements(drawArrayMode, numberOfElementsToDraw, drawType, 0);
|
||||||
glBindVertexArray(vertexArrayObject);
|
glBindVertexArray(0);
|
||||||
glDrawElements(drawArrayMode, numberOfElementsToDraw, drawType, 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 = sin(timeValue) / 2.0f + 0.5f;
|
||||||
const float greenValue =
|
// vary the color from 0.0 to 1.0 using sin
|
||||||
sin(timeValue) / 2.0f + 0.5f;
|
// query the location of our uniform
|
||||||
// vary the color from 0.0 to 1.0 using sin
|
const int vertexColorLocation =
|
||||||
// query the location of our uniform
|
glGetUniformLocation(shaderProgram, uniformName);
|
||||||
const int vertexColorLocation =
|
// if glGetUniformLocation returns -1 it could not find the location
|
||||||
glGetUniformLocation(shaderProgram, uniformName);
|
if (vertexColorLocation != -1) {
|
||||||
// if glGetUniformLocation returns -1 it could not find the location
|
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
|
||||||
if (vertexColorLocation != -1) {
|
/* we set the uniform value using glUniform4f
|
||||||
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
|
4f means that it expects 4 floats
|
||||||
/* we set the uniform value using glUniform4f
|
few of possible postfixes:
|
||||||
4f means that it expects 4 floats
|
f: the function expects a float as its value.
|
||||||
few of possible postfixes:
|
i: the function expects an int as its value.
|
||||||
f: the function expects a float as its value.
|
ui: the function expects an unsigned int as its value.
|
||||||
i: the function expects an int as its value.
|
3f: the function expects 3 floats as its value.
|
||||||
ui: the function expects an unsigned int as its value.
|
fv: the function expects a float vector/array as its value.
|
||||||
3f: the function expects 3 floats as its value.
|
*/
|
||||||
fv: the function expects a float vector/array as its value.
|
}
|
||||||
*/
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void doDrawArrays(
|
void doDrawArrays(const unsigned int shaderProgram,
|
||||||
const unsigned int shaderProgram,
|
const unsigned int vertexArrayObject,
|
||||||
const unsigned int vertexArrayObject,
|
const GLenum drawArrayMode, const int firstIndex,
|
||||||
const GLenum drawArrayMode,
|
const unsigned int numberOfIndicesToBeRendered) {
|
||||||
const int firstIndex,
|
// use shader program to render an object
|
||||||
const unsigned int numberOfIndicesToBeRendered) {
|
glUseProgram(shaderProgram);
|
||||||
// use shader program to render an object
|
updateUniformColor(shaderProgram, "ourColor");
|
||||||
glUseProgram(shaderProgram);
|
glBindVertexArray(vertexArrayObject);
|
||||||
updateUniformColor(shaderProgram, "ourColor");
|
// From left:
|
||||||
glBindVertexArray(vertexArrayObject);
|
// primitive type we want to draw
|
||||||
// From left:
|
// starting index of vertex array
|
||||||
// primitive type we want to draw
|
// how many vertices we want to draw
|
||||||
// starting index of vertex array
|
glDrawArrays(drawArrayMode, firstIndex, numberOfIndicesToBeRendered);
|
||||||
// how many vertices we want to draw
|
|
||||||
glDrawArrays(drawArrayMode, firstIndex, numberOfIndicesToBeRendered);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // #ifndef DRAW_CPP
|
#endif // #ifndef DRAW_CPP
|
||||||
|
|||||||
@ -1,22 +1,24 @@
|
|||||||
// "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 {
|
||||||
offsetsStruct(): xOffset(0), yOffset(0), zOffset(0) { }
|
offsetsStruct() : xOffset(0), yOffset(0), zOffset(0) {}
|
||||||
float xOffset, yOffset, zOffset;
|
float xOffset, yOffset, zOffset;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct drawFigureReturn {
|
struct drawFigureReturn {
|
||||||
drawFigureReturn(): success(-1), VAO(0), VBO(0), EBO(0) { }
|
drawFigureReturn() : success(-1), VAO(0), VBO(0), EBO(0) {}
|
||||||
int success;
|
int success;
|
||||||
unsigned int VAO;
|
unsigned int VAO;
|
||||||
unsigned int VBO;
|
unsigned int VBO;
|
||||||
unsigned int EBO;
|
unsigned int EBO;
|
||||||
};
|
};
|
||||||
|
|
||||||
drawFigureReturn drawFigure(const int whatToDraw);
|
drawFigureReturn drawFigure(const int whatToDraw);
|
||||||
@ -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 drawType,
|
||||||
const GLenum drawArrayMode,
|
const int numberOfElementsToDraw);
|
||||||
const GLenum drawType,
|
|
||||||
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 int firstIndex,
|
||||||
const GLenum drawArrayMode,
|
const unsigned int numberOfIndicesToBeRendered);
|
||||||
const int firstIndex,
|
|
||||||
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.
@ -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,21 +11,20 @@
|
|||||||
// 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
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // #ifndef MAIN_CPP
|
#endif // #ifndef MAIN_CPP
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -1,229 +1,215 @@
|
|||||||
// "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;
|
||||||
static bool lockedRight = false;
|
static bool lockedRight = false;
|
||||||
const bool PRESSED_CHANGE_LEFT =
|
const bool PRESSED_CHANGE_LEFT =
|
||||||
(glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS);
|
(glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS);
|
||||||
const bool PRESSED_CHANGE_RIGHT =
|
const bool PRESSED_CHANGE_RIGHT =
|
||||||
(glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS);
|
(glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS);
|
||||||
|
|
||||||
// glfwGetKey takes window and key as
|
// glfwGetKey takes window and key as
|
||||||
// an input and checks is currently being pressed
|
// an input and checks is currently being pressed
|
||||||
// if the user pressed escape we close window
|
// if the user pressed escape we close window
|
||||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
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 )
|
if (PRESSED_CHANGE_RIGHT && lockedRight == 0) {
|
||||||
lockedRight = 0;
|
lockedRight = 1;
|
||||||
if ( PRESSED_CHANGE_RIGHT && lockedRight == 0 ) {
|
return (whatToDraw == constants::MAX_DRAW_CALL ? 0 : whatToDraw + 1);
|
||||||
lockedRight = 1;
|
}
|
||||||
return (whatToDraw == constants::MAX_DRAW_CALL ? 0 : whatToDraw + 1);
|
if (PRESSED_CHANGE_LEFT && lockedLeft == 0) {
|
||||||
}
|
lockedLeft = 1;
|
||||||
if ( PRESSED_CHANGE_LEFT && lockedLeft == 0 ) {
|
return (whatToDraw == 0 ? constants::MAX_DRAW_CALL : whatToDraw - 1);
|
||||||
lockedLeft = 1;
|
}
|
||||||
return (whatToDraw == 0 ? constants::MAX_DRAW_CALL : whatToDraw - 1);
|
return whatToDraw;
|
||||||
}
|
|
||||||
return 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
|
unsigned int vertexBufferObject;
|
||||||
unsigned int vertexBufferObject;
|
// this is open gl object so we refer
|
||||||
// this is open gl object so we refer
|
// it by its ID generated here and stored in vertexBufferObject variable
|
||||||
// it by its ID generated here and stored in vertexBufferObject variable
|
glGenBuffers(1, &vertexBufferObject);
|
||||||
glGenBuffers(1, &vertexBufferObject);
|
// buffer type of vertex buffer object is GL_ARRAY_BUFFER
|
||||||
// buffer type of vertex buffer object is GL_ARRAY_BUFFER
|
glBindBuffer(boundBufferTarget, vertexBufferObject);
|
||||||
glBindBuffer(boundBufferTarget, vertexBufferObject);
|
// now whenever we change GL_ARRAY_BUFFER
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
unsigned int vertexBufferObject;
|
||||||
unsigned int vertexBufferObject;
|
// this is open gl object so we refer it
|
||||||
// this is open gl object so we refer it
|
// 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(boundBufferTarget, vertexBufferObject);
|
||||||
glBindBuffer(
|
// now whenever we change GL_ARRAY_BUFFER
|
||||||
boundBufferTarget,
|
// we change bound buffer vertexBufferObject
|
||||||
vertexBufferObject);
|
|
||||||
// now whenever we change GL_ARRAY_BUFFER
|
|
||||||
// we change bound buffer vertexBufferObject
|
|
||||||
|
|
||||||
/* we copy vertex data into buffer memory
|
/* we copy vertex data into buffer memory
|
||||||
GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few times.
|
GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few
|
||||||
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
|
||||||
which vertex attribute we configure (from shader source code layout (location = 0))
|
= 0)) size of vertex attribute (we use vec3 so it contains 3 values) type of
|
||||||
size of vertex attribute (we use vec3 so it contains 3 values)
|
data of which vec consists of should data be normalized, (useful when we use
|
||||||
type of data of which vec consists of
|
integer data) space between vertex attributes, each position data is 3 times
|
||||||
should data be normalized, (useful when we use integer data)
|
the size of float offset of where position data begins in buffer see:
|
||||||
space between vertex attributes, each position data is 3 times the size of float
|
https://learnopengl.com/img/getting-started/vertex_attribute_pointer.png
|
||||||
offset of where position data begins in buffer
|
vertex attribute data take data from memory managed by VBO bound to
|
||||||
see: https://learnopengl.com/img/getting-started/vertex_attribute_pointer.png
|
GL_ARRAY_BUFFER */
|
||||||
vertex attribute data take data from memory managed by VBO bound to GL_ARRAY_BUFFER */
|
if (!colorIncluded) {
|
||||||
if (!colorIncluded) {
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float),
|
||||||
glVertexAttribPointer(
|
reinterpret_cast<void *>(0));
|
||||||
0, 3, GL_FLOAT, GL_FALSE,
|
// enable vertex attribute
|
||||||
3 * sizeof(float), reinterpret_cast<void *>(0));
|
glEnableVertexAttribArray(0);
|
||||||
// enable vertex attribute
|
return;
|
||||||
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
|
||||||
|
*/
|
||||||
|
if (colorIncluded && !textureIncluded) {
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
|
||||||
|
reinterpret_cast<void *>(0));
|
||||||
|
// enable vertex attribute
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
|
||||||
|
reinterpret_cast<void *>(3 * sizeof(float)));
|
||||||
|
// enable vertex attribute
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* we change attribute location, color values have size of 3 floats
|
// Since we've added an extra vertex
|
||||||
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
|
// attribute we again have to notify OpenGL of the new vertex format
|
||||||
https://learnopengl.com/img/getting-started/vertex_attribute_pointer_interleaved.png */
|
// this time for textures we add just
|
||||||
if (colorIncluded && !textureIncluded) {
|
// 2 more attributes and not 3 as with color
|
||||||
glVertexAttribPointer(
|
if (textureIncluded) {
|
||||||
0, 3, GL_FLOAT, GL_FALSE,
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
||||||
6 * sizeof(float), reinterpret_cast<void *>(0));
|
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)));
|
||||||
6 * sizeof(float), reinterpret_cast<void *>(3* sizeof(float)));
|
// enable vertex attribute
|
||||||
// enable vertex attribute
|
glEnableVertexAttribArray(1);
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Since we've added an extra vertex
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
||||||
// attribute we again have to notify OpenGL of the new vertex format
|
reinterpret_cast<void *>(6 * sizeof(float)));
|
||||||
// this time for textures we add just
|
glEnableVertexAttribArray(2);
|
||||||
// 2 more attributes and not 3 as with color
|
return;
|
||||||
if (textureIncluded) {
|
}
|
||||||
glVertexAttribPointer(
|
|
||||||
0, 3, GL_FLOAT, GL_FALSE,
|
|
||||||
8 * sizeof(float), reinterpret_cast<void *>(0));
|
|
||||||
// enable vertex attribute
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
|
|
||||||
glVertexAttribPointer(
|
|
||||||
1, 3, GL_FLOAT, GL_FALSE,
|
|
||||||
8 * sizeof(float), 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)));
|
|
||||||
glEnableVertexAttribArray(2);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int generateBindVAO() {
|
unsigned int generateBindVAO() {
|
||||||
// vertex array object is used to draw objects by binding them to vao
|
// vertex array object is used to draw objects by binding them to vao
|
||||||
// generate vao
|
// generate vao
|
||||||
unsigned int vertexArrayObject;
|
unsigned int vertexArrayObject;
|
||||||
glGenVertexArrays(1, &vertexArrayObject);
|
glGenVertexArrays(1, &vertexArrayObject);
|
||||||
// bind vao
|
// bind vao
|
||||||
glBindVertexArray(vertexArrayObject);
|
glBindVertexArray(vertexArrayObject);
|
||||||
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 GLenum boundBufferTarget) {
|
||||||
const size_t sizeOfVertices,
|
// copy vertices array in array useful for OGL
|
||||||
const GLenum boundBufferTarget) {
|
glBindBuffer(boundBufferTarget, vertexBufferObject);
|
||||||
// copy vertices array in array useful for OGL
|
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
|
||||||
glBindBuffer(boundBufferTarget, vertexBufferObject);
|
|
||||||
glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int renderLoopInside(GLFWwindow *window, int whatToDraw) {
|
int renderLoopInside(GLFWwindow *window, int whatToDraw) {
|
||||||
// input
|
// input
|
||||||
whatToDraw = processInput(window, whatToDraw);
|
whatToDraw = processInput(window, whatToDraw);
|
||||||
// We specify the color to clear the screen with
|
// We specify the color to clear the screen with
|
||||||
// RGB and alpha value
|
// RGB and alpha value
|
||||||
glClearColor(
|
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);
|
// There is
|
||||||
// There is
|
// 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);
|
||||||
|
if (successAndVao.success == -1) {
|
||||||
|
print("error with drawing!");
|
||||||
|
glfwSetWindowShouldClose(window, true);
|
||||||
|
}
|
||||||
|
|
||||||
drawFigureReturn successAndVao = drawFigure(whatToDraw);
|
// swaps buffer containing color values of each pixel in window
|
||||||
if (successAndVao.success == -1) {
|
// there is front buffer (final image)
|
||||||
print("error with drawing!");
|
// and back buffer (where all rendering commands draw to)
|
||||||
glfwSetWindowShouldClose(window, true);
|
// when back buffer is ready we swap it
|
||||||
}
|
// with front buffer to eliminate flickering
|
||||||
|
glfwSwapBuffers(window);
|
||||||
|
|
||||||
// swaps buffer containing color values of each pixel in window
|
// glfwPollEvents checks if any event
|
||||||
// there is front buffer (final image)
|
// (like mouse/keyboard input was triggered),
|
||||||
// and back buffer (where all rendering commands draw to)
|
// updates window state and calls functions
|
||||||
// when back buffer is ready we swap it
|
// (which we register via callback methods)
|
||||||
// with front buffer to eliminate flickering
|
glfwPollEvents();
|
||||||
glfwSwapBuffers(window);
|
|
||||||
|
|
||||||
// glfwPollEvents checks if any event
|
glDeleteVertexArrays(1, &successAndVao.VAO);
|
||||||
// (like mouse/keyboard input was triggered),
|
glDeleteBuffers(1, &successAndVao.VBO);
|
||||||
// updates window state and calls functions
|
glDeleteBuffers(1, &successAndVao.EBO);
|
||||||
// (which we register via callback methods)
|
|
||||||
glfwPollEvents();
|
|
||||||
|
|
||||||
glDeleteVertexArrays(1, &successAndVao.VAO);
|
return whatToDraw;
|
||||||
glDeleteBuffers(1, &successAndVao.VBO);
|
|
||||||
glDeleteBuffers(1, &successAndVao.EBO);
|
|
||||||
|
|
||||||
|
|
||||||
return whatToDraw;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void renderLoop(GLFWwindow *window) {
|
void renderLoop(GLFWwindow *window) {
|
||||||
int whatToDraw = 0;
|
int whatToDraw = 0;
|
||||||
// glfwWindowShouldClose checks if GLFW was instructed to close
|
// glfwWindowShouldClose checks if GLFW was instructed to close
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
whatToDraw = renderLoopInside(window, whatToDraw);
|
whatToDraw = renderLoopInside(window, whatToDraw);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -2,33 +2,29 @@
|
|||||||
#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 size_t sizeOfVertices,
|
||||||
const float vertices[],
|
const GLenum boundBufferTarget);
|
||||||
const size_t sizeOfVertices,
|
|
||||||
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);
|
|
||||||
|
|
||||||
#endif // ENGINE_ENGINE_RENDERLOOP_HPP_
|
#endif // ENGINE_ENGINE_RENDERLOOP_HPP_
|
||||||
|
|||||||
@ -2,113 +2,106 @@
|
|||||||
#ifndef SHADER_CPP
|
#ifndef SHADER_CPP
|
||||||
#define SHADER_CPP
|
#define SHADER_CPP
|
||||||
|
|
||||||
#include <glad/glad.h>
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <fstream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <iostream>
|
|
||||||
#include "./shader.hpp"
|
#include "./shader.hpp"
|
||||||
|
|
||||||
|
#include <glad/glad.h>
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#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
|
||||||
std::string shaderCode;
|
std::string shaderCode;
|
||||||
std::ifstream shaderFile;
|
std::ifstream shaderFile;
|
||||||
// ensure ifstream objects can throw exceptions:
|
// ensure ifstream objects can throw exceptions:
|
||||||
shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
try {
|
try {
|
||||||
// open files
|
// open files
|
||||||
shaderFile.open(shaderPath);
|
shaderFile.open(shaderPath);
|
||||||
std::stringstream shaderStream;
|
std::stringstream shaderStream;
|
||||||
// read file's buffer contents into streams
|
// read file's buffer contents into streams
|
||||||
shaderStream << shaderFile.rdbuf();
|
shaderStream << shaderFile.rdbuf();
|
||||||
// close file handlers
|
// close file handlers
|
||||||
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 =
|
const char *vShaderCode = vshaderCodeString.c_str();
|
||||||
this -> fileToShader(fragmentPath);
|
const char *fShaderCode = fShaderCodeString.c_str();
|
||||||
|
// 2. compile shaders
|
||||||
const char* vShaderCode = vshaderCodeString.c_str();
|
unsigned int vertex, fragment;
|
||||||
const char * fShaderCode = fShaderCodeString.c_str();
|
// vertex shader
|
||||||
// 2. compile shaders
|
vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||||
unsigned int vertex, fragment;
|
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
||||||
// vertex shader
|
glCompileShader(vertex);
|
||||||
vertex = glCreateShader(GL_VERTEX_SHADER);
|
checkCompileErrors(vertex, "VERTEX");
|
||||||
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
// fragment Shader
|
||||||
glCompileShader(vertex);
|
fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
checkCompileErrors(vertex, "VERTEX");
|
glShaderSource(fragment, 1, &fShaderCode, NULL);
|
||||||
// fragment Shader
|
glCompileShader(fragment);
|
||||||
fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
checkCompileErrors(fragment, "FRAGMENT");
|
||||||
glShaderSource(fragment, 1, &fShaderCode, NULL);
|
// shader Program
|
||||||
glCompileShader(fragment);
|
ID = glCreateProgram();
|
||||||
checkCompileErrors(fragment, "FRAGMENT");
|
glAttachShader(ID, vertex);
|
||||||
// shader Program
|
glAttachShader(ID, fragment);
|
||||||
ID = glCreateProgram();
|
glLinkProgram(ID);
|
||||||
glAttachShader(ID, vertex);
|
checkCompileErrors(ID, "PROGRAM");
|
||||||
glAttachShader(ID, fragment);
|
// delete the shaders as they're linked
|
||||||
glLinkProgram(ID);
|
// into our program now and no longer necessary
|
||||||
checkCompileErrors(ID, "PROGRAM");
|
glDeleteShader(vertex);
|
||||||
// delete the shaders as they're linked
|
glDeleteShader(fragment);
|
||||||
// into our program now and no longer necessary
|
|
||||||
glDeleteShader(vertex);
|
|
||||||
glDeleteShader(fragment);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::use() {
|
void Shader::use() { glUseProgram(ID); }
|
||||||
glUseProgram(ID);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Shader::setBool(const std::string &name, bool value) const {
|
void Shader::setBool(const std::string &name, bool value) const {
|
||||||
glUniform1i(
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), int(value));
|
||||||
glGetUniformLocation(ID, name.c_str()),
|
|
||||||
reinterpret_cast<int>(value));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setInt(const std::string &name, int value) const {
|
void Shader::setInt(const std::string &name, int value) const {
|
||||||
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::setFloat(const std::string &name, float value) const {
|
void Shader::setFloat(const std::string &name, float value) const {
|
||||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::checkCompileErrors(unsigned int shader, std::string type) {
|
void Shader::checkCompileErrors(unsigned int shader, std::string type) {
|
||||||
int success;
|
int success;
|
||||||
char infoLog[1024];
|
char infoLog[1024];
|
||||||
if (type != "PROGRAM") {
|
if (type != "PROGRAM") {
|
||||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
|
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
|
||||||
std::cout <<
|
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 {
|
|
||||||
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::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::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // SHADER_CPP
|
#endif // SHADER_CPP
|
||||||
@ -4,38 +4,36 @@
|
|||||||
|
|
||||||
#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:
|
||||||
unsigned int ID;
|
unsigned int ID;
|
||||||
// constructor generates the shader on the fly
|
// constructor generates the shader on the fly
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
Shader(const std::string vertexPath, const std::string fragmentPath);
|
Shader(const std::string vertexPath, const std::string fragmentPath);
|
||||||
const std::string fileToShader(const std::string shaderPath);
|
const std::string fileToShader(const std::string shaderPath);
|
||||||
// activate the shader
|
// activate the shader
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
void use();
|
void use();
|
||||||
// utility uniform functions
|
// utility uniform functions
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
void setBool(const std::string &name, bool value) const;
|
void setBool(const std::string &name, bool value) const;
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
void setInt(const std::string &name, int value) const;
|
void setInt(const std::string &name, int value) const;
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
void setFloat(const std::string &name, float value) const;
|
void setFloat(const std::string &name, float value) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// utility function for checking shader compilation/linking errors.
|
// utility function for checking shader compilation/linking errors.
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
void checkCompileErrors(unsigned int shader, std::string type);
|
void checkCompileErrors(unsigned int shader, std::string type);
|
||||||
|
|
||||||
void compileErrorsMessage(
|
void compileErrorsMessage(const unsigned int shader, const bool compilation,
|
||||||
const unsigned int shader,
|
const std::string type,
|
||||||
const bool compilation,
|
const std::string errorMessage);
|
||||||
const std::string type,
|
|
||||||
const std::string errorMessage);
|
|
||||||
};
|
};
|
||||||
#endif // ENGINE_ENGINE_SHADER_HPP_
|
#endif // ENGINE_ENGINE_SHADER_HPP_
|
||||||
|
|||||||
@ -1,103 +1,98 @@
|
|||||||
// "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
|
// creates program
|
||||||
// creates program
|
const unsigned int shaderProgram = glCreateProgram();
|
||||||
const unsigned int shaderProgram = glCreateProgram();
|
|
||||||
|
|
||||||
// attachShaders
|
// attachShaders
|
||||||
glAttachShader(shaderProgram, vertexShaders);
|
glAttachShader(shaderProgram, vertexShaders);
|
||||||
glAttachShader(shaderProgram, fragmentShader);
|
glAttachShader(shaderProgram, fragmentShader);
|
||||||
|
|
||||||
// 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
|
||||||
glUseProgram(shaderProgram);
|
glUseProgram(shaderProgram);
|
||||||
|
|
||||||
// delete shaders
|
// delete shaders
|
||||||
// (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,
|
// we create vertex shader and assign its id to shader variable
|
||||||
const char *shaderSource) {
|
const unsigned int shaderID = glCreateShader(shaderType);
|
||||||
// we create vertex shader and assign its id to shader variable
|
|
||||||
const unsigned int shaderID = glCreateShader(shaderType);
|
|
||||||
|
|
||||||
// attach shader source code to shader object
|
// attach shader source code to shader object
|
||||||
// from left: shader object to compile,
|
// from left: shader object to compile,
|
||||||
// how many strings as source code, actual source code
|
// how many strings as source code, actual source code
|
||||||
// (we leave the 4th as nullptr)
|
// (we leave the 4th as nullptr)
|
||||||
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);
|
|
||||||
|
|
||||||
if (vertexShader == 0) {
|
if (vertexShader == 0) {
|
||||||
print("Vertex Shader Compilation Failed");
|
print("Vertex Shader Compilation Failed");
|
||||||
return std::make_pair(0, 0);
|
return std::make_pair(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned int fragmentShader =
|
const unsigned int fragmentShader =
|
||||||
compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
|
compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
|
||||||
|
|
||||||
if (fragmentShader == 0) {
|
if (fragmentShader == 0) {
|
||||||
print("Fragment Shader Compilation Failed");
|
print("Fragment Shader Compilation Failed");
|
||||||
return std::make_pair(0, 0);
|
return std::make_pair(0, 0);
|
||||||
}
|
}
|
||||||
return std::make_pair(vertexShader, fragmentShader);
|
return std::make_pair(vertexShader, fragmentShader);
|
||||||
}
|
}
|
||||||
|
|
||||||
void shaderFailedMessage(const unsigned int shader, const bool compilation) {
|
void shaderFailedMessage(const unsigned int shader, const bool compilation) {
|
||||||
char infoLog[512];
|
char infoLog[512];
|
||||||
const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]);
|
const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]);
|
||||||
compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, nullptr, infoLog)
|
compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, nullptr, infoLog)
|
||||||
: glGetProgramInfoLog(shader, sizeOfInfoLog, nullptr, infoLog);
|
: glGetProgramInfoLog(shader, sizeOfInfoLog, nullptr, infoLog);
|
||||||
std::cout << "ERROR vertex shader compilation failed \n"
|
std::cout << "ERROR vertex shader compilation failed \n"
|
||||||
<< infoLog << std::endl;
|
<< infoLog << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int shaderSuccessful(const unsigned int shader, const bool compilation) {
|
int shaderSuccessful(const unsigned int shader, const bool compilation) {
|
||||||
// check if compilation was successful
|
// check if compilation was successful
|
||||||
// int because glGetShaderiv requires int
|
// int because glGetShaderiv requires int
|
||||||
int success;
|
int success;
|
||||||
// here we store info about compilation
|
// here we store info about compilation
|
||||||
// check if compilation was successful
|
// check if compilation was successful
|
||||||
compilation ? glGetShaderiv(shader, GL_COMPILE_STATUS, &success)
|
compilation ? glGetShaderiv(shader, GL_COMPILE_STATUS, &success)
|
||||||
: glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
: glGetProgramiv(shader, GL_LINK_STATUS, &success);
|
||||||
// if not display compilation log
|
// if not display compilation log
|
||||||
if (!success) {
|
if (!success) {
|
||||||
shaderFailedMessage(shader, compilation);
|
shaderFailedMessage(shader, compilation);
|
||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -1,20 +1,19 @@
|
|||||||
// "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);
|
||||||
int shaderProgramLinkingSuccessful(const unsigned int shaderProgram);
|
int shaderProgramLinkingSuccessful(const unsigned int shaderProgram);
|
||||||
int shaderSuccessful(const unsigned int shader, const bool compilation);
|
int shaderSuccessful(const unsigned int shader, const bool compilation);
|
||||||
|
|||||||
@ -1,138 +1,158 @@
|
|||||||
// "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>
|
||||||
// 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)
|
#include "stb_image.h"
|
||||||
// second argument is what option we want to set and for which texture axis (s, t or r)
|
|
||||||
// Third argument is texture wrapping mode
|
void setTextureSCoordinate(const GLenum textureTarget,
|
||||||
glTexParameteri(textureTarget, GL_TEXTURE_WRAP_S, sCoordinateOption);
|
const GLint sCoordinateOption,
|
||||||
if(sCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL)
|
const float* borderColor) {
|
||||||
{
|
// https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexParameter.xhtml
|
||||||
// if we use GL_CLAMP_TO_BORDER all of the space that is not occupied by texture will be occupied by color
|
// first argument is texture target (by default we work with 2D textures so it
|
||||||
// we pass the color we want to use in float array (RGB + transparency)
|
// will be GL_TEXTURE_2D) second argument is what option we want to set and
|
||||||
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
|
// 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)
|
||||||
|
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) {
|
||||||
|
glTexParameteri(textureTarget, GL_TEXTURE_WRAP_T, tCoordinateOption);
|
||||||
|
if (tCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) {
|
||||||
|
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setTextureTCoordinate(const GLenum textureTarget, const GLint tCoordinateOption, 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_2D || textureTarget == GL_TEXTURE_3D)
|
// for comments concerning glTexParameteri and glTexParameterfv go to
|
||||||
{
|
// setTextureSCoordinate function
|
||||||
glTexParameteri(textureTarget, GL_TEXTURE_WRAP_T, tCoordinateOption);
|
if (textureTarget == GL_TEXTURE_3D) {
|
||||||
if(tCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL)
|
glTexParameteri(textureTarget, GL_TEXTURE_WRAP_R, rCoordinateOption);
|
||||||
{
|
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);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setTextureRCoordinate(const GLenum textureTarget, 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
|
||||||
// for comments concerning glTexParameteri and glTexParameterfv go to setTextureSCoordinate function
|
void setTextureParametersINT(const GLenum textureTarget,
|
||||||
if(textureTarget == GL_TEXTURE_3D)
|
const GLint sCoordinateOption,
|
||||||
{
|
const GLint tCoordinateOption,
|
||||||
glTexParameteri(textureTarget, GL_TEXTURE_WRAP_R, rCoordinateOption);
|
const GLint rCoordinateOption,
|
||||||
if(rCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL)
|
const float* borderColor) {
|
||||||
{
|
setTextureSCoordinate(textureTarget, sCoordinateOption, borderColor);
|
||||||
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
|
setTextureTCoordinate(textureTarget, tCoordinateOption, borderColor);
|
||||||
}
|
setTextureRCoordinate(textureTarget, rCoordinateOption, borderColor);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Coordinate options can be: GL_REPEAT, GL_MIRRORED_REPEAT, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER
|
void setTextureFilteringAndMipMap(const GLenum textureTarget,
|
||||||
// s t r are equivalent to x y z
|
const GLenum filterType,
|
||||||
void setTextureParametersINT(const GLenum textureTarget, const GLint sCoordinateOption, const GLint tCoordinateOption, const GLint rCoordinateOption, const float* borderColor)
|
const GLint textureFilteringMethod,
|
||||||
{
|
const GLint mipMapFilteringMethod) {
|
||||||
setTextureSCoordinate(textureTarget, sCoordinateOption, borderColor);
|
// mipMapFilteringMethod can be equal to: GL_NEAREST_MIPMAP_NEAREST,
|
||||||
setTextureTCoordinate(textureTarget, tCoordinateOption, borderColor);
|
// GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR
|
||||||
setTextureRCoordinate(textureTarget, rCoordinateOption, borderColor);
|
// 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
|
||||||
|
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
|
||||||
|
glTexParameteri(textureTarget, filterType, textureFilteringMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setTextureFilteringAndMipMap(const GLenum textureTarget, const GLenum filterType, const GLint textureFilteringMethod, const GLint mipMapFilteringMethod)
|
unsigned int generateAndBindTexture(const GLenum textureTarget,
|
||||||
{
|
const GLsizei numberOfTextures) {
|
||||||
// mipMapFilteringMethod can be equal to: GL_NEAREST_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR
|
unsigned int texture;
|
||||||
// NEAREST_MIPMAP - take nearest mipmap to match pixel size
|
// first argument is how many textures we want to generate
|
||||||
// LINEAR_MIPMAP - interpolate between two mipmaps that match the size of a pixel
|
// second is an array of unsigned int in which we will store id of those
|
||||||
// NEAREST (at the end) - samples texture based on nearest neighbor interpolation
|
// textures
|
||||||
// LINEAR (at the end) - samples texture using linear interpolation
|
glGenTextures(numberOfTextures, &texture);
|
||||||
// setting anything else than GL_TEXTURE_MIN_FILTER as a second parameter results in GL_INVALID_ENUM error
|
// then we bind texture
|
||||||
glTexParameteri(textureTarget, GL_TEXTURE_MIN_FILTER, mipMapFilteringMethod);
|
glBindTexture(textureTarget, texture);
|
||||||
// https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexParameter.xhtml
|
return texture;
|
||||||
// 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 loadAndBindTextureFile(unsigned int texture,
|
||||||
unsigned int generateAndBindTexture(const GLenum textureTarget, const GLsizei numberOfTextures ) {
|
const char* texturePath, bool flipImage) {
|
||||||
unsigned int texture;
|
int textureWidth, textureHeight, colorChannels;
|
||||||
// first argument is how many textures we want to generate
|
stbi_set_flip_vertically_on_load(flipImage);
|
||||||
// second is an array of unsigned int in which we will store id of those textures
|
unsigned char* data = stbi_load(texturePath, &textureWidth, &textureHeight,
|
||||||
glGenTextures(numberOfTextures, &texture);
|
&colorChannels, STBI_rgb_alpha);
|
||||||
// then we bind texture
|
if (data) {
|
||||||
glBindTexture(textureTarget, texture);
|
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
|
||||||
|
*/
|
||||||
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||||
|
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;
|
return texture;
|
||||||
|
} else {
|
||||||
|
std::cout << "Failed to load texture! " << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int loadAndBindTextureFile(unsigned int texture, const char* texturePath, bool flipImage) {
|
// const char* texturePath, int textureWidth, int textureHeight, int
|
||||||
int textureWidth, textureHeight, colorChannels;
|
// colorChannels, const GLenum textureTarget, const GLint sCoordinateOption,
|
||||||
stbi_set_flip_vertically_on_load(flipImage);
|
// const GLint tCoordinateOption, const GLint rCoordinateOption, const float*
|
||||||
unsigned char *data = stbi_load(texturePath, &textureWidth, &textureHeight, &colorChannels, STBI_rgb_alpha);
|
// borderColor
|
||||||
if(data) {
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
|
||||||
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
|
|
||||||
{
|
|
||||||
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
|
|
||||||
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
|
||||||
// -------------------------
|
// -------------------------
|
||||||
unsigned int texture = generateAndBindTexture(GL_TEXTURE_2D, 1);
|
unsigned int texture = generateAndBindTexture(GL_TEXTURE_2D, 1);
|
||||||
setTextureParametersINT(GL_TEXTURE_2D, GL_REPEAT, GL_REPEAT);
|
setTextureParametersINT(GL_TEXTURE_2D, GL_REPEAT, GL_REPEAT);
|
||||||
|
|
||||||
// set texture filtering parameters
|
// set texture filtering parameters
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
// load image, create texture and generate mipmaps
|
// load image, create texture and generate mipmaps
|
||||||
texture = loadAndBindTextureFile(texture, texturePath, flipImage);
|
texture = loadAndBindTextureFile(texture, texturePath, 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // TEXTURES_CPP
|
#endif // TEXTURES_CPP
|
||||||
|
|||||||
@ -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
|
||||||
Loading…
Reference in New Issue
Block a user