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

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