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
|
/* This source code is based on Direct3D-based code written by Stephen
Coy of Microsoft. All credit for the original implementation of the
idea should go to Stephen. While I referenced Stephen's code during
the writing of my code, my code was completely written by me from
scratch. The algorithms are basically the same to ease critical
comparison of the OpenGL and Direct3D versions. */
/* The image files used by this program are derived from images
developed by Microsoft. */
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#if !defined(GL_VERSION_1_1)
/* Assume the texture object extension is supported. */
#define glBindTexture(A,B) glBindTextureEXT(A,B)
#endif
#include "loadlum.h"
GLfloat red[3] = {1.0, 0.0, 0.0};
GLfloat green[3] = {0.0, 1.0, 0.0};
GLfloat blue[3] = {0.0, 0.0, 1.0};
GLfloat from[3] = {0.0, 0.0, 20.0};
GLfloat at[3] = {0.0, 0.0, 0.0};
GLfloat near_clip = 1.0;
int useMipmaps = 0, verbose = 0;
GLuint logoTex, flareTex[6], shineTex[10];
typedef struct t_flare {
int type; /* flare texture index, 0..5 */
float scale;
float loc; /* postion on axis */
GLfloat color[3];
} Flare;
Flare
set_flare(int type, float location, float scale, GLfloat color[3], float colorScale)
{
Flare ret;
ret.type = type;
ret.loc = location;
ret.scale = scale;
ret.color[0] = color[0] * colorScale;
ret.color[1] = color[1] * colorScale;
ret.color[2] = color[2] * colorScale;
return ret;
}
Flare flare[20];
int num_flares = 0;
static float tic = 0.0;
float position[3];
int shine_tic = 0;
void
init_flares(void)
{
/* Shines */
flare[0] = set_flare(-1, 1.0, 0.3, blue, 1.0);
flare[1] = set_flare(-1, 1.0, 0.2, green, 1.0);
flare[2] = set_flare(-1, 1.0, 0.25, red, 1.0);
/* Flares, ordered to eliminate redundant texture binds */
flare[3] = set_flare(1, 0.5, 0.2f, red, 0.3);
flare[4] = set_flare(2, 1.3, 0.04f, red, 0.6);
flare[5] = set_flare(3, 1.0, 0.1f, red, 0.4);
flare[6] = set_flare(3, 0.2, 0.05f, red, 0.3);
flare[7] = set_flare(0, 0.0, 0.04f, red, 0.3);
flare[8] = set_flare(5, -0.25, 0.07f, red, 0.5);
flare[9] = set_flare(5, -0.4, 0.02f, red, 0.6);
flare[10] = set_flare(5, -0.6, 0.04f, red, 0.4);
flare[11] = set_flare(5, -1.0, 0.03f, red, 0.2);
num_flares = 12;
}
#include "vec3d.c" /* Simple 3D vector math routines. */
void
DoFlares(GLfloat from[3], GLfloat at[3], GLfloat light[3], GLfloat near_clip)
{
GLfloat view_dir[3], tmp[3], light_dir[3], position[3], dx[3], dy[3],
center[3], axis[3], sx[3], sy[3], dot, global_scale = 1.5;
GLuint bound_to = 0;
int i;
/* view_dir = normalize(at-from) */
vdiff(view_dir, at, from);
vnorm(view_dir);
/* center = from + near_clip * view_dir */
vscale(tmp, view_dir, near_clip);
vadd(center, from, tmp);
/* light_dir = normalize(light-from) */
vdiff(light_dir, light, from);
vnorm(light_dir);
/* light = from + dot(light,view_dir)*near_clip*light_dir */
dot = vdot(light_dir, view_dir);
vscale(tmp, light_dir, near_clip / dot);
vadd(light, from, light_dir);
/* axis = light - center */
vdiff(axis, light, center);
vcopy(dx, axis);
/* dx = normalize(axis) */
vnorm(dx);
/* dy = cross(dx,view_dir) */
vcross(dy, dx, view_dir);
glDisable(GL_DEPTH_TEST);
glDisable(GL_DITHER);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
for (i = 0; i < num_flares; i++) {
vscale(sx, dx, flare[i].scale * global_scale);
vscale(sy, dy, flare[i].scale * global_scale);
glColor3fv(flare[i].color);
/* Note logic below to eliminate duplicate texture binds. */
if (flare[i].type < 0) {
if (bound_to)
glEnd();
glBindTexture(GL_TEXTURE_2D, shineTex[shine_tic]);
bound_to = shineTex[shine_tic];
shine_tic = (shine_tic + 1) % 10;
glBegin(GL_QUADS);
} else {
if (bound_to != flareTex[flare[i].type]) {
glEnd();
glBindTexture(GL_TEXTURE_2D, flareTex[flare[i].type]);
bound_to = flareTex[flare[i].type];
glBegin(GL_QUADS);
}
}
/* position = center + flare[i].loc * axis */
vscale(tmp, axis, flare[i].loc);
vadd(position, center, tmp);
glTexCoord2f(0.0, 0.0);
vadd(tmp, position, sx);
vadd(tmp, tmp, sy);
glVertex3fv(tmp);
glTexCoord2f(1.0, 0.0);
vdiff(tmp, position, sx);
vadd(tmp, tmp, sy);
glVertex3fv(tmp);
glTexCoord2f(1.0, 1.0);
vdiff(tmp, position, sx);
vdiff(tmp, tmp, sy);
glVertex3fv(tmp);
glTexCoord2f(0.0, 1.0);
vadd(tmp, position, sx);
vdiff(tmp, tmp, sy);
glVertex3fv(tmp);
}
glEnd();
}
void
DoBackground(void)
{
glEnable(GL_DITHER);
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, logoTex);
glBegin(GL_QUADS);
glColor3f(0.0, 0.0, 1.0);
glTexCoord2f(0.075, 0.1);
glVertex3f(-11.0, -7.0, 0.0);
glColor3f(0.8, 0.8, 1.0);
glTexCoord2f(1.0, 0.1);
glVertex3f(11.0, -7.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glTexCoord2f(1.0, 0.9);
glVertex3f(11.0, 7.0, 0.0);
glColor3f(0.0, 0.5, 1.0);
glTexCoord2f(0.075, 0.9);
glVertex3f(-11.0, 7.0, 0.0);
glEnd();
}
void
display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
position[0] = sin(tic * 0.73) * 6;
position[1] = 4.0 + 8.0 * sin(tic * 0.678);
position[2] = sin(tic * 0.895) * 6;
DoBackground();
DoFlares(from, at, position, near_clip);
glutSwapBuffers();
}
void
idle(void)
{
tic += 0.08f;
glutPostRedisplay();
}
void
visible(int vis)
{
if (vis == GLUT_VISIBLE)
glutIdleFunc(idle);
else
glutIdleFunc(NULL);
}
void
setup_texture(char *filename, GLuint texobj,
GLenum minFilter, GLenum maxFilter)
{
unsigned char *buf;
int width, height, components;
glBindTexture(GL_TEXTURE_2D, texobj);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxFilter);
if (verbose)
printf("Loading %s:", filename);
buf = load_luminance(filename, &width, &height, &components);
if (verbose)
printf(" %dx%dx%d\n", width, height, components);
if (useMipmaps)
gluBuild2DMipmaps(GL_TEXTURE_2D, 1, width, height,
GL_LUMINANCE, GL_UNSIGNED_BYTE, buf);
else
glTexImage2D(GL_TEXTURE_2D, 0, 1, width, height, 0,
GL_LUMINANCE, GL_UNSIGNED_BYTE, buf);
free(buf);
}
void
load_textures(void)
{
char filename[256];
GLenum minFilter, maxFilter;
int id = 1, i;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
if (useMipmaps) {
minFilter = GL_LINEAR_MIPMAP_LINEAR;
maxFilter = GL_LINEAR;
} else {
minFilter = GL_LINEAR;
maxFilter = GL_LINEAR;
}
logoTex = id;
setup_texture("OpenGL.bw", logoTex, minFilter, maxFilter);
id++;
if (!useMipmaps) {
minFilter = GL_NEAREST;
maxFilter = GL_NEAREST;
}
for (i = 0; i < 10; i++) {
shineTex[i] = id;
sprintf(filename, "Shine%d.bw", i);
setup_texture(filename, shineTex[i], minFilter, maxFilter);
id++;
}
for (i = 0; i < 6; i++) {
flareTex[i] = id;
sprintf(filename, "Flare%d.bw", i + 1);
setup_texture(filename, flareTex[i], minFilter, maxFilter);
id++;
}
}
int
main(int argc, char **argv)
{
int i;
glutInit(&argc, argv);
for (i=1; i<argc; i++) {
if (!strcmp(argv[i], "-mipmap"))
useMipmaps = 1;
else if (!strcmp(argv[i], "-v"))
verbose = 1;
}
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("glflare");
glutDisplayFunc(display);
glutVisibilityFunc(visible);
init_flares();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(
60.0, /* field of view in degree */
1.0, /* aspect ratio */
0.5, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
from[0], from[1], from[2],
at[0], at[1], at[2],
0.0, 1.0, 0.); /* up is in positive Y direction */
load_textures();
glEnable(GL_TEXTURE_2D);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
|