HPC_Voxel_Engine 0.2.0
High-Performance C++ Voxel Engine
Loading...
Searching...
No Matches
Shader.h
Go to the documentation of this file.
1#pragma once
2#include <assert.h>
3#include <glad/glad.h>
4#include <fstream>
5#include <iostream>
6#include <sstream>
7#include <string>
8#include "../core/Matrix.h"
9
10namespace Renderer {
11class Shader {
12public:
13 unsigned int ID;
14
15 // Constructor
16 Shader(const char *cVertexPath, const char *cFragmentPath) {
17 std::string strVertexCode;
18 std::string strFragmentCode;
19
20 std::ifstream istrmVertexFile;
21 std::ifstream istrmFragmentFile;
22 istrmVertexFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
23 istrmFragmentFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
24
25 try {
26 istrmVertexFile.open(cVertexPath);
27 istrmFragmentFile.open(cFragmentPath);
28
29 std::stringstream strstrmVertex, strstrmFragment;
30 strstrmVertex << istrmVertexFile.rdbuf();
31 strstrmFragment << istrmFragmentFile.rdbuf();
32
33 istrmVertexFile.close();
34 istrmFragmentFile.close();
35
36 strVertexCode = strstrmVertex.str();
37 strFragmentCode = strstrmFragment.str();
38 } catch (std::ifstream::failure &e) {
39 std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ\n" << e.what() << std::endl;
40 assert(false && "Shader File Not Found!");
41 exit(-1);
42 }
43
44 const char *cVertexShaderCode = strVertexCode.c_str();
45 const char *cFragmentShaderCode = strFragmentCode.c_str();
46
47 unsigned int uiVertex, uiFragment;
48 uiVertex = glCreateShader(GL_VERTEX_SHADER);
49 glShaderSource(uiVertex, 1, &cVertexShaderCode, nullptr);
50 glCompileShader(uiVertex);
51 checkCompileErrors(uiVertex, "VERTEX");
52
53 uiFragment = glCreateShader(GL_FRAGMENT_SHADER);
54 glShaderSource(uiFragment, 1, &cFragmentShaderCode, nullptr);
55 glCompileShader(uiFragment);
56 checkCompileErrors(uiFragment, "FRAGMENT");
57
58 ID = glCreateProgram();
59 glAttachShader(ID, uiVertex);
60 glAttachShader(ID, uiFragment);
61 glLinkProgram(ID);
62 checkCompileErrors(ID, "PROGRAM");
63
64 glDeleteShader(uiVertex);
65 glDeleteShader(uiFragment);
66 }
67
68 void Use() const { glUseProgram(ID); }
69
70 void SetMat4(const std::string &name, const Core::Mat4 &mat) const {
71 glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, mat.m_fElements);
72 }
73
74 void SetVec2(const std::string &name, const Core::Vec3 &vec) const {
75 glUniform2f(glGetUniformLocation(ID, name.c_str()), vec.x, vec.y);
76 }
77
78 void SetVec3(const std::string &name, const Core::Vec3 &vec) const {
79 glUniform3f(glGetUniformLocation(ID, name.c_str()), vec.x, vec.y, vec.z);
80 }
81
82 void SetInt(const std::string &name, int iValue) const {
83 glUniform1i(glGetUniformLocation(ID, name.c_str()), iValue);
84 }
85
86private:
87 void checkCompileErrors(unsigned int uiShader, std::string strType) {
88 int iSuccess;
89 char cInfoLog[1024];
90 if (strType != "PROGRAM") {
91 glGetShaderiv(uiShader, GL_COMPILE_STATUS, &iSuccess);
92 if (!iSuccess) {
93 glGetShaderInfoLog(uiShader, 1024, nullptr, cInfoLog);
94 std::cout << "ERROR::SHADER::COMPILATION_ERROR\n" << cInfoLog;
95 std::cout
96 << "-----------------------------------------------------------------------";
97 }
98 } else {
99 glGetProgramiv(uiShader, GL_LINK_STATUS, &iSuccess);
100 if (!iSuccess) {
101 glGetProgramInfoLog(uiShader, 1024, nullptr, cInfoLog);
102 std::cout << "ERROR::PROGRAM::LINKING_ERROR\n" << cInfoLog;
103 std::cout
104 << "-----------------------------------------------------------------------";
105 }
106 }
107 }
108};
109} // namespace Renderer
Definition Shader.h:11
void SetMat4(const std::string &name, const Core::Mat4 &mat) const
Definition Shader.h:70
void Use() const
Definition Shader.h:68
void SetInt(const std::string &name, int iValue) const
Definition Shader.h:82
void SetVec3(const std::string &name, const Core::Vec3 &vec) const
Definition Shader.h:78
Shader(const char *cVertexPath, const char *cFragmentPath)
Definition Shader.h:16
unsigned int ID
Definition Shader.h:13
void checkCompileErrors(unsigned int uiShader, std::string strType)
Definition Shader.h:87
void SetVec2(const std::string &name, const Core::Vec3 &vec) const
Definition Shader.h:74
Definition Buffer.h:4
A 4x4 Matrix structure stored in Column-Major order (OpenGL Standard).
Definition Matrix.h:18
float m_fElements[16]
Definition Matrix.h:19
A 3-component vector structure (x, y, z) with standard math operations.
Definition MathUtils.h:11
float x
Definition MathUtils.h:12
float z
Definition MathUtils.h:12
float y
Definition MathUtils.h:12