-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglprogram.cpp
More file actions
153 lines (126 loc) · 4.21 KB
/
glprogram.cpp
File metadata and controls
153 lines (126 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <cassert>
#include <fstream>
#include <SDL2/SDL.h>
#include "GL/glew.h"
#include "config.h"
#include "GLError.h"
#include "glprogram.h"
////////////////////////////////////////////////////////////////////////////////
constexpr int INFO_LOG_SIZE = 512;
constexpr char SHADER_COMPILATION_FAILED_MSG[] = "Failed to compile shader %d. Log:\n%s";
constexpr char PROGRAM_COMPILATION_FAILED_MSG[] = "Failed to compile the program. Log:\n%s";
////////////////////////////////////////////////////////////////////////////////
/**
* @brief GLProgram::GLProgram Constructor for the class
* @param vertPath Path to the vertex shader.
* @param fragPath Path to the fragment shader.
*/
GLProgram::GLProgram( const char* const vertPath, const char* const fragPath )
: m_vertPath( vertPath )
, m_fragPath( fragPath )
, m_programId( 0 )
{
CompileShaders();
}
////////////////////////////////////////////////////////////////////////////////
/**
* @brief GLProgram::~GLProgram Destructor for the clas.
*/
GLProgram::~GLProgram()
{
glDeleteProgram( m_programId );
}
////////////////////////////////////////////////////////////////////////////////
/**
* @brief GLProgram::Use Binds the program.
*/
void GLProgram::Use()
{
glUseProgram( m_programId );
}
////////////////////////////////////////////////////////////////////////////////
/**
* @brief GLProgram::CompileShaders This function compiles shaders and links
* them in a program.
*/
void GLProgram::CompileShaders()
{
uint32_t vertId = CompileShader< GL_VERTEX_SHADER >( m_vertPath );
uint32_t fragId = CompileShader< GL_FRAGMENT_SHADER >( m_fragPath );
m_programId = CompileProgram( vertId, fragId );
glDeleteShader( vertId );
glDeleteShader( fragId );
glUseProgram( m_programId );
}
////////////////////////////////////////////////////////////////////////////////
/**
* @brief GLProgram::CompileShader This function compiles a shader and returns
* the id of the shader.
* Takes path to the file and shader type (as a template parameter).
*/
template< int SHADER_TYPE >
uint32_t GLProgram::CompileShader( const char* const path )
{
auto code = ReadFile( path );
if( code.empty() )
return 0;
const char* cstrCode = code.c_str();
int32_t success;
char info_log[ INFO_LOG_SIZE ];
uint32_t shaderId = glCreateShader( SHADER_TYPE );
glShaderSource( shaderId, 1, &cstrCode, nullptr );
glCompileShader( shaderId );
glGetShaderiv( shaderId, GL_COMPILE_STATUS, &success );
GL::ErrCh;
if( GL_TRUE != success )
{
glGetShaderInfoLog( shaderId, INFO_LOG_SIZE, nullptr, info_log );
SDL_LogError( SDL_LOG_CATEGORY_RENDER, SHADER_COMPILATION_FAILED_MSG,
SHADER_TYPE, info_log );
SDL_Log( SHADER_COMPILATION_FAILED_MSG, SHADER_TYPE, info_log );
return 0;
}
return shaderId;
}
////////////////////////////////////////////////////////////////////////////////
/**
* @brief GLProgram::ReadFile Function that reads from a file.
* @param path Path to the file.
* @return Returns the contents of the file as std::string.
*/
std::string GLProgram::ReadFile( const char* const path )
{
std::ifstream fileStream;
fileStream.open( path );
assert( fileStream.is_open() );
return std::string( ( std::istreambuf_iterator< char >( fileStream ) ),
std::istreambuf_iterator< char >() );
}
////////////////////////////////////////////////////////////////////////////////
/**
* @brief GLProgram::CompileProgram Function that links two shaders to a
* program.
* @param vertId Id of the vertex shader.
* @param fragId Id of the fragment shader.
* @return Returns the id of the program.
*/
uint32_t GLProgram::CompileProgram( uint32_t vertId, uint32_t fragId )
{
int success;
char info_log[ INFO_LOG_SIZE ];
uint32_t programId = glCreateProgram();
glAttachShader( programId, vertId );
glAttachShader( programId, fragId );
glLinkProgram( programId );
glGetProgramiv( programId, GL_LINK_STATUS, &success );
if( GL_TRUE != success )
{
glGetProgramInfoLog( programId, INFO_LOG_SIZE, nullptr, info_log ); GL::ErrCh;
SDL_LogError( SDL_LOG_CATEGORY_RENDER, PROGRAM_COMPILATION_FAILED_MSG,
info_log );
SDL_Log( PROGRAM_COMPILATION_FAILED_MSG, info_log );
return 0;
}
return programId;
}
////////////////////////////////////////////////////////////////////////////////