PageRenderTime 455ms CodeModel.GetById 262ms app.highlight 10ms RepoModel.GetById 181ms app.codeStats 0ms

/opengles/redbook/fog.c

http://ftk.googlecode.com/
C | 182 lines | 116 code | 14 blank | 52 comment | 10 complexity | dd653fc2efc8c0400ee39c6444f9701b MD5 | raw file
  1/*
  2 * License Applicability. Except to the extent portions of this file are
  3 * made subject to an alternative license as permitted in the SGI Free
  4 * Software License B, Version 1.1 (the "License"), the contents of this
  5 * file are subject only to the provisions of the License. You may not use
  6 * this file except in compliance with the License. You may obtain a copy
  7 * of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
  8 * Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
  9 * 
 10 * http://oss.sgi.com/projects/FreeB
 11 * 
 12 * Note that, as provided in the License, the Software is distributed on an
 13 * "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
 14 * DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
 15 * CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
 16 * PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
 17 * 
 18 * Original Code. The Original Code is: OpenGL Sample Implementation,
 19 * Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
 20 * Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
 21 * Copyright in any portions created by third parties is as indicated
 22 * elsewhere herein. All Rights Reserved.
 23 * 
 24 * Additional Notice Provisions: The application programming interfaces
 25 * established by SGI in conjunction with the Original Code are The
 26 * OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
 27 * April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
 28 * 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
 29 * Window System(R) (Version 1.3), released October 19, 1998. This software
 30 * was created using the OpenGL(R) version 1.2.1 Sample Implementation
 31 * published by SGI, but has not been independently verified as being
 32 * compliant with the OpenGL(R) version 1.2.1 Specification.
 33 *
 34 */
 35
 36/*
 37 *  fog.c
 38 *  This program draws 5 red spheres, each at a different 
 39 *  z distance from the eye, in different types of fog.  
 40 *  Pressing the f key chooses between 3 types of 
 41 *  fog:  exponential, exponential squared, and linear.  
 42 *  In this program, there is a fixed density value, as well 
 43 *  as fixed start and end values for the linear fog.
 44 */
 45#include <math.h>
 46#include <stdlib.h>
 47#include <stdio.h>
 48#include "ug.h"
 49
 50static GLint fogMode;
 51
 52/*  Initialize depth buffer, fog, light source, 
 53 *  material property, and lighting model.
 54 */
 55static void init(void)
 56{
 57   GLfloat position[] = { 0.5, 0.5, 3.0, 0.0 };
 58
 59   glEnable(GL_DEPTH_TEST);
 60
 61   glLightfv(GL_LIGHT0, GL_POSITION, position);
 62   glEnable(GL_LIGHTING);
 63   glEnable(GL_LIGHT0);
 64   {
 65      GLfloat mat[3] = {0.1745, 0.01175, 0.01175};	
 66      glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, mat);
 67      mat[0] = 0.61424; mat[1] = 0.04136; mat[2] = 0.04136;	
 68      glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, mat);
 69      mat[0] = 0.727811; mat[1] = 0.626959; mat[2] = 0.626959;
 70      glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, mat);
 71      glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 0.6*128.0);
 72   }
 73
 74   glEnable(GL_FOG);
 75   {
 76      GLfloat fogColor[4] = {0.5, 0.5, 0.5, 1.0};
 77
 78      fogMode = GL_EXP;
 79      glFogx (GL_FOG_MODE, fogMode);
 80      glFogfv (GL_FOG_COLOR, fogColor);
 81      glFogf (GL_FOG_DENSITY, 0.35);
 82      glHint (GL_FOG_HINT, GL_DONT_CARE);
 83      glFogf (GL_FOG_START, 1.0);
 84      glFogf (GL_FOG_END, 5.0);
 85   }
 86   glClearColor(0.5, 0.5, 0.5, 1.0);  /* fog color */
 87}
 88
 89static void renderSphere (GLfloat x, GLfloat y, GLfloat z)
 90{
 91   glPushMatrix();
 92   glTranslatef (x, y, z);
 93   ugSolidSpheref(0.4, 32, 32);
 94   glPopMatrix();
 95}
 96
 97/* display() draws 5 spheres at different z positions.
 98 */
 99static void display(UGWindow uwin)
100{
101   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
102   renderSphere (-2., -0.5, -1.0);
103   renderSphere (-1., -0.5, -2.0);
104   renderSphere (0., -0.5, -3.0);
105   renderSphere (1., -0.5, -4.0);
106   renderSphere (2., -0.5, -5.0);
107   glFlush();
108   ugSwapBuffers(uwin);
109}
110
111static void reshape(UGWindow uwin, int w, int h)
112{
113   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
114   glMatrixMode(GL_PROJECTION);
115   glLoadIdentity();
116   if (w <= h)
117      glOrthof(-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w,
118         2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
119   else
120      glOrthof(-2.5*(GLfloat)w/(GLfloat)h,
121         2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0);
122   glMatrixMode(GL_MODELVIEW);
123   glLoadIdentity ();
124}
125
126static void keyboard(UGWindow uwin, int key, int x, int y)
127{
128   switch (key) {
129      case 'f':
130      case 'F':
131         if (fogMode == GL_EXP) {
132	    fogMode = GL_EXP2;
133	    printf ("Fog mode is GL_EXP2\n");
134         }
135         else if (fogMode == GL_EXP2) {
136            fogMode = GL_LINEAR;
137            printf ("Fog mode is GL_LINEAR\n");
138         }
139         else if (fogMode == GL_LINEAR) {
140            fogMode = GL_EXP;
141            printf ("Fog mode is GL_EXP\n");
142         }
143         glFogx(GL_FOG_MODE, fogMode);
144         ugPostRedisplay(uwin);
145         break;
146      case 27:
147         exit(0);
148         break;
149      default:
150         break;
151   }
152}
153
154
155/*  Main Loop
156 *  Open window with initial window size, title bar, 
157 *  RGBA display mode, depth buffer, and handle input events.
158 */
159#ifdef FTK_AS_PLUGIN
160#include "ftk_app_demo.h"
161FTK_HIDE int FTK_MAIN(int argc, char* argv[]);
162FtkApp* ftk_app_demo_fog_create()
163{
164	return ftk_app_demo_create(_("fog"), ftk_main);
165}
166#else
167#define FTK_HIDE extern
168#endif /*FTK_AS_PLUGIN*/
169
170FTK_HIDE int FTK_MAIN(int argc, char* argv[])
171{
172   UGCtx ug = ugInit();
173   UGWindow uwin = ugCreateWindow (ug, "UG_DEPTH", "fog", 500, 500, 100, 100);
174   init();
175   ugDisplayFunc(uwin, display); 
176   ugReshapeFunc(uwin, reshape);
177   ugKeyboardFunc(uwin, keyboard);
178#ifndef FTK_AS_PLUGIN
179   ugMainLoop(ug);
180#endif
181   return 0;
182}