Thursday, September 29, 2011

Figure 47


$40 off Craftsman 5 Drawer Homeowner Tool Center with Riser and Parts Bins $99.99 on sale 10/2-10/8
Sears Deal of the Day: Thule Car Top Carrier, Quest
Wholesale Fleece Blankets
400950_BuddhaGroove.com-Culture Infused Living: Home Accents, Jewelry, Accessories from around the world!
400948_Culture Infused Living: Home Accents, Jewelry, and accessories from around the world. CulturalElemen
504427_Save up to 75% on batteries and accessories at Brooklyn Battery Works!
504080_10% OFF Orders of $75 or More from Journelle.com! Use Promo Code: NOV11 (Valid 11/1 - 11/30/11)
502239_Brand names, bargain prices at shoezoo.com. If you don't love your new shoes, return them for free!

400949_GaneshMall.com-Culture Infused Living: Home Accents, Jewelry, and accessories from around the world!


Figure 47
==========================



/*
//Author: Cheikh Drame
//To support, please do one of the following
//- Visit irap123.com many times a day
//- Join irap123.com
//- Buy my app irap123 (rhyming dictionary) available for android, and iphone. On windows phone, it is called iLyrics. If not, you can get the desktop version at www.irap123.com/apps/webstore/
//- Include irap123.com in your blogs, forums, facebook walls, email footers, comments
//- send a donation at my paypal address at cbdsigned.vevo@gmail.com
//- Buy a story at http://shop.pparzygnat.com
*/
// Polygon.cpp : Defines the entry point for the console application.
//




/* Copyright (c) Mark J. Kilgard, 1997. */

/* A version of the OpenGL Programming Guide's depthcue.c that
uses the "addfog" routines to add depth cueing as a post-rendering
pass. This is not a rather poor application for "addfog" but it
does demonstrate how it use "addfog". See "addfog.c" for details. */

/*
* (c) Copyright 1993, Silicon Graphics, Inc.
* ALL RIGHTS RESERVED
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the above
* copyright notice appear in all copies and that both the copyright notice
* and this permission notice appear in supporting documentation, and that
* the name of Silicon Graphics, Inc. not be used in advertising
* or publicity pertaining to distribution of the software without specific,
* written prior permission.
*
* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
* AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
* GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
* KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
* LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
* THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
* POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
*
* US Government Users Restricted Rights
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
* (c)(1)(ii) of the Rights in Technical Data and Computer Software
* clause at DFARS 252.227-7013 and/or in similar or successor
* clauses in the FAR or the DOD or NASA FAR Supplement.
* Unpublished-- rights reserved under the copyright laws of the
* United States. Contractor/manufacturer is Silicon Graphics,
* Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
/*
* depthcue.c
* This program draws a wireframe model, which uses
* intensity (brightness) to give clues to distance.
* Fog is used to achieve this effect.
*/
#include
#include
#include "addfog.h"

/* Initialize linear fog for depth cueing.
*/
void myinit(void)
{
GLfloat fogColor[4] = {0.0, 0.0, 0.0, 1.0};

glFogi (GL_FOG_MODE, GL_LINEAR);
glHint (GL_FOG_HINT, GL_NICEST); /* per pixel */
glFogf (GL_FOG_START, 3.0);
glFogf (GL_FOG_END, 5.0);
glFogfv (GL_FOG_COLOR, fogColor);
glClearColor(0.0, 0.0, 0.0, 1.0);

glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);

afEyeNearFar(3.0, 5.0);
afFogStartEnd(3.0, 5.0);
afFogMode(GL_LINEAR);
afFogColor(0.0, 0.0, 0.0);
}

int width, height;
int twopass = 1;

/* display() draws an icosahedron.
*/
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (!twopass) {
glEnable(GL_FOG);
} else {
glDisable(GL_FOG);
}

glColor3f (1.0, 1.0, 1.0);
glutWireIcosahedron();

if (twopass) {
afDoFinalFogPass(0, 0, width, height);
}

glutSwapBuffers();
}

void myReshape(int w, int h)
{
width = w;
height = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (45.0, (GLfloat) w/(GLfloat) h, 3.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.0, 0.0, -4.0); /* move object into view */
}

/* ARGSUSED1 */
void
keyboard(unsigned char c, int x, int y)
{
switch(c) {
case 'T':
case 't':
twopass = 1 - twopass;
case ' ':
glutPostRedisplay();
break;
}
}

void
menu(int value)
{
switch(value) {
case 1:
twopass = 0;
break;
case 2:
twopass = 1;
break;
}
glutPostRedisplay();
}

/* Main Loop
*/
int main(int argc, char** argv)
{
glutInitWindowSize(380, 380);
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("depthcuing by post-rendering pass");
myinit();
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutCreateMenu(menu);
glutAddMenuEntry("GL_LINEAR depthcueing", 1);
glutAddMenuEntry("\"add fog\" post-render depthcueing", 2);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
============

extern void afEyeNearFar(GLfloat fnear, GLfloat ffar);
extern void afFogStartEnd(GLfloat start, GLfloat end);
extern void afFogMode(GLenum mode);
extern void afFogDensity(GLfloat density);
extern void afFogColor(GLfloat red, GLfloat green, GLfloat blue);
extern void afDoFinalFogPass(GLint x, GLint y, GLsizei width, GLsizei height);

=============

/* */

/** Using the "addfog" routines:

1) Given your near and far ranges (generally from glOrtho, glFrustum, or
gluPerspective), call:

afEyeNearFar(near, far);

Careful since "near" and "far" are reserved words on PC compilers.

2) Instead calling glFog to set fog parameters, use the corresponding
"addfog" routines as shown below:

Instead of:

glFogf(GL_FOG_START, start); glFogf(GL_FOG_END, end);

Use:

afFogStartEnd(start, end);

Instead of:

glFogi(GL_FOG_MODE, mode);

Use:

afFogMode(mode);

Instead of:

glFogf(GL_FOG_DENSITY, density);

Use:

afFogDensity(density);

Instead of:

GLfloat fogcolor[4] = { red, green, blue, alpha };
glFogfv(GL_FOG_COLOR, fog_color);

Use:

afFogColor(red, green, blue);

3) Draw you scene *without* OpenGL fog enabled.

4) Assuming you want to fog the entire window of size width by
height, call:

afDoFinalFogPass(0, 0, width, height);

Note: x & y are OpenGL-style lower-left hand window coordinates.

5) Call glFinish or do a buffer swap.

That's it. View your fogged scene. */

#ifdef __sgi /* SGI has a good alloca; many other machines don't. */
#define HAS_ALLOCA
#endif

#include "stdafx.h"
#include
#ifdef HAS_ALLOCA
#include
#endif
#ifdef WIN32
#include
#endif
#include
#include

static GLfloat eye_near = 0.0, eye_far = 1.0;
static GLfloat fog_start = 0.0, fog_end = 1.0;
static GLenum fog_mode = GL_EXP;
static GLfloat fog_density = 1.0;
static int valid = 0;
static GLfloat fog_red, fog_green, fog_blue;

void
afEyeNearFar(GLfloat fnear, GLfloat ffar)
{
eye_near = fnear;
eye_far = ffar;
valid = 0;
}

void
afFogStartEnd(GLfloat start, GLfloat end)
{
fog_start = start;
fog_end = end;
valid = 0;
}

void
afFogMode(GLenum mode)
{
fog_mode = mode;
valid = 0;
}

void
afFogDensity(GLfloat density)
{
fog_density = density;
valid = 0;
}

void
afFogColor(GLfloat red, GLfloat green, GLfloat blue)
{
fog_red = red;
fog_green = green;
fog_blue = blue;
}

#define LEN 256

void
afDoFinalFogPass(GLint x, GLint y, GLsizei width, GLsizei height)
{
static GLfloat alpha_scale, alpha_bias;
static GLfloat fog_map[LEN];
int i;

#ifdef HAS_ALLOCA
void *buffer = alloca((unsigned int) sizeof(GLushort) * width * height);
#else
static void *buffer = NULL;
static int last_width, last_height;

if (width * height != last_width * last_height) {
buffer = realloc(buffer, sizeof(GLushort) * width * height);
last_width = width;
last_height = height;
}
#endif

if (!valid) {
switch (fog_mode) {
case GL_LINEAR:
/* Figure out linear fog blending from "f = (e-z)/(e-s)". */
alpha_scale = (eye_far - eye_near) / (fog_end - fog_start);
alpha_bias = (eye_near - fog_start) / (fog_end - fog_start);
break;
case GL_EXP:
/* Setup fog_map to be "f = exp(-d*z)". */
for (i = 0; i < LEN; i += 1) {
float fi, z, dz;

fi = i * 1.0 / (LEN - 1);
z = eye_near + fi * (eye_far - eye_near);
dz = fog_density * z;
fog_map[i] = 1.0 - exp(-dz);
}
break;
case GL_EXP2:
/* Setup fog_map to be "f = exp(-(d*z)^2)". */
for (i = 0; i < LEN; i += 1) {
float fi, z, dz;

fi = i * 1.0 / (LEN - 1);
z = eye_near + fi * (eye_far - eye_near);
dz = fog_density * z;
fog_map[i] = 1.0 - exp(-dz * dz);
}
break;
}
}

/* XXX Careful, afDoFinalFogPass makes no attempt to preserve your
pixel store state and assumes the initial pixel store state! */
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);

/* Preserve the current raster position, viewport, matrix mode,
blend function, enable state, and pixel path state. */
/* XXX This is pretty expensive. A real application should just
"know" to reload all the OpenGL state mucked with by afDoFinalFogPass
and then you could get rid of all this glPushAttrib and glPopMatrix
garbage. */
glPushAttrib(GL_PIXEL_MODE_BIT | GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT |
GL_TRANSFORM_BIT | GL_VIEWPORT_BIT | GL_CURRENT_BIT);

/* Reposition the current raster position as location (x,y). */
glMatrixMode(GL_MODELVIEW);
glViewport(x-1, y-1, 2, 2);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glRasterPos2i(0, 0);

/* Definitely don't want fog or depth enabled. */
glDisable(GL_FOG);
glDisable(GL_DEPTH_TEST);

/* The alpha on the glDrawPixels after the pixel path transformation
will be "1 - f" where f is the blending factor described in Section
3.9 of the OpenGL 1.1 specification. */
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

switch (fog_mode) {
case GL_LINEAR:
/* Force red, green, and blue to the fog color. */
glPixelTransferf(GL_RED_SCALE, 0);
glPixelTransferf(GL_GREEN_SCALE, 0);
glPixelTransferf(GL_BLUE_SCALE, 0);
glPixelTransferf(GL_RED_BIAS, fog_red);
glPixelTransferf(GL_GREEN_BIAS, fog_green);
glPixelTransferf(GL_BLUE_BIAS, fog_blue);

glPixelTransferf(GL_ALPHA_SCALE, alpha_scale);
glPixelTransferf(GL_ALPHA_BIAS, alpha_bias);
break;
case GL_EXP:
case GL_EXP2:
/* Force red, green, and blue to the fog color. */
glPixelMapfv(GL_PIXEL_MAP_R_TO_R, 1, &fog_red);
glPixelMapfv(GL_PIXEL_MAP_G_TO_G, 1, &fog_green);
glPixelMapfv(GL_PIXEL_MAP_B_TO_B, 1, &fog_blue);

glPixelMapfv(GL_PIXEL_MAP_A_TO_A, LEN, fog_map);
glPixelTransferi(GL_MAP_COLOR, GL_TRUE);
break;
}

/* Read out the depth buffer... */
glReadPixels(x, y, width, height,
GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, buffer);

/* ... and write it back as alpha. */
glDrawPixels(width, height, GL_ALPHA,
GL_UNSIGNED_SHORT, buffer);

/* Restore state saved earlier. */
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopAttrib();
}

No comments:

Post a Comment