132001f49Smrg 232001f49Smrg/* Copyright (c) Mark J. Kilgard, 1994. */ 332001f49Smrg 432001f49Smrg/** 532001f49Smrg * (c) Copyright 1993, Silicon Graphics, Inc. 632001f49Smrg * ALL RIGHTS RESERVED 732001f49Smrg * Permission to use, copy, modify, and distribute this software for 832001f49Smrg * any purpose and without fee is hereby granted, provided that the above 932001f49Smrg * copyright notice appear in all copies and that both the copyright notice 1032001f49Smrg * and this permission notice appear in supporting documentation, and that 1132001f49Smrg * the name of Silicon Graphics, Inc. not be used in advertising 1232001f49Smrg * or publicity pertaining to distribution of the software without specific, 1332001f49Smrg * written prior permission. 1432001f49Smrg * 1532001f49Smrg * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" 1632001f49Smrg * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, 1732001f49Smrg * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR 1832001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON 1932001f49Smrg * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, 2032001f49Smrg * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY 2132001f49Smrg * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, 2232001f49Smrg * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF 2332001f49Smrg * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN 2432001f49Smrg * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON 2532001f49Smrg * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE 2632001f49Smrg * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE. 2732001f49Smrg * 2832001f49Smrg * US Government Users Restricted Rights 2932001f49Smrg * Use, duplication, or disclosure by the Government is subject to 3032001f49Smrg * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph 3132001f49Smrg * (c)(1)(ii) of the Rights in Technical Data and Computer Software 3232001f49Smrg * clause at DFARS 252.227-7013 and/or in similar or successor 3332001f49Smrg * clauses in the FAR or the DOD or NASA FAR Supplement. 3432001f49Smrg * Unpublished-- rights reserved under the copyright laws of the 3532001f49Smrg * United States. Contractor/manufacturer is Silicon Graphics, 3632001f49Smrg * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311. 3732001f49Smrg * 3832001f49Smrg * OpenGL(TM) is a trademark of Silicon Graphics, Inc. 3932001f49Smrg */ 4032001f49Smrg/* 4132001f49Smrg * fog.c 4232001f49Smrg * This program draws 5 red teapots, each at a different 4332001f49Smrg * z distance from the eye, in different types of fog. 4432001f49Smrg * Pressing the left mouse button chooses between 3 types of 4532001f49Smrg * fog: exponential, exponential squared, and linear. 4632001f49Smrg * In this program, there is a fixed density value, as well 4732001f49Smrg * as fixed start and end values for the linear fog. 4832001f49Smrg */ 4932001f49Smrg#include <stdlib.h> 5032001f49Smrg#include <math.h> 5132001f49Smrg#include "glut_wrap.h" 5232001f49Smrg 5332001f49SmrgGLint fogMode; 5432001f49Smrg 5532001f49Smrgstatic void 5632001f49SmrgselectFog(int mode) 5732001f49Smrg{ 5832001f49Smrg switch(mode) { 5932001f49Smrg case GL_LINEAR: 6032001f49Smrg glFogf(GL_FOG_START, 1.0); 6132001f49Smrg glFogf(GL_FOG_END, 5.0); 6232001f49Smrg /* falls through */ 6332001f49Smrg case GL_EXP2: 6432001f49Smrg case GL_EXP: 6532001f49Smrg glFogi(GL_FOG_MODE, mode); 6632001f49Smrg glutPostRedisplay(); 6732001f49Smrg break; 6832001f49Smrg case 0: 6932001f49Smrg exit(0); 7032001f49Smrg } 7132001f49Smrg} 7232001f49Smrg 7332001f49Smrg/* Initialize z-buffer, projection matrix, light source, 7432001f49Smrg * and lighting model. Do not specify a material property here. 7532001f49Smrg */ 7632001f49Smrgstatic void 7732001f49Smrgmyinit(void) 7832001f49Smrg{ 7932001f49Smrg GLfloat position[] = 8032001f49Smrg {0.0, 3.0, 3.0, 0.0}; 8132001f49Smrg GLfloat local_view[] = 8232001f49Smrg {0.0}; 8332001f49Smrg 8432001f49Smrg glEnable(GL_DEPTH_TEST); 8532001f49Smrg glDepthFunc(GL_LESS); 8632001f49Smrg 8732001f49Smrg glLightfv(GL_LIGHT0, GL_POSITION, position); 8832001f49Smrg glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view); 8932001f49Smrg 9032001f49Smrg glFrontFace(GL_CW); 9132001f49Smrg glEnable(GL_LIGHTING); 9232001f49Smrg glEnable(GL_LIGHT0); 9332001f49Smrg glEnable(GL_AUTO_NORMAL); 9432001f49Smrg glEnable(GL_NORMALIZE); 9532001f49Smrg glEnable(GL_FOG); 9632001f49Smrg { 9732001f49Smrg GLfloat fogColor[4] = 9832001f49Smrg {0.5, 0.5, 0.5, 1.0}; 9932001f49Smrg 10032001f49Smrg fogMode = GL_EXP; 10132001f49Smrg glFogi(GL_FOG_MODE, fogMode); 10232001f49Smrg glFogfv(GL_FOG_COLOR, fogColor); 10332001f49Smrg glFogf(GL_FOG_DENSITY, 0.35); 10432001f49Smrg glHint(GL_FOG_HINT, GL_DONT_CARE); 10532001f49Smrg glClearColor(0.5, 0.5, 0.5, 1.0); 10632001f49Smrg } 10732001f49Smrg} 10832001f49Smrg 10932001f49Smrgstatic void 11032001f49SmrgrenderRedTeapot(GLfloat x, GLfloat y, GLfloat z) 11132001f49Smrg{ 11232001f49Smrg float mat[4]; 11332001f49Smrg 11432001f49Smrg glPushMatrix(); 11532001f49Smrg glTranslatef(x, y, z); 11632001f49Smrg mat[0] = 0.1745; 11732001f49Smrg mat[1] = 0.01175; 11832001f49Smrg mat[2] = 0.01175; 11932001f49Smrg mat[3] = 1.0; 12032001f49Smrg glMaterialfv(GL_FRONT, GL_AMBIENT, mat); 12132001f49Smrg mat[0] = 0.61424; 12232001f49Smrg mat[1] = 0.04136; 12332001f49Smrg mat[2] = 0.04136; 12432001f49Smrg glMaterialfv(GL_FRONT, GL_DIFFUSE, mat); 12532001f49Smrg mat[0] = 0.727811; 12632001f49Smrg mat[1] = 0.626959; 12732001f49Smrg mat[2] = 0.626959; 12832001f49Smrg glMaterialfv(GL_FRONT, GL_SPECULAR, mat); 12932001f49Smrg glMaterialf(GL_FRONT, GL_SHININESS, 0.6 * 128.0); 13032001f49Smrg glutSolidTeapot(1.0); 13132001f49Smrg glPopMatrix(); 13232001f49Smrg} 13332001f49Smrg 13432001f49Smrg/* display() draws 5 teapots at different z positions. 13532001f49Smrg */ 13632001f49Smrgstatic void 13732001f49Smrgdisplay(void) 13832001f49Smrg{ 13932001f49Smrg glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 14032001f49Smrg renderRedTeapot(-4.0, -0.5, -1.0); 14132001f49Smrg renderRedTeapot(-2.0, -0.5, -2.0); 14232001f49Smrg renderRedTeapot(0.0, -0.5, -3.0); 14332001f49Smrg renderRedTeapot(2.0, -0.5, -4.0); 14432001f49Smrg renderRedTeapot(4.0, -0.5, -5.0); 14532001f49Smrg glFlush(); 14632001f49Smrg} 14732001f49Smrg 14832001f49Smrgstatic void 14932001f49SmrgmyReshape(int w, int h) 15032001f49Smrg{ 15132001f49Smrg glViewport(0, 0, w, h); 15232001f49Smrg glMatrixMode(GL_PROJECTION); 15332001f49Smrg glLoadIdentity(); 15432001f49Smrg if (w <= (h * 3)) 15532001f49Smrg glOrtho(-6.0, 6.0, -2.0 * ((GLfloat) h * 3) / (GLfloat) w, 15632001f49Smrg 2.0 * ((GLfloat) h * 3) / (GLfloat) w, 0.0, 10.0); 15732001f49Smrg else 15832001f49Smrg glOrtho(-6.0 * (GLfloat) w / ((GLfloat) h * 3), 15932001f49Smrg 6.0 * (GLfloat) w / ((GLfloat) h * 3), -2.0, 2.0, 0.0, 10.0); 16032001f49Smrg glMatrixMode(GL_MODELVIEW); 16132001f49Smrg glLoadIdentity(); 16232001f49Smrg} 16332001f49Smrg 16432001f49Smrgstatic void 16532001f49Smrgkey(unsigned char k, int x, int y) 16632001f49Smrg{ 16732001f49Smrg switch (k) { 16832001f49Smrg case 27: /* Escape */ 16932001f49Smrg exit(0); 17032001f49Smrg break; 17132001f49Smrg default: 17232001f49Smrg return; 17332001f49Smrg } 17432001f49Smrg glutPostRedisplay(); 17532001f49Smrg} 17632001f49Smrg 17732001f49Smrg/* Main Loop 17832001f49Smrg * Open window with initial window size, title bar, 17932001f49Smrg * RGBA display mode, depth buffer, and handle input events. 18032001f49Smrg */ 18132001f49Smrgint 18232001f49Smrgmain(int argc, char **argv) 18332001f49Smrg{ 18432001f49Smrg glutInit(&argc, argv); 18532001f49Smrg glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); 18632001f49Smrg glutInitWindowSize(450, 150); 18732001f49Smrg glutCreateWindow(argv[0]); 18832001f49Smrg myinit(); 18932001f49Smrg glutReshapeFunc(myReshape); 19032001f49Smrg glutDisplayFunc(display); 19132001f49Smrg glutCreateMenu(selectFog); 19232001f49Smrg glutAddMenuEntry("Fog EXP", GL_EXP); 19332001f49Smrg glutAddMenuEntry("Fog EXP2", GL_EXP2); 19432001f49Smrg glutAddMenuEntry("Fog LINEAR", GL_LINEAR); 19532001f49Smrg glutAddMenuEntry("Quit", 0); 19632001f49Smrg glutAttachMenu(GLUT_RIGHT_BUTTON); 19732001f49Smrg glutKeyboardFunc(key); 19832001f49Smrg glutMainLoop(); 19932001f49Smrg return 0; /* ANSI C requires main to return int. */ 20032001f49Smrg} 201