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.

multiarb.c 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* $Id: multiarb.c,v 1.1 1999/08/19 00:55:40 jtg Exp $ */
  2. /*
  3. * GL_ARB_multitexture demo
  4. * Brian Paul November 1998 This program is in the public domain.
  5. */
  6. /*
  7. * $Log: multiarb.c,v $
  8. * Revision 1.1 1999/08/19 00:55:40 jtg
  9. * Initial revision
  10. *
  11. * Revision 1.3 1999/03/28 18:20:49 brianp
  12. * minor clean-up
  13. *
  14. * Revision 1.2 1998/11/05 04:34:04 brianp
  15. * moved image files to ../images/ directory
  16. *
  17. * Revision 1.1 1998/11/03 01:36:33 brianp
  18. * Initial revision
  19. *
  20. */
  21. #include <math.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <GL/glut.h>
  26. #include "../util/readtex.c" /* I know, this is a hack. */
  27. #define TEXTURE_1_FILE "../images/girl.rgb"
  28. #define TEXTURE_2_FILE "../images/reflect.rgb"
  29. #define TEX0 1
  30. #define TEX1 2
  31. #define TEXBOTH 3
  32. #define ANIMATE 10
  33. #define QUIT 100
  34. static GLboolean Animate = GL_TRUE;
  35. static GLfloat Drift = 0.0;
  36. static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
  37. static void Idle( void )
  38. {
  39. if (Animate) {
  40. Drift += 0.05;
  41. #ifdef GL_ARB_multitexture
  42. glActiveTextureARB(GL_TEXTURE0_ARB);
  43. #endif
  44. glMatrixMode(GL_TEXTURE);
  45. glLoadIdentity();
  46. glTranslatef(Drift, 0.0, 0.0);
  47. glMatrixMode(GL_MODELVIEW);
  48. #ifdef GL_ARB_multitexture
  49. glActiveTextureARB(GL_TEXTURE1_ARB);
  50. #endif
  51. glMatrixMode(GL_TEXTURE);
  52. glLoadIdentity();
  53. glTranslatef(0.0, Drift, 0.0);
  54. glMatrixMode(GL_MODELVIEW);
  55. glutPostRedisplay();
  56. }
  57. }
  58. static void DrawObject(void)
  59. {
  60. glBegin(GL_QUADS);
  61. #ifdef GL_ARB_multitexture
  62. glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
  63. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
  64. glVertex2f(-1.0, -1.0);
  65. glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 0.0);
  66. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
  67. glVertex2f(1.0, -1.0);
  68. glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 2.0);
  69. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
  70. glVertex2f(1.0, 1.0);
  71. glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 2.0);
  72. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
  73. glVertex2f(-1.0, 1.0);
  74. #else
  75. glTexCoord2f(0.0, 0.0);
  76. glVertex2f(-1.0, -1.0);
  77. glTexCoord2f(1.0, 0.0);
  78. glVertex2f(1.0, -1.0);
  79. glTexCoord2f(1.0, 1.0);
  80. glVertex2f(1.0, 1.0);
  81. glTexCoord2f(0.0, 1.0);
  82. glVertex2f(-1.0, 1.0);
  83. #endif
  84. glEnd();
  85. }
  86. static void Display( void )
  87. {
  88. glClear( GL_COLOR_BUFFER_BIT );
  89. glPushMatrix();
  90. glRotatef(Xrot, 1.0, 0.0, 0.0);
  91. glRotatef(Yrot, 0.0, 1.0, 0.0);
  92. glRotatef(Zrot, 0.0, 0.0, 1.0);
  93. glScalef(5.0, 5.0, 5.0);
  94. DrawObject();
  95. glPopMatrix();
  96. glutSwapBuffers();
  97. }
  98. static void Reshape( int width, int height )
  99. {
  100. glViewport( 0, 0, width, height );
  101. glMatrixMode( GL_PROJECTION );
  102. glLoadIdentity();
  103. glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
  104. /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
  105. glMatrixMode( GL_MODELVIEW );
  106. glLoadIdentity();
  107. glTranslatef( 0.0, 0.0, -70.0 );
  108. }
  109. static void ModeMenu(int entry)
  110. {
  111. GLboolean enable0 = GL_FALSE, enable1 = GL_FALSE;
  112. if (entry==TEX0) {
  113. enable0 = GL_TRUE;
  114. }
  115. else if (entry==TEX1) {
  116. enable1 = GL_TRUE;
  117. }
  118. else if (entry==TEXBOTH) {
  119. enable0 = GL_TRUE;
  120. enable1 = GL_TRUE;
  121. }
  122. else if (entry==ANIMATE) {
  123. Animate = !Animate;
  124. }
  125. else if (entry==QUIT) {
  126. exit(0);
  127. }
  128. if (entry != ANIMATE) {
  129. #ifdef GL_ARB_multitexture
  130. glActiveTextureARB(GL_TEXTURE0_ARB);
  131. #endif
  132. if (enable0) {
  133. glEnable(GL_TEXTURE_2D);
  134. }
  135. else
  136. glDisable(GL_TEXTURE_2D);
  137. #ifdef GL_ARB_multitexture
  138. glActiveTextureARB(GL_TEXTURE1_ARB);
  139. #endif
  140. if (enable1) {
  141. glEnable(GL_TEXTURE_2D);
  142. }
  143. else
  144. glDisable(GL_TEXTURE_2D);
  145. }
  146. glutPostRedisplay();
  147. }
  148. static void Key( unsigned char key, int x, int y )
  149. {
  150. (void) x;
  151. (void) y;
  152. switch (key) {
  153. case 27:
  154. exit(0);
  155. break;
  156. }
  157. glutPostRedisplay();
  158. }
  159. static void SpecialKey( int key, int x, int y )
  160. {
  161. float step = 3.0;
  162. (void) x;
  163. (void) y;
  164. switch (key) {
  165. case GLUT_KEY_UP:
  166. Xrot += step;
  167. break;
  168. case GLUT_KEY_DOWN:
  169. Xrot -= step;
  170. break;
  171. case GLUT_KEY_LEFT:
  172. Yrot += step;
  173. break;
  174. case GLUT_KEY_RIGHT:
  175. Yrot -= step;
  176. break;
  177. }
  178. glutPostRedisplay();
  179. }
  180. static void Init( void )
  181. {
  182. const char *exten = (const char *) glGetString(GL_EXTENSIONS);
  183. if (!strstr(exten, "GL_ARB_multitexture")) {
  184. printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
  185. exit(1);
  186. }
  187. /* setup texture env 0 */
  188. #ifdef GL_ARB_multitexture
  189. glActiveTextureARB(GL_TEXTURE0_ARB);
  190. #endif
  191. #ifdef LINEAR_FILTER
  192. /* linear filtering looks much nicer but is much slower for Mesa */
  193. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  194. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  195. #else
  196. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  197. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  198. #endif
  199. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  200. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  201. if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
  202. printf("Error: couldn't load texture image\n");
  203. exit(1);
  204. }
  205. /* setup texture env 1 */
  206. #ifdef GL_ARB_multitexture
  207. glActiveTextureARB(GL_TEXTURE1_ARB);
  208. #endif
  209. #ifdef LINEAR_FILTER
  210. /* linear filtering looks much nicer but is much slower for Mesa */
  211. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  212. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  213. #else
  214. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  215. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  216. #endif
  217. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  218. if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
  219. printf("Error: couldn't load texture image\n");
  220. exit(1);
  221. }
  222. glShadeModel(GL_FLAT);
  223. glClearColor(0.3, 0.3, 0.4, 1.0);
  224. ModeMenu(TEXBOTH);
  225. }
  226. int main( int argc, char *argv[] )
  227. {
  228. glutInit( &argc, argv );
  229. glutInitWindowSize( 300, 300 );
  230. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  231. glutCreateWindow(argv[0] );
  232. Init();
  233. glutReshapeFunc( Reshape );
  234. glutKeyboardFunc( Key );
  235. glutSpecialFunc( SpecialKey );
  236. glutDisplayFunc( Display );
  237. glutIdleFunc( Idle );
  238. glutCreateMenu(ModeMenu);
  239. glutAddMenuEntry("Texture 0", TEX0);
  240. glutAddMenuEntry("Texture 1", TEX1);
  241. glutAddMenuEntry("Multi-texture", TEXBOTH);
  242. glutAddMenuEntry("Toggle Animation", ANIMATE);
  243. glutAddMenuEntry("Quit", QUIT);
  244. glutAttachMenu(GLUT_RIGHT_BUTTON);
  245. glutMainLoop();
  246. return 0;
  247. }