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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * GL_ARB_multitexture demo
  3. *
  4. * Command line options:
  5. * -info print GL implementation information
  6. *
  7. *
  8. * Brian Paul November 1998 This program is in the public domain.
  9. * Modified on 12 Feb 2002 for > 2 texture units.
  10. */
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <GL/glut.h>
  16. #include "readtex.h"
  17. #define TEXTURE_1_FILE "../images/girl.rgb"
  18. #define TEXTURE_2_FILE "../images/reflect.rgb"
  19. #define TEX0 1
  20. #define TEX7 8
  21. #define ANIMATE 10
  22. #define QUIT 100
  23. static GLboolean Animate = GL_TRUE;
  24. static GLint NumUnits = 1;
  25. static GLboolean TexEnabled[8];
  26. static GLfloat Drift = 0.0;
  27. static GLfloat drift_increment = 0.005;
  28. static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
  29. static void Idle( void )
  30. {
  31. if (Animate) {
  32. GLint i;
  33. Drift += drift_increment;
  34. if (Drift >= 1.0)
  35. Drift = 0.0;
  36. for (i = 0; i < NumUnits; i++) {
  37. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  38. glMatrixMode(GL_TEXTURE);
  39. glLoadIdentity();
  40. if (i == 0) {
  41. glTranslatef(Drift, 0.0, 0.0);
  42. glScalef(2, 2, 1);
  43. }
  44. else if (i == 1) {
  45. glTranslatef(0.0, Drift, 0.0);
  46. }
  47. else {
  48. glTranslatef(0.5, 0.5, 0.0);
  49. glRotatef(180.0 * Drift, 0, 0, 1);
  50. glScalef(1.0/i, 1.0/i, 1.0/i);
  51. glTranslatef(-0.5, -0.5, 0.0);
  52. }
  53. }
  54. glMatrixMode(GL_MODELVIEW);
  55. glutPostRedisplay();
  56. }
  57. }
  58. static void DrawObject(void)
  59. {
  60. GLint i;
  61. GLint j;
  62. static const GLfloat tex_coords[] = { 0.0, 0.0, 1.0, 1.0, 0.0 };
  63. static const GLfloat vtx_coords[] = { -1.0, -1.0, 1.0, 1.0, -1.0 };
  64. if (!TexEnabled[0] && !TexEnabled[1])
  65. glColor3f(0.1, 0.1, 0.1); /* add onto this */
  66. else
  67. glColor3f(1, 1, 1); /* modulate this */
  68. glBegin(GL_QUADS);
  69. /* Toggle between the vector and scalar entry points. This is done purely
  70. * to hit multiple paths in the driver.
  71. */
  72. if ( Drift > 0.49 ) {
  73. for (j = 0; j < 4; j++ ) {
  74. for (i = 0; i < NumUnits; i++)
  75. glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i,
  76. tex_coords[j], tex_coords[j+1]);
  77. glVertex2f( vtx_coords[j], vtx_coords[j+1] );
  78. }
  79. }
  80. else {
  81. for (j = 0; j < 4; j++ ) {
  82. for (i = 0; i < NumUnits; i++)
  83. glMultiTexCoord2fvARB(GL_TEXTURE0_ARB + i, & tex_coords[j]);
  84. glVertex2fv( & vtx_coords[j] );
  85. }
  86. }
  87. glEnd();
  88. }
  89. static void Display( void )
  90. {
  91. static GLint T0 = 0;
  92. static GLint Frames = 0;
  93. GLint t;
  94. glClear( GL_COLOR_BUFFER_BIT );
  95. glPushMatrix();
  96. glRotatef(Xrot, 1.0, 0.0, 0.0);
  97. glRotatef(Yrot, 0.0, 1.0, 0.0);
  98. glRotatef(Zrot, 0.0, 0.0, 1.0);
  99. glScalef(5.0, 5.0, 5.0);
  100. DrawObject();
  101. glPopMatrix();
  102. glutSwapBuffers();
  103. Frames++;
  104. t = glutGet(GLUT_ELAPSED_TIME);
  105. if (t - T0 >= 250) {
  106. GLfloat seconds = (t - T0) / 1000.0;
  107. drift_increment = 2.2 * seconds / Frames;
  108. T0 = t;
  109. Frames = 0;
  110. }
  111. }
  112. static void Reshape( int width, int height )
  113. {
  114. glViewport( 0, 0, width, height );
  115. glMatrixMode( GL_PROJECTION );
  116. glLoadIdentity();
  117. glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
  118. /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
  119. glMatrixMode( GL_MODELVIEW );
  120. glLoadIdentity();
  121. glTranslatef( 0.0, 0.0, -70.0 );
  122. }
  123. static void ModeMenu(int entry)
  124. {
  125. if (entry >= TEX0 && entry <= TEX7) {
  126. /* toggle */
  127. GLint i = entry - TEX0;
  128. TexEnabled[i] = !TexEnabled[i];
  129. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  130. if (TexEnabled[i])
  131. glEnable(GL_TEXTURE_2D);
  132. else
  133. glDisable(GL_TEXTURE_2D);
  134. printf("Enabled: ");
  135. for (i = 0; i < NumUnits; i++)
  136. printf("%d ", (int) TexEnabled[i]);
  137. printf("\n");
  138. }
  139. else if (entry==ANIMATE) {
  140. Animate = !Animate;
  141. }
  142. else if (entry==QUIT) {
  143. exit(0);
  144. }
  145. glutPostRedisplay();
  146. }
  147. static void Key( unsigned char key, int x, int y )
  148. {
  149. (void) x;
  150. (void) y;
  151. switch (key) {
  152. case 27:
  153. exit(0);
  154. break;
  155. }
  156. glutPostRedisplay();
  157. }
  158. static void SpecialKey( int key, int x, int y )
  159. {
  160. float step = 3.0;
  161. (void) x;
  162. (void) y;
  163. switch (key) {
  164. case GLUT_KEY_UP:
  165. Xrot += step;
  166. break;
  167. case GLUT_KEY_DOWN:
  168. Xrot -= step;
  169. break;
  170. case GLUT_KEY_LEFT:
  171. Yrot += step;
  172. break;
  173. case GLUT_KEY_RIGHT:
  174. Yrot -= step;
  175. break;
  176. }
  177. glutPostRedisplay();
  178. }
  179. static void Init( int argc, char *argv[] )
  180. {
  181. GLuint texObj[8];
  182. GLint size, i;
  183. const char *exten = (const char *) glGetString(GL_EXTENSIONS);
  184. if (!strstr(exten, "GL_ARB_multitexture")) {
  185. printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
  186. exit(1);
  187. }
  188. glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
  189. printf("%d texture units supported\n", NumUnits);
  190. if (NumUnits > 8)
  191. NumUnits = 8;
  192. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
  193. printf("%d x %d max texture size\n", size, size);
  194. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  195. for (i = 0; i < NumUnits; i++) {
  196. if (i < 2)
  197. TexEnabled[i] = GL_TRUE;
  198. else
  199. TexEnabled[i] = GL_FALSE;
  200. }
  201. /* allocate two texture objects */
  202. glGenTextures(NumUnits, texObj);
  203. /* setup the texture objects */
  204. for (i = 0; i < NumUnits; i++) {
  205. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  206. glBindTexture(GL_TEXTURE_2D, texObj[i]);
  207. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  208. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  209. if (i == 0) {
  210. if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
  211. printf("Error: couldn't load texture image\n");
  212. exit(1);
  213. }
  214. }
  215. else if (i == 1) {
  216. if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
  217. printf("Error: couldn't load texture image\n");
  218. exit(1);
  219. }
  220. }
  221. else {
  222. /* checker */
  223. GLubyte image[8][8][3];
  224. GLint i, j;
  225. for (i = 0; i < 8; i++) {
  226. for (j = 0; j < 8; j++) {
  227. if ((i + j) & 1) {
  228. image[i][j][0] = 50;
  229. image[i][j][1] = 50;
  230. image[i][j][2] = 50;
  231. }
  232. else {
  233. image[i][j][0] = 25;
  234. image[i][j][1] = 25;
  235. image[i][j][2] = 25;
  236. }
  237. }
  238. }
  239. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0,
  240. GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) image);
  241. }
  242. /* Bind texObj[i] to ith texture unit */
  243. if (i < 2)
  244. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  245. else
  246. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
  247. if (TexEnabled[i])
  248. glEnable(GL_TEXTURE_2D);
  249. }
  250. glShadeModel(GL_FLAT);
  251. glClearColor(0.3, 0.3, 0.4, 1.0);
  252. if (argc > 1 && strcmp(argv[1], "-info")==0) {
  253. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  254. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  255. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  256. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  257. }
  258. }
  259. int main( int argc, char *argv[] )
  260. {
  261. GLint i;
  262. glutInit( &argc, argv );
  263. glutInitWindowSize( 300, 300 );
  264. glutInitWindowPosition( 0, 0 );
  265. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  266. glutCreateWindow(argv[0] );
  267. Init( argc, argv );
  268. glutReshapeFunc( Reshape );
  269. glutKeyboardFunc( Key );
  270. glutSpecialFunc( SpecialKey );
  271. glutDisplayFunc( Display );
  272. glutIdleFunc( Idle );
  273. glutCreateMenu(ModeMenu);
  274. for (i = 0; i < NumUnits; i++) {
  275. char s[100];
  276. sprintf(s, "Toggle Texture %d", i);
  277. glutAddMenuEntry(s, TEX0 + i);
  278. }
  279. glutAddMenuEntry("Toggle Animation", ANIMATE);
  280. glutAddMenuEntry("Quit", QUIT);
  281. glutAttachMenu(GLUT_RIGHT_BUTTON);
  282. glutMainLoop();
  283. return 0;
  284. }