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.

tex1d.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Exercise 1D textures
  2. */
  3. #include <assert.h>
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "GL/glew.h"
  9. #include "GL/glut.h"
  10. static GLuint Window = 0;
  11. static GLuint TexObj[2];
  12. static GLfloat Angle = 0.0f;
  13. static void draw( void )
  14. {
  15. glClear( GL_COLOR_BUFFER_BIT );
  16. glColor3f( 1.0, 1.0, 1.0 );
  17. /* draw first polygon */
  18. glPushMatrix();
  19. glTranslatef( -1.0, 0.0, 0.0 );
  20. glRotatef( Angle, 0.0, 0.0, 1.0 );
  21. glBindTexture( GL_TEXTURE_1D, TexObj[0] );
  22. glBegin( GL_POLYGON );
  23. glTexCoord1f( 0.0 ); glVertex2f( -1.0, -1.0 );
  24. glTexCoord1f( 1.0 ); glVertex2f( 1.0, -1.0 );
  25. glTexCoord1f( 1.0 ); glVertex2f( 1.0, 1.0 );
  26. glTexCoord1f( 0.0 ); glVertex2f( -1.0, 1.0 );
  27. glEnd();
  28. glPopMatrix();
  29. glutSwapBuffers();
  30. }
  31. /*
  32. static void idle( void )
  33. {
  34. Angle += 2.0;
  35. glutPostRedisplay();
  36. }
  37. */
  38. /* change view Angle, exit upon ESC */
  39. static void key(unsigned char k, int x, int y)
  40. {
  41. (void) x;
  42. (void) y;
  43. switch (k) {
  44. case 27:
  45. exit(0);
  46. }
  47. }
  48. /* new window size or exposure */
  49. static void reshape( int width, int height )
  50. {
  51. glViewport(0, 0, (GLint)width, (GLint)height);
  52. glMatrixMode(GL_PROJECTION);
  53. glLoadIdentity();
  54. /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
  55. glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
  56. glMatrixMode(GL_MODELVIEW);
  57. glLoadIdentity();
  58. glTranslatef( 0.0, 0.0, -8.0 );
  59. }
  60. static void init( void )
  61. {
  62. GLubyte tex[256][3];
  63. GLint i;
  64. glDisable( GL_DITHER );
  65. /* Setup texturing */
  66. glEnable( GL_TEXTURE_1D );
  67. glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
  68. /* generate texture object IDs */
  69. glGenTextures( 2, TexObj );
  70. /* setup first texture object */
  71. glBindTexture( GL_TEXTURE_1D, TexObj[0] );
  72. for (i = 0; i < 256; i++) {
  73. GLfloat f;
  74. /* map 0..255 to -PI .. PI */
  75. f = ((i / 255.0) - .5) * (3.141592 * 2);
  76. f = sin(f);
  77. /* map -1..1 to 0..255 */
  78. tex[i][0] = (f+1.0)/2.0 * 255.0;
  79. tex[i][1] = 0;
  80. tex[i][2] = 0;
  81. }
  82. glTexImage1D( GL_TEXTURE_1D, 0, 3, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, tex );
  83. glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  84. glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  85. glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  86. glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  87. }
  88. int main( int argc, char *argv[] )
  89. {
  90. glutInit(&argc, argv);
  91. glutInitWindowPosition(0, 0);
  92. glutInitWindowSize(300, 300);
  93. glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
  94. Window = glutCreateWindow("Texture Objects");
  95. glewInit();
  96. if (!Window) {
  97. exit(1);
  98. }
  99. init();
  100. glutReshapeFunc( reshape );
  101. glutKeyboardFunc( key );
  102. /* glutIdleFunc( idle ); */
  103. glutDisplayFunc( draw );
  104. glutMainLoop();
  105. return 0;
  106. }