/* * Copyright (c) 1993-1997, Silicon Graphics, Inc. * ALL RIGHTS RESERVED * Permission to use, copy, modify, and distribute this software for * any purpose and without fee is hereby granted, provided that the above * copyright notice appear in all copies and that both the copyright notice * and this permission notice appear in supporting documentation, and that * the name of Silicon Graphics, Inc. not be used in advertising * or publicity pertaining to distribution of the software without specific, * written prior permission. * */ /* * surface.c * This program draws a parametric surface. * * It began as a demo program from SGI * * http://www.sgi.com/software/opengl/examples/redbook/source/torus.c * * Not much of the original is still left. Just the #includes and * the function names. And the copyright notice above. * * Copyright (c) 2000 * David C. Banks * */ #include #include #include #include /* Evaluate a point and normal on a planar surface. * * [x] [ a0 a1 ] [ u ] * [y] = [ b0 b1 ] * [ ] * [z] [ c0 c1 ] [ v ] * * The normal vector is the cross product of the partial * derivatives dp/du, dp/dv where p = (x,y,z). */ void surfacePoint(double u, double v) { double x, y, z; double a0 = 2.0, a1 = 0.2; double b0 = -0.1, b1 = 2.0; double c0 = 0.1, c1 = 0.2; x = a0*u + a1*v; y = b0*u + b1*v; z = c0*u + c1*v; glNormal3f(b0*c1-b1*c0, -(a0*c1-a1*c0), a0*b1-a1*b0); glVertex3f(x, y, z); } /* Draw some triangles on the surface */ void surface(int uDim, int vDim) { glBegin(GL_TRIANGLES); surfacePoint(0.0, 0.0); surfacePoint(0.0, 1.0); surfacePoint(1.0, 1.0); glEnd(); } /* initialize state */ void init(void) { GLfloat mat_diffuse[] = { 1.0, 0.5, 0.4, 1.0 }; /* pink */ GLfloat mat_specular[] = { 0.2, 0.2, 0.2, 1.0 }; /* white highlight */ GLfloat mat_shininess[] = { 50.0 }; GLfloat light_position[] = { -5.0, 5.0, 5.0, 0.0 }; glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); glDepthFunc (GL_LESS); glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess); glLightfv (GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_NORMALIZE); /* Make my normals unit length for me. */ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); } /* Clear window and draw surface. */ void display(void) { GLfloat matrix[16]; int uDim = 64, vDim = 32; glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glGetFloatv(GL_MODELVIEW_MATRIX, matrix); glPushMatrix(); glLoadIdentity(); gluLookAt(0, 0, 8, 0, 0, 0, 0, 1, 0); glMultMatrixf(matrix); surface(uDim, vDim); glPopMatrix(); glutSwapBuffers(); } /* Handle window resize. */ void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(30, (GLfloat) w/(GLfloat) h, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 0, 0, 0, 0, -1, 0, 1, 0); } int main(int argc, char **argv) { glutInitWindowSize(300, 300); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow(argv[0]); init(); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); return 0; }