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
|
/* accumaa.c - by Tom McReynolds, SGI */
/* Using the accumulation buffer for scene antialiasing. */
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
const GLdouble FRUSTDIM = 100.f;
/* Create a single component texture map */
GLfloat *
make_texture(int maxs, int maxt)
{
int s, t;
static GLfloat *texture;
texture = (GLfloat *) malloc(maxs * maxt * sizeof(GLfloat));
for (t = 0; t < maxt; t++) {
for (s = 0; s < maxs; s++) {
texture[s + maxs * t] = ((s >> 4) & 0x1) ^ ((t >> 4) & 0x1);
}
}
return texture;
}
enum {
SPHERE = 1, CONE
};
void
render(void)
{
/* material properties for objects in scene */
static GLfloat wall_mat[] =
{1.f, 1.f, 1.f, 1.f};
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
/* Note: wall verticies are ordered so they are all front facing this lets
me do back face culling to speed things up. */
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, wall_mat);
/* floor */
/* make the floor textured */
glEnable(GL_TEXTURE_2D);
/* Since we want to turn texturing on for floor only, we have to make floor
a separate glBegin()/glEnd() sequence. You can't turn texturing on and
off between begin and end calls */
glBegin(GL_QUADS);
glNormal3f(0.f, 1.f, 0.f);
glTexCoord2i(0, 0);
glVertex3f(-100.f, -100.f, -320.f);
glTexCoord2i(1, 0);
glVertex3f(100.f, -100.f, -320.f);
glTexCoord2i(1, 1);
glVertex3f(100.f, -100.f, -520.f);
glTexCoord2i(0, 1);
glVertex3f(-100.f, -100.f, -520.f);
glEnd();
glDisable(GL_TEXTURE_2D);
/* walls */
glBegin(GL_QUADS);
/* left wall */
glNormal3f(1.f, 0.f, 0.f);
glVertex3f(-100.f, -100.f, -320.f);
glVertex3f(-100.f, -100.f, -520.f);
glVertex3f(-100.f, 100.f, -520.f);
glVertex3f(-100.f, 100.f, -320.f);
/* right wall */
glNormal3f(-1.f, 0.f, 0.f);
glVertex3f(100.f, -100.f, -320.f);
glVertex3f(100.f, 100.f, -320.f);
glVertex3f(100.f, 100.f, -520.f);
glVertex3f(100.f, -100.f, -520.f);
/* ceiling */
glNormal3f(0.f, -1.f, 0.f);
glVertex3f(-100.f, 100.f, -320.f);
glVertex3f(-100.f, 100.f, -520.f);
glVertex3f(100.f, 100.f, -520.f);
glVertex3f(100.f, 100.f, -320.f);
/* back wall */
glNormal3f(0.f, 0.f, 1.f);
glVertex3f(-100.f, -100.f, -520.f);
glVertex3f(100.f, -100.f, -520.f);
glVertex3f(100.f, 100.f, -520.f);
glVertex3f(-100.f, 100.f, -520.f);
glEnd();
glPushMatrix();
glTranslatef(-80.f, -60.f, -420.f);
glCallList(SPHERE);
glPopMatrix();
glPushMatrix();
glTranslatef(-20.f, -80.f, -500.f);
glCallList(CONE);
glPopMatrix();
if (glGetError()) /* to catch programming errors; should never happen */
printf("Oops! I screwed up my OpenGL calls somewhere\n");
glFlush(); /* high end machines may need this */
}
/* compute scale factor for window->object space transform could use
gluUnProject(), but probably too much trouble */
void
computescale(GLfloat * sx, GLfloat * sy)
{
enum {
XORG, YORG, WID, HT
};
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
*sx = 2 * FRUSTDIM / viewport[WID];
*sy = 2 * FRUSTDIM / viewport[WID];
}
enum {
NONE, AA
};
int rendermode = NONE;
void
menu(int selection)
{
rendermode = selection;
glutPostRedisplay();
}
/* Called when window needs to be redrawn */
void
redraw(void)
{
int i, j;
int min, max;
int count;
GLfloat invx, invy;
GLfloat scale, dx, dy;
switch (rendermode) {
case NONE:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-FRUSTDIM, FRUSTDIM, -FRUSTDIM, FRUSTDIM, 320., 640.);
glMatrixMode(GL_MODELVIEW);
render();
break;
case AA:
min = -2;
max = -min + 1;
count = -2 * min + 1;
count *= count;
/* uniform scaling, less than one pixel wide */
scale = -.9f / min;
computescale(&invx, &invy);
glClear(GL_ACCUM_BUFFER_BIT);
for (j = min; j < max; j++) {
for (i = min; i < max; i++) {
dx = invx * scale * i;
dy = invy * scale * j;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-FRUSTDIM + dx,
FRUSTDIM + dy,
-FRUSTDIM + dx,
FRUSTDIM + dy,
320., 640.);
glMatrixMode(GL_MODELVIEW);
render();
glAccum(GL_ACCUM, 1.f / count);
}
}
glAccum(GL_RETURN, 1.f);
break;
}
glutSwapBuffers();
}
/* ARGSUSED1 */
void
key(unsigned char key, int x, int y)
{
if (key == '\033')
exit(0);
}
const int TEXDIM = 256;
/* Parse arguments, and set up interface between OpenGL and window system */
int
main(int argc, char *argv[])
{
GLfloat *tex;
static GLfloat lightpos[] =
{50.f, 50.f, -320.f, 1.f};
static GLfloat sphere_mat[] =
{1.f, .5f, 0.f, 1.f};
static GLfloat cone_mat[] =
{0.f, .5f, 1.f, 1.f};
GLUquadricObj *sphere, *cone, *base;
glutInit(&argc, argv);
glutInitWindowSize(512, 512);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_ACCUM | GLUT_DOUBLE);
(void) glutCreateWindow("scene antialiasing");
glutDisplayFunc(redraw);
glutKeyboardFunc(key);
glutCreateMenu(menu);
glutAddMenuEntry("Aliased View", NONE);
glutAddMenuEntry("AntiAliased", AA);
glutAttachMenu(GLUT_RIGHT_BUTTON);
/* draw a perspective scene */
glMatrixMode(GL_PROJECTION);
glFrustum(-FRUSTDIM, FRUSTDIM, -FRUSTDIM, FRUSTDIM, 320., 640.);
glMatrixMode(GL_MODELVIEW);
/* turn on features */
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
/* place light 0 in the right place */
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
/* remove back faces to speed things up */
glCullFace(GL_BACK);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glNewList(SPHERE, GL_COMPILE);
/* make display lists for sphere and cone; for efficiency */
sphere = gluNewQuadric();
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, sphere_mat);
gluSphere(sphere, 20.f, 20, 20);
gluDeleteQuadric(sphere);
glEndList();
glNewList(CONE, GL_COMPILE);
cone = gluNewQuadric();
base = gluNewQuadric();
glRotatef(-90.f, 1.f, 0.f, 0.f);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cone_mat);
gluDisk(base, 0., 20., 20, 1);
gluCylinder(cone, 20., 0., 60., 20, 20);
gluDeleteQuadric(cone);
gluDeleteQuadric(base);
glEndList();
/* load pattern for current 2d texture */
tex = make_texture(TEXDIM, TEXDIM);
glTexImage2D(GL_TEXTURE_2D, 0, 1, TEXDIM, TEXDIM, 0, GL_RED, GL_FLOAT, tex);
free(tex);
glReadBuffer(GL_BACK); /* input to accum buffer */
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
|