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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
/* convolve.c - by Tom McReynolds, SGI */
/* Using the accumulation buffer for fast convolutions. */
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* convolution choices */
enum {CONV_NONE,
CONV_BOX_3X3,
CONV_BOX_5X5,
CONV_SOBEL_X,
CONV_LAPLACE
};
/* Filter contents and size */
typedef struct {
GLfloat scale; /* 1/scale applied to image to prevent overflow */
GLfloat bias; /* for biasing images */
int rows;
int cols;
GLfloat *array;
} Filter;
Filter *curmat; /* current filter to use for redrawing */
/* identity filter */
void
identity(Filter *mat)
{
int n, size;
size = mat->rows * mat->cols;
mat->array[0] = 1.f;
for(n = 1; n < size; n++)
mat->array[n] = 0.f;
mat->scale = 1.f;
mat->bias = 0.f;
}
/* create a new filter with identity filter in it */
Filter *
newfilter(int rows, int cols)
{
Filter *mat;
mat = (Filter *)malloc(sizeof(Filter));
mat->rows = rows;
mat->cols = cols;
mat->array = (GLfloat *)malloc(rows * cols * sizeof(GLfloat));
identity(mat);
return mat;
}
/* doesn't re-initialize matrix */
void
resize(Filter *mat, int rows, int cols)
{
if(mat->rows != rows ||
mat->cols != cols) {
free(mat->array);
mat->array = (GLfloat *)realloc(mat->array, rows * cols * sizeof(GLfloat));
}
mat->rows = rows;
mat->cols = cols;
}
/* box filter blur */
void
box(Filter *mat)
{
int n, count;
GLfloat blur;
count = mat->cols * mat->rows;
blur = 1.f/count;
for(n = 0; n < count; n++)
mat->array[n] = blur;
mat->scale = 1.f;
mat->bias = 0.f;
}
/* sobel filter */
void
sobel(Filter *mat)
{
static GLfloat sobel[] = {-.5f, 0.f, .5f,
-1.f, 0.f, 1.f,
-.5f, 0.f, .5f};
/* sobel is fixed size */
resize(mat, 3, 3); /* will do nothing if size is right already */
memcpy(mat->array, sobel, sizeof(sobel));
mat->scale = 2.f;
mat->bias = 0.f;
}
/* laplacian filter */
void
laplace(Filter *mat)
{
static GLfloat laplace[] = { 0.f, -.25f, 0.f,
-.25f, 1.f, -.25f,
0.f, -.25f, 0.f};
/* sobel is fixed size */
resize(mat, 3, 3); /* will do nothing if size is right already */
memcpy(mat->array, laplace, sizeof(laplace));
mat->scale = 4.f;
mat->bias = .125f;
}
/* add menu callback */
void menu(int filter)
{
switch(filter) {
case CONV_NONE:
resize(curmat, 1,1);
identity(curmat);
break;
case CONV_BOX_3X3:
resize(curmat, 3, 3);
box(curmat);
break;
case CONV_BOX_5X5:
resize(curmat, 5, 5);
box(curmat);
break;
case CONV_SOBEL_X:
sobel(curmat);
break;
case CONV_LAPLACE:
laplace(curmat);
break;
}
glutPostRedisplay();
}
int winWidth = 0;
int winHeight = 0;
/* used to get current width and height of viewport */
void
reshape(int wid, int ht)
{
glViewport(0, 0, wid, ht);
winWidth = wid;
winHeight = ht;
}
/* ARGSUSED1 */
void key(unsigned char key, int x, int y)
{
if(key == '\033')
exit(0);
}
/*
** 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();
}
void
convolve(void (*draw)(void), Filter *mat)
{
int i, j;
int imax, jmax;
imax = mat->cols;
jmax = mat->rows;
for(j = 0; j < jmax; j++) {
for(i = 0; i < imax; i++) {
glViewport(-i, -j, winWidth - i, winHeight - j);
draw();
glAccum(GL_ACCUM, mat->array[i + j * imax]);
}
}
}
/* Called when window needs to be redrawn */
void redraw(void)
{
glClearAccum(curmat->bias,
curmat->bias,
curmat->bias,
1.0);
glClear(GL_ACCUM_BUFFER_BIT);
convolve(render, curmat);
glViewport(0, 0, winWidth, winHeight);
glAccum(GL_RETURN, curmat->scale);
glutSwapBuffers();
if(glGetError()) /* to catch programming errors; should never happen */
printf("Oops! I screwed up my OpenGL calls somewhere\n");
}
const int TEXDIM = 256;
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("accumulation buffer convolve");
glutDisplayFunc(redraw);
glutKeyboardFunc(key);
glutReshapeFunc(reshape);
/* draw a perspective scene */
glMatrixMode(GL_PROJECTION);
glFrustum(-100., 100., -100., 100., 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);
/* make display lists for sphere and cone; for efficiency */
glNewList(SPHERE, GL_COMPILE);
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();
glutCreateMenu(menu);
glutAddMenuEntry("none", CONV_NONE);
glutAddMenuEntry("box filter (3x3 blur)", CONV_BOX_3X3);
glutAddMenuEntry("box filter (5x5 blur)", CONV_BOX_5X5);
glutAddMenuEntry("Sobel(x direction)", CONV_SOBEL_X);
glutAddMenuEntry("Laplace", CONV_LAPLACE);
glutAttachMenu(GLUT_RIGHT_BUTTON);
/* 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);
curmat = newfilter(1, 1);
identity(curmat);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
|