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.

tri-multitex-vbo.c 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and
  5. * its documentation for any purpose is hereby granted without fee, provided
  6. * that (i) the above copyright notices and this permission notice appear in
  7. * all copies of the software and related documentation, and (ii) the name of
  8. * Silicon Graphics may not be used in any advertising or
  9. * publicity relating to the software without the specific, prior written
  10. * permission of Silicon Graphics.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13. * ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <GL/glew.h>
  28. #include <GL/glut.h>
  29. #define NR_VERTS 4
  30. struct {
  31. GLfloat position[NR_VERTS][4];
  32. GLubyte color[NR_VERTS][4];
  33. GLfloat tex0[NR_VERTS][2];
  34. GLfloat tex1[NR_VERTS][2];
  35. } verts = {
  36. { { 0.9, -0.9, 0.0, 1.0 },
  37. { 0.9, 0.9, 0.0, 1.0 },
  38. { -0.9, 0.9, 0.0, 1.0 },
  39. { -0.9, -0.9, 0.0, 1.0 } },
  40. { { 0x00, 0x00, 0xff, 0x00 },
  41. { 0x00, 0xff, 0x00, 0x00 },
  42. { 0xff, 0x00, 0x00, 0x00 },
  43. { 0xff, 0xff, 0xff, 0x00 }
  44. },
  45. { { 1, -1 },
  46. { 1, 1 },
  47. { -1, 1 },
  48. { -1, -1 } },
  49. { { 3, 0 },
  50. { 0, 3 },
  51. { -3, 0 },
  52. { 0, -3} },
  53. };
  54. GLuint indices[] = { 0, 1, 2, 3 };
  55. GLuint arrayObj, elementObj;
  56. GLenum doubleBuffer;
  57. #define Offset(ptr, member) (void *)((const char *)&((ptr)->member) - (const char *)(ptr))
  58. static void Init(void)
  59. {
  60. GLuint texObj[2];
  61. fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  62. fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  63. fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  64. fflush(stderr);
  65. glClearColor(0.0, 0.0, 1.0, 0.0);
  66. glGenTextures(2, texObj);
  67. #define SIZE 32
  68. {
  69. GLubyte tex2d[SIZE][SIZE][3];
  70. GLint s, t;
  71. for (s = 0; s < SIZE; s++) {
  72. for (t = 0; t < SIZE; t++) {
  73. tex2d[t][s][0] = s*255/(SIZE-1);
  74. tex2d[t][s][1] = t*255/(SIZE-1);
  75. tex2d[t][s][2] = 0;
  76. }
  77. }
  78. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  79. glActiveTextureARB(GL_TEXTURE0_ARB);
  80. glBindTexture(GL_TEXTURE_2D, texObj[0]);
  81. glTexImage2D(GL_TEXTURE_2D, 0, 3, SIZE, SIZE, 0,
  82. GL_RGB, GL_UNSIGNED_BYTE, tex2d);
  83. glEnable(GL_TEXTURE_2D);
  84. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  85. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
  86. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  87. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  88. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  89. }
  90. {
  91. GLubyte tex2d[SIZE][SIZE][3];
  92. GLint s, t;
  93. for (s = 0; s < SIZE; s++) {
  94. for (t = 0; t < SIZE; t++) {
  95. GLboolean on = ((s/4) ^ (t/4)) & 1;
  96. tex2d[t][s][0] = on ? 128 : 0;
  97. tex2d[t][s][1] = on ? 128 : 0;
  98. tex2d[t][s][2] = on ? 128 : 0;
  99. }
  100. }
  101. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  102. glActiveTextureARB(GL_TEXTURE1_ARB);
  103. glBindTexture(GL_TEXTURE_2D, texObj[1]);
  104. glTexImage2D(GL_TEXTURE_2D, 0, 3, SIZE, SIZE, 0,
  105. GL_RGB, GL_UNSIGNED_BYTE, tex2d);
  106. glEnable(GL_TEXTURE_2D);
  107. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
  108. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
  109. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  110. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  111. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  112. }
  113. glActiveTextureARB( GL_TEXTURE0_ARB );
  114. {
  115. glGenBuffersARB(1, &arrayObj);
  116. glGenBuffersARB(1, &elementObj);
  117. glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrayObj);
  118. glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, elementObj);
  119. glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), &verts, GL_STATIC_DRAW_ARB);
  120. glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(indices), indices, GL_STATIC_DRAW_ARB);
  121. glEnableClientState( GL_VERTEX_ARRAY );
  122. glVertexPointer( 4, GL_FLOAT, 0, Offset(&verts, position) );
  123. glEnableClientState( GL_COLOR_ARRAY );
  124. glColorPointer( 4, GL_UNSIGNED_BYTE, 0, Offset(&verts, color) );
  125. glClientActiveTextureARB( GL_TEXTURE0_ARB );
  126. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  127. glTexCoordPointer( 2, GL_FLOAT, 0, Offset(&verts, tex0) );
  128. glClientActiveTextureARB( GL_TEXTURE1_ARB );
  129. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  130. glTexCoordPointer( 2, GL_FLOAT, 0, Offset(&verts, tex1) );
  131. glClientActiveTextureARB( GL_TEXTURE0_ARB );
  132. }
  133. }
  134. static void Reshape(int width, int height)
  135. {
  136. glViewport(0, 0, (GLint)width, (GLint)height);
  137. glMatrixMode(GL_PROJECTION);
  138. glLoadIdentity();
  139. /* glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); */
  140. glMatrixMode(GL_MODELVIEW);
  141. }
  142. static void Key(unsigned char key, int x, int y)
  143. {
  144. switch (key) {
  145. case 27:
  146. exit(1);
  147. default:
  148. break;
  149. }
  150. glutPostRedisplay();
  151. }
  152. static void Draw(void)
  153. {
  154. glClear(GL_COLOR_BUFFER_BIT);
  155. glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, NULL );
  156. glFlush();
  157. if (doubleBuffer) {
  158. glutSwapBuffers();
  159. }
  160. }
  161. static GLenum Args(int argc, char **argv)
  162. {
  163. GLint i;
  164. doubleBuffer = GL_FALSE;
  165. for (i = 1; i < argc; i++) {
  166. if (strcmp(argv[i], "-sb") == 0) {
  167. doubleBuffer = GL_FALSE;
  168. } else if (strcmp(argv[i], "-db") == 0) {
  169. doubleBuffer = GL_TRUE;
  170. } else {
  171. fprintf(stderr, "%s (Bad option).\n", argv[i]);
  172. return GL_FALSE;
  173. }
  174. }
  175. return GL_TRUE;
  176. }
  177. int main(int argc, char **argv)
  178. {
  179. GLenum type;
  180. glutInit(&argc, argv);
  181. if (Args(argc, argv) == GL_FALSE) {
  182. exit(1);
  183. }
  184. glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
  185. type = GLUT_RGB | GLUT_ALPHA;
  186. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  187. glutInitDisplayMode(type);
  188. if (glutCreateWindow(*argv) == GL_FALSE) {
  189. exit(1);
  190. }
  191. glewInit();
  192. Init();
  193. glutReshapeFunc(Reshape);
  194. glutKeyboardFunc(Key);
  195. glutDisplayFunc(Draw);
  196. glutMainLoop();
  197. return 0;
  198. }