feat: Create shader class

This commit is contained in:
Huseyn Ismayilov 2024-07-31 17:40:16 +04:00
parent b3b7638e30
commit 5d536e7b71
10 changed files with 195 additions and 5 deletions

View File

@ -21,6 +21,9 @@ private:
Engine engine;
UI ui;
int screenWidth = 800;
int screenHeight = 600;
void InitializeWindow();
void InitializeImGui();
};

View File

@ -48,7 +48,7 @@ void Editor::InitializeWindow() {
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(1280, 720, "Editor", nullptr, nullptr);
window = glfwCreateWindow(screenWidth, screenHeight, "Editor", nullptr, nullptr);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
@ -56,4 +56,5 @@ void Editor::InitializeWindow() {
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, Engine::FramebufferSizeCallback);
}

View File

@ -10,6 +10,8 @@ public:
Engine();
~Engine();
static void FramebufferSizeCallback(GLFWwindow* window, int width, int height);
void Initialize(GLFWwindow* window);
void Render();
void Shutdown();

View File

@ -2,6 +2,7 @@
#define GRAPHICS_H
#include <GLFW/glfw3.h>
#include "Shader.h"
class Graphics
{
@ -9,14 +10,18 @@ public:
Graphics();
~Graphics();
static void FramebufferSizeCallback(GLFWwindow* window, int width, int height);
void Initialize(GLFWwindow* window);
void Render();
void Shutdown();
private:
GLFWwindow* window;
Shader shader;
unsigned int VAO, VBO;
void LoadShaders();
void SetupBuffers();
};

28
engine/include/Shader.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef SHADER_H
#define SHADER_H
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
class Shader
{
public:
Shader();
Shader(const char* vPath, const char* fPath);
~Shader();
void Use();
unsigned int GetID();
void SetBool(const char* name, bool value) const;
void SetInt(const char* name, int value) const;
void SetFloat(const char* name, float value) const;
void SetMat4(const char* name, glm::mat4 value) const;
private:
unsigned int ID;
};
#endif

View File

@ -0,0 +1,9 @@
#version 330 core
out vec4 fragColor;
in vec3 ourColor;
void main()
{
fragColor = vec4(ourColor, 1.0f);
}

View File

@ -0,0 +1,11 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
out vec3 ourColor;
void main()
{
gl_Position = vec4(aPos, 1.0f);
ourColor = aColor;
}

View File

@ -7,6 +7,11 @@ Engine::~Engine()
Shutdown();
}
void Engine::FramebufferSizeCallback(GLFWwindow* window, int width, int height)
{
Graphics::FramebufferSizeCallback(window, width, height);
}
void Engine::Initialize(GLFWwindow* window)
{
graphics.Initialize(window);

View File

@ -1,6 +1,7 @@
#include <glad/glad.h>
#include <iostream>
#include "Graphics.h"
#include "Shader.h"
Graphics::Graphics() : window(nullptr), VAO(0), VBO(0) {}
@ -18,15 +19,21 @@ void Graphics::Initialize(GLFWwindow* window)
return;
}
LoadShaders();
SetupBuffers();
}
void Graphics::LoadShaders()
{
shader = Shader(RESOURCES_PATH"shaders/shader.vs", RESOURCES_PATH"shaders/shader.fs");
}
void Graphics::SetupBuffers()
{
float vertices[] = {
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
};
glGenVertexArrays(1, &VAO);
@ -37,16 +44,25 @@ void Graphics::SetupBuffers()
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void Graphics::FramebufferSizeCallback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void Graphics::Render() {
glClear(GL_COLOR_BUFFER_BIT);
shader.Use();
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
}

110
engine/src/Shader.cpp Normal file
View File

@ -0,0 +1,110 @@
#include <glad/glad.h>
#include <sstream>
#include <fstream>
#include <iostream>
#include "Shader.h"
Shader::Shader(): ID(0){}
Shader::Shader(const char* vPath, const char* fPath)
{
std::string vCode;
std::string fCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try
{
vShaderFile.open(vPath);
fShaderFile.open(fPath);
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
vShaderFile.close();
fShaderFile.close();
vCode = vShaderStream.str();
fCode = fShaderStream.str();
}
catch(std::ifstream::failure& e)
{
std::cerr << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ::" << e.what() << std::endl;
std::cerr << "VPATH::" << vPath << std::endl << "FPATH::" << fPath << std::endl;
}
const char* vShaderCode = vCode.c_str();
const char* fShaderCode = fCode.c_str();
unsigned int vertex, fragment;
int success;
char infoLog[512];
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, nullptr);
glCompileShader(vertex);
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(vertex, 512, nullptr, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
};
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, nullptr);
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(fragment, 512, nullptr, infoLog);
std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
};
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if(!success)
{
glGetProgramInfoLog(ID, 512, nullptr, infoLog);
std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertex);
glDeleteShader(fragment);
}
Shader::~Shader(){};
void Shader::Use()
{
glUseProgram(ID);
}
unsigned int Shader::GetID()
{
return ID;
}
void Shader::SetBool(const char* name, bool value) const
{
glUniform1i(glGetUniformLocation(ID, name), (int)value);
}
void Shader::SetInt(const char* name, int value) const
{
glUniform1i(glGetUniformLocation(ID, name), value);
}
void Shader::SetFloat(const char* name, float value) const
{
glUniform1f(glGetUniformLocation(ID, name), value);
}
void Shader::SetMat4(const char* name, glm::mat4 value) const
{
glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, glm::value_ptr(value));
}