-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader_management.c
131 lines (97 loc) · 3.04 KB
/
shader_management.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "shader_management.h"
void print_shader_info_log(GLuint object);
void print_program_info_log(GLuint object);
//Reads a file into a string, returns the contents.
char* read_file(char* file_name){
char* result;
FILE* file = fopen(file_name, "r");
int count;
if(file == NULL){
printf("Error: File '%s' unsuccessfully opened.\n", file_name);
return NULL;
}
fseek(file, 0, SEEK_END);
count = ftell(file);
rewind(file);
if(count == 0){
printf("Error: file '%s' empty.\n", file_name);
return NULL;
}
result = (char*) malloc(sizeof(char)*(count+1));
count = fread(result, sizeof(char), count, file);
result[count] = '\0';
fclose(file);
return result;
}
//Makes the shader of the given type in the given file, returns its GL
// identifier.
GLuint make_shader(GLenum type, char* file_name){
char* shader_text = read_file(file_name);
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, (const GLchar **)&shader_text, NULL);
glCompileShader(shader);
print_shader_info_log(shader);
free(shader_text);
return shader;
}
//The shader program identifier.
GLuint program;
//Shader attributes.
GLuint vertex_attribute, colour_attribute;
//Uniform attributes.
GLuint projection_attribute, view_attribute, model_attribute;
//Makes a program with the given shaders
GLuint make_program(char* vert_file, char* frag_file){
GLuint vert, frag;
vert = make_shader(GL_VERTEX_SHADER, vert_file);
frag = make_shader(GL_FRAGMENT_SHADER, frag_file);
program = glCreateProgram();
glAttachShader(program, vert);
glAttachShader(program, frag);
glBindFragDataLocation(program, 0, "frag_out");
glLinkProgram(program);
print_program_info_log(program);
//Extract attribute locations
vertex_attribute = glGetAttribLocation(program, "position");
//Extract uniform locations
projection_attribute = glGetUniformLocation(program, "projection");
view_attribute = glGetUniformLocation(program, "view");
// model_attribute = glGetUniformLocation(program, "model");
printf("%d %d\n", projection_attribute, view_attribute);
return program;
}
void activate_program(){
glUseProgram(program);
}
void print_shader_info_log(GLuint object)
{
int infologLength = 0;
int charsWritten = 0;
char *infoLog;
glGetShaderiv(object, GL_INFO_LOG_LENGTH,&infologLength);
if (infologLength > 0)
{
infoLog = (char *)malloc(infologLength);
glGetShaderInfoLog(object, infologLength, &charsWritten, infoLog);
printf("%s\n",infoLog);
free(infoLog);
}
}
void print_program_info_log(GLuint object){
int infologLength = 0;
int charsWritten = 0;
char *infoLog;
glGetProgramiv(object, GL_INFO_LOG_LENGTH,&infologLength);
if (infologLength > 0)
{
infoLog = (char *)malloc(infologLength);
glGetProgramInfoLog(object, infologLength, &charsWritten, infoLog);
printf("%s\n",infoLog);
free(infoLog);
}
}