AireEngine
A 3D Open-World Game Engine
Loading...
Searching...
No Matches
shaderProgram.h
1#pragma once
2
3#include <glad/glad.h>
4
5#include <string>
6#include <vector>
7
8namespace Aire {
10 struct ShaderSource {
11 GLenum type;
12 std::string sourcePath;
13 };
14
17 public:
20 explicit ShaderProgram(std::vector<ShaderSource> shaderList = {});
21
23 GLuint getProgramId() const;
24
26 void reloadShaders();
27
29 void useProgram();
30
33
34 // Delete copy constructor and assignment operator
35 ShaderProgram( ShaderProgram const& ) = delete;
36 ShaderProgram& operator= (ShaderProgram const&) = delete;
37
41 void set2DTextureUniform(const char* uniformName, const GLuint textureHandle);
42
46 void setCubeTextureUniform(const char* uniformName, const GLuint textureHandle);
47
49 void unbindAllTextures();
50
53 void unbind2DTextures(int numTextures);
54
57 void unbindCubeTextures(int numTextures);
58
59 private:
60 GLuint programId;
61 std::vector<ShaderSource> shaderSources;
62
63 GLuint current2DTextureUnit = 0;
64 GLuint currentCubeTextureUnit = 0;
65 };
66
67 //##########################################//
68 //######## INLINE DEFINITIONS BELOW ########//
69 //##########################################//
70
71 inline GLuint ShaderProgram::getProgramId() const {
72 return programId;
73 }
74
76 glUseProgram(programId);
77
78 current2DTextureUnit = 0;
79 currentCubeTextureUnit = 0;
80 }
81
82 inline void ShaderProgram::set2DTextureUniform(const char* uniformName, const GLuint textureHandle) {
83 glActiveTexture(GL_TEXTURE0 + current2DTextureUnit);
84 glBindTexture(GL_TEXTURE_2D, textureHandle);
85 glUniform1i(glGetUniformLocation(programId, uniformName), current2DTextureUnit);
86 current2DTextureUnit++;
87 }
88
89 inline void ShaderProgram::setCubeTextureUniform(const char* uniformName, const GLuint textureHandle) {
90 glActiveTexture(GL_TEXTURE0 + currentCubeTextureUnit);
91 glBindTexture(GL_TEXTURE_CUBE_MAP, textureHandle);
92 glUniform1i(glGetUniformLocation(programId, uniformName), currentCubeTextureUnit);
93 currentCubeTextureUnit++;
94 }
95
97 for (GLuint i = 0; i < current2DTextureUnit; i++) {
98 glActiveTexture(GL_TEXTURE0 + i);
99 glBindTexture(GL_TEXTURE_2D, 0);
100 current2DTextureUnit--;
101 }
102 for (GLuint i = 0; i < currentCubeTextureUnit; i++) {
103 glActiveTexture(GL_TEXTURE0 + i);
104 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
105 currentCubeTextureUnit--;
106 }
107 }
108
109 inline void ShaderProgram::unbind2DTextures(int numTextures) {
110 for (GLuint i = 0; i < numTextures; i++) {
111 glActiveTexture(GL_TEXTURE0 + current2DTextureUnit - 1);
112 glBindTexture(GL_TEXTURE_2D, 0);
113 current2DTextureUnit--;
114 }
115 }
116
117 inline void ShaderProgram::unbindCubeTextures(int numTextures) {
118 for (GLuint i = 0; i < numTextures; i++) {
119 glActiveTexture(GL_TEXTURE0 + currentCubeTextureUnit - 1);
120 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
121 currentCubeTextureUnit--;
122 }
123 }
124
125}
A linked OpenGL program.
Definition shaderProgram.h:16
~ShaderProgram()
Destroy the program.
Definition shaderProgram.cpp:102
void setCubeTextureUniform(const char *uniformName, const GLuint textureHandle)
Definition shaderProgram.h:89
void set2DTextureUniform(const char *uniformName, const GLuint textureHandle)
Definition shaderProgram.h:82
void unbindCubeTextures(int numTextures)
Definition shaderProgram.h:117
GLuint getProgramId() const
Definition shaderProgram.h:71
void unbindAllTextures()
Unbind all textures currently bound to the program.
Definition shaderProgram.h:96
void useProgram()
Set as the current program to use.
Definition shaderProgram.h:75
void reloadShaders()
Reload the shaders in the program. Can be used to reload shaders during execution.
Definition shaderProgram.cpp:49
void unbind2DTextures(int numTextures)
Definition shaderProgram.h:109
ShaderProgram(std::vector< ShaderSource > shaderList={})
Definition shaderProgram.cpp:39
Struct to store the path to a shader source file.
Definition shaderProgram.h:10
std::string sourcePath
The path to the shader source.
Definition shaderProgram.h:12
GLenum type
The shader type (vertex, fragment, etc)
Definition shaderProgram.h:11