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