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

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