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");
37 fprintf(stderr
, "Sorry, GLSL not supported with this OpenGL.\n");
43 CompileShaderText(GLenum shaderType
, const char *text
)
49 shader
= glCreateShader(shaderType
);
50 glShaderSource(shader
, 1, (const GLchar
**) &text
, NULL
);
52 t0
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
53 glCompileShader(shader
);
54 t1
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
56 CompileTime
= t1
- t0
;
58 glGetShaderiv(shader
, GL_COMPILE_STATUS
, &stat
);
62 glGetShaderInfoLog(shader
, 1000, &len
, log
);
63 fprintf(stderr
, "Error: problem compiling shader: %s\n", log
);
67 /*printf("Shader compiled OK\n");*/
74 * Read a shader from a file.
77 CompileShaderFile(GLenum shaderType
, const char *filename
)
79 const int max
= 100*1000;
81 char *buffer
= (char*) malloc(max
);
85 f
= fopen(filename
, "r");
87 fprintf(stderr
, "Unable to open shader file %s\n", filename
);
92 n
= fread(buffer
, 1, max
, f
);
93 /*printf("read %d bytes from shader file %s\n", n, filename);*/
96 shader
= CompileShaderText(shaderType
, buffer
);
112 LinkShaders(GLuint vertShader
, GLuint fragShader
)
114 GLuint program
= glCreateProgram();
117 assert(vertShader
|| fragShader
);
120 glAttachShader(program
, fragShader
);
122 glAttachShader(program
, vertShader
);
124 t0
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
125 glLinkProgram(program
);
126 t1
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
133 glGetProgramiv(program
, GL_LINK_STATUS
, &stat
);
137 glGetProgramInfoLog(program
, 1000, &len
, log
);
138 fprintf(stderr
, "Shader link error:\n%s\n", log
);
148 ValidateShaderProgram(GLuint program
)
151 glValidateProgramARB(program
);
152 glGetProgramiv(program
, GL_VALIDATE_STATUS
, &stat
);
157 glGetProgramInfoLog(program
, 1000, &len
, log
);
158 fprintf(stderr
, "Program validation error:\n%s\n", log
);
162 return (GLboolean
) stat
;
167 GetShaderCompileTime(void)
174 GetShaderLinkTime(void)
181 SetUniformValues(GLuint program
, struct uniform_info uniforms
[])
185 for (i
= 0; uniforms
[i
].name
; i
++) {
187 = glGetUniformLocation(program
, uniforms
[i
].name
);
189 switch (uniforms
[i
].type
) {
194 case GL_SAMPLER_CUBE
:
195 case GL_SAMPLER_2D_RECT_ARB
:
196 assert(uniforms
[i
].value
[0] >= 0.0F
);
197 glUniform1i(uniforms
[i
].location
,
198 (GLint
) uniforms
[i
].value
[0]);
201 glUniform1fv(uniforms
[i
].location
, 1, uniforms
[i
].value
);
204 glUniform2fv(uniforms
[i
].location
, 1, uniforms
[i
].value
);
207 glUniform3fv(uniforms
[i
].location
, 1, uniforms
[i
].value
);
210 glUniform4fv(uniforms
[i
].location
, 1, uniforms
[i
].value
);
213 if (strncmp(uniforms
[i
].name
, "gl_", 3) == 0) {
214 /* built-in uniform: ignore */
218 "Unexpected uniform data type in SetUniformValues\n");
226 /** Get list of uniforms used in the program */
228 GetUniforms(GLuint program
, struct uniform_info uniforms
[])
232 glGetProgramiv(program
, GL_ACTIVE_UNIFORMS
, &n
);
233 glGetProgramiv(program
, GL_ACTIVE_UNIFORM_MAX_LENGTH
, &max
);
235 for (i
= 0; i
< n
; i
++) {
240 glGetActiveUniform(program
, i
, 100, &len
, &size
, &type
, name
);
242 uniforms
[i
].name
= strdup(name
);
243 uniforms
[i
].size
= size
;
244 uniforms
[i
].type
= type
;
245 uniforms
[i
].location
= glGetUniformLocation(program
, name
);
248 uniforms
[i
].name
= NULL
; /* end of list */
255 PrintUniforms(const struct uniform_info uniforms
[])
259 printf("Uniforms:\n");
261 for (i
= 0; uniforms
[i
].name
; i
++) {
262 printf(" %d: %s size=%d type=0x%x loc=%d value=%g, %g, %g, %g\n",
267 uniforms
[i
].location
,
268 uniforms
[i
].value
[0],
269 uniforms
[i
].value
[1],
270 uniforms
[i
].value
[2],
271 uniforms
[i
].value
[3]);
276 /** Get list of attribs used in the program */
278 GetAttribs(GLuint program
, struct attrib_info attribs
[])
282 glGetProgramiv(program
, GL_ACTIVE_ATTRIBUTES
, &n
);
283 glGetProgramiv(program
, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
, &max
);
285 for (i
= 0; i
< n
; i
++) {
290 glGetActiveAttrib(program
, i
, 100, &len
, &size
, &type
, name
);
292 attribs
[i
].name
= strdup(name
);
293 attribs
[i
].size
= size
;
294 attribs
[i
].type
= type
;
295 attribs
[i
].location
= glGetAttribLocation(program
, name
);
298 attribs
[i
].name
= NULL
; /* end of list */
305 PrintAttribs(const struct attrib_info attribs
[])
309 printf("Attribs:\n");
311 for (i
= 0; attribs
[i
].name
; i
++) {
312 printf(" %d: %s size=%d type=0x%x loc=%d\n",
317 attribs
[i
].location
);