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.6KB

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