-
Notifications
You must be signed in to change notification settings - Fork 1
/
SimpleShader.cpp
163 lines (132 loc) · 3.85 KB
/
SimpleShader.cpp
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
154
155
156
157
158
159
160
161
162
163
/* EGLBenchmark
*
* Author: Jarkko Vatjus-Anttila <[email protected]>
*
* For conditions of distribution and use, see copyright notice in license.txt
*/
#include "SimpleShader.h"
#include "GLWrapper.h"
#include "DebugLog.h"
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
SimpleShader::SimpleShader()
{
}
SimpleShader::~SimpleShader()
{
destroyProgram();
}
/******************************************************************************
* File I/O
*/
bool SimpleShader::fromFiles(const char *v_src, const char *f_src)
{
programObject = createShaderProgram(v_src, f_src);
if (programObject == 0)
{
DebugLog::Instance()->MESSAGE(1, "Shader program object creation failed\n");
return false;
}
return true;
}
void SimpleShader::bindProgram(void)
{
GLWrapper::Instance()->GLUSEPROGRAM(programObject);
}
void SimpleShader::linkProgram(void)
{
GLWrapper::Instance()->GLLINKPROGRAM(programObject);
bindProgram();
}
void SimpleShader::destroyProgram(void)
{
if (programObject == 0)
{
return;
}
GLWrapper::Instance()->GLDELETEPROGRAM(programObject);
GLWrapper::Instance()->GLDELETESHADER(vertexShader);
GLWrapper::Instance()->GLDELETESHADER(fragmentShader);
programObject = 0;
}
GLuint SimpleShader::getProgramObject(void)
{
return programObject;
}
/******************************************************************************
* Private methods
*/
/*
* Shader program helpers
* ----------------------
*/
bool SimpleShader::printShaderInfo ( GLuint shader )
{
GLint length;
GLWrapper::Instance()->GLGETSHADERIV( shader , GL_INFO_LOG_LENGTH , &length );
if ( length )
{
char buffer[length];
GLint success;
GLWrapper::Instance()->GLGETSHADERINFOLOG( shader , length , NULL , buffer );
DebugLog::Instance()->MESSAGE(4, "Shader info: '%s'\n", buffer);
GLWrapper::Instance()->GLGETSHADERIV( shader, GL_COMPILE_STATUS, &success );
if ( success != GL_TRUE )
{
DebugLog::Instance()->MESSAGE(4, "Error: Shader compilation failed\n");
return false;
}
}
return true;
}
GLuint SimpleShader::loadShaderProgram ( const char *shader_source, GLenum type)
{
GLuint shader;
shader = GLWrapper::Instance()->GLCREATESHADER( type );
if (shader == 0)
{
DebugLog::Instance()->MESSAGE(2, "loadShader: shader creation failed.\n");
return 0;
}
GLWrapper::Instance()->GLSHADERSOURCE( shader , 1 , &shader_source , NULL );
GLWrapper::Instance()->GLCOMPILESHADER( shader );
if (false == printShaderInfo ( shader ))
{
return 0;
}
return shader;
}
GLuint SimpleShader::createShaderProgram(const char *v_src, const char *f_src)
{
GLuint shaderProgram;
shaderProgram = GLWrapper::Instance()->GLCREATEPROGRAM();
if (shaderProgram == 0)
{
DebugLog::Instance()->MESSAGE(1, "Error: Shader program creation failed\n");
return 0;
}
DebugLog::Instance()->MESSAGE(4, "Initializing shaders...\n");
if (v_src == NULL || f_src == NULL)
{
DebugLog::Instance()->MESSAGE(4, "Vertex nor fragment shader source must not be NULL.\n");
return 0;
}
vertexShader = loadShaderProgram ( v_src , GL_VERTEX_SHADER );
fragmentShader = loadShaderProgram ( f_src , GL_FRAGMENT_SHADER );
if (vertexShader == 0 || fragmentShader == 0)
{
DebugLog::Instance()->MESSAGE(1, "Error: Shader program loading failed\n");
return 0;
}
GLWrapper::Instance()->GLATTACHSHADER(shaderProgram, vertexShader);
GLWrapper::Instance()->GLATTACHSHADER(shaderProgram, fragmentShader);
return shaderProgram;
}
void SimpleShader::linkShaderProgram(GLuint shaderProgram)
{
GLWrapper::Instance()->GLLINKPROGRAM(shaderProgram);
GLWrapper::Instance()->GLUSEPROGRAM(shaderProgram);
}