1/** 2 * Draw a series of textured quads after each quad, use glTexSubImage() 3 * to change one row of the texture image. 4 */ 5 6#include <stdio.h> 7#include <string.h> 8#include <stdlib.h> 9#include <GL/glew.h> 10#include "glut_wrap.h" 11 12 13static GLint Win = 0; 14static GLuint Tex = 0; 15 16 17static void Init(void) 18{ 19 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); 20 fflush(stderr); 21 22 glGenTextures(1, &Tex); 23 glBindTexture(GL_TEXTURE_2D, Tex); 24 25 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 26 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 27 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 28 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 29 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 30} 31 32 33static void Reshape(int width, int height) 34{ 35 float ar = (float) width / height; 36 glViewport(0, 0, width, height); 37 glMatrixMode(GL_PROJECTION); 38 glLoadIdentity(); 39 glOrtho(-ar, ar, -1.0, 1.0, -1.0, 1.0); 40 glMatrixMode(GL_MODELVIEW); 41} 42 43 44static void Key(unsigned char key, int x, int y) 45{ 46 if (key == 27) { 47 glDeleteTextures(1, &Tex); 48 glutDestroyWindow(Win); 49 exit(1); 50 } 51 glutPostRedisplay(); 52} 53 54 55static void Draw(void) 56{ 57 GLubyte tex[16][16][4]; 58 GLubyte row[16][4]; 59 int i, j; 60 61 for (i = 0; i < 16; i++) { 62 for (j = 0; j < 16; j++) { 63 if ((i + j) & 1) { 64 tex[i][j][0] = 128; 65 tex[i][j][1] = 128; 66 tex[i][j][2] = 128; 67 tex[i][j][3] = 255; 68 } 69 else { 70 tex[i][j][0] = 255; 71 tex[i][j][1] = 255; 72 tex[i][j][2] = 255; 73 tex[i][j][3] = 255; 74 } 75 } 76 } 77 78 for (i = 0; i < 16; i++) { 79 row[i][0] = 255; 80 row[i][1] = 0; 81 row[i][2] = 0; 82 row[i][3] = 255; 83 } 84 85 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, 86 GL_RGBA, GL_UNSIGNED_BYTE, tex); 87 glEnable(GL_TEXTURE_2D); 88 89 glClear(GL_COLOR_BUFFER_BIT); 90 91 for (i = 0; i < 9; i++) { 92 93 glPushMatrix(); 94 glTranslatef(-4.0 + i, 0, 0); 95 glScalef(0.5, 0.5, 1.0); 96 97 glBegin(GL_QUADS); 98 glTexCoord2f(1,0); 99 glVertex3f( 0.9, -0.9, 0.0); 100 glTexCoord2f(1,1); 101 glVertex3f( 0.9, 0.9, 0.0); 102 glTexCoord2f(0,1); 103 glVertex3f(-0.9, 0.9, 0.0); 104 glTexCoord2f(0,0); 105 glVertex3f(-0.9, -0.9, 0.0); 106 glEnd(); 107 108 glPopMatrix(); 109 110 /* replace a row of the texture image with red texels */ 111 if (i * 2 < 16) 112 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, i*2, 16, 1, 113 GL_RGBA, GL_UNSIGNED_BYTE, row); 114 } 115 116 117 glutSwapBuffers(); 118} 119 120 121int main(int argc, char **argv) 122{ 123 glutInit(&argc, argv); 124 glutInitWindowSize(900, 200); 125 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 126 Win = glutCreateWindow(*argv); 127 if (!Win) { 128 exit(1); 129 } 130 glewInit(); 131 Init(); 132 glutReshapeFunc(Reshape); 133 glutKeyboardFunc(Key); 134 glutDisplayFunc(Draw); 135 glutMainLoop(); 136 return 0; 137} 138