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