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

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