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.5KB

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