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.

texrect.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* GL_NV_texture_rectangle test
  2. *
  3. * Brian Paul
  4. * 14 June 2002
  5. */
  6. #define GL_GLEXT_PROTOTYPES
  7. #include <math.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <GL/glut.h>
  12. #include "readtex.h"
  13. #define TEXTURE_0_FILE "../images/girl.rgb"
  14. #define TEXTURE_1_FILE "../images/reflect.rgb"
  15. #define TEX0 1
  16. #define TEX7 8
  17. #define ANIMATE 10
  18. #define CLAMP 20
  19. #define CLAMP_TO_EDGE 21
  20. #define CLAMP_TO_BORDER 22
  21. #define LINEAR_FILTER 30
  22. #define NEAREST_FILTER 31
  23. #define QUIT 100
  24. static GLboolean Animate = GL_FALSE;
  25. static GLint NumUnits = 2;
  26. static GLboolean TexEnabled[8];
  27. static GLint Width[8], Height[8]; /* image sizes */
  28. static GLenum Format[8];
  29. static GLfloat Xrot = 00.0, Yrot = 00.0, Zrot = 0.0;
  30. static void Idle( void )
  31. {
  32. Zrot = glutGet(GLUT_ELAPSED_TIME) * 0.01;
  33. glutPostRedisplay();
  34. }
  35. static void DrawObject(void)
  36. {
  37. GLint i;
  38. GLfloat d = 10; /* so we can see how borders are handled */
  39. glColor3f(.1, .1, .1); /* modulate this */
  40. glPushMatrix();
  41. glRotatef(Zrot, 0, 0, 1);
  42. glBegin(GL_QUADS);
  43. for (i = 0; i < NumUnits; i++)
  44. glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, -d, -d);
  45. glVertex2f(-1.0, -1.0);
  46. for (i = 0; i < NumUnits; i++)
  47. glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, Width[i]+d, -d);
  48. glVertex2f(1.0, -1.0);
  49. for (i = 0; i < NumUnits; i++)
  50. glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, Width[i]+d, Height[i]+d);
  51. glVertex2f(1.0, 1.0);
  52. for (i = 0; i < NumUnits; i++)
  53. glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, -d, Height[i]+d);
  54. glVertex2f(-1.0, 1.0);
  55. glEnd();
  56. glPopMatrix();
  57. }
  58. static void Display( void )
  59. {
  60. glClear( GL_COLOR_BUFFER_BIT );
  61. glPushMatrix();
  62. glRotatef(Xrot, 1.0, 0.0, 0.0);
  63. glRotatef(Yrot, 0.0, 1.0, 0.0);
  64. glRotatef(Zrot, 0.0, 0.0, 1.0);
  65. glScalef(5.0, 5.0, 5.0);
  66. DrawObject();
  67. glPopMatrix();
  68. glutSwapBuffers();
  69. }
  70. static void Reshape( int width, int height )
  71. {
  72. glViewport( 0, 0, width, height );
  73. glMatrixMode( GL_PROJECTION );
  74. glLoadIdentity();
  75. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 100.0 );
  76. glMatrixMode( GL_MODELVIEW );
  77. glLoadIdentity();
  78. glTranslatef( 0.0, 0.0, -35.0 );
  79. }
  80. static void ModeMenu(int entry)
  81. {
  82. GLint i;
  83. if (entry >= TEX0 && entry < TEX0 + NumUnits) {
  84. /* toggle */
  85. i = entry - TEX0;
  86. TexEnabled[i] = !TexEnabled[i];
  87. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  88. if (TexEnabled[i]) {
  89. glEnable(GL_TEXTURE_RECTANGLE_NV);
  90. }
  91. else {
  92. glDisable(GL_TEXTURE_RECTANGLE_NV);
  93. }
  94. printf("Enabled: ");
  95. for (i = 0; i < NumUnits; i++)
  96. printf("%d ", (int) TexEnabled[i]);
  97. printf("\n");
  98. }
  99. else if (entry==ANIMATE) {
  100. Animate = !Animate;
  101. if (Animate)
  102. glutIdleFunc(Idle);
  103. else
  104. glutIdleFunc(NULL);
  105. }
  106. else if (entry==CLAMP) {
  107. for (i = 0; i < NumUnits; i++) {
  108. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  109. glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP);
  110. glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP);
  111. }
  112. }
  113. else if (entry==CLAMP_TO_EDGE) {
  114. for (i = 0; i < NumUnits; i++) {
  115. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  116. glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  117. glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  118. }
  119. }
  120. else if (entry==CLAMP_TO_BORDER) {
  121. for (i = 0; i < NumUnits; i++) {
  122. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  123. glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  124. glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  125. }
  126. }
  127. else if (entry==NEAREST_FILTER) {
  128. for (i = 0; i < NumUnits; i++) {
  129. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  130. glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  131. glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  132. }
  133. }
  134. else if (entry==LINEAR_FILTER) {
  135. for (i = 0; i < NumUnits; i++) {
  136. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  137. glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  138. glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  139. }
  140. }
  141. else if (entry==QUIT) {
  142. exit(0);
  143. }
  144. glutPostRedisplay();
  145. }
  146. static void Key( unsigned char key, int x, int y )
  147. {
  148. (void) x;
  149. (void) y;
  150. switch (key) {
  151. case 'z':
  152. Zrot -= 1.0;
  153. break;
  154. case 'Z':
  155. Zrot += 1.0;
  156. break;
  157. case 'a':
  158. Animate = !Animate;
  159. if (Animate)
  160. glutIdleFunc(Idle);
  161. else
  162. glutIdleFunc(NULL);
  163. break;
  164. case 27:
  165. exit(0);
  166. break;
  167. }
  168. glutPostRedisplay();
  169. }
  170. static void SpecialKey( int key, int x, int y )
  171. {
  172. float step = 3.0;
  173. (void) x;
  174. (void) y;
  175. switch (key) {
  176. case GLUT_KEY_UP:
  177. Xrot += step;
  178. break;
  179. case GLUT_KEY_DOWN:
  180. Xrot -= step;
  181. break;
  182. case GLUT_KEY_LEFT:
  183. Yrot += step;
  184. break;
  185. case GLUT_KEY_RIGHT:
  186. Yrot -= step;
  187. break;
  188. }
  189. glutPostRedisplay();
  190. }
  191. static void Init( int argc, char *argv[] )
  192. {
  193. const GLenum wrap = GL_CLAMP;
  194. GLuint texObj[8];
  195. GLint size, i;
  196. if (!glutExtensionSupported("GL_ARB_multitexture")) {
  197. printf("Sorry, GL_ARB_multitexture needed by this program\n");
  198. exit(1);
  199. }
  200. if (!glutExtensionSupported("GL_NV_texture_rectangle")) {
  201. printf("Sorry, GL_NV_texture_rectangle needed by this program\n");
  202. exit(1);
  203. }
  204. glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
  205. printf("%d texture units supported, using 2.\n", NumUnits);
  206. if (NumUnits > 2)
  207. NumUnits = 2;
  208. glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_NV, &size);
  209. printf("%d x %d max texture rectangle size\n", size, size);
  210. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  211. for (i = 0; i < NumUnits; i++) {
  212. TexEnabled[i] = GL_TRUE;
  213. }
  214. /* allocate two texture objects */
  215. glGenTextures(NumUnits, texObj);
  216. /* setup the texture objects */
  217. for (i = 0; i < NumUnits; i++) {
  218. glActiveTextureARB(GL_TEXTURE0_ARB + i);
  219. glBindTexture(GL_TEXTURE_RECTANGLE_NV, texObj[i]);
  220. glTexParameteri(GL_TEXTURE_RECTANGLE_NV,
  221. GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  222. glTexParameteri(GL_TEXTURE_RECTANGLE_NV,
  223. GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  224. glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, wrap);
  225. glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, wrap);
  226. if (i == 0) {
  227. GLubyte *img = LoadRGBImage(TEXTURE_0_FILE, &Width[0], &Height[0],
  228. &Format[0]);
  229. if (!img) {
  230. printf("Error: couldn't load texture image\n");
  231. exit(1);
  232. }
  233. printf("Texture %d: %s (%d x %d)\n", i,
  234. TEXTURE_0_FILE, Width[0], Height[0]);
  235. glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB,
  236. Width[0], Height[0], 0,
  237. Format[0], GL_UNSIGNED_BYTE, img);
  238. }
  239. else {
  240. GLubyte *img = LoadRGBImage(TEXTURE_1_FILE, &Width[1], &Height[1],
  241. &Format[1]);
  242. if (!img) {
  243. printf("Error: couldn't load texture image\n");
  244. exit(1);
  245. }
  246. printf("Texture %d: %s (%d x %d)\n", i,
  247. TEXTURE_1_FILE, Width[1], Height[1]);
  248. glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB,
  249. Width[1], Height[1], 0,
  250. Format[1], GL_UNSIGNED_BYTE, img);
  251. }
  252. if (i < 1)
  253. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
  254. else
  255. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
  256. if (TexEnabled[i])
  257. glEnable(GL_TEXTURE_RECTANGLE_NV);
  258. }
  259. glShadeModel(GL_FLAT);
  260. glClearColor(0.3, 0.3, 0.4, 1.0);
  261. if (argc > 1 && strcmp(argv[1], "-info")==0) {
  262. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  263. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  264. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  265. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  266. }
  267. }
  268. int main( int argc, char *argv[] )
  269. {
  270. GLint i;
  271. glutInit( &argc, argv );
  272. glutInitWindowSize( 300, 300 );
  273. glutInitWindowPosition( 0, 0 );
  274. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  275. glutCreateWindow(argv[0] );
  276. Init( argc, argv );
  277. glutReshapeFunc( Reshape );
  278. glutKeyboardFunc( Key );
  279. glutSpecialFunc( SpecialKey );
  280. glutDisplayFunc( Display );
  281. if (Animate)
  282. glutIdleFunc( Idle );
  283. glutCreateMenu(ModeMenu);
  284. for (i = 0; i < NumUnits; i++) {
  285. char s[100];
  286. sprintf(s, "Toggle Texture %d", i);
  287. glutAddMenuEntry(s, TEX0 + i);
  288. }
  289. glutAddMenuEntry("Toggle Animation", ANIMATE);
  290. glutAddMenuEntry("GL_CLAMP", CLAMP);
  291. glutAddMenuEntry("GL_CLAMP_TO_EDGE", CLAMP_TO_EDGE);
  292. glutAddMenuEntry("GL_CLAMP_TO_BORDER", CLAMP_TO_BORDER);
  293. glutAddMenuEntry("GL_NEAREST", NEAREST_FILTER);
  294. glutAddMenuEntry("GL_LINEAR", LINEAR_FILTER);
  295. glutAddMenuEntry("Quit", QUIT);
  296. glutAttachMenu(GLUT_RIGHT_BUTTON);
  297. glutMainLoop();
  298. return 0;
  299. }