rev |
line source |
nuclear@35
|
1 #include "opengl.h"
|
elene@22
|
2
|
elene@22
|
3 #include <errno.h>
|
elene@22
|
4 #include <stdio.h>
|
elene@22
|
5 #include <stdlib.h>
|
elene@22
|
6 #include <string.h>
|
elene@22
|
7
|
elene@22
|
8 #include "sdr.h"
|
elene@22
|
9
|
elene@22
|
10 char* sdr_load(const char* fname)
|
elene@22
|
11 {
|
elene@22
|
12 if(!fname) {
|
elene@22
|
13 return 0;
|
elene@22
|
14 }
|
elene@22
|
15
|
elene@22
|
16 FILE *fp;
|
elene@22
|
17 int ctr = 0;
|
elene@22
|
18
|
elene@22
|
19 if(!(fp = fopen(fname, "rb"))) {
|
elene@22
|
20 fprintf(stderr, "Failed to open file %s: %s\n", fname, strerror(errno));
|
elene@22
|
21 return 0;
|
elene@22
|
22 }
|
elene@22
|
23 fseek(fp, 0, SEEK_END);
|
elene@22
|
24 ctr = ftell(fp);
|
elene@22
|
25 rewind(fp);
|
elene@22
|
26
|
elene@22
|
27 char *glsl;
|
elene@22
|
28 if(!(glsl = (char*) malloc(ctr + 1))) {
|
elene@22
|
29 perror("Failed to allocate memory");
|
elene@22
|
30 return 0;
|
elene@22
|
31 }
|
elene@22
|
32 fread(glsl, 1, ctr, fp);
|
elene@22
|
33 fclose(fp);
|
elene@22
|
34
|
elene@22
|
35 glsl[ctr] = 0;
|
elene@22
|
36
|
elene@22
|
37 return glsl;
|
elene@22
|
38 }
|
elene@22
|
39
|
elene@22
|
40 bool sdr_compile(unsigned int sdr)
|
elene@22
|
41 {
|
elene@22
|
42 int status, loglen;
|
elene@22
|
43 char infolog[512];
|
elene@22
|
44
|
elene@22
|
45 glCompileShader(sdr);
|
elene@22
|
46 glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
|
elene@22
|
47 glGetShaderInfoLog(sdr, sizeof infolog, &loglen, infolog);
|
elene@22
|
48 if(status == GL_FALSE) {
|
elene@22
|
49 fprintf(stderr, "Failed to compile shader: %s\n", infolog);
|
elene@22
|
50 return false;
|
elene@22
|
51 } else if(loglen) {
|
elene@22
|
52 fprintf(stderr, "%s\n", infolog);
|
elene@22
|
53 }
|
elene@22
|
54 return true;
|
elene@22
|
55 }
|
elene@22
|
56
|
elene@22
|
57 unsigned int sdr_getprog(const char* vfname, const char* ffname)
|
elene@22
|
58 {
|
elene@22
|
59 char *vsdr_glsl = NULL;
|
elene@22
|
60 char *fsdr_glsl = NULL;
|
elene@22
|
61
|
elene@22
|
62 GLuint vsdr; GLuint fsdr;
|
elene@22
|
63 vsdr = glCreateShader(GL_VERTEX_SHADER);
|
elene@22
|
64 fsdr = glCreateShader(GL_FRAGMENT_SHADER);
|
elene@22
|
65
|
elene@22
|
66 if(!(vsdr_glsl = sdr_load(vfname)))
|
elene@22
|
67 return 0;
|
elene@22
|
68 if(!(fsdr_glsl = sdr_load(ffname)))
|
elene@22
|
69 return 0;
|
elene@22
|
70
|
elene@22
|
71 glShaderSource(vsdr, 1, (const GLchar**)&vsdr_glsl, NULL);
|
elene@22
|
72 glShaderSource(fsdr, 1, (const GLchar**)&fsdr_glsl, NULL);
|
elene@22
|
73
|
elene@22
|
74 free(vsdr_glsl);
|
elene@22
|
75 free(fsdr_glsl);
|
elene@22
|
76
|
elene@22
|
77 if(!sdr_compile(vsdr)) {
|
elene@22
|
78 fprintf(stderr, "Shader %s failed to compile.\n", vfname);
|
elene@22
|
79 return 0;
|
elene@22
|
80 }
|
elene@22
|
81 if(!sdr_compile(fsdr)) {
|
elene@22
|
82 fprintf(stderr, "Shader %s failed to compile.\n", ffname);
|
elene@22
|
83 return 0;
|
elene@22
|
84 }
|
elene@22
|
85
|
elene@22
|
86 unsigned int sprog;
|
elene@22
|
87 int status, loglen;
|
elene@22
|
88 char infolog[512];
|
elene@22
|
89
|
elene@22
|
90 sprog = glCreateProgram();
|
elene@22
|
91 glAttachShader(sprog, vsdr);
|
elene@22
|
92 glAttachShader(sprog, fsdr);
|
elene@22
|
93 glLinkProgram(sprog);
|
elene@22
|
94 glGetProgramiv(sprog, GL_LINK_STATUS, &status);
|
elene@22
|
95 glGetProgramInfoLog(sprog, sizeof infolog, &loglen, infolog);
|
elene@22
|
96 if(status == GL_FALSE) {
|
elene@22
|
97 fprintf(stderr, "Error while linking shader program:\n%s\n", infolog);
|
elene@22
|
98 return 0;
|
elene@22
|
99 } else if(loglen) {
|
elene@22
|
100 fprintf(stderr, "%s\n", infolog);
|
elene@22
|
101 }
|
elene@22
|
102 return sprog;
|
elene@22
|
103 }
|