-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.cpp
145 lines (122 loc) · 3.65 KB
/
Shader.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
#define GL_GLEXT_PROTOTYPES
#include "Shader.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <GL/glu.h>
char* Shader::shaderToString(const char* filename){
FILE* input = fopen(filename,"r");
fseek(input,0,SEEK_END);
long size = ftell(input);
fseek(input,0,SEEK_SET);
char* content = (char*) malloc((size_t) size+1);
fread(content, 1, (size_t) size, input);
fclose(input);
content[size]='\0';
return content;
}
/**
* Display compilation errors from the OpenGL shader compiler
*/
void Shader::print_log(GLuint object)
{
GLint log_length = 0;
if (glIsShader(object))
glGetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length);
else if (glIsProgram(object))
glGetProgramiv(object, GL_INFO_LOG_LENGTH, &log_length);
else {
fprintf(stderr, "printlog: Not a shader or a program\n");
return;
}
char* log = (char*)malloc(log_length);
if (glIsShader(object))
glGetShaderInfoLog(object, log_length, NULL, log);
else if (glIsProgram(object))
glGetProgramInfoLog(object, log_length, NULL, log);
fprintf(stderr, "%s", log);
free(log);
}
GLuint Shader::createShader(const char* filename, GLenum type){
const char* source =shaderToString(filename);
GLuint shaderID = glCreateShader(type);
const GLchar* sources[2] = {
#ifdef GL_ES_VERSION_2_0
"#version 100\n"
"#define GLES2\n",
#else
"#version 120\n",
#endif
source };
glShaderSource(shaderID,2,sources,NULL);
free((void*)source);
glCompileShader(shaderID);
//error?
GLint compile_ok = GL_FALSE;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compile_ok);
if (compile_ok == GL_FALSE) {
fprintf(stderr, "%s:", filename);
print_log(shaderID);
glDeleteShader(shaderID);
return 0;
}
//
return shaderID;
}
Shader::Shader(const char *vs, const char *fs){
vertex = createShader(vs,GL_VERTEX_SHADER);
fragment = createShader(fs,GL_FRAGMENT_SHADER);
GLint link_ok = GL_FALSE;
program = glCreateProgram();
glAttachShader(program, vertex);
glAttachShader(program, fragment);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
printf("glLinkProgram:\n");
}
}
char* readWord(char* list, int n){
int i;
int count=1;
for(i=0;count<n;i++){
if(list[i]==' ') count++;
}
int length=1;
while(list[i+length]!=' ' && list[i+length]!='\0') length++;
char * word= new char[length+1];
int j;
for(j=0;j<length;j++) word[j]=list[i+j];
word[length]='\0';
return word;
}
void Shader::getProperties(char* list, int attributeNum, int uniformNum){
attributeNumber=attributeNum;
int i;
for(i=0;i<attributeNum;i++){
std::string attribute_name(readWord(list,i+1));
attributes[attribute_name] = glGetAttribLocation(program, attribute_name.data());
if (attributes[attribute_name] == -1) {
fprintf(stderr, "Could not find attribute %s\n", attribute_name.data());
}
}
for(i=0;i<uniformNum;i++){
std::string uniform_name(readWord(list,attributeNum+i+1));
uniforms[uniform_name] = glGetUniformLocation(program, uniform_name.data());
if (uniforms[uniform_name] == -1) {
fprintf(stderr, "Could not find uniform %s\n", uniform_name.data());
}
}
}
void Shader::enableAttributes(){
for(auto &kv: attributes) glEnableVertexAttribArray(kv.second);
}
void Shader::enableAttributes(std::string attributeName){
glEnableVertexAttribArray(attributes[attributeName]);
}
void Shader::disableAttributes(){
for(auto &kv: attributes) glDisableVertexAttribArray(kv.second);
}
void Shader::disableAttributes(std::string attributeName){
glDisableVertexAttribArray(attributes[attributeName]);
}