|
@@ -1,20 +1,27 @@
|
1
|
1
|
#include "shader.hpp"
|
2
|
2
|
|
3
|
|
-Shader::Shader( std::string vert, std::string frag) {
|
4
|
|
- vert = resource->getRealPath(vert);
|
5
|
|
- frag = resource->getRealPath(frag);
|
6
|
|
- // Create the program to link to.
|
7
|
|
- m_program = glCreateProgram();
|
8
|
|
-
|
9
|
|
- std::ifstream v( vert.c_str() );
|
10
|
|
- std::string vert_contents((std::istreambuf_iterator<char>(v)),
|
11
|
|
- std::istreambuf_iterator<char>());
|
12
|
|
- std::ifstream f( frag.c_str() );
|
13
|
|
- std::string frag_contents((std::istreambuf_iterator<char>(f)),
|
14
|
|
- std::istreambuf_iterator<char>());
|
|
3
|
+Shader::Shader( std::string vert, std::string frag, bool file ) {
|
|
4
|
+ std::string vert_contents;
|
|
5
|
+ std::string frag_contents;
|
|
6
|
+ if ( file ) {
|
|
7
|
+ vert = resource->getRealPath(vert);
|
|
8
|
+ frag = resource->getRealPath(frag);
|
|
9
|
+ std::ifstream v( vert.c_str() );
|
|
10
|
+ vert_contents = std::string((std::istreambuf_iterator<char>(v)),
|
|
11
|
+ std::istreambuf_iterator<char>());
|
|
12
|
+ std::ifstream f( frag.c_str() );
|
|
13
|
+ frag_contents = std::string((std::istreambuf_iterator<char>(f)),
|
|
14
|
+ std::istreambuf_iterator<char>());
|
|
15
|
+ } else {
|
|
16
|
+ vert_contents = vert;
|
|
17
|
+ frag_contents = frag;
|
|
18
|
+ }
|
15
|
19
|
|
16
|
20
|
const char* vertsrc = vert_contents.c_str();
|
17
|
21
|
const char* fragsrc = frag_contents.c_str();
|
|
22
|
+
|
|
23
|
+ // Create the program to link to.
|
|
24
|
+ program = glCreateProgram();
|
18
|
25
|
|
19
|
26
|
if ( vert_contents.length() <= 0 ) {
|
20
|
27
|
std::string errstring = "Failed to open file (or is empty) `" + vert + "`.\n";
|
|
@@ -67,15 +74,15 @@ Shader::Shader( std::string vert, std::string frag) {
|
67
|
74
|
}
|
68
|
75
|
|
69
|
76
|
Shader::~Shader() {
|
70
|
|
- glDeleteProgram( m_program );
|
|
77
|
+ glDeleteProgram( program );
|
71
|
78
|
}
|
72
|
79
|
|
73
|
80
|
unsigned int Shader::getProgram() {
|
74
|
|
- return m_program;
|
|
81
|
+ return program;
|
75
|
82
|
}
|
76
|
83
|
|
77
|
84
|
void Shader::bind() {
|
78
|
|
- glUseProgram( m_program );
|
|
85
|
+ glUseProgram( program );
|
79
|
86
|
}
|
80
|
87
|
|
81
|
88
|
int Shader::compile( unsigned int shader, std::string& error ) {
|
|
@@ -97,18 +104,18 @@ int Shader::compile( unsigned int shader, std::string& error ) {
|
97
|
104
|
}
|
98
|
105
|
|
99
|
106
|
int Shader::link( unsigned int vertshader, unsigned int fragshader, std::string& error ) {
|
100
|
|
- glAttachShader( m_program, vertshader );
|
101
|
|
- glAttachShader( m_program, fragshader );
|
102
|
|
- glLinkProgram( m_program );
|
|
107
|
+ glAttachShader( program, vertshader );
|
|
108
|
+ glAttachShader( program, fragshader );
|
|
109
|
+ glLinkProgram( program );
|
103
|
110
|
|
104
|
111
|
// Linking the shader is the easy part, all this junk down here is for printing the error it might generate.
|
105
|
112
|
int result = GL_FALSE;
|
106
|
113
|
int logLength;
|
107
|
|
- glGetProgramiv( m_program, GL_LINK_STATUS, &result);
|
108
|
|
- glGetProgramiv( m_program, GL_INFO_LOG_LENGTH, &logLength);
|
|
114
|
+ glGetProgramiv( program, GL_LINK_STATUS, &result);
|
|
115
|
+ glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logLength);
|
109
|
116
|
if ( result == GL_FALSE ) {
|
110
|
117
|
char* errormsg = new char[ logLength ];
|
111
|
|
- glGetProgramInfoLog( m_program, logLength, NULL, errormsg );
|
|
118
|
+ glGetProgramInfoLog( program, logLength, NULL, errormsg );
|
112
|
119
|
error = errormsg;
|
113
|
120
|
delete[] errormsg;
|
114
|
121
|
return 1;
|
|
@@ -117,8 +124,8 @@ int Shader::link( unsigned int vertshader, unsigned int fragshader, std::string&
|
117
|
124
|
}
|
118
|
125
|
|
119
|
126
|
unsigned int Shader::getUniformLocation( std::string name ) {
|
120
|
|
- glUseProgram( m_program );
|
121
|
|
- return glGetUniformLocation( m_program, name.c_str() );
|
|
127
|
+ glUseProgram( program );
|
|
128
|
+ return glGetUniformLocation( program, name.c_str() );
|
122
|
129
|
}
|
123
|
130
|
|
124
|
131
|
void Shader::setParameter( std::string name, int foo ) {
|
|
@@ -142,17 +149,17 @@ void Shader::setParameter( std::string name, glm::vec2 foo ) {
|
142
|
149
|
}
|
143
|
150
|
|
144
|
151
|
void Shader::setAttribute( std::string name, unsigned int buffer, unsigned int stepsize ) {
|
145
|
|
- unsigned int a = glGetAttribLocation( m_program, name.c_str() );
|
|
152
|
+ unsigned int a = glGetAttribLocation( program, name.c_str() );
|
146
|
153
|
glEnableVertexAttribArray( a );
|
147
|
154
|
glBindBuffer( GL_ARRAY_BUFFER, buffer );
|
148
|
155
|
glVertexAttribPointer( a, stepsize, GL_FLOAT, GL_FALSE, 0, NULL );
|
149
|
|
- m_activeattribs.push_back( a );
|
|
156
|
+ activeAttributes.push_back( a );
|
150
|
157
|
}
|
151
|
158
|
|
152
|
159
|
void Shader::unbind() {
|
153
|
|
- for ( unsigned int i=0; i<m_activeattribs.size(); i++ ) {
|
154
|
|
- glDisableVertexAttribArray( m_activeattribs[i] );
|
|
160
|
+ for ( unsigned int i=0; i<activeAttributes.size(); i++ ) {
|
|
161
|
+ glDisableVertexAttribArray( activeAttributes[i] );
|
155
|
162
|
}
|
156
|
|
- m_activeattribs.clear();
|
|
163
|
+ activeAttributes.clear();
|
157
|
164
|
glUseProgram( 0 );
|
158
|
165
|
}
|