Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

multipal.c 8.6KB

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