Changed the OpenGL part and the GLSL shaders to use UBO and
[demo] / src / opengl / shader-gl.cc
1 #include <GL/glew.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "opengl/shader-gl.h"
6
7 ShaderGL::ShaderGL()
8 {
9         sdr = 0;
10 }
11
12 ShaderGL::~ShaderGL()
13 {
14         destroy();
15 }
16
17 bool ShaderGL::create(char *buf, unsigned int bsz, const char *fname)
18 {
19         /* find shader type and create shader */
20         unsigned int stype;
21         switch(type) {
22         case SDR_VERTEX:
23                 stype = GL_VERTEX_SHADER;
24                 break;
25         case SDR_FRAGMENT:
26                 stype = GL_FRAGMENT_SHADER;
27                 break;
28         default:
29                 fprintf(stderr, "Unknown shader type.\n");
30                 return false;
31         }
32         name = std::string(fname);
33         sdr = glCreateShader(stype);
34
35         /* compile */
36         glShaderSource(sdr, 1, (const char **)&buf, 0);
37         glCompileShader(sdr);
38
39         delete [] buf;
40
41         /* check compile status */
42         int status;
43         glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
44         if(status)
45                 printf("Successfully compiled shader: %s\n", fname);
46         else
47                 fprintf(stderr, "Failed to compile %s shader.\n", fname);
48
49         /* print the info log */
50         int loglen;
51         glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &loglen);
52         if(loglen > 0 && (buf = new char[loglen + 1])) {
53                 glGetShaderInfoLog(sdr, loglen, 0, buf);
54                 buf[loglen] = 0;
55                 printf("%s\n", buf);
56
57                 delete [] buf;
58         }
59
60         if(!status) {
61                 destroy();
62                 return false;
63         }
64
65         return true;
66 }
67
68 void ShaderGL::destroy()
69 {
70         if(sdr) {
71                 glDeleteShader(sdr);
72         }
73         sdr = 0;
74         type = SDR_UNKNOWN;
75 }
76
77 /* Shader Program */
78
79 ShaderProgramGL::ShaderProgramGL()
80 {
81         prog = 0;
82         memset(shaders, 0, sizeof shaders / sizeof *shaders);
83
84         is_linked = false;
85 }
86
87 ShaderProgramGL::~ShaderProgramGL()
88 {
89         destroy();
90 }
91
92 bool ShaderProgramGL::create()
93 {
94         prog = glCreateProgram();
95         if(!prog) {
96                 fprintf(stderr, "Failed to create shader program.\n");
97                 return false;
98         }
99         return true;
100 }
101
102 bool ShaderProgramGL::link()
103 {
104         if(is_linked)
105                 return true;
106
107         glLinkProgram(prog);
108
109         int status;
110         glGetProgramiv(prog, GL_LINK_STATUS, &status);
111         if(status) {
112                 printf("Successfully linked shader program.\n");
113                 is_linked = true;
114         }
115         else
116                 printf("Failed to link shader program.\n");
117
118         int loglen;
119         glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &loglen);
120
121         char *buf;
122         if(loglen > 0 && (buf = new char[loglen + 1])) {
123                 glGetProgramInfoLog(prog, loglen, 0, buf);
124                 buf[loglen] = 0;
125                 printf("%s\n", buf);
126                 delete [] buf;
127         }
128
129         return status ? true : false;
130 }
131
132 bool ShaderProgramGL::use() const
133 {
134         if(!is_linked) {
135                 return false;
136         }
137
138         if(!prog) {
139                 return false;
140         }
141
142         glUseProgram(prog);
143         return true;
144 }
145
146 void ShaderProgramGL::destroy()
147 {
148         glDeleteProgram(prog);
149         prog = 0;
150         is_linked = false;
151 }
152
153 void ShaderProgramGL::attach_shader(Shader *shader)
154 {
155         glAttachShader(prog, ((ShaderGL *)shader)->sdr);
156         is_linked = false;
157 }