PageRenderTime 384ms CodeModel.GetById 161ms app.highlight 9ms RepoModel.GetById 212ms app.codeStats 0ms

/opengles/redbook/colormat.c

http://ftk.googlecode.com/
C | 147 lines | 93 code | 10 blank | 44 comment | 12 complexity | bcde745bfb4956f7a975cad7306fc453 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 *  colormat.c
 38 *  After initialization, the program will be in
 39 *  ColorMaterial mode.  Interaction:  pressing the 
 40 *  mouse buttons will change the diffuse reflection values.
 41 */
 42#include <stdlib.h>
 43#include "ug.h"
 44
 45GLfloat diffuseMaterial[4] = { 0.5, 0.5, 0.5, 1.0 };
 46
 47/*  Initialize material property, light source, lighting model,
 48 *  and depth buffer.
 49 */
 50void init(void) 
 51{
 52   GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
 53   GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
 54
 55   glClearColor (0.0, 0.0, 0.0, 0.0);
 56   glShadeModel (GL_SMOOTH);
 57   glEnable(GL_DEPTH_TEST);
 58   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuseMaterial);
 59   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
 60   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 25.0);
 61   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
 62   glEnable(GL_LIGHTING);
 63   glEnable(GL_LIGHT0);
 64
 65   //glColorMaterial(GL_FRONT, GL_DIFFUSE);
 66   glEnable(GL_COLOR_MATERIAL);
 67}
 68
 69void display(UGWindow uwin)
 70{
 71   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 72   ugSolidSpheref(1.0, 20, 16);
 73   glFlush ();
 74   ugSwapBuffers(uwin);
 75}
 76
 77void reshape (UGWindow uwin, int w, int h)
 78{
 79   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
 80   glMatrixMode (GL_PROJECTION);
 81   glLoadIdentity();
 82   if (w <= h)
 83      glOrthof(-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
 84         1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
 85   else
 86      glOrthof(-1.5*(GLfloat)w/(GLfloat)h,
 87         1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
 88   glMatrixMode(GL_MODELVIEW);
 89   glLoadIdentity();
 90}
 91
 92void mouse(UGWindow uwin, int button, int state, int x, int y)
 93{
 94   switch (button) {
 95       case UG_BUT_LEFT:
 96         if (state == UG_BUT_DOWN) {
 97            diffuseMaterial[0] += 0.1;
 98            if (diffuseMaterial[0] > 1.0)
 99               diffuseMaterial[0] = 0.0;
100            glColor4f(diffuseMaterial[0], diffuseMaterial[1], diffuseMaterial[2], diffuseMaterial[3]);
101            ugPostRedisplay(uwin);
102         }
103         break;
104      case UG_BUT_MIDDLE:
105         if (state == UG_BUT_DOWN) {
106            diffuseMaterial[1] += 0.1;
107            if (diffuseMaterial[1] > 1.0)
108               diffuseMaterial[1] = 0.0;
109            glColor4f(diffuseMaterial[0], diffuseMaterial[1], diffuseMaterial[2], diffuseMaterial[3]);
110            ugPostRedisplay(uwin);
111         }
112         break;
113      case UG_BUT_RIGHT:
114         if (state == UG_BUT_DOWN) {
115            diffuseMaterial[2] += 0.1;
116            if (diffuseMaterial[2] > 1.0)
117               diffuseMaterial[2] = 0.0;
118            glColor4f(diffuseMaterial[0], diffuseMaterial[1], diffuseMaterial[2], diffuseMaterial[3]);
119            ugPostRedisplay(uwin);
120         }
121         break;
122      default:
123         break;
124   }
125}
126
127void keyboard(UGWindow uwin, int key, int x, int y)
128{
129   switch (key) {
130      case 27:
131         exit(0);
132         break;
133   }
134}
135
136int main(int argc, char** argv)
137{
138   UGCtx ug = ugInit();
139   UGWindow uwin = ugCreateWindow (ug, "UG_DEPTH", argv[0], 500, 500, 100, 100);
140   init();
141   ugDisplayFunc(uwin, display); 
142   ugReshapeFunc(uwin, reshape);
143   ugPointerFunc(uwin, mouse);
144   ugKeyboardFunc(uwin, keyboard);
145   ugMainLoop(ug);
146   return 0;
147}