2 * Utilities for OpenGL shading language
15 #include "shaderutil.h"
17 /** time to compile previous shader */
18 static GLdouble CompileTime
= 0.0;
20 /** time to linke previous program */
21 static GLdouble LinkTime
= 0.0;
25 ShadersSupported(void)
27 const char *version
= (const char *) glGetString(GL_VERSION
);
28 if (version
[0] == '2' && version
[1] == '.') {
31 else if (glutExtensionSupported("GL_ARB_vertex_shader")
32 && glutExtensionSupported("GL_ARB_fragment_shader")
33 && glutExtensionSupported("GL_ARB_shader_objects")) {
34 fprintf(stderr
, "Warning: Trying ARB GLSL instead of OpenGL 2.x. This may not work.\n");
42 CompileShaderText(GLenum shaderType
, const char *text
)
48 shader
= glCreateShader(shaderType
);
49 glShaderSource(shader
, 1, (const GLchar
**) &text
, NULL
);
51 t0
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
52 glCompileShader(shader
);
53 t1
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
55 CompileTime
= t1
- t0
;
57 glGetShaderiv(shader
, GL_COMPILE_STATUS
, &stat
);
61 glGetShaderInfoLog(shader
, 1000, &len
, log
);
62 fprintf(stderr
, "Error: problem compiling shader: %s\n", log
);
66 /*printf("Shader compiled OK\n");*/
73 * Read a shader from a file.
76 CompileShaderFile(GLenum shaderType
, const char *filename
)
78 const int max
= 100*1000;
80 char *buffer
= (char*) malloc(max
);
84 f
= fopen(filename
, "r");
86 fprintf(stderr
, "Unable to open shader file %s\n", filename
);
90 n
= fread(buffer
, 1, max
, f
);
91 /*printf("read %d bytes from shader file %s\n", n, filename);*/
94 shader
= CompileShaderText(shaderType
, buffer
);
108 LinkShaders(GLuint vertShader
, GLuint fragShader
)
110 GLuint program
= glCreateProgram();
113 assert(vertShader
|| fragShader
);
116 glAttachShader(program
, fragShader
);
118 glAttachShader(program
, vertShader
);
120 t0
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
121 glLinkProgram(program
);
122 t1
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
129 glGetProgramiv(program
, GL_LINK_STATUS
, &stat
);
133 glGetProgramInfoLog(program
, 1000, &len
, log
);
134 fprintf(stderr
, "Shader link error:\n%s\n", log
);
144 ValidateShaderProgram(GLuint program
)
147 glValidateProgramARB(program
);
148 glGetProgramiv(program
, GL_VALIDATE_STATUS
, &stat
);
153 glGetProgramInfoLog(program
, 1000, &len
, log
);
154 fprintf(stderr
, "Program validation error:\n%s\n", log
);
158 return (GLboolean
) stat
;
163 GetShaderCompileTime(void)
170 GetShaderLinkTime(void)
177 SetUniformValues(GLuint program
, struct uniform_info uniforms
[])
181 for (i
= 0; uniforms
[i
].name
; i
++) {
183 = glGetUniformLocation(program
, uniforms
[i
].name
);
185 switch (uniforms
[i
].type
) {
190 case GL_SAMPLER_CUBE
:
191 case GL_SAMPLER_2D_RECT_ARB
:
192 assert(uniforms
[i
].value
[0] >= 0.0F
);
193 glUniform1i(uniforms
[i
].location
,
194 (GLint
) uniforms
[i
].value
[0]);
197 glUniform1fv(uniforms
[i
].location
, 1, uniforms
[i
].value
);
200 glUniform2fv(uniforms
[i
].location
, 1, uniforms
[i
].value
);
203 glUniform3fv(uniforms
[i
].location
, 1, uniforms
[i
].value
);
206 glUniform4fv(uniforms
[i
].location
, 1, uniforms
[i
].value
);
209 if (strncmp(uniforms
[i
].name
, "gl_", 3) == 0) {
210 /* built-in uniform: ignore */
214 "Unexpected uniform data type in SetUniformValues\n");
222 /** Get list of uniforms used in the program */
224 GetUniforms(GLuint program
, struct uniform_info uniforms
[])
228 glGetProgramiv(program
, GL_ACTIVE_UNIFORMS
, &n
);
229 glGetProgramiv(program
, GL_ACTIVE_UNIFORM_MAX_LENGTH
, &max
);
231 for (i
= 0; i
< n
; i
++) {
236 glGetActiveUniform(program
, i
, 100, &len
, &size
, &type
, name
);
238 uniforms
[i
].name
= strdup(name
);
239 uniforms
[i
].size
= size
;
240 uniforms
[i
].type
= type
;
241 uniforms
[i
].location
= glGetUniformLocation(program
, name
);
244 uniforms
[i
].name
= NULL
; /* end of list */
251 PrintUniforms(const struct uniform_info uniforms
[])
255 printf("Uniforms:\n");
257 for (i
= 0; uniforms
[i
].name
; i
++) {
258 printf(" %d: %s size=%d type=0x%x loc=%d value=%g, %g, %g, %g\n",
263 uniforms
[i
].location
,
264 uniforms
[i
].value
[0],
265 uniforms
[i
].value
[1],
266 uniforms
[i
].value
[2],
267 uniforms
[i
].value
[3]);
272 /** Get list of attribs used in the program */
274 GetAttribs(GLuint program
, struct attrib_info attribs
[])
278 glGetProgramiv(program
, GL_ACTIVE_ATTRIBUTES
, &n
);
279 glGetProgramiv(program
, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
, &max
);
281 for (i
= 0; i
< n
; i
++) {
286 glGetActiveAttrib(program
, i
, 100, &len
, &size
, &type
, name
);
288 attribs
[i
].name
= strdup(name
);
289 attribs
[i
].size
= size
;
290 attribs
[i
].type
= type
;
291 attribs
[i
].location
= glGetAttribLocation(program
, name
);
294 attribs
[i
].name
= NULL
; /* end of list */
301 PrintAttribs(const struct attrib_info attribs
[])
305 printf("Attribs:\n");
307 for (i
= 0; attribs
[i
].name
; i
++) {
308 printf(" %d: %s size=%d type=0x%x loc=%d\n",
313 attribs
[i
].location
);