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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * Test multi-texturing with GL shading language.
  3. *
  4. * Copyright (C) 2008 Brian Paul All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  20. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <assert.h>
  24. #include <math.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "GL/glut.h"
  29. #include "readtex.h"
  30. #include "extfuncs.h"
  31. #include "shaderutil.h"
  32. static const char *Demo = "multitex";
  33. static const char *VertFile = "multitex.vert.txt";
  34. static const char *FragFile = "multitex.frag.txt";
  35. static const char *TexFiles[2] =
  36. {
  37. "../images/tile.rgb",
  38. "../images/tree2.rgba"
  39. };
  40. static GLuint Program;
  41. static GLfloat Xrot = -90.0, Yrot = .0, Zrot = 0.0;
  42. static GLfloat EyeDist = 10;
  43. static GLboolean Anim = GL_TRUE;
  44. /* value[0] = tex unit */
  45. static struct uniform_info Uniforms[] = {
  46. { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 },
  47. { "tex2", 1, GL_INT, { 1, 0, 0, 0 }, -1 },
  48. END_OF_UNIFORMS
  49. };
  50. static void
  51. DrawPolygon(GLfloat size)
  52. {
  53. glPushMatrix();
  54. glRotatef(90, 1, 0, 0);
  55. glNormal3f(0, 0, 1);
  56. glBegin(GL_POLYGON);
  57. glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
  58. glMultiTexCoord2f(GL_TEXTURE1, 0, 0);
  59. glVertex2f(-size, -size);
  60. glMultiTexCoord2f(GL_TEXTURE0, 2, 0);
  61. glMultiTexCoord2f(GL_TEXTURE1, 1, 0);
  62. glVertex2f( size, -size);
  63. glMultiTexCoord2f(GL_TEXTURE0, 2, 2);
  64. glMultiTexCoord2f(GL_TEXTURE1, 1, 1);
  65. glVertex2f( size, size);
  66. glMultiTexCoord2f(GL_TEXTURE0, 0, 2);
  67. glMultiTexCoord2f(GL_TEXTURE1, 0, 1);
  68. glVertex2f(-size, size);
  69. glEnd();
  70. glPopMatrix();
  71. }
  72. static void
  73. draw(void)
  74. {
  75. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  76. glPushMatrix(); /* modelview matrix */
  77. glTranslatef(0.0, 0.0, -EyeDist);
  78. glRotatef(Zrot, 0, 0, 1);
  79. glRotatef(Yrot, 0, 1, 0);
  80. glRotatef(Xrot, 1, 0, 0);
  81. DrawPolygon(3.0);
  82. glPopMatrix();
  83. glutSwapBuffers();
  84. }
  85. static void
  86. idle(void)
  87. {
  88. GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
  89. Yrot = t;
  90. glutPostRedisplay();
  91. }
  92. static void
  93. key(unsigned char k, int x, int y)
  94. {
  95. (void) x;
  96. (void) y;
  97. switch (k) {
  98. case ' ':
  99. case 'a':
  100. Anim = !Anim;
  101. if (Anim)
  102. glutIdleFunc(idle);
  103. else
  104. glutIdleFunc(NULL);
  105. break;
  106. case 'z':
  107. EyeDist -= 0.5;
  108. if (EyeDist < 3.0)
  109. EyeDist = 3.0;
  110. break;
  111. case 'Z':
  112. EyeDist += 0.5;
  113. if (EyeDist > 90.0)
  114. EyeDist = 90;
  115. break;
  116. case 27:
  117. exit(0);
  118. }
  119. glutPostRedisplay();
  120. }
  121. static void
  122. specialkey(int key, int x, int y)
  123. {
  124. GLfloat step = 2.0;
  125. (void) x;
  126. (void) y;
  127. switch (key) {
  128. case GLUT_KEY_UP:
  129. Xrot += step;
  130. break;
  131. case GLUT_KEY_DOWN:
  132. Xrot -= step;
  133. break;
  134. case GLUT_KEY_LEFT:
  135. Yrot -= step;
  136. break;
  137. case GLUT_KEY_RIGHT:
  138. Yrot += step;
  139. break;
  140. }
  141. glutPostRedisplay();
  142. }
  143. /* new window size or exposure */
  144. static void
  145. Reshape(int width, int height)
  146. {
  147. GLfloat ar = (float) width / (float) height;
  148. glViewport(0, 0, (GLint)width, (GLint)height);
  149. glMatrixMode(GL_PROJECTION);
  150. glLoadIdentity();
  151. glFrustum(-2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0);
  152. glMatrixMode(GL_MODELVIEW);
  153. glLoadIdentity();
  154. }
  155. static void
  156. InitTextures(void)
  157. {
  158. GLenum filter = GL_LINEAR;
  159. int i;
  160. for (i = 0; i < 2; i++) {
  161. GLint imgWidth, imgHeight;
  162. GLenum imgFormat;
  163. GLubyte *image = NULL;
  164. image = LoadRGBImage(TexFiles[i], &imgWidth, &imgHeight, &imgFormat);
  165. if (!image) {
  166. printf("Couldn't read %s\n", TexFiles[i]);
  167. exit(0);
  168. }
  169. glActiveTexture(GL_TEXTURE0 + i);
  170. glBindTexture(GL_TEXTURE_2D, 42 + i);
  171. gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imgWidth, imgHeight,
  172. imgFormat, GL_UNSIGNED_BYTE, image);
  173. free(image);
  174. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  175. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  176. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
  177. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
  178. }
  179. }
  180. static GLuint
  181. CreateProgram(const char *vertProgFile, const char *fragProgFile,
  182. struct uniform_info *uniforms)
  183. {
  184. GLuint fragShader, vertShader, program;
  185. vertShader = CompileShaderFile(GL_VERTEX_SHADER, vertProgFile);
  186. fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, fragProgFile);
  187. assert(vertShader);
  188. program = LinkShaders(vertShader, fragShader);
  189. glUseProgram_func(program);
  190. InitUniforms(program, uniforms);
  191. return program;
  192. }
  193. static void
  194. InitPrograms(void)
  195. {
  196. Program = CreateProgram(VertFile, FragFile, Uniforms);
  197. }
  198. static void
  199. InitGL(void)
  200. {
  201. const char *version = (const char *) glGetString(GL_VERSION);
  202. if (version[0] != '2' || version[1] != '.') {
  203. printf("Warning: this program expects OpenGL 2.0\n");
  204. /*exit(1);*/
  205. }
  206. printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
  207. GetExtensionFuncs();
  208. InitTextures();
  209. InitPrograms();
  210. glEnable(GL_DEPTH_TEST);
  211. glClearColor(.6, .6, .9, 0);
  212. glColor3f(1.0, 1.0, 1.0);
  213. }
  214. int
  215. main(int argc, char *argv[])
  216. {
  217. glutInit(&argc, argv);
  218. glutInitWindowSize(500, 400);
  219. glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  220. glutCreateWindow(Demo);
  221. glutReshapeFunc(Reshape);
  222. glutKeyboardFunc(key);
  223. glutSpecialFunc(specialkey);
  224. glutDisplayFunc(draw);
  225. if (Anim)
  226. glutIdleFunc(idle);
  227. InitGL();
  228. glutMainLoop();
  229. return 0;
  230. }