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.

multipal.c 9.3KB

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