2 * Utilities for OpenGL shading language
13 #include "shaderutil.h"
19 static GLboolean firstCall
= GL_TRUE
;
28 ShadersSupported(void)
30 const char *version
= (const char *) glGetString(GL_VERSION
);
31 if (version
[0] == '2' && version
[1] == '.') {
34 else if (glutExtensionSupported("GL_ARB_vertex_shader")
35 && glutExtensionSupported("GL_ARB_fragment_shader")
36 && glutExtensionSupported("GL_ARB_shader_objects")) {
37 fprintf(stderr
, "Warning: Trying ARB GLSL instead of OpenGL 2.x. This may not work.\n");
45 CompileShaderText(GLenum shaderType
, const char *text
)
52 shader
= glCreateShader_func(shaderType
);
53 glShaderSource_func(shader
, 1, (const GLchar
**) &text
, NULL
);
54 glCompileShader_func(shader
);
55 glGetShaderiv_func(shader
, GL_COMPILE_STATUS
, &stat
);
59 glGetShaderInfoLog_func(shader
, 1000, &len
, log
);
60 fprintf(stderr
, "Error: problem compiling shader: %s\n", log
);
64 /*printf("Shader compiled OK\n");*/
71 * Read a shader from a file.
74 CompileShaderFile(GLenum shaderType
, const char *filename
)
76 const int max
= 100*1000;
78 char *buffer
= (char*) malloc(max
);
81 FILE *f
= fopen(filename
, "r");
83 fprintf(stderr
, "Unable to open shader file %s\n", filename
);
87 n
= fread(buffer
, 1, max
, f
);
88 /*printf("read %d bytes from shader file %s\n", n, filename);*/
91 shader
= CompileShaderText(shaderType
, buffer
);
105 LinkShaders(GLuint vertShader
, GLuint fragShader
)
107 GLuint program
= glCreateProgram_func();
109 glAttachShader_func(program
, fragShader
);
110 glAttachShader_func(program
, vertShader
);
111 glLinkProgram_func(program
);
116 glGetProgramiv_func(program
, GL_LINK_STATUS
, &stat
);
120 glGetProgramInfoLog_func(program
, 1000, &len
, log
);
121 fprintf(stderr
, "Shader link error:\n%s\n", log
);
131 InitUniforms(GLuint program
, struct uniform_info uniforms
[])
135 for (i
= 0; uniforms
[i
].name
; i
++) {
137 = glGetUniformLocation_func(program
, uniforms
[i
].name
);
139 printf("Uniform %s location: %d\n", uniforms
[i
].name
,
140 uniforms
[i
].location
);
142 switch (uniforms
[i
].size
) {
144 if (uniforms
[i
].type
== GL_INT
)
145 glUniform1i_func(uniforms
[i
].location
,
146 (GLint
) uniforms
[i
].value
[0]);
148 glUniform1fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
151 glUniform2fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
154 glUniform3fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
157 glUniform4fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);