PageRenderTime 17ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/opengles/redbook/alpha3D.c

http://ftk.googlecode.com/
C | 175 lines | 115 code | 18 blank | 42 comment | 5 complexity | e59d52815763bc9f41efe4d824db0faa 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 *  alpha3D.c
 38 *  This program demonstrates how to intermix opaque and
 39 *  alpha blended polygons in the same scene, by using 
 40 *  glDepthMask.  Press the 'a' key to animate moving the 
 41 *  transparent object through the opaque object.  Press 
 42 *  the 'r' key to reset the scene.
 43 */
 44#include <stdlib.h>
 45#include <stdio.h>
 46#include "ug.h"
 47
 48#define MAXZ 8.0
 49#define MINZ -8.0
 50#define ZINC 0.4
 51
 52static float solidZ = MAXZ;
 53static float transparentZ = MINZ;
 54
 55static void init(void)
 56{
 57   GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 0.15 };
 58   GLfloat mat_shininess[] = { 100.0 };
 59   GLfloat position[] = { 0.5, 0.5, 1.0, 0.0 };
 60
 61   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
 62   glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
 63   glLightfv(GL_LIGHT0, GL_POSITION, position);
 64
 65   glEnable(GL_LIGHTING);
 66   glEnable(GL_LIGHT0);
 67   glEnable(GL_DEPTH_TEST);
 68}
 69
 70static void display(UGWindow uwin)
 71{
 72   GLfloat mat_solid[] = { 0.75, 0.75, 0.0, 1.0 };
 73   GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 };
 74   GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 };
 75   GLfloat mat_emission[] = { 0.0, 0.3, 0.3, 0.6 };
 76
 77   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 78
 79   glPushMatrix ();
 80      glTranslatef (-0.15, -0.15, solidZ);
 81      glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_zero);
 82      glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_solid);
 83      ugSolidSpheref(0.4, 32, 32);
 84   glPopMatrix ();
 85
 86   glPushMatrix ();
 87      glTranslatef (0.15, 0.15, transparentZ);
 88      glRotatef (15.0, 1.0, 1.0, 0.0);
 89      glRotatef (30.0, 0.0, 1.0, 0.0);
 90      glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission);
 91      glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_transparent);
 92      glEnable (GL_BLEND);
 93      glDepthMask (GL_FALSE);
 94	  glBlendFunc (GL_SRC_ALPHA, GL_ONE);
 95      ugSolidCubef(0.6);
 96	  glDepthMask (GL_TRUE);
 97      glDisable (GL_BLEND);
 98   glPopMatrix ();
 99
100   ugSwapBuffers(uwin);
101}
102
103static void reshape(UGWindow uwin, int w, int h)
104{
105   glViewport(0, 0, (GLint) w, (GLint) h);
106   glMatrixMode(GL_PROJECTION);
107   glLoadIdentity();
108   if (w <= h)
109      glOrthof(-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
110               1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
111   else
112      glOrthof(-1.5*(GLfloat)w/(GLfloat)h,
113               1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
114   glMatrixMode(GL_MODELVIEW);
115   glLoadIdentity();
116}
117
118static void animate(UGWindow uwin)
119{
120   if (solidZ <= MINZ || transparentZ >= MAXZ)
121      ugIdleFunc(ugCtxFromWin(uwin), NULL);
122   else {
123      solidZ -= ZINC;
124      transparentZ += ZINC;
125      ugPostRedisplay(uwin);
126   }
127}
128
129static void keyboard(UGWindow uwin, int key, int x, int y)
130{
131   switch (key) {
132      case 'a':
133      case 'A':
134         solidZ = MAXZ;
135         transparentZ = MINZ;
136         ugIdleFunc(ugCtxFromWin(uwin), animate);
137         break;
138      case 'r':
139      case 'R':
140         solidZ = MAXZ;
141         transparentZ = MINZ;
142         ugPostRedisplay(uwin);
143         break;
144      case 27:
145        exit(0);
146    }
147}
148
149#ifdef FTK_AS_PLUGIN
150#include "ftk_app_demo.h"
151FTK_HIDE int FTK_MAIN(int argc, char* argv[]);
152FtkApp* ftk_app_demo_alpha3D_create()
153{
154	return ftk_app_demo_create(_("alpha3D"), ftk_main);
155}
156#else
157#define FTK_HIDE extern
158#endif /*FTK_AS_PLUGIN*/
159
160FTK_HIDE int FTK_MAIN(int argc, char* argv[])
161{
162   UGCtx ug = ugInit();
163   UGWindow uwin = ugCreateWindow (ug, "", "alpha3D", 500, 500, 100, 100);
164   init();
165   ugDisplayFunc(uwin, display); 
166   ugReshapeFunc(uwin, reshape);
167   ugKeyboardFunc(uwin, keyboard);
168   ugIdleFunc(ugCtxFromWin(uwin), animate);
169
170#ifndef FTK_AS_PLUGIN
171   ugMainLoop(ug);
172#endif
173
174   return 0;
175}