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.

persp_hint.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Test the GL_PERSPECTIVE_CORRECTION_HINT setting and its effect on
  3. * color interpolation.
  4. *
  5. * Press 'i' to toggle between GL_NICEST/GL_FASTEST/GL_DONT_CARE.
  6. *
  7. * Depending on the driver, the hint may make a difference, or not.
  8. */
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <GL/glut.h>
  12. static GLenum PerspHint = GL_DONT_CARE;
  13. static void
  14. init(void)
  15. {
  16. GLubyte image[256][256][4];
  17. GLuint i, j;
  18. for (i = 0; i < 256; i++) {
  19. for (j = 0; j < 256; j++) {
  20. image[i][j][0] = j;
  21. image[i][j][1] = j;
  22. image[i][j][2] = j;
  23. image[i][j][3] = 255;
  24. }
  25. }
  26. glTexImage2D(GL_TEXTURE_2D, 0, 4, 256, 256, 0,
  27. GL_RGBA, GL_UNSIGNED_BYTE, image);
  28. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  29. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  30. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  31. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  32. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  33. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  34. }
  35. static void
  36. display(void)
  37. {
  38. switch (PerspHint) {
  39. case GL_NICEST:
  40. printf("hint = GL_NICEST\n");
  41. break;
  42. case GL_FASTEST:
  43. printf("hint = GL_FASTEST\n");
  44. break;
  45. case GL_DONT_CARE:
  46. printf("hint = GL_DONT_CARE\n");
  47. break;
  48. default:
  49. ;
  50. }
  51. glClear(GL_COLOR_BUFFER_BIT);
  52. #if 1
  53. glBegin(GL_QUADS);
  54. /* exercise perspective interpolation */
  55. glColor3f(0.0, 0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0);
  56. glColor3f(0.0, 0.0, 0.0); glVertex3f(-1.0, 1.0, -1.0);
  57. glColor3f(0.0, 1.0, 0.0); glVertex3f( 7.0, 1.0, -7.0);
  58. glColor3f(0.0, 1.0, 0.0); glVertex3f( 7.0, -1.0, -7.0);
  59. /* stripe of linear interpolation */
  60. glColor3f(0.0, 0.0, 0.0); glVertex3f(-1.0, -0.1, -1.001);
  61. glColor3f(0.0, 0.0, 0.0); glVertex3f(-1.0, 0.1, -1.001);
  62. glColor3f(0.0, 1.0, 0.0); glVertex3f( 1.0, 0.1, -1.001);
  63. glColor3f(0.0, 1.0, 0.0); glVertex3f( 1.0, -0.1, -1.001);
  64. glEnd();
  65. #else
  66. glEnable(GL_TEXTURE_2D);
  67. glBegin(GL_QUADS);
  68. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 0.0, -1.0);
  69. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0);
  70. glTexCoord2f(1.0, 1.0); glVertex3f( 5.0, 1.0, -7.0);
  71. glTexCoord2f(1.0, 0.0); glVertex3f( 5.0, 0.0, -7.0);
  72. glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.001);
  73. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 0.0, -1.001);
  74. glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 0.0, -1.001);
  75. glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.001);
  76. glEnd();
  77. #endif
  78. glFlush();
  79. }
  80. static void
  81. reshape(int w, int h)
  82. {
  83. glViewport(0, 0, w, h);
  84. glMatrixMode(GL_PROJECTION);
  85. glLoadIdentity();
  86. gluPerspective(90.0, (GLfloat) w / h, 1.0, 300.0);
  87. glMatrixMode(GL_MODELVIEW);
  88. glLoadIdentity();
  89. }
  90. static void
  91. key(unsigned char k, int x, int y)
  92. {
  93. switch (k) {
  94. case 27: /* Escape */
  95. exit(0);
  96. break;
  97. case 'i':
  98. if (PerspHint == GL_FASTEST)
  99. PerspHint = GL_NICEST;
  100. else if (PerspHint == GL_NICEST)
  101. PerspHint = GL_DONT_CARE;
  102. else
  103. PerspHint = GL_FASTEST;
  104. glHint(GL_PERSPECTIVE_CORRECTION_HINT, PerspHint);
  105. break;
  106. default:
  107. return;
  108. }
  109. glutPostRedisplay();
  110. }
  111. int
  112. main(int argc, char** argv)
  113. {
  114. glutInit(&argc, argv);
  115. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  116. glutInitWindowSize (500, 500);
  117. glutCreateWindow (argv[0]);
  118. glutReshapeFunc (reshape);
  119. glutDisplayFunc(display);
  120. glutKeyboardFunc(key);
  121. printf("Main quad: perspective projection\n");
  122. printf("Middle stripe: linear interpolation\n");
  123. printf("Press 'i' to toggle interpolation hint\n");
  124. init();
  125. glutMainLoop();
  126. return 0;
  127. }