PageRenderTime 304ms CodeModel.GetById 161ms app.highlight 9ms RepoModel.GetById 132ms app.codeStats 0ms

/opengles/redbook/torus.c

http://ftk.googlecode.com/
C | 149 lines | 93 code | 12 blank | 44 comment | 5 complexity | 846a8dbf1f8d5fd61d16003f698b3850 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 *  torus.c
 38 *  This program demonstrates the drawing of a torus.
 39 */
 40
 41#include <stdio.h>
 42#include <math.h>
 43#include <stdlib.h>
 44#include <math.h>
 45#include "ug.h"
 46
 47#define PI_ 3.14159265358979323846
 48
 49
 50/* Draw a torus */
 51static void torus(int numc, int numt)
 52{
 53   int i, j, k;
 54   double s, t, x, y, z, twopi;
 55   static GLfloat* v;
 56   GLfloat* p;
 57
 58   if (!v) {
 59       p = v = malloc(numc*(numt+1)*2*3*sizeof *v);
 60       twopi = 2 * PI_;
 61       for (i = 0; i < numc; i++) {
 62	  for (j = 0; j <= numt; j++) {
 63	     for (k = 1; k >= 0; k--) {
 64		s = (i + k) % numc + 0.5;
 65		t = j % numt;
 66
 67		x = (1+.1*cos(s*twopi/numc))*cos(t*twopi/numt);
 68		y = (1+.1*cos(s*twopi/numc))*sin(t*twopi/numt);
 69		z = .1 * sin(s * twopi / numc);
 70		*p++ = x;
 71		*p++ = y;
 72		*p++ = z;
 73	     }
 74	  }
 75       }
 76       glVertexPointer(3, GL_FLOAT, 0, v);
 77       glEnableClientState (GL_VERTEX_ARRAY);
 78    }
 79    glDrawArrays(GL_TRIANGLE_STRIP, 0, numc*(numt+1)*2);
 80}
 81
 82/* Initialize state */
 83static void init(void)
 84{
 85   glShadeModel(GL_FLAT);
 86   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
 87}
 88
 89/* Clear window and draw torus */
 90void display(UGWindow win)
 91{
 92   glClear(GL_COLOR_BUFFER_BIT);
 93   glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
 94   torus(8, 25);
 95   glFlush();
 96   ugSwapBuffers(win);
 97}
 98
 99/* Handle window resize */
100void reshape(UGWindow uwin, int w, int h)
101{
102printf("reshape\n");
103   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
104   glMatrixMode(GL_PROJECTION);
105   glLoadIdentity();
106   ugluPerspectivef(30, (GLfloat) w/(GLfloat) h, 1.0, 100.0);
107   glMatrixMode(GL_MODELVIEW);
108   glLoadIdentity();
109   ugluLookAtf(0, 0, 10, 0, 0, 0, 0, 1, 0);
110}
111
112/* Rotate about x-axis when "x" typed; rotate about y-axis
113   when "y" typed; "i" returns torus to original view */
114void keyboard(UGWindow uwin, int key, int x, int y)
115{
116   switch (key) {
117   case 'x':
118   case 'X':
119      glRotatef(30.,1.0,0.0,0.0);
120      ugPostRedisplay(uwin);
121      break;
122   case 'y':
123   case 'Y':
124      glRotatef(30.,0.0,1.0,0.0);
125      ugPostRedisplay(uwin);
126      break;
127   case 'i':
128   case 'I':
129      glLoadIdentity();
130      ugluLookAtf(0, 0, 10, 0, 0, 0, 0, 1, 0);
131      ugPostRedisplay(uwin);
132      break;
133   case 27:
134      exit(0);
135      break;
136   }
137}
138
139int main(int argc, char **argv)
140{
141   UGCtx ug = ugInit();
142   UGWindow win = ugCreateWindow(ug, "", argv[0], 200, 200, 0, 0);
143   init();
144   ugReshapeFunc(win, reshape);
145   ugKeyboardFunc(win, keyboard);
146   ugDisplayFunc(win, display);
147   ugMainLoop(ug);
148   return 0;
149}