[4] | 1 | /****************************************************************************** |
---|
| 2 | * SAGE - Scalable Adaptive Graphics Environment |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2004 Electronic Visualization Laboratory, |
---|
| 5 | * University of Illinois at Chicago |
---|
| 6 | * |
---|
| 7 | * All rights reserved. |
---|
| 8 | * |
---|
| 9 | * Redistribution and use in source and binary forms, with or without |
---|
| 10 | * modification, are permitted provided that the following conditions are met: |
---|
| 11 | * |
---|
| 12 | * * Redistributions of source code must retain the above copyright |
---|
| 13 | * notice, this list of conditions and the following disclaimer. |
---|
| 14 | * * Redistributions in binary form must reproduce the above |
---|
| 15 | * copyright notice, this list of conditions and the following disclaimer |
---|
| 16 | * in the documentation and/or other materials provided with the distribution. |
---|
| 17 | * * Neither the name of the University of Illinois at Chicago nor |
---|
| 18 | * the names of its contributors may be used to endorse or promote |
---|
| 19 | * products derived from this software without specific prior written permission. |
---|
| 20 | * |
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | * |
---|
| 33 | * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/ |
---|
| 34 | * |
---|
| 35 | *****************************************************************************/ |
---|
| 36 | #include "glsl.h" |
---|
| 37 | |
---|
| 38 | #if defined(GLSL_YCOCG) |
---|
| 39 | |
---|
| 40 | GLhandleARB FSHandle,PHandle; |
---|
| 41 | |
---|
| 42 | /*---------------------------------------------------------------------------*/ |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | #define GLSLVertexShader 1 |
---|
| 46 | #define GLSLFragmentShader 2 |
---|
| 47 | |
---|
| 48 | int GLSLprintlError(char *file, int line) |
---|
| 49 | { |
---|
| 50 | // |
---|
| 51 | // Returns 1 if an OpenGL error occurred, 0 otherwise. |
---|
| 52 | // |
---|
| 53 | GLenum glErr; |
---|
| 54 | int retCode = 0; |
---|
| 55 | |
---|
| 56 | glErr = glGetError(); |
---|
| 57 | while (glErr != GL_NO_ERROR) |
---|
| 58 | { |
---|
| 59 | fprintf(stderr, "GLSL> glError in file %s @ line %d: %s\n", file, line, gluErrorString(glErr)); |
---|
| 60 | retCode = 1; |
---|
| 61 | glErr = glGetError(); |
---|
| 62 | } |
---|
| 63 | return retCode; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | // |
---|
| 67 | // Print out the information log for a shader object |
---|
| 68 | // |
---|
| 69 | static |
---|
| 70 | void GLSLprintShaderInfoLog(GLuint shader) |
---|
| 71 | { |
---|
| 72 | GLint infologLength = 0; |
---|
| 73 | GLint charsWritten = 0; |
---|
| 74 | GLchar *infoLog; |
---|
| 75 | |
---|
| 76 | GLSLprintlError(__FILE__, __LINE__); // Check for OpenGL errors |
---|
| 77 | |
---|
| 78 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infologLength); |
---|
| 79 | |
---|
| 80 | GLSLprintlError(__FILE__, __LINE__); // Check for OpenGL errors |
---|
| 81 | |
---|
| 82 | if (infologLength > 0) |
---|
| 83 | { |
---|
| 84 | infoLog = (GLchar *)malloc(infologLength); |
---|
| 85 | if (infoLog == NULL) |
---|
| 86 | { |
---|
| 87 | fprintf(stderr, "GLSL> ERROR: Could not allocate InfoLog buffer\n"); |
---|
| 88 | exit(1); |
---|
| 89 | } |
---|
| 90 | glGetShaderInfoLog(shader, infologLength, &charsWritten, infoLog); |
---|
| 91 | fprintf(stderr, "GLSL> Shader InfoLog:%s\n", infoLog); |
---|
| 92 | free(infoLog); |
---|
| 93 | } |
---|
| 94 | GLSLprintlError(__FILE__, __LINE__); // Check for OpenGL errors |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | // |
---|
| 98 | // Print out the information log for a program object |
---|
| 99 | // |
---|
| 100 | static |
---|
| 101 | void GLSLprintProgramInfoLog(GLuint program) |
---|
| 102 | { |
---|
| 103 | GLint infologLength = 0; |
---|
| 104 | GLint charsWritten = 0; |
---|
| 105 | GLchar *infoLog; |
---|
| 106 | |
---|
| 107 | GLSLprintlError(__FILE__, __LINE__); // Check for OpenGL errors |
---|
| 108 | |
---|
| 109 | glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infologLength); |
---|
| 110 | |
---|
| 111 | GLSLprintlError(__FILE__, __LINE__); // Check for OpenGL errors |
---|
| 112 | |
---|
| 113 | if (infologLength > 0) |
---|
| 114 | { |
---|
| 115 | infoLog = (GLchar *)malloc(infologLength); |
---|
| 116 | if (infoLog == NULL) |
---|
| 117 | { |
---|
| 118 | fprintf(stderr, "GLSL> ERROR: Could not allocate InfoLog buffer\n"); |
---|
| 119 | exit(1); |
---|
| 120 | } |
---|
| 121 | glGetProgramInfoLog(program, infologLength, &charsWritten, infoLog); |
---|
| 122 | fprintf(stderr, "GLSL> Program InfoLog:%s\n", infoLog); |
---|
| 123 | free(infoLog); |
---|
| 124 | } |
---|
| 125 | GLSLprintlError(__FILE__, __LINE__); // Check for OpenGL errors |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | static |
---|
| 129 | int GLSLShaderSize(char *fileName, int shaderType) |
---|
| 130 | { |
---|
| 131 | // |
---|
| 132 | // Returns the size in bytes of the shader fileName. |
---|
| 133 | // If an error occurred, it returns -1. |
---|
| 134 | // |
---|
| 135 | // File name convention: |
---|
| 136 | // |
---|
| 137 | // <fileName>.vert |
---|
| 138 | // <fileName>.frag |
---|
| 139 | // |
---|
| 140 | int fd; |
---|
| 141 | char name[256]; |
---|
| 142 | int count = -1; |
---|
| 143 | |
---|
| 144 | memset(name, 0, 256); |
---|
| 145 | strcpy(name, fileName); |
---|
| 146 | |
---|
| 147 | switch (shaderType) |
---|
| 148 | { |
---|
| 149 | case GLSLVertexShader: |
---|
| 150 | strcat(name, ".vert"); |
---|
| 151 | break; |
---|
| 152 | case GLSLFragmentShader: |
---|
| 153 | strcat(name, ".frag"); |
---|
| 154 | break; |
---|
| 155 | default: |
---|
| 156 | fprintf(stderr, "GLSL> ERROR: unknown shader file type\n"); |
---|
| 157 | exit(1); |
---|
| 158 | break; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | // |
---|
| 162 | // Open the file, seek to the end to find its length |
---|
| 163 | // |
---|
| 164 | #ifdef WIN32 |
---|
| 165 | fd = _open(name, _O_RDONLY); |
---|
| 166 | if (fd != -1) |
---|
| 167 | { |
---|
| 168 | count = _lseek(fd, 0, SEEK_END) + 1; |
---|
| 169 | _close(fd); |
---|
| 170 | } |
---|
| 171 | #else |
---|
| 172 | fd = open(name, O_RDONLY); |
---|
| 173 | if (fd != -1) |
---|
| 174 | { |
---|
| 175 | count = lseek(fd, 0, SEEK_END) + 1; |
---|
| 176 | close(fd); |
---|
| 177 | } |
---|
| 178 | #endif |
---|
| 179 | return count; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | int GLSLreadShader(char *fileName, int shaderType, char *shaderText, int size) |
---|
| 184 | { |
---|
| 185 | // |
---|
| 186 | // Reads a shader from the supplied file and returns the shader in the |
---|
| 187 | // arrays passed in. Returns 1 if successful, 0 if an error occurred. |
---|
| 188 | // The parameter size is an upper limit of the amount of bytes to read. |
---|
| 189 | // It is ok for it to be too big. |
---|
| 190 | // |
---|
| 191 | FILE *fh; |
---|
| 192 | char name[100]; |
---|
| 193 | int count; |
---|
| 194 | |
---|
| 195 | strcpy(name, fileName); |
---|
| 196 | |
---|
| 197 | switch (shaderType) |
---|
| 198 | { |
---|
| 199 | case GLSLVertexShader: |
---|
| 200 | strcat(name, ".vert"); |
---|
| 201 | break; |
---|
| 202 | case GLSLFragmentShader: |
---|
| 203 | strcat(name, ".frag"); |
---|
| 204 | break; |
---|
| 205 | default: |
---|
| 206 | fprintf(stderr, "GLSL> ERROR: unknown shader file type\n"); |
---|
| 207 | exit(1); |
---|
| 208 | break; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | // |
---|
| 212 | // Open the file |
---|
| 213 | // |
---|
| 214 | fh = fopen(name, "r"); |
---|
| 215 | if (!fh) |
---|
| 216 | return -1; |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | // |
---|
| 220 | // Get the shader from a file. |
---|
| 221 | // |
---|
| 222 | fseek(fh, 0, SEEK_SET); |
---|
| 223 | count = (int) fread(shaderText, 1, size, fh); |
---|
| 224 | shaderText[count] = '\0'; |
---|
| 225 | |
---|
| 226 | if (ferror(fh)) |
---|
| 227 | count = 0; |
---|
| 228 | |
---|
| 229 | fclose(fh); |
---|
| 230 | return count; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | int GLSLreadShaderSource(char *fileName, GLchar **vertexShader, GLchar **fragmentShader) |
---|
| 234 | { |
---|
| 235 | int vSize, fSize; |
---|
| 236 | |
---|
| 237 | // |
---|
| 238 | // Allocate memory to hold the source of our shaders. |
---|
| 239 | // |
---|
| 240 | |
---|
| 241 | fprintf(stderr, "GLSL> load shader %s\n", fileName); |
---|
| 242 | if (vertexShader) { |
---|
| 243 | vSize = GLSLShaderSize(fileName, GLSLVertexShader); |
---|
| 244 | |
---|
| 245 | if (vSize == -1) { |
---|
| 246 | fprintf(stderr, "GLSL> Cannot determine size of the vertex shader %s\n", fileName); |
---|
| 247 | return 0; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | *vertexShader = (GLchar *) malloc(vSize); |
---|
| 251 | |
---|
| 252 | // |
---|
| 253 | // Read the source code |
---|
| 254 | // |
---|
| 255 | if (!GLSLreadShader(fileName, GLSLVertexShader, *vertexShader, vSize)) { |
---|
| 256 | fprintf(stderr, "GLSL> Cannot read the file %s.vert\n", fileName); |
---|
| 257 | return 0; |
---|
| 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | if (fragmentShader) { |
---|
| 262 | fSize = GLSLShaderSize(fileName, GLSLFragmentShader); |
---|
| 263 | |
---|
| 264 | if (fSize == -1) { |
---|
| 265 | fprintf(stderr, "GLSL> Cannot determine size of the fragment shader %s\n", fileName); |
---|
| 266 | return 0; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | *fragmentShader = (GLchar *) malloc(fSize); |
---|
| 270 | |
---|
| 271 | if (!GLSLreadShader(fileName, GLSLFragmentShader, *fragmentShader, fSize)) { |
---|
| 272 | fprintf(stderr, "GLSL> Cannot read the file %s.frag\n", fileName); |
---|
| 273 | return 0; |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | return 1; |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | GLuint GLSLinstallShaders(const GLchar *Vertex, const GLchar *Fragment) |
---|
| 282 | { |
---|
| 283 | GLuint VS, FS, Prog; // handles to objects |
---|
| 284 | GLint vertCompiled, fragCompiled; // status values |
---|
| 285 | GLint linked; |
---|
| 286 | |
---|
| 287 | // Create a program object |
---|
| 288 | Prog = glCreateProgram(); |
---|
| 289 | |
---|
| 290 | // Create a vertex shader object and a fragment shader object |
---|
| 291 | if (Vertex) { |
---|
| 292 | VS = glCreateShader(GL_VERTEX_SHADER); |
---|
| 293 | |
---|
| 294 | // Load source code strings into shaders |
---|
| 295 | glShaderSource(VS, 1, &Vertex, NULL); |
---|
| 296 | |
---|
| 297 | // Compile the vertex shader, and print out |
---|
| 298 | // the compiler log file. |
---|
| 299 | |
---|
| 300 | glCompileShader(VS); |
---|
| 301 | GLSLprintlError(__FILE__, __LINE__); // Check for OpenGL errors |
---|
| 302 | glGetShaderiv(VS, GL_COMPILE_STATUS, &vertCompiled); |
---|
| 303 | GLSLprintShaderInfoLog(VS); |
---|
| 304 | |
---|
| 305 | if (!vertCompiled) |
---|
| 306 | return 0; |
---|
| 307 | |
---|
| 308 | glAttachShader(Prog, VS); |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | |
---|
| 312 | if (Fragment) { |
---|
| 313 | FS = glCreateShader(GL_FRAGMENT_SHADER); |
---|
| 314 | |
---|
| 315 | glShaderSource(FS, 1, &Fragment, NULL); |
---|
| 316 | |
---|
| 317 | glCompileShader(FS); |
---|
| 318 | GLSLprintlError(__FILE__, __LINE__); // Check for OpenGL errors |
---|
| 319 | glGetShaderiv(FS, GL_COMPILE_STATUS, &fragCompiled); |
---|
| 320 | GLSLprintShaderInfoLog(FS); |
---|
| 321 | |
---|
| 322 | if (!fragCompiled) |
---|
| 323 | return 0; |
---|
| 324 | |
---|
| 325 | glAttachShader(Prog, FS); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | // Link the program object and print out the info log |
---|
| 329 | glLinkProgram(Prog); |
---|
| 330 | GLSLprintlError(__FILE__, __LINE__); // Check for OpenGL errors |
---|
| 331 | glGetProgramiv(Prog, GL_LINK_STATUS, &linked); |
---|
| 332 | GLSLprintProgramInfoLog(Prog); |
---|
| 333 | |
---|
| 334 | if (!linked) |
---|
| 335 | return 0; |
---|
| 336 | |
---|
| 337 | return Prog; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | #endif |
---|
| 341 | |
---|