-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderProgram.cpp
132 lines (109 loc) · 3.57 KB
/
ShaderProgram.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
#include <GL/glew.h>
#include <GL/glut.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include "ShaderProgram.h"
const char* ShaderProgram::SHADER_FOLDER = "shaders";
ShaderProgram::ShaderProgram() {
programID = glCreateProgram();
}
void ShaderProgram::addShader(GLuint type, string path) {
GLuint shaderID = glCreateShader(type);
loadSource(shaderID, path);
std::cout << "Compiling Shader " << path << std::endl;
glCompileShader(shaderID);
printCompileInfoLog(shaderID);
glAttachShader(programID, shaderID);
}
void ShaderProgram::compile() {
glLinkProgram(programID);
printLinkInfoLog(programID);
validateProgram(programID);
}
void ShaderProgram::uniformMatrix4fv(std::string str, GLuint n, GLboolean b, const GLfloat* pointer) {
GLuint loc = glGetUniformLocation(programID, str.c_str());
glUniformMatrix4fv( loc, n, b, pointer );
}
void ShaderProgram::vertexAttribPointer(BufferObject<GL_SHADER_STORAGE_BUFFER>* buffer, std::string str, GLuint size, GLuint type, GLboolean b, GLuint offset, void* pointer) {
glBindBuffer(GL_ARRAY_BUFFER, buffer->id());
GLuint loc = glGetAttribLocation(programID, str.c_str());
glEnableVertexAttribArray(loc);
glVertexAttribPointer( loc, size, type, b, offset, (char *)NULL + 0 );
}
// BEGIN: Carga shaders ////////////////////////////////////////////////////////////////////////////////////////////
void loadSource(GLuint &shaderID, std::string name) {
std::ifstream f(name.c_str());
if (!f.is_open()) {
std::cerr << "File not found " << name.c_str() << std::endl;
system("pause");
exit(EXIT_FAILURE);
}
// now read in the data
std::string *source;
source = new std::string( std::istreambuf_iterator<char>(f),
std::istreambuf_iterator<char>() );
f.close();
// add a null to the string
*source += "\0";
const GLchar * data = source->c_str();
glShaderSource(shaderID, 1, &data, NULL);
delete source;
}
void printCompileInfoLog(GLuint shadID)
{
GLint compiled;
glGetShaderiv( shadID, GL_COMPILE_STATUS, &compiled );
if (compiled == GL_FALSE)
{
GLint infoLength = 0;
glGetShaderiv( shadID, GL_INFO_LOG_LENGTH, &infoLength );
GLchar *infoLog = new GLchar[infoLength];
GLint chsWritten = 0;
glGetShaderInfoLog( shadID, infoLength, &chsWritten, infoLog );
std::cerr << "Shader compiling failed:" << infoLog << std::endl;
system("pause");
delete [] infoLog;
exit(EXIT_FAILURE);
}
}
void printLinkInfoLog(GLuint programID)
{
GLint linked;
glGetProgramiv( programID, GL_LINK_STATUS, &linked );
if(! linked)
{
GLint infoLength = 0;
glGetProgramiv( programID, GL_INFO_LOG_LENGTH, &infoLength );
GLchar *infoLog = new GLchar[infoLength];
GLint chsWritten = 0;
glGetProgramInfoLog( programID, infoLength, &chsWritten, infoLog );
std::cerr << "Shader linking failed:" << infoLog << std::endl;
system("pause");
delete [] infoLog;
exit(EXIT_FAILURE);
}
}
void validateProgram(GLuint programID)
{
GLint status;
glValidateProgram( programID );
glGetProgramiv( programID, GL_VALIDATE_STATUS, &status );
if( status == GL_FALSE )
{
GLint infoLength = 0;
glGetProgramiv( programID, GL_INFO_LOG_LENGTH, &infoLength );
if( infoLength > 0 )
{
GLchar *infoLog = new GLchar[infoLength];
GLint chsWritten = 0;
glGetProgramInfoLog( programID, infoLength, &chsWritten, infoLog );
std::cerr << "Program validating failed:" << infoLog << std::endl;
system("pause");
delete [] infoLog;
exit(EXIT_FAILURE);
}
}
}
// END: Carga shaders ////////////////////////////////////////////////////////////////////////////////////////////