2 * Utilities for OpenGL shading language
15 #include "shaderutil.h"
21 static GLboolean firstCall
= GL_TRUE
;
30 ShadersSupported(void)
32 const char *version
= (const char *) glGetString(GL_VERSION
);
33 if (version
[0] == '2' && version
[1] == '.') {
36 else if (glutExtensionSupported("GL_ARB_vertex_shader")
37 && glutExtensionSupported("GL_ARB_fragment_shader")
38 && glutExtensionSupported("GL_ARB_shader_objects")) {
39 fprintf(stderr
, "Warning: Trying ARB GLSL instead of OpenGL 2.x. This may not work.\n");
47 CompileShaderText(GLenum shaderType
, const char *text
)
54 shader
= glCreateShader_func(shaderType
);
55 glShaderSource_func(shader
, 1, (const GLchar
**) &text
, NULL
);
56 glCompileShader_func(shader
);
57 glGetShaderiv_func(shader
, GL_COMPILE_STATUS
, &stat
);
61 glGetShaderInfoLog_func(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
);
87 f
= fopen(filename
, "r");
89 fprintf(stderr
, "Unable to open shader file %s\n", filename
);
93 n
= fread(buffer
, 1, max
, f
);
94 /*printf("read %d bytes from shader file %s\n", n, filename);*/
97 shader
= CompileShaderText(shaderType
, buffer
);
111 LinkShaders(GLuint vertShader
, GLuint fragShader
)
113 GLuint program
= glCreateProgram_func();
115 assert(vertShader
|| fragShader
);
118 glAttachShader_func(program
, fragShader
);
120 glAttachShader_func(program
, vertShader
);
121 glLinkProgram_func(program
);
126 glGetProgramiv_func(program
, GL_LINK_STATUS
, &stat
);
130 glGetProgramInfoLog_func(program
, 1000, &len
, log
);
131 fprintf(stderr
, "Shader link error:\n%s\n", log
);
141 InitUniforms(GLuint program
, struct uniform_info uniforms
[])
145 for (i
= 0; uniforms
[i
].name
; i
++) {
147 = glGetUniformLocation_func(program
, uniforms
[i
].name
);
149 printf("Uniform %s location: %d\n", uniforms
[i
].name
,
150 uniforms
[i
].location
);
152 switch (uniforms
[i
].size
) {
154 if (uniforms
[i
].type
== GL_INT
)
155 glUniform1i_func(uniforms
[i
].location
,
156 (GLint
) uniforms
[i
].value
[0]);
158 glUniform1fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
161 glUniform2fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
164 glUniform3fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
167 glUniform4fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);