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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
/**
* Implement glRasterPos + glBitmap with textures + shaders.
* Brian Paul
* 14 May 2007
*/
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glext.h>
#include "extfuncs.h"
static GLuint FragShader;
static GLuint VertShader;
static GLuint Program;
static GLint Win = 0;
static GLint WinWidth = 500, WinHeight = 500;
static GLboolean Anim = GL_TRUE;
static GLboolean Bitmap = GL_FALSE;
static GLfloat Xrot = 20.0f, Yrot = 70.0f;
static GLint uTex, uScale;
static GLuint Textures[2];
#define TEX_WIDTH 16
#define TEX_HEIGHT 8
static void
BitmapText(const char *s)
{
while (*s) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
s++;
}
}
static void
Redisplay(void)
{
static const GLfloat px[3] = { 1.2, 0, 0};
static const GLfloat nx[3] = {-1.2, 0, 0};
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(Xrot, 1.0f, 0.0f, 0.0f);
glRotatef(Yrot, 0.0f, 1.0f, 0.0f);
glEnable(GL_LIGHTING);
glPushMatrix();
glScalef(0.5, 0.5, 0.5);
glutSolidDodecahedron();
glPopMatrix();
glDisable(GL_LIGHTING);
glColor3f(0, 1, 0);
glBegin(GL_LINES);
glVertex3f(-1, 0, 0);
glVertex3f( 1, 0, 0);
glEnd();
glColor3f(1, 1, 0);
if (Bitmap) {
glRasterPos3fv(px);
BitmapText("+X");
glRasterPos3fv(nx);
BitmapText("-X");
}
else {
glUseProgram_func(Program);
/* vertex positions (deltas) depend on texture size and window size */
if (uScale != -1) {
glUniform2f_func(uScale,
2.0 * TEX_WIDTH / WinWidth,
2.0 * TEX_HEIGHT / WinHeight);
}
/* draw +X */
glBindTexture(GL_TEXTURE_2D, Textures[0]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3fv(px);
glTexCoord2f(1, 0); glVertex3fv(px);
glTexCoord2f(1, 1); glVertex3fv(px);
glTexCoord2f(0, 1); glVertex3fv(px);
glEnd();
/* draw -X */
glBindTexture(GL_TEXTURE_2D, Textures[1]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3fv(nx);
glTexCoord2f(1, 0); glVertex3fv(nx);
glTexCoord2f(1, 1); glVertex3fv(nx);
glTexCoord2f(0, 1); glVertex3fv(nx);
glEnd();
glUseProgram_func(0);
}
glPopMatrix();
glutSwapBuffers();
}
static void
Idle(void)
{
Yrot = glutGet(GLUT_ELAPSED_TIME) * 0.01;
glutPostRedisplay();
}
static void
Reshape(int width, int height)
{
WinWidth = width;
WinHeight = height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
}
static void
Key(unsigned char key, int x, int y)
{
(void) x;
(void) y;
switch(key) {
case ' ':
case 'a':
Anim = !Anim;
if (Anim)
glutIdleFunc(Idle);
else
glutIdleFunc(NULL);
break;
case 'b':
Bitmap = !Bitmap;
if (Bitmap)
printf("Using glBitmap\n");
else
printf("Using billboard texture\n");
break;
case 27:
glDeleteShader_func(FragShader);
glDeleteShader_func(VertShader);
glDeleteProgram_func(Program);
glutDestroyWindow(Win);
exit(0);
}
glutPostRedisplay();
}
static void
SpecialKey(int key, int x, int y)
{
const GLfloat step = 0.125f;
switch(key) {
case GLUT_KEY_UP:
Xrot -= step;
break;
case GLUT_KEY_DOWN:
Xrot += step;
break;
case GLUT_KEY_LEFT:
Yrot -= step;
break;
case GLUT_KEY_RIGHT:
Yrot += step;
break;
}
/*printf("Xrot: %f Yrot: %f\n", Xrot, Yrot);*/
glutPostRedisplay();
}
static void
MakeTexImage(const char *p, GLuint texobj)
{
GLubyte image[TEX_HEIGHT][TEX_WIDTH];
GLuint i, j, k;
for (i = 0; i < TEX_HEIGHT; i++) {
for (j = 0; j < TEX_WIDTH; j++) {
k = i * TEX_WIDTH + j;
if (p[k] == ' ') {
image[i][j] = 0;
}
else {
image[i][j] = 255;
}
}
}
glBindTexture(GL_TEXTURE_2D, texobj);
glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, TEX_WIDTH, TEX_HEIGHT, 0,
GL_RED, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
static void
MakeBitmapTextures(void)
{
const char *px =
" X X "
" X X X "
" X X X "
" XXXXX X "
" X X X "
" X X X "
" X X "
" X X ";
const char *nx =
" X X "
" X X "
" X X "
" XXXXX X "
" X X "
" X X "
" X X "
" X X ";
glGenTextures(2, Textures);
MakeTexImage(px, Textures[0]);
MakeTexImage(nx, Textures[1]);
}
static void
LoadAndCompileShader(GLuint shader, const char *text)
{
GLint stat;
glShaderSource_func(shader, 1, (const GLchar **) &text, NULL);
glCompileShader_func(shader);
glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
if (!stat) {
GLchar log[1000];
GLsizei len;
glGetShaderInfoLog_func(shader, 1000, &len, log);
fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log);
exit(1);
}
}
static void
CheckLink(GLuint prog)
{
GLint stat;
glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
if (!stat) {
GLchar log[1000];
GLsizei len;
glGetProgramInfoLog_func(prog, 1000, &len, log);
fprintf(stderr, "Linker error:\n%s\n", log);
}
}
static void
Init(void)
{
/* Fragment shader: modulate raster color by texture, discard fragments
* with alpha < 1.0
*/
static const char *fragShaderText =
"uniform sampler2D tex2d; \n"
"void main() {\n"
" vec4 c = texture2D(tex2d, gl_TexCoord[0].xy); \n"
" if (c.w < 1.0) \n"
" discard; \n"
" gl_FragColor = c * gl_Color; \n"
"}\n";
/* Vertex shader: compute new vertex position based on incoming vertex pos,
* texcoords and special scale factor.
*/
static const char *vertShaderText =
"uniform vec2 scale; \n"
"void main() {\n"
" vec4 p = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
" gl_Position.xy = p.xy + gl_MultiTexCoord0.xy * scale * p.w; \n"
" gl_Position.zw = p.zw; \n"
" gl_TexCoord[0] = gl_MultiTexCoord0; \n"
" gl_FrontColor = gl_Color; \n"
"}\n";
const char *version;
version = (const char *) glGetString(GL_VERSION);
if (version[0] != '2' || version[1] != '.') {
printf("This program requires OpenGL 2.x, found %s\n", version);
exit(1);
}
printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
GetExtensionFuncs();
FragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
LoadAndCompileShader(FragShader, fragShaderText);
VertShader = glCreateShader_func(GL_VERTEX_SHADER);
LoadAndCompileShader(VertShader, vertShaderText);
Program = glCreateProgram_func();
glAttachShader_func(Program, FragShader);
glAttachShader_func(Program, VertShader);
glLinkProgram_func(Program);
CheckLink(Program);
glUseProgram_func(Program);
uScale = glGetUniformLocation_func(Program, "scale");
uTex = glGetUniformLocation_func(Program, "tex2d");
if (uTex != -1) {
glUniform1i_func(uTex, 0); /* tex unit 0 */
}
glUseProgram_func(0);
glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHT0);
MakeBitmapTextures();
}
int
main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(WinWidth, WinHeight);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
Win = glutCreateWindow(argv[0]);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Key);
glutSpecialFunc(SpecialKey);
glutDisplayFunc(Redisplay);
if (Anim)
glutIdleFunc(Idle);
Init();
glutMainLoop();
return 0;
}
|