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.

noise.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/glew.h>
  11. #include <GL/glut.h>
  12. #include "shaderutil.h"
  13. static const char *VertShaderText =
  14. "void main() {\n"
  15. " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
  16. " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
  17. "}\n";
  18. static const char *FragShaderText =
  19. "uniform vec4 Scale, Bias;\n"
  20. "uniform float Slice;\n"
  21. "void main()\n"
  22. "{\n"
  23. " vec4 scale = vec4(5.0);\n"
  24. " vec4 p;\n"
  25. " p.xy = gl_TexCoord[0].xy;\n"
  26. " p.z = Slice;\n"
  27. " p.w = 0.0;\n"
  28. " vec4 n = noise4(p * scale);\n"
  29. " gl_FragColor = n * Scale + Bias;\n"
  30. "}\n";
  31. static struct uniform_info Uniforms[] = {
  32. { "Scale", 1, GL_FLOAT_VEC4, { 0.5, 0.4, 0.0, 0}, -1 },
  33. { "Bias", 1, GL_FLOAT_VEC4, { 0.5, 0.3, 0.0, 0}, -1 },
  34. { "Slice", 1, GL_FLOAT, { 0.5, 0, 0, 0}, -1 },
  35. END_OF_UNIFORMS
  36. };
  37. /* program/shader objects */
  38. static GLuint fragShader;
  39. static GLuint vertShader;
  40. static GLuint program;
  41. static GLint win = 0;
  42. static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
  43. static GLfloat Slice = 0.0;
  44. static GLboolean Anim = GL_FALSE;
  45. static void
  46. Idle(void)
  47. {
  48. Slice += 0.01;
  49. glutPostRedisplay();
  50. }
  51. static void
  52. Redisplay(void)
  53. {
  54. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  55. glUniform1fv(Uniforms[2].location, 1, &Slice);
  56. glPushMatrix();
  57. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  58. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  59. glRotatef(zRot, 0.0f, 0.0f, 1.0f);
  60. glBegin(GL_POLYGON);
  61. glTexCoord2f(0, 0); glVertex2f(-2, -2);
  62. glTexCoord2f(1, 0); glVertex2f( 2, -2);
  63. glTexCoord2f(1, 1); glVertex2f( 2, 2);
  64. glTexCoord2f(0, 1); glVertex2f(-2, 2);
  65. glEnd();
  66. glPopMatrix();
  67. glutSwapBuffers();
  68. }
  69. static void
  70. Reshape(int width, int height)
  71. {
  72. glViewport(0, 0, width, height);
  73. glMatrixMode(GL_PROJECTION);
  74. glLoadIdentity();
  75. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  76. glMatrixMode(GL_MODELVIEW);
  77. glLoadIdentity();
  78. glTranslatef(0.0f, 0.0f, -15.0f);
  79. }
  80. static void
  81. CleanUp(void)
  82. {
  83. glDeleteShader(fragShader);
  84. glDeleteShader(vertShader);
  85. glDeleteProgram(program);
  86. glutDestroyWindow(win);
  87. }
  88. static void
  89. Key(unsigned char key, int x, int y)
  90. {
  91. const GLfloat step = 0.01;
  92. (void) x;
  93. (void) y;
  94. switch(key) {
  95. case 'a':
  96. Anim = !Anim;
  97. glutIdleFunc(Anim ? Idle : NULL);
  98. break;
  99. case 's':
  100. Slice -= step;
  101. break;
  102. case 'S':
  103. Slice += step;
  104. break;
  105. case 'z':
  106. zRot -= 1.0;
  107. break;
  108. case 'Z':
  109. zRot += 1.0;
  110. break;
  111. case 27:
  112. CleanUp();
  113. exit(0);
  114. break;
  115. }
  116. glutPostRedisplay();
  117. }
  118. static void
  119. SpecialKey(int key, int x, int y)
  120. {
  121. const GLfloat step = 3.0f;
  122. (void) x;
  123. (void) y;
  124. switch(key) {
  125. case GLUT_KEY_UP:
  126. xRot -= step;
  127. break;
  128. case GLUT_KEY_DOWN:
  129. xRot += step;
  130. break;
  131. case GLUT_KEY_LEFT:
  132. yRot -= step;
  133. break;
  134. case GLUT_KEY_RIGHT:
  135. yRot += step;
  136. break;
  137. }
  138. glutPostRedisplay();
  139. }
  140. static void
  141. Init(void)
  142. {
  143. if (!ShadersSupported())
  144. exit(1);
  145. vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
  146. fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
  147. program = LinkShaders(vertShader, fragShader);
  148. glUseProgram(program);
  149. SetUniformValues(program, Uniforms);
  150. PrintUniforms(Uniforms);
  151. assert(glGetError() == 0);
  152. glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
  153. printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
  154. assert(glIsProgram(program));
  155. assert(glIsShader(fragShader));
  156. assert(glIsShader(vertShader));
  157. glColor3f(1, 0, 0);
  158. }
  159. int
  160. main(int argc, char *argv[])
  161. {
  162. glutInit(&argc, argv);
  163. glutInitWindowSize(400, 400);
  164. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  165. win = glutCreateWindow(argv[0]);
  166. glewInit();
  167. glutReshapeFunc(Reshape);
  168. glutKeyboardFunc(Key);
  169. glutSpecialFunc(SpecialKey);
  170. glutDisplayFunc(Redisplay);
  171. Init();
  172. glutMainLoop();
  173. return 0;
  174. }