Clone of mesa.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

noise.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * Test noise() functions.
  3. * 28 Jan 2007
  4. */
  5. #include <assert.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <GL/gl.h>
  11. #include <GL/glut.h>
  12. #include <GL/glext.h>
  13. #include "extfuncs.h"
  14. static const char *VertShaderText =
  15. "void main() {\n"
  16. " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
  17. " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
  18. "}\n";
  19. static const char *FragShaderText =
  20. "uniform vec4 Scale, Bias;\n"
  21. "uniform float Slice;\n"
  22. "void main()\n"
  23. "{\n"
  24. " vec4 scale = vec4(5.0);\n"
  25. " vec4 p;\n"
  26. " p.xy = gl_TexCoord[0].xy;\n"
  27. " p.z = Slice;\n"
  28. " vec4 n = noise4(p * scale);\n"
  29. " gl_FragColor = n * Scale + Bias;\n"
  30. "}\n";
  31. struct uniform_info {
  32. const char *name;
  33. GLuint size;
  34. GLint location;
  35. GLfloat value[4];
  36. };
  37. static struct uniform_info Uniforms[] = {
  38. { "Scale", 4, -1, { 0.5, 0.4, 0.0, 0} },
  39. { "Bias", 4, -1, { 0.5, 0.3, 0.0, 0} },
  40. { "Slice", 1, -1, { 0.5, 0, 0, 0} },
  41. { NULL, 0, 0, { 0, 0, 0, 0 } }
  42. };
  43. /* program/shader objects */
  44. static GLuint fragShader;
  45. static GLuint vertShader;
  46. static GLuint program;
  47. static GLint win = 0;
  48. static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
  49. static GLfloat Slice = 0.0;
  50. static GLboolean Anim = GL_FALSE;
  51. static void
  52. Idle(void)
  53. {
  54. Slice += 0.01;
  55. glutPostRedisplay();
  56. }
  57. static void
  58. Redisplay(void)
  59. {
  60. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  61. glUniform1fv_func(Uniforms[2].location, 1, &Slice);
  62. glPushMatrix();
  63. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  64. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  65. glRotatef(zRot, 0.0f, 0.0f, 1.0f);
  66. glBegin(GL_POLYGON);
  67. glTexCoord2f(0, 0); glVertex2f(-2, -2);
  68. glTexCoord2f(1, 0); glVertex2f( 2, -2);
  69. glTexCoord2f(1, 1); glVertex2f( 2, 2);
  70. glTexCoord2f(0, 1); glVertex2f(-2, 2);
  71. glEnd();
  72. glPopMatrix();
  73. glutSwapBuffers();
  74. }
  75. static void
  76. Reshape(int width, int height)
  77. {
  78. glViewport(0, 0, width, height);
  79. glMatrixMode(GL_PROJECTION);
  80. glLoadIdentity();
  81. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  82. glMatrixMode(GL_MODELVIEW);
  83. glLoadIdentity();
  84. glTranslatef(0.0f, 0.0f, -15.0f);
  85. }
  86. static void
  87. CleanUp(void)
  88. {
  89. glDeleteShader_func(fragShader);
  90. glDeleteShader_func(vertShader);
  91. glDeleteProgram_func(program);
  92. glutDestroyWindow(win);
  93. }
  94. static void
  95. Key(unsigned char key, int x, int y)
  96. {
  97. const GLfloat step = 0.01;
  98. (void) x;
  99. (void) y;
  100. switch(key) {
  101. case 'a':
  102. Anim = !Anim;
  103. glutIdleFunc(Anim ? Idle : NULL);
  104. case 's':
  105. Slice -= step;
  106. break;
  107. case 'S':
  108. Slice += step;
  109. break;
  110. case 'z':
  111. zRot -= 1.0;
  112. break;
  113. case 'Z':
  114. zRot += 1.0;
  115. break;
  116. case 27:
  117. CleanUp();
  118. exit(0);
  119. break;
  120. }
  121. glutPostRedisplay();
  122. }
  123. static void
  124. SpecialKey(int key, int x, int y)
  125. {
  126. const GLfloat step = 3.0f;
  127. (void) x;
  128. (void) y;
  129. switch(key) {
  130. case GLUT_KEY_UP:
  131. xRot -= step;
  132. break;
  133. case GLUT_KEY_DOWN:
  134. xRot += step;
  135. break;
  136. case GLUT_KEY_LEFT:
  137. yRot -= step;
  138. break;
  139. case GLUT_KEY_RIGHT:
  140. yRot += step;
  141. break;
  142. }
  143. glutPostRedisplay();
  144. }
  145. static void
  146. LoadAndCompileShader(GLuint shader, const char *text)
  147. {
  148. GLint stat;
  149. glShaderSource_func(shader, 1, (const GLchar **) &text, NULL);
  150. glCompileShader_func(shader);
  151. glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
  152. if (!stat) {
  153. GLchar log[1000];
  154. GLsizei len;
  155. glGetShaderInfoLog_func(shader, 1000, &len, log);
  156. fprintf(stderr, "brick: problem compiling shader: %s\n", log);
  157. exit(1);
  158. }
  159. else {
  160. printf("Shader compiled OK\n");
  161. }
  162. }
  163. static void
  164. CheckLink(GLuint prog)
  165. {
  166. GLint stat;
  167. glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
  168. if (!stat) {
  169. GLchar log[1000];
  170. GLsizei len;
  171. glGetProgramInfoLog_func(prog, 1000, &len, log);
  172. fprintf(stderr, "Linker error:\n%s\n", log);
  173. }
  174. else {
  175. fprintf(stderr, "Link success!\n");
  176. }
  177. }
  178. static void
  179. Init(void)
  180. {
  181. const char *version;
  182. GLint i;
  183. version = (const char *) glGetString(GL_VERSION);
  184. if (version[0] != '2' || version[1] != '.') {
  185. printf("Warning: this program expects OpenGL 2.0\n");
  186. /*exit(1);*/
  187. }
  188. GetExtensionFuncs();
  189. vertShader = glCreateShader_func(GL_VERTEX_SHADER);
  190. LoadAndCompileShader(vertShader, VertShaderText);
  191. fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
  192. LoadAndCompileShader(fragShader, FragShaderText);
  193. program = glCreateProgram_func();
  194. glAttachShader_func(program, fragShader);
  195. glAttachShader_func(program, vertShader);
  196. glLinkProgram_func(program);
  197. CheckLink(program);
  198. glUseProgram_func(program);
  199. for (i = 0; Uniforms[i].name; i++) {
  200. Uniforms[i].location
  201. = glGetUniformLocation_func(program, Uniforms[i].name);
  202. printf("Uniform %s location: %d\n", Uniforms[i].name,
  203. Uniforms[i].location);
  204. switch (Uniforms[i].size) {
  205. case 1:
  206. glUniform1fv_func(Uniforms[i].location, 1, Uniforms[i].value);
  207. break;
  208. case 2:
  209. glUniform2fv_func(Uniforms[i].location, 1, Uniforms[i].value);
  210. break;
  211. case 3:
  212. glUniform3fv_func(Uniforms[i].location, 1, Uniforms[i].value);
  213. break;
  214. case 4:
  215. glUniform4fv_func(Uniforms[i].location, 1, Uniforms[i].value);
  216. break;
  217. default:
  218. abort();
  219. }
  220. }
  221. assert(glGetError() == 0);
  222. glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
  223. printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
  224. assert(glIsProgram_func(program));
  225. assert(glIsShader_func(fragShader));
  226. assert(glIsShader_func(vertShader));
  227. glColor3f(1, 0, 0);
  228. }
  229. int
  230. main(int argc, char *argv[])
  231. {
  232. glutInit(&argc, argv);
  233. glutInitWindowPosition( 0, 0);
  234. glutInitWindowSize(400, 400);
  235. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  236. win = glutCreateWindow(argv[0]);
  237. glutReshapeFunc(Reshape);
  238. glutKeyboardFunc(Key);
  239. glutSpecialFunc(SpecialKey);
  240. glutDisplayFunc(Redisplay);
  241. Init();
  242. glutMainLoop();
  243. return 0;
  244. }