2 * Utilities for OpenGL shading language
14 #include "shaderutil.h"
20 static GLboolean firstCall
= GL_TRUE
;
29 ShadersSupported(void)
31 const char *version
= (const char *) glGetString(GL_VERSION
);
32 if (version
[0] == '2' && version
[1] == '.') {
35 else if (glutExtensionSupported("GL_ARB_vertex_shader")
36 && glutExtensionSupported("GL_ARB_fragment_shader")
37 && glutExtensionSupported("GL_ARB_shader_objects")) {
38 fprintf(stderr
, "Warning: Trying ARB GLSL instead of OpenGL 2.x. This may not work.\n");
46 CompileShaderText(GLenum shaderType
, const char *text
)
53 shader
= glCreateShader_func(shaderType
);
54 glShaderSource_func(shader
, 1, (const GLchar
**) &text
, NULL
);
55 glCompileShader_func(shader
);
56 glGetShaderiv_func(shader
, GL_COMPILE_STATUS
, &stat
);
60 glGetShaderInfoLog_func(shader
, 1000, &len
, log
);
61 fprintf(stderr
, "Error: problem compiling shader: %s\n", log
);
65 /*printf("Shader compiled OK\n");*/
72 * Read a shader from a file.
75 CompileShaderFile(GLenum shaderType
, const char *filename
)
77 const int max
= 100*1000;
79 char *buffer
= (char*) malloc(max
);
82 FILE *f
= fopen(filename
, "r");
84 fprintf(stderr
, "Unable to open shader file %s\n", filename
);
88 n
= fread(buffer
, 1, max
, f
);
89 /*printf("read %d bytes from shader file %s\n", n, filename);*/
92 shader
= CompileShaderText(shaderType
, buffer
);
106 LinkShaders(GLuint vertShader
, GLuint fragShader
)
108 GLuint program
= glCreateProgram_func();
110 assert(vertShader
|| fragShader
);
113 glAttachShader_func(program
, fragShader
);
115 glAttachShader_func(program
, vertShader
);
116 glLinkProgram_func(program
);
121 glGetProgramiv_func(program
, GL_LINK_STATUS
, &stat
);
125 glGetProgramInfoLog_func(program
, 1000, &len
, log
);
126 fprintf(stderr
, "Shader link error:\n%s\n", log
);
136 InitUniforms(GLuint program
, struct uniform_info uniforms
[])
140 for (i
= 0; uniforms
[i
].name
; i
++) {
142 = glGetUniformLocation_func(program
, uniforms
[i
].name
);
144 printf("Uniform %s location: %d\n", uniforms
[i
].name
,
145 uniforms
[i
].location
);
147 switch (uniforms
[i
].size
) {
149 if (uniforms
[i
].type
== GL_INT
)
150 glUniform1i_func(uniforms
[i
].location
,
151 (GLint
) uniforms
[i
].value
[0]);
153 glUniform1fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
156 glUniform2fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
159 glUniform3fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
162 glUniform4fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);