-
Notifications
You must be signed in to change notification settings - Fork 1
/
cshader.c
159 lines (124 loc) · 4.66 KB
/
cshader.c
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
#include "cshader.h"
Shader ShaderInit(){
Shader shader;
shader.Load = &ShaderLoad;
shader.Destroy = &ShaderDestroy;
return shader;
}
void ShaderLoad(Shader *shader, const char *vertex_file_path, const char *fragment_file_path){
/* Load Vertex Shader File */
FILE *VertexShaderStream;
if( (VertexShaderStream = fopen(vertex_file_path, "r")) == NULL ){
printf("Shaders: Unable to open %s\n", vertex_file_path);
printf("Shaders: Terminating...\n");
}
FILE *VertexErrorLog;
if( (VertexErrorLog = fopen("shader.log", "w")) == NULL ){
printf("Shaders: Unable to open %s\n", "shader.log");
printf("Shaders: Terminating...\n");
}
fseek(VertexShaderStream, 0, SEEK_END); /* set pointer at the end of file */
long fsize = ftell(VertexShaderStream); /* get file size */
rewind(VertexShaderStream); /* set pointer and the beginning of a file */
int vlength = fsize;
shader->vertexcode = malloc(fsize + 1);
shader->vertexcode[fsize] = 0;
int i = 0;
printf("Shaders: Reading shader...\n");
while(true){
if( (shader->vertexcode[i] = fgetc(VertexShaderStream)) == EOF ){
break;
}
i++;
}
shader->vertexcode[i] = 0;
fclose(VertexShaderStream);
/* Same thing for Fragment Shader */
FILE *FragmentShaderStream;
if( (FragmentShaderStream = fopen(fragment_file_path, "r")) == NULL ){
printf("Shaders: Cannot open %s. \n Terminating...\n", fragment_file_path);
fclose(FragmentShaderStream);
}
fseek(FragmentShaderStream, 0, SEEK_END);
fsize = ftell(FragmentShaderStream);
rewind(FragmentShaderStream);
int flength = fsize;
shader->fragmentcode = malloc(fsize + 1);
i = 0;
while(true){
if( (shader->fragmentcode[i] = fgetc(FragmentShaderStream)) == EOF ){
break;
}
i++;
}
shader->fragmentcode[i] = 0;
fclose(FragmentShaderStream);
/* Creating shader objects */
if( (shader->vertex = glCreateShader(GL_VERTEX_SHADER)) == 0 ){
printf("Shaders: Error creating vertex shader object!\n");
}
if( (shader->fragment = glCreateShader(GL_FRAGMENT_SHADER)) == 0 ){
printf("Shaders: Error creating fragment shader object!\n");
}
const char *VertexShaderPointer = shader->vertexcode;
const char *FragmentShaderPointer = shader->fragmentcode;
/* Attaching Shaders */
glShaderSource(shader->vertex, 1, &VertexShaderPointer, &vlength);
glShaderSource(shader->fragment, 1, &FragmentShaderPointer, &flength);
/* Compiling Shaders */
printf("Shaders: Compiling...\n");
glCompileShader(shader->vertex);
glCompileShader(shader->fragment);
/* Error checking for compilation */
printf("Shaders: Error checking...\n");
GLint VertexShaderLogLength, vlen, FragmentShaderLogLength, flen;
glGetShaderiv(shader->vertex, GL_INFO_LOG_LENGTH, &VertexShaderLogLength);
glGetShaderiv(shader->fragment, GL_INFO_LOG_LENGTH, &FragmentShaderLogLength);
if( VertexShaderLogLength > 1 ){
GLchar *VertexCompilerLog = (GLchar*)malloc(VertexShaderLogLength * 2);
glGetShaderInfoLog(shader->vertex, VertexShaderLogLength, &vlen, VertexCompilerLog);
printf("Shaders: Vertex shader error! Check shader.log for more info.\n");
fprintf(VertexErrorLog, "%s", VertexCompilerLog);
free(VertexCompilerLog);
}
if( FragmentShaderLogLength > 1){
GLchar *FragmentCompilerLog = (GLchar*)malloc(FragmentShaderLogLength * 2);
glGetShaderInfoLog(shader->fragment, FragmentShaderLogLength, &flen, FragmentCompilerLog);
printf("Shaders: Fragment shader error! Check shader.log for more info. \n");
fprintf(VertexErrorLog, "%s", FragmentCompilerLog);
free(FragmentCompilerLog);
}
/* Link the program */
printf("Shaders: Linking...\n");
if( (shader->program = glCreateProgram()) == 0 ){
printf("Shaders: Creating program object failed.\n");
}
glAttachShader(shader->program, shader->vertex);
glAttachShader(shader->program, shader->fragment);
glLinkProgram(shader->program);
/* Error checking for linking */
GLint linked, ProgramLogLength, plen;
glGetProgramiv(shader->program, GL_LINK_STATUS, &linked);
if(linked == GL_TRUE){
/* ok */
}else{
printf("Shaders: Shader program linking error.\n");
glGetProgramiv(shader->program, GL_INFO_LOG_LENGTH, &ProgramLogLength);
GLchar *ProgramCompilerLog = (GLchar*)malloc(sizeof(ProgramLogLength));
glGetProgramInfoLog(shader->program, ProgramLogLength, &plen, ProgramCompilerLog);
fprintf(VertexErrorLog, "%s", ProgramCompilerLog);
free(ProgramCompilerLog);
}
free(shader->vertexcode);
free(shader->fragmentcode);
fclose(VertexErrorLog);
}
void ShaderDestroy(Shader *shader){
glDetachShader(shader->vertex, shader->program);
glDetachShader(shader->fragment, shader->program);
glDeleteShader(shader->vertex);
glDeleteShader(shader->fragment);
glDeleteProgram(shader->program);
shader->Load = NULL;
shader->Destroy = NULL;
}