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

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