-
Notifications
You must be signed in to change notification settings - Fork 1
/
shader.c
142 lines (122 loc) · 3.98 KB
/
shader.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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "./shader.h"
#include "./utils.h"
void shader_create(Shader** shdr, const char* name, const char* vert, const char* frag) {
Shader* new_shdr = calloc(1, sizeof(Shader));
new_shdr->name = malloc(strlen(name) + 1);
strncpy(new_shdr->name, name, strlen(name));
new_shdr->name[strlen(name)] = 0;
new_shdr->vert_file = malloc(strlen(vert) + 1);
strncpy(new_shdr->vert_file, vert, strlen(vert));
new_shdr->vert_file[strlen(vert)] = 0;
new_shdr->frag_file = malloc(strlen(frag) + 1);
strncpy(new_shdr->frag_file, frag, strlen(frag));
new_shdr->frag_file[strlen(frag)] = 0;
{
char *vert_code = read_file(new_shdr->vert_file);
char *frag_code = read_file(new_shdr->frag_file);
GLuint vertex, fragment;
int success;
char infoLog[512];
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, (const GLchar* const*)&vert_code, NULL);
glCompileShader(vertex);
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
die("ERROR: Failed to Compile %s shader file: %s\n", new_shdr->vert_file, infoLog);
}
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, (const GLchar* const*)&frag_code, NULL);
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
die("ERROR: Failed to Compile %s shader file: %s\n", new_shdr->frag_file, infoLog);
}
new_shdr->program = glCreateProgram();
glAttachShader(new_shdr->program, vertex);
glAttachShader(new_shdr->program, fragment);
glLinkProgram(new_shdr->program);
glGetProgramiv(new_shdr->program, GL_LINK_STATUS, &success);
if (!success)
{
die("Shader Link Failed: %s\n", new_shdr->name);
}
glDeleteShader(vertex);
glDeleteShader(fragment);
free(vert_code);
free(frag_code);
}
new_shdr->next = *shdr;
*shdr = new_shdr;
}
void shader_free(Shader** shdr) {
Shader* next = NULL;
for (; *shdr; *shdr = next) {
next = (*shdr)->next;
free((*shdr)->name);
free((*shdr)->vert_file);
free((*shdr)->frag_file);
free(*shdr);
}
}
void shader_destroy(Shader** shdr, const char* name) {
Shader* temp = *shdr;
Shader* prev;
if (!strncmp(temp->name, name, strlen(name))) {
*shdr = temp->next;
goto ret;
}
while (temp && strncmp(temp->name, name, strlen(name))) {
prev = temp;
temp = temp->next;
}
if (!temp) return;
prev->next = temp->next;
ret:
free(temp->name);
free(temp->vert_file);
free(temp->frag_file);
free(temp);
}
void shader_use(Shader* shdr, const char* name) {
for (; shdr; shdr = shdr->next) {
int len = MIN(strlen(shdr->name), strlen(name));
if (!strncmp(shdr->name, name, len)) {
glUseProgram(shdr->program);
}
}
}
void shader_print_files(Shader* shdr, const char* name) {
for (; shdr; shdr = shdr->next) {
if (!strncmp(shdr->name, name, strlen(name))) {
printf("Vertex Shader: %s\n"
"Fragment Shader: %s\n",
shdr->vert_file, shdr->frag_file);
return;
}
}
printf("Shader not found: %s\n", name);
}
Shader* shader_get(Shader* shdr, const char* name) {
for (; shdr; shdr = shdr->next) {
if (!strncmp(shdr->name, name, strlen(name))) {
return shdr;
}
}
return NULL;
}
GLuint shader_get_program(Shader* shdr, const char* name) {
for (; shdr; shdr = shdr->next) {
if (!strncmp(shdr->name, name, strlen(name))) {
return shdr->program;
}
}
return -1;
}