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

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