-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cpp
157 lines (135 loc) · 3.37 KB
/
Program.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
//
// sueda
// October, 2014
//
#include "Program.h"
using namespace std;
Program::Program() :
pid(0),
vShaderName(""),
fShaderName("")
{
}
Program::~Program()
{
}
void Program::setShaderNames(const string &v, const string &f)
{
vShaderName = v;
fShaderName = f;
}
bool Program::init()
{
GLint rc;
hasTex = false;
// Create shader handles
GLuint VS = glCreateShader(GL_VERTEX_SHADER);
GLuint FS = glCreateShader(GL_FRAGMENT_SHADER);
// Read shader sources
const char *vshader = GLSL::textFileRead(vShaderName.c_str());
const char *fshader = GLSL::textFileRead(fShaderName.c_str());
glShaderSource(VS, 1, &vshader, NULL);
glShaderSource(FS, 1, &fshader, NULL);
// Compile vertex shader
glCompileShader(VS);
glGetShaderiv(VS, GL_COMPILE_STATUS, &rc);
GLSL::printShaderInfoLog(VS);
if(!rc) {
printf("Error compiling vertex shader %s\n", vShaderName.c_str());
return false;
}
// Compile fragment shader
glCompileShader(FS);
glGetShaderiv(FS, GL_COMPILE_STATUS, &rc);
GLSL::printShaderInfoLog(FS);
if(!rc) {
printf("Error compiling fragment shader %s\n", fShaderName.c_str());
return false;
}
// Create the program and link
pid = glCreateProgram();
glAttachShader(pid, VS);
glAttachShader(pid, FS);
glLinkProgram(pid);
glGetProgramiv(pid, GL_LINK_STATUS, &rc);
GLSL::printProgramInfoLog(pid);
if(!rc) {
printf("Error linking shaders %s and %s\n", vShaderName.c_str(), fShaderName.c_str());
return false;
}
/*addAttribute("vertPosition");
addAttribute("vertTexCoords");
addUniform("P");
addUniform("MV");
addUniform("scale");
addUniform("color");*/
GLSL::printError();
assert(glGetError() == GL_NO_ERROR);
return true;
}
void Program::bind()
{
glUseProgram(pid);
if (hasTex) {
for(map<string,ParticleTexture*>::iterator it = textures.begin(); it != textures.end(); ++it) {
it->second->bind();
}
}
}
void Program::unbind()
{
if (hasTex) {
for(map<string,ParticleTexture*>::iterator it = textures.begin(); it != textures.end(); ++it) {
it->second->unbind();
}
}
glUseProgram(0);
}
GLint Program::addAttribute(const string &name)
{
attributes[name] = GLSL::getAttribLocation(pid, name.c_str());
return attributes[name];
}
GLint Program::addUniform(const string &name)
{
uniforms[name] = GLSL::getUniformLocation(pid, name.c_str());
return uniforms[name];
}
void Program::addTexture(ParticleTexture *texture)
{
const string &name = texture->getName();
GLint handle = GLSL::getUniformLocation(pid, name.c_str());
texture->setHandle(handle);
textures[name] = texture;
hasTex = true;
}
GLint Program::getAttribute(const string &name) const
{
map<string,GLint>::const_iterator attribute = attributes.find(name.c_str());
if(attribute == attributes.end()) {
cout << name << " is not an attribute variable" << endl;
return 0;
}
return attribute->second;
}
GLint Program::getUniform(const string &name) const
{
map<string,GLint>::const_iterator uniform = uniforms.find(name.c_str());
if(uniform == uniforms.end()) {
cout << name << " is not a uniform variable" << endl;
return 0;
}
return uniform->second;
}
ParticleTexture *Program::getTexture(const string &name) const
{
if (!hasTex) {
return NULL;
}
map<string,ParticleTexture*>::const_iterator texture = textures.find(name.c_str());
if(texture == textures.end()) {
cout << name << " is not a texture" << endl;
return 0;
}
return texture->second;
}