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.

texobj.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Example of using the 1.1 texture object functions.
  3. * Also, this demo utilizes Mesa's fast texture map path.
  4. *
  5. * Brian Paul June 1996 This file is in the public domain.
  6. */
  7. #include <assert.h>
  8. #include <math.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "GL/glut.h"
  13. static GLuint Window = 0;
  14. static GLuint TexObj[2];
  15. static GLfloat Angle = 0.0f;
  16. static GLboolean UseObj = GL_FALSE;
  17. #if defined(GL_VERSION_1_1) || defined(GL_VERSION_1_2)
  18. # define TEXTURE_OBJECT 1
  19. #elif defined(GL_EXT_texture_object)
  20. # define TEXTURE_OBJECT 1
  21. # define glBindTexture(A,B) glBindTextureEXT(A,B)
  22. # define glGenTextures(A,B) glGenTexturesEXT(A,B)
  23. # define glDeleteTextures(A,B) glDeleteTexturesEXT(A,B)
  24. #endif
  25. static void draw( void )
  26. {
  27. glClear( GL_COLOR_BUFFER_BIT );
  28. glColor3f( 1.0, 1.0, 1.0 );
  29. /* draw first polygon */
  30. glPushMatrix();
  31. glTranslatef( -1.0, 0.0, 0.0 );
  32. glRotatef( Angle, 0.0, 0.0, 1.0 );
  33. if (UseObj) {
  34. #ifdef TEXTURE_OBJECT
  35. glBindTexture( GL_TEXTURE_2D, TexObj[0] );
  36. #endif
  37. }
  38. else {
  39. glCallList( TexObj[0] );
  40. }
  41. glBegin( GL_POLYGON );
  42. glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
  43. glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
  44. glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
  45. glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
  46. glEnd();
  47. glPopMatrix();
  48. /* draw second polygon */
  49. glPushMatrix();
  50. glTranslatef( 1.0, 0.0, 0.0 );
  51. glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
  52. if (UseObj) {
  53. #ifdef TEXTURE_OBJECT
  54. glBindTexture( GL_TEXTURE_2D, TexObj[1] );
  55. #endif
  56. }
  57. else {
  58. glCallList( TexObj[1] );
  59. }
  60. glBegin( GL_POLYGON );
  61. glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
  62. glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
  63. glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
  64. glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
  65. glEnd();
  66. glPopMatrix();
  67. glutSwapBuffers();
  68. }
  69. static void idle( void )
  70. {
  71. static double t0 = -1.;
  72. double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  73. if (t0 < 0.0)
  74. t0 = t;
  75. dt = t - t0;
  76. t0 = t;
  77. Angle += 120.0*dt;
  78. glutPostRedisplay();
  79. }
  80. /* change view Angle, exit upon ESC */
  81. static void key(unsigned char k, int x, int y)
  82. {
  83. (void) x;
  84. (void) y;
  85. switch (k) {
  86. case 27:
  87. #ifdef TEXTURE_OBJECT
  88. glDeleteTextures( 2, TexObj );
  89. #endif
  90. glutDestroyWindow(Window);
  91. exit(0);
  92. }
  93. }
  94. /* new window size or exposure */
  95. static void reshape( int width, int height )
  96. {
  97. glViewport(0, 0, (GLint)width, (GLint)height);
  98. glMatrixMode(GL_PROJECTION);
  99. glLoadIdentity();
  100. /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
  101. glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
  102. glMatrixMode(GL_MODELVIEW);
  103. glLoadIdentity();
  104. glTranslatef( 0.0, 0.0, -8.0 );
  105. }
  106. static void init( void )
  107. {
  108. static int width=8, height=8;
  109. static GLubyte tex1[] = {
  110. 0, 0, 0, 0, 0, 0, 0, 0,
  111. 0, 0, 0, 0, 1, 0, 0, 0,
  112. 0, 0, 0, 1, 1, 0, 0, 0,
  113. 0, 0, 0, 0, 1, 0, 0, 0,
  114. 0, 0, 0, 0, 1, 0, 0, 0,
  115. 0, 0, 0, 0, 1, 0, 0, 0,
  116. 0, 0, 0, 1, 1, 1, 0, 0,
  117. 0, 0, 0, 0, 0, 0, 0, 0 };
  118. static GLubyte tex2[] = {
  119. 0, 0, 0, 0, 0, 0, 0, 0,
  120. 0, 0, 0, 2, 2, 0, 0, 0,
  121. 0, 0, 2, 0, 0, 2, 0, 0,
  122. 0, 0, 0, 0, 0, 2, 0, 0,
  123. 0, 0, 0, 0, 2, 0, 0, 0,
  124. 0, 0, 0, 2, 0, 0, 0, 0,
  125. 0, 0, 2, 2, 2, 2, 0, 0,
  126. 0, 0, 0, 0, 0, 0, 0, 0 };
  127. GLubyte tex[64][3];
  128. GLint i, j;
  129. glDisable( GL_DITHER );
  130. /* Setup texturing */
  131. glEnable( GL_TEXTURE_2D );
  132. glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
  133. glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
  134. /* generate texture object IDs */
  135. if (UseObj) {
  136. #ifdef TEXTURE_OBJECT
  137. glGenTextures( 2, TexObj );
  138. #endif
  139. }
  140. else {
  141. TexObj[0] = glGenLists(2);
  142. TexObj[1] = TexObj[0]+1;
  143. }
  144. /* setup first texture object */
  145. if (UseObj) {
  146. #ifdef TEXTURE_OBJECT
  147. glBindTexture( GL_TEXTURE_2D, TexObj[0] );
  148. assert(glIsTexture(TexObj[0]));
  149. #endif
  150. }
  151. else {
  152. glNewList( TexObj[0], GL_COMPILE );
  153. }
  154. /* red on white */
  155. for (i=0;i<height;i++) {
  156. for (j=0;j<width;j++) {
  157. int p = i*width+j;
  158. if (tex1[(height-i-1)*width+j]) {
  159. tex[p][0] = 255; tex[p][1] = 0; tex[p][2] = 0;
  160. }
  161. else {
  162. tex[p][0] = 255; tex[p][1] = 255; tex[p][2] = 255;
  163. }
  164. }
  165. }
  166. glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
  167. GL_RGB, GL_UNSIGNED_BYTE, tex );
  168. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  169. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  170. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  171. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  172. if (!UseObj) {
  173. glEndList();
  174. }
  175. /* end of texture object */
  176. /* setup second texture object */
  177. if (UseObj) {
  178. #ifdef TEXTURE_OBJECT
  179. glBindTexture( GL_TEXTURE_2D, TexObj[1] );
  180. assert(glIsTexture(TexObj[1]));
  181. #endif
  182. assert(!glIsTexture(TexObj[1] + 999));
  183. }
  184. else {
  185. glNewList( TexObj[1], GL_COMPILE );
  186. }
  187. /* green on blue */
  188. for (i=0;i<height;i++) {
  189. for (j=0;j<width;j++) {
  190. int p = i*width+j;
  191. if (tex2[(height-i-1)*width+j]) {
  192. tex[p][0] = 0; tex[p][1] = 255; tex[p][2] = 0;
  193. }
  194. else {
  195. tex[p][0] = 0; tex[p][1] = 0; tex[p][2] = 255;
  196. }
  197. }
  198. }
  199. glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
  200. GL_RGB, GL_UNSIGNED_BYTE, tex );
  201. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  202. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  203. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  204. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  205. if (!UseObj) {
  206. glEndList();
  207. }
  208. /* end texture object */
  209. }
  210. int main( int argc, char *argv[] )
  211. {
  212. glutInit(&argc, argv);
  213. glutInitWindowPosition(0, 0);
  214. glutInitWindowSize(300, 300);
  215. glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
  216. Window = glutCreateWindow("Texture Objects");
  217. if (!Window) {
  218. exit(1);
  219. }
  220. /* check that renderer has the GL_EXT_texture_object extension
  221. * or supports OpenGL 1.1
  222. */
  223. #ifdef TEXTURE_OBJECT
  224. {
  225. char *exten = (char *) glGetString( GL_EXTENSIONS );
  226. char *version = (char *) glGetString( GL_VERSION );
  227. if ( strstr( exten, "GL_EXT_texture_object" )
  228. || strncmp( version, "1.1", 3 )==0
  229. || strncmp( version, "1.2", 3 )==0 ) {
  230. UseObj = GL_TRUE;
  231. }
  232. }
  233. #endif
  234. init();
  235. glutReshapeFunc( reshape );
  236. glutKeyboardFunc( key );
  237. glutIdleFunc( idle );
  238. glutDisplayFunc( draw );
  239. glutMainLoop();
  240. return 0;
  241. }