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

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