-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaders.c
110 lines (86 loc) · 2.15 KB
/
shaders.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
#warning ['TODO']: This isnt used... (Also the OpenGL 1.1 windows uses doesnt support it without extentions).
#if 0
#include "shaders.h"
#include <stdio.h>
#include <stdlib.h>
#include "console.h"
void ShaderProgram_0ShaderProgram(ShaderProgram* sp) {
if(sp->vert) {
glDetachShader(sp->program, sp->vert);
glDeleteShader(sp->vert);
}
if(sp->frag) {
glDetachShader(sp->program, sp->frag);
glDeleteShader(sp->frag);
}
if(sp->program) {
glDeleteProgram(sp->program);
}
free(sp);
}
static const ClassFunctions ShaderProgram_c = {
(void*)ShaderProgram_0ShaderProgram
};
ShaderProgram* ShaderProgram_ShaderProgram() {
ALLOCATE(ShaderProgram, sp);
sp->class_ = &ShaderProgram_c;
}
int load_file(const char* filename, char**data) {
int size = 0;
FILE *fp = fopen(filename, "rb");
if(!fp) {
#warning ['TODO']: Print perror message...
ERROR("Failed to open file '%s'...", filename);
return -1;
}
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
*data = (char*)malloc(size+1);
if(!*data) {
#warning ['TODO']: Print perror message...
ERROR("Failed to allocate space for data...");
fclose(fp);
return -2;
}
int bytes = fread(*data, sizeof(char), size, fp);
if(size != bytes) {
#warning ['TODO']: Print perror message...
ERROR("Failed to read data...");
free(*data);
*data = NULL;
fclose(fp);
return -3;
}
fclose(fp);
(*data)[size] = '\0';
return size;
}
GLuint load_shader(char* filename, int type) {
char* source;
int result = load_file(filename, &source);
if(result < 0) {
ERROR("Failed to load shader...");
return 0;
}
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
free(source);
return shader;
}
ShaderProgram* load_phong_shader() {
ShaderProgram* sp = NEW(ShaderProgram);
sp->vert = load_shader("phong.vert", GL_VERTEX_SHADER);
sp->frag = load_shader("phong.frag", GL_FRAGMENT_SHADER);
sp->program = glCreateProgram();
if(!sp->vert || !sp->frag || !sp->program) {
DELETE(sp);
return NULL;
}
glAttachShader(sp->program, sp->vert);
glAttachShader(sp->program, sp->frag);
glLinkProgram(sp->program);
return sp;
}
#endif