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 8.0KB

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