477209ab45f5d4a41312ae29cb0bbec3002608c6
2 * Utilities for OpenGL shading language
13 #include "shaderutil.h"
19 static GLboolean firstCall
= GL_TRUE
;
28 ShadersSupported(void)
32 version
= (const char *) glGetString(GL_VERSION
);
33 if (version
[0] != '2' || version
[1] != '.') {
34 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
42 CompileShaderText(GLenum shaderType
, const char *text
)
49 shader
= glCreateShader_func(shaderType
);
50 glShaderSource_func(shader
, 1, (const GLchar
**) &text
, NULL
);
51 glCompileShader_func(shader
);
52 glGetShaderiv_func(shader
, GL_COMPILE_STATUS
, &stat
);
56 glGetShaderInfoLog_func(shader
, 1000, &len
, log
);
57 fprintf(stderr
, "Error: problem compiling shader: %s\n", log
);
61 /*printf("Shader compiled OK\n");*/
68 * Read a shader from a file.
71 CompileShaderFile(GLenum shaderType
, const char *filename
)
73 const int max
= 100*1000;
75 char *buffer
= (char*) malloc(max
);
78 FILE *f
= fopen(filename
, "r");
83 n
= fread(buffer
, 1, max
, f
);
84 /*printf("read %d bytes from shader file %s\n", n, filename);*/
87 shader
= CompileShaderText(shaderType
, buffer
);
101 LinkShaders(GLuint vertShader
, GLuint fragShader
)
103 GLuint program
= glCreateProgram_func();
105 glAttachShader_func(program
, fragShader
);
106 glAttachShader_func(program
, vertShader
);
107 glLinkProgram_func(program
);
112 glGetProgramiv_func(program
, GL_LINK_STATUS
, &stat
);
116 glGetProgramInfoLog_func(program
, 1000, &len
, log
);
117 fprintf(stderr
, "Shader link error:\n%s\n", log
);
127 InitUniforms(GLuint program
, struct uniform_info uniforms
[])
131 for (i
= 0; uniforms
[i
].name
; i
++) {
133 = glGetUniformLocation_func(program
, uniforms
[i
].name
);
135 printf("Uniform %s location: %d\n", uniforms
[i
].name
,
136 uniforms
[i
].location
);
138 switch (uniforms
[i
].size
) {
140 if (uniforms
[i
].type
== GL_INT
)
141 glUniform1i_func(uniforms
[i
].location
,
142 (GLint
) uniforms
[i
].value
[0]);
144 glUniform1fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
147 glUniform2fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
150 glUniform3fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
153 glUniform4fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);