Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shaderutil.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Utilities for OpenGL shading language
  3. *
  4. * Brian Paul
  5. * 9 April 2008
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <GL/glut.h>
  10. #include "extfuncs.h"
  11. #include "shaderutil.h"
  12. static void
  13. Init(void)
  14. {
  15. static GLboolean firstCall = GL_TRUE;
  16. if (firstCall) {
  17. GetExtensionFuncs();
  18. firstCall = GL_FALSE;
  19. }
  20. }
  21. GLboolean
  22. ShadersSupported(void)
  23. {
  24. const char *version;
  25. version = (const char *) glGetString(GL_VERSION);
  26. if (version[0] != '2' || version[1] != '.') {
  27. printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
  28. return GL_FALSE;
  29. }
  30. return GL_TRUE;
  31. }
  32. GLuint
  33. CompileShaderText(GLenum shaderType, const char *text)
  34. {
  35. GLuint shader;
  36. GLint stat;
  37. Init();
  38. shader = glCreateShader_func(shaderType);
  39. glShaderSource_func(shader, 1, (const GLchar **) &text, NULL);
  40. glCompileShader_func(shader);
  41. glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
  42. if (!stat) {
  43. GLchar log[1000];
  44. GLsizei len;
  45. glGetShaderInfoLog_func(shader, 1000, &len, log);
  46. fprintf(stderr, "Error: problem compiling shader: %s\n", log);
  47. exit(1);
  48. }
  49. else {
  50. /*printf("Shader compiled OK\n");*/
  51. }
  52. return shader;
  53. }
  54. /**
  55. * Read a shader from a file.
  56. */
  57. GLuint
  58. CompileShaderFile(GLenum shaderType, const char *filename)
  59. {
  60. const int max = 100*1000;
  61. int n;
  62. char *buffer = (char*) malloc(max);
  63. GLuint shader;
  64. FILE *f = fopen(filename, "r");
  65. if (!f) {
  66. return 0;
  67. }
  68. n = fread(buffer, 1, max, f);
  69. /*printf("read %d bytes from shader file %s\n", n, filename);*/
  70. if (n > 0) {
  71. buffer[n] = 0;
  72. shader = CompileShaderText(shaderType, buffer);
  73. }
  74. else {
  75. return 0;
  76. }
  77. fclose(f);
  78. free(buffer);
  79. return shader;
  80. }
  81. GLuint
  82. LinkShaders(GLuint vertShader, GLuint fragShader)
  83. {
  84. GLuint program = glCreateProgram_func();
  85. glAttachShader_func(program, fragShader);
  86. glAttachShader_func(program, vertShader);
  87. glLinkProgram_func(program);
  88. /* check link */
  89. {
  90. GLint stat;
  91. glGetProgramiv_func(program, GL_LINK_STATUS, &stat);
  92. if (!stat) {
  93. GLchar log[1000];
  94. GLsizei len;
  95. glGetProgramInfoLog_func(program, 1000, &len, log);
  96. fprintf(stderr, "Shader link error:\n%s\n", log);
  97. return 0;
  98. }
  99. }
  100. return program;
  101. }
  102. void
  103. InitUniforms(GLuint program, struct uniform_info uniforms[])
  104. {
  105. GLuint i;
  106. for (i = 0; uniforms[i].name; i++) {
  107. uniforms[i].location
  108. = glGetUniformLocation_func(program, uniforms[i].name);
  109. printf("Uniform %s location: %d\n", uniforms[i].name,
  110. uniforms[i].location);
  111. switch (uniforms[i].size) {
  112. case 1:
  113. if (uniforms[i].type == GL_INT)
  114. glUniform1i_func(uniforms[i].location,
  115. (GLint) uniforms[i].value[0]);
  116. else
  117. glUniform1fv_func(uniforms[i].location, 1, uniforms[i].value);
  118. break;
  119. case 2:
  120. glUniform2fv_func(uniforms[i].location, 1, uniforms[i].value);
  121. break;
  122. case 3:
  123. glUniform3fv_func(uniforms[i].location, 1, uniforms[i].value);
  124. break;
  125. case 4:
  126. glUniform4fv_func(uniforms[i].location, 1, uniforms[i].value);
  127. break;
  128. default:
  129. abort();
  130. }
  131. }
  132. }