Clone of mesa.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

multitexarray.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Test vertex arrays and multitexture.
  3. * Press 'a' to toggle vertex arrays on/off.
  4. * When you run this program you should see a square with four colors:
  5. *
  6. * +------+------+
  7. * |yellow| pink |
  8. * +------+------+
  9. * |green | blue |
  10. * +------+------+
  11. */
  12. #include <assert.h>
  13. #include <math.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "GL/glut.h"
  18. static GLuint Window = 0;
  19. static GLuint TexObj[2];
  20. static GLfloat Angle = 0.0f;
  21. static GLboolean UseArrays = 1, Anim = 0;
  22. static GLfloat VertArray[4][2] = {
  23. {-1.2, -1.2}, {1.2, -1.2}, {1.2, 1.2}, {-1.2, 1.2}
  24. };
  25. static GLfloat Tex0Array[4][2] = {
  26. {0, 0}, {1, 0}, {1, 1}, {0, 1}
  27. };
  28. static GLfloat Tex1Array[4][2] = {
  29. {0, 0}, {1, 0}, {1, 1}, {0, 1}
  30. };
  31. static void init_arrays(void)
  32. {
  33. glVertexPointer(2, GL_FLOAT, 0, VertArray);
  34. glEnableClientState(GL_VERTEX_ARRAY);
  35. glClientActiveTextureARB(GL_TEXTURE0_ARB);
  36. glTexCoordPointer(2, GL_FLOAT, 0, Tex0Array);
  37. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  38. glClientActiveTextureARB(GL_TEXTURE1_ARB);
  39. glTexCoordPointer(2, GL_FLOAT, 0, Tex1Array);
  40. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  41. }
  42. static void draw( void )
  43. {
  44. glClear( GL_COLOR_BUFFER_BIT );
  45. glColor3f( 0.0, 0.0, 0.0 );
  46. /* draw first polygon */
  47. glPushMatrix();
  48. glRotatef( Angle, 0.0, 0.0, 1.0 );
  49. if (UseArrays) {
  50. glDrawArrays(GL_POLYGON, 0, 4);
  51. }
  52. else {
  53. glBegin( GL_POLYGON );
  54. glTexCoord2f( 0.0, 0.0 );
  55. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
  56. glVertex2f( -1.0, -1.0 );
  57. glTexCoord2f( 1.0, 0.0 );
  58. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
  59. glVertex2f( 1.0, -1.0 );
  60. glTexCoord2f( 1.0, 1.0 );
  61. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
  62. glVertex2f( 1.0, 1.0 );
  63. glTexCoord2f( 0.0, 1.0 );
  64. glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
  65. glVertex2f( -1.0, 1.0 );
  66. glEnd();
  67. }
  68. glPopMatrix();
  69. glutSwapBuffers();
  70. }
  71. static void idle( void )
  72. {
  73. Angle += 2.0;
  74. glutPostRedisplay();
  75. }
  76. /* change view Angle, exit upon ESC */
  77. static void key(unsigned char k, int x, int y)
  78. {
  79. (void) x;
  80. (void) y;
  81. switch (k) {
  82. case 'a':
  83. UseArrays = !UseArrays;
  84. printf("UseArrays: %d\n", UseArrays);
  85. break;
  86. case ' ':
  87. Anim = !Anim;
  88. if (Anim)
  89. glutIdleFunc(idle);
  90. else
  91. glutIdleFunc(NULL);
  92. break;
  93. case 27:
  94. glDeleteTextures( 2, TexObj );
  95. glutDestroyWindow(Window);
  96. exit(0);
  97. }
  98. glutPostRedisplay();
  99. }
  100. /* new window size or exposure */
  101. static void reshape( int width, int height )
  102. {
  103. glViewport(0, 0, (GLint)width, (GLint)height);
  104. glMatrixMode(GL_PROJECTION);
  105. glLoadIdentity();
  106. /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
  107. glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
  108. glMatrixMode(GL_MODELVIEW);
  109. glLoadIdentity();
  110. glTranslatef( 0.0, 0.0, -8.0 );
  111. }
  112. static void init( void )
  113. {
  114. static int width=8, height=8;
  115. GLubyte tex[64][3];
  116. GLint i, j;
  117. /* generate texture object IDs */
  118. glGenTextures( 2, TexObj );
  119. /*
  120. * setup first texture object
  121. */
  122. glActiveTextureARB(GL_TEXTURE0_ARB);
  123. glEnable( GL_TEXTURE_2D );
  124. glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD );
  125. glBindTexture( GL_TEXTURE_2D, TexObj[0] );
  126. assert(glIsTexture(TexObj[0]));
  127. /* red over black */
  128. for (i=0;i<height;i++) {
  129. for (j=0;j<width;j++) {
  130. int p = i*width+j;
  131. if (i < height / 2) {
  132. tex[p][0] = 0; tex[p][1] = 0; tex[p][2] = 0;
  133. }
  134. else {
  135. tex[p][0] = 255; tex[p][1] = 0; tex[p][2] = 0;
  136. }
  137. }
  138. }
  139. glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
  140. GL_RGB, GL_UNSIGNED_BYTE, tex );
  141. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  142. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  143. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  144. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  145. /*
  146. * setup second texture object
  147. */
  148. glActiveTextureARB(GL_TEXTURE1_ARB);
  149. glEnable( GL_TEXTURE_2D );
  150. glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD );
  151. glBindTexture( GL_TEXTURE_2D, TexObj[1] );
  152. assert(glIsTexture(TexObj[1]));
  153. /* left=green, right = blue */
  154. for (i=0;i<height;i++) {
  155. for (j=0;j<width;j++) {
  156. int p = i*width+j;
  157. if (j < width / 2) {
  158. tex[p][0] = 0; tex[p][1] = 255; tex[p][2] = 0;
  159. }
  160. else {
  161. tex[p][0] = 0; tex[p][1] = 0; tex[p][2] = 255;
  162. }
  163. }
  164. }
  165. glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
  166. GL_RGB, GL_UNSIGNED_BYTE, tex );
  167. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  168. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  169. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  170. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  171. }
  172. int main( int argc, char *argv[] )
  173. {
  174. glutInit(&argc, argv);
  175. glutInitWindowPosition(0, 0);
  176. glutInitWindowSize(300, 300);
  177. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  178. Window = glutCreateWindow("Texture Objects");
  179. if (!Window) {
  180. exit(1);
  181. }
  182. init();
  183. init_arrays();
  184. glutReshapeFunc( reshape );
  185. glutKeyboardFunc( key );
  186. if (Anim)
  187. glutIdleFunc( idle );
  188. glutDisplayFunc( draw );
  189. glutMainLoop();
  190. return 0;
  191. }