* ALL RIGHTS RESERVED - sistemas.riopomba.ifsudestemg.edu.br · Exercícios – Computação...
Transcript of * ALL RIGHTS RESERVED - sistemas.riopomba.ifsudestemg.edu.br · Exercícios – Computação...
Exercícios – Computação Gráfica – 05/07/2013
/* * (c) Copyright 1993, 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. * * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE. * * US Government Users Restricted Rights * Use, duplication, or disclosure by the Government is subject to * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph * (c)(1)(ii) of the Rights in Technical Data and Computer Software * clause at DFARS 252.227-7013 and/or in similar or successor * clauses in the FAR or the DOD or NASA FAR Supplement. * Unpublished-- rights reserved under the copyright laws of the * United States. Contractor/manufacturer is Silicon Graphics, * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311. * * OpenGL(TM) is a trademark of Silicon Graphics, Inc. */ /* * disk.c * This program demonstrates the use of the quadrics * Utility Library routines to draw circles and arcs. */ #include <GL/glut.h> #include <stdlib.h> GLUquadricObj * quadObj; /* Clear the screen. For each triangle, set the current * color and modify the modelview matrix. */ void display(void) {
glClearColor (0.0, 0.0, 0.0, 1.0); glClear (GL_COLOR_BUFFER_BIT); glPushMatrix(); gluQuadricDrawStyle (quadObj, GLU_FILL); glColor3f (1.0, 1.0, 1.0); glTranslatef (10.0, 10.0, 0.0); gluDisk (quadObj, 0.0, 5.0, 10, 2); glPopMatrix(); glPushMatrix(); glColor3f (1.0, 1.0, 0.0); glTranslatef (20.0, 20.0, 0.0); gluPartialDisk (quadObj, 0.0, 5.0, 10, 3, 30.0, 120.0); glPopMatrix(); glPushMatrix(); gluQuadricDrawStyle (quadObj, GLU_SILHOUETTE); glColor3f (0.0, 1.0, 1.0); glTranslatef (30.0, 30.0, 0.0); gluPartialDisk (quadObj, 0.0, 5.0, 10, 3, 135.0, 270.0); glPopMatrix(); glPushMatrix(); gluQuadricDrawStyle (quadObj, GLU_LINE); glColor3f (1.0, 0.0, 1.0); glTranslatef (40.0, 40.0, 0.0); gluDisk (quadObj, 2.0, 5.0, 10, 10); glPopMatrix(); glFlush(); } void myinit (void) { quadObj = gluNewQuadric (); glShadeModel(GL_FLAT); } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho (0.0, 50.0, 0.0, 50.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0); else glOrtho (0.0, 50.0*(GLfloat)w/(GLfloat)h, 0.0, 50.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity (); } /* Main Loop * Open window with initial window size, title bar, * RGBA display mode, and handle input events. */
int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutCreateWindow(argv[0]); glutReshapeFunc(myReshape); glutDisplayFunc(display); myinit(); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }
/-----------------------------------------------------------------------------------/ /* * smooth.c * This program demonstrates smooth shading. * A smooth shaded polygon is drawn in a 2-D projection. */ #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include <stdlib.h> /* GL_SMOOTH is actually the default shading model. */ void myinit (void) { glShadeModel (GL_SMOOTH); } void triangle(void) { glBegin (GL_TRIANGLES); glColor3f (1.0, 0.0, 0.0); glVertex2f (5.0, 5.0); glColor3f (0.0, 1.0, 0.0);
glVertex2f (25.0, 5.0); glColor3f (0.0, 0.0, 1.0); glVertex2f (5.0, 25.0); glEnd (); } void display(void) { glClear (GL_COLOR_BUFFER_BIT); triangle (); glFlush (); } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) gluOrtho2D (0.0, 30.0, 0.0, 30.0 * (GLfloat) h/(GLfloat) w); else gluOrtho2D (0.0, 30.0 * (GLfloat) w/(GLfloat) h, 0.0, 30.0); glMatrixMode(GL_MODELVIEW); } /* Main Loop * Open window with initial window size, title bar, * RGBA display mode, and handle input events. */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutCreateWindow(argv[0]); glutReshapeFunc(myReshape); glutDisplayFunc(display); myinit(); glutMainLoop(); return 0; }
/--------------------------------------------------------------------------------------/ /* * cube.c * This program demonstrates a single modeling transformation, * glScalef() and a single viewing transformation, gluLookAt(). * A wireframe cube is rendered. */ #include <GL/glut.h> #include <stdlib.h> void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); } void display(void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glLoadIdentity (); /* clear the matrix */ /* viewing transformation */ gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glScalef (1.0, 2.0, 1.0); /* modeling transformation */ glutWireCube (1.0); glFlush (); } void reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); glMatrixMode (GL_MODELVIEW); }
/* ARGSUSED1 */ void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }
//--------------------------------------------------------------------------------------/ /* * aim.c * This program calculates the fovy (field of view angle * in the y direction), by using trigonometry, given the * size of an object and its size. */ #include <GL/glut.h> #include <stdio.h>
#include <stdlib.h> #include <math.h> void myinit (void) { glShadeModel (GL_FLAT); } /* Clear the screen. Set the current color to white. * Draw the wire frame cube and sphere. */ void display (void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glLoadIdentity (); /* glTranslatef() as viewing transformation */ glTranslatef (0.0, 0.0, -5.0); glutWireCube(2.0); glutWireSphere(1.0, 10, 10); glFlush(); } #define PI 3.1415926535 /* atan2 () is a system math routine which calculates * the arctangent of an angle, given length of the * opposite and adjacent sides of a right triangle. * atan2 () is not an OpenGL routine. */ GLdouble calculateAngle (double size, double distance) { GLdouble radtheta, degtheta; radtheta = 2.0 * atan2 (size/2.0, distance); degtheta = (180.0 * radtheta) / PI; printf ("degtheta is %lf\n", degtheta); return ((GLdouble) degtheta); } /* Called when the window is first opened and whenever * the window is reconfigured (moved or resized). */ void myReshape(int w, int h) { GLdouble theta; glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); theta = calculateAngle (2.0, 5.0); gluPerspective(theta, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }
/* Main Loop * Open window with initial window size, title bar, * RGBA display mode, and handle input events. */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutCreateWindow(argv[0]); glutReshapeFunc(myReshape); glutDisplayFunc(display); myinit(); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }
/-----------------------------------------------------------------------------------------------------------------------------------/
/* * clip.c * This program demonstrates arbitrary clipping planes. */ #include <GL/glut.h> #include <stdlib.h> void display(void) { GLdouble eqn[4] = {0.0, 1.0, 0.0, 0.0}; GLdouble eqn2[4] = {1.0, 0.0, 0.0, 0.0}; glClear(GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glPushMatrix(); glTranslatef (0.0, 0.0, -5.0);
/* clip lower half -- y < 0 */ glClipPlane (GL_CLIP_PLANE0, eqn); glEnable (GL_CLIP_PLANE0); /* clip left half -- x < 0 */ glClipPlane (GL_CLIP_PLANE1, eqn2); glEnable (GL_CLIP_PLANE1); glRotatef (90.0, 1.0, 0.0, 0.0); glutWireSphere(1.0, 20, 20); glPopMatrix(); glFlush(); } void myinit (void) { glShadeModel (GL_FLAT); } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); } /* Main Loop * Open window with initial window size, title bar, * RGBA display mode, and handle input events. */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutCreateWindow(argv[0]); glutReshapeFunc(myReshape); glutDisplayFunc(display); myinit(); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }
/------------------------------------------------------------------------------------------------------------------------------------/
/* * tea.c * This program demonstrates two-sided lighting and compares * it with one-sided lighting. Three teapots are drawn, with * a clipping plane to expose the interior of the objects. */ #include <GL/glut.h> #include <stdlib.h> /* Initialize light source. */ void myinit(void) { GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; /* light_position is NOT default value */ GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glFrontFace (GL_CW); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); } void display(void)
{ GLdouble eqn[4] = {1.0, 0.0, -1.0, 1.0}; GLfloat two_side_on[] = { GL_TRUE }; GLfloat two_side_off[] = { GL_FALSE }; GLfloat mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 }; GLfloat back_diffuse[] = { 0.8, 0.2, 0.8, 1.0 }; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix (); glClipPlane (GL_CLIP_PLANE0, eqn); /* slice objects */ glEnable (GL_CLIP_PLANE0); glPushMatrix (); glTranslatef (0.0, 2.0, 0.0); glutSolidTeapot(1.0); /* one-sided lighting */ glPopMatrix (); /* two-sided lighting, but same material */ glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse); glPushMatrix (); glTranslatef (0.0, 0.0, 0.0); glutSolidTeapot(1.0); glPopMatrix (); /* two-sided lighting, two different materials */ glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv (GL_BACK, GL_DIFFUSE, back_diffuse); glPushMatrix (); glTranslatef (0.0, -2.0, 0.0); glutSolidTeapot(1.0); glPopMatrix (); glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); glDisable (GL_CLIP_PLANE0); glPopMatrix (); glFlush(); } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho (-4.0, 4.0, -4.0*(GLfloat)h/(GLfloat)w, 4.0*(GLfloat)h/(GLfloat)w, -10.0, 10.0); else glOrtho (-4.0*(GLfloat)w/(GLfloat)h, 4.0*(GLfloat)w/(GLfloat)h, -4.0, 4.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); } /* Main Loop * Open window with initial window size, title bar,
* RGBA display mode, and handle input events. */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutCreateWindow(argv[0]); glutReshapeFunc(myReshape); glutDisplayFunc(display); myinit(); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }
//---------------------------------------------------------------------------------------------------------------------------------//
/*stenciltst.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glut.h>
GLboolean doubleBuffer;
/* ARGSUSED1 */
static void
Key(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
}
}
static void
Draw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
/* red triangle */
glColor3ub(200, 0, 0);
glBegin(GL_POLYGON);
glVertex3i(-4, -4, 0);
glVertex3i(4, -4, 0);
glVertex3i(0, 4, 0);
glEnd();
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_INCR, GL_KEEP, GL_DECR);
/* green square */
glColor3ub(0, 200, 0);
glBegin(GL_POLYGON);
glVertex3i(3, 3, 0);
glVertex3i(-3, 3, 0);
glVertex3i(-3, -3, 0);
glVertex3i(3, -3, 0);
glEnd();
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
/* blue square */
glColor3ub(0, 0, 200);
glBegin(GL_POLYGON);
glVertex3i(3, 3, 0);
glVertex3i(-3, 3, 0);
glVertex3i(-3, -3, 0);
glVertex3i(3, -3, 0);
glEnd();
if (doubleBuffer) {
glutSwapBuffers();
} else {
glFlush();
}
}
static void
Args(int argc, char **argv)
{
GLint i;
doubleBuffer = GL_TRUE;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-sb") == 0) {
doubleBuffer = GL_FALSE;
} else if (strcmp(argv[i], "-db") == 0) {
doubleBuffer = GL_TRUE;
}
}
}
int
main(int argc, char **argv)
{
GLenum type;
glutInit(&argc, argv);
Args(argc, argv);
type = GLUT_RGB | GLUT_STENCIL;
type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
glutInitDisplayMode(type);
glutCreateWindow("Stencil Test");
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearStencil(0);
glStencilMask(1);
glEnable(GL_STENCIL_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glutKeyboardFunc(Key);
glutDisplayFunc(Draw);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
//------------------------------------------------------------------------------------------------------------------------------
/* stencil.c
* This program draws two rotated tori in a window.
* A diamond in the center of the window masks out part
* of the scene. Within this mask, a different model
* (a sphere) is drawn in a different color.
*/
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#define YELLOWMAT 1
#define BLUEMAT 2
void init (void)
{
GLfloat yellow_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
GLfloat yellow_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat blue_diffuse[] = { 0.1, 0.1, 0.7, 1.0 };
GLfloat blue_specular[] = { 0.1, 1.0, 1.0, 1.0 };
GLfloat position_one[] = { 1.0, 1.0, 1.0, 0.0 };
glNewList(YELLOWMAT, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, yellow_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 64.0);
glEndList();
glNewList(BLUEMAT, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_DIFFUSE, blue_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, blue_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 45.0);
glEndList();
glLightfv(GL_LIGHT0, GL_POSITION, position_one);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClearStencil(0x0);
glEnable(GL_STENCIL_TEST);
}
/* Draw a sphere in a diamond-shaped section in the
* middle of a window with 2 tori.
*/
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* draw blue sphere where the stencil is 1 */
glStencilFunc (GL_EQUAL, 0x1, 0x1);
glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
glCallList (BLUEMAT);
glutSolidSphere (0.5, 15, 15);
/* draw the tori where the stencil is not 1 */
glStencilFunc (GL_NOTEQUAL, 0x1, 0x1);
glPushMatrix();
glRotatef (45.0, 0.0, 0.0, 1.0);
glRotatef (45.0, 0.0, 1.0, 0.0);
glCallList (YELLOWMAT);
glutSolidTorus (0.275, 0.85, 15, 15);
glPushMatrix();
glRotatef (90.0, 1.0, 0.0, 0.0);
glutSolidTorus (0.275, 0.85, 15, 15);
glPopMatrix();
glPopMatrix();
glFlush ();
}
/* Whenever the window is reshaped, redefine the
* coordinate system and redraw the stencil area.
*/
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
/* create a diamond shaped stencil area */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D(-3.0, 3.0, -3.0*(GLfloat)h/(GLfloat)w,
3.0*(GLfloat)h/(GLfloat)w);
else
gluOrtho2D(-3.0*(GLfloat)w/(GLfloat)h,
3.0*(GLfloat)w/(GLfloat)h, -3.0, 3.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_STENCIL_BUFFER_BIT);
glStencilFunc (GL_ALWAYS, 0x1, 0x1);
glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE);
glBegin(GL_QUADS);
glVertex2f (-1.0, 0.0);
glVertex2f (0.0, 1.0);
glVertex2f (1.0, 0.0);
glVertex2f (0.0, -1.0);
glEnd();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat) w/(GLfloat) h, 3.0, 7.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
}
/* Main Loop
* Be certain to request stencil bits.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB
| GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize (400, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
//---------------------------------------------------------------------------------------------------------------------------
/*
* planes.c
* This program demonstrates the use of glDepthFunc.
* It is adpted from plane.c by Ting.
*/
#include <GL/glut.h>
#include <stdlib.h>
void myinit(void)
{
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}
void drawPlane(void)
{
glBegin (GL_QUADS);
glNormal3f (0.0, 0.0, 1.0);
glVertex3f (-1.0, -1.0, 0.0);
glVertex3f (0.0, -1.0, 0.0);
glVertex3f (0.0, 0.0, 0.0);
glVertex3f (-1.0, 0.0, 0.0);
glNormal3f (0.0, 0.0, 1.0);
glVertex3f (0.0, -1.0, 0.0);
glVertex3f (1.0, -1.0, 0.0);
glVertex3f (1.0, 0.0, 0.0);
glVertex3f (0.0, 0.0, 0.0);
glNormal3f (0.0, 0.0, 1.0);
glVertex3f (0.0, 0.0, 0.0);
glVertex3f (1.0, 0.0, 0.0);
glVertex3f (1.0, 1.0, 0.0);
glVertex3f (0.0, 1.0, 0.0);
glNormal3f (0.0, 0.0, 1.0);
glVertex3f (0.0, 0.0, 0.0);
glVertex3f (0.0, 1.0, 0.0);
glVertex3f (-1.0, 1.0, 0.0);
glVertex3f (-1.0, 0.0, 0.0);
glEnd();
}
void display (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
glTranslatef (0.75, 1.0, 1.0);
glColor3f (0.0, 0.0, 1.0);
drawPlane ();
glPopMatrix ();
glPushMatrix ();
glTranslatef (0.0, 0.0, 0.5);
glColor3f (0.0, 1.0, 0.0);
drawPlane ();
glPopMatrix ();
glPushMatrix ();
glTranslatef (1.5, 0.0, 0.0);
glColor3f (1.0, 0.0, 0.0);
drawPlane ();
glPopMatrix ();
glFlush ();
}
void myReshape(int w, int h)
{
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if (w <= h)
glOrtho (-1.5, 3.0, -1.5*(GLdouble)h/(GLdouble)w,
3.0*(GLdouble)h/(GLdouble)w, -10.0, 10.0);
else
glOrtho (-1.5*(GLdouble)w/(GLdouble)h,
1.5*(GLdouble)w/(GLdouble)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode (GL_MODELVIEW);
}
/* Main Loop
* Open window with initial window size, title bar,
* RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(300, 300);
glutInitWindowPosition(0, 0);
glutCreateWindow(argv[0]);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
//-----------------------------------------------------------------------------------------------------------------------
/*
* light.c
* This program demonstrates the use of the OpenGL lighting
* model. A sphere is drawn using a grey material characteristic.
* A single light source illuminates the object.
*/
#include <GL/glut.h>
#include <stdlib.h>
/* Initialize material property, light source, lighting model,
* and depth buffer.
*/
void myinit(void)
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(1.0, 40, 40);
glFlush();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/* Main Loop
* Open window with initial window size, title bar,
* RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow(argv[0]);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
//------------------------------------------------------------------------------------------------------------------------------
/*
* alpha3D.c
* This program demonstrates how to intermix opaque and
* alpha blended polygons in the same scene, by using
* glDepthMask. Press the 'a' key to animate moving the
* transparent object through the opaque object. Press
* the 'r' key to reset the scene.
*/
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#define MAXZ 8.0
#define MINZ -8.0
#define ZINC 0.4
static float solidZ = MAXZ;
static float transparentZ = MINZ;
static GLuint sphereList, cubeList;
static void init(void)
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 0.15 };
GLfloat mat_shininess[] = { 100.0 };
GLfloat position[] = { 0.5, 0.5, 1.0, 0.0 };
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
sphereList = glGenLists(1);
glNewList(sphereList, GL_COMPILE);
glutSolidSphere (0.4, 16, 16);
glEndList();
cubeList = glGenLists(1);
glNewList(cubeList, GL_COMPILE);
glutSolidCube (0.6);
glEndList();
}
void display(void)
{
GLfloat mat_solid[] = { 0.75, 0.75, 0.0, 1.0 };
GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 };
GLfloat mat_emission[] = { 0.0, 0.3, 0.3, 0.6 };
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
glTranslatef (-0.15, -0.15, solidZ);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid);
glCallList (sphereList);
glPopMatrix ();
glPushMatrix ();
glTranslatef (0.15, 0.15, transparentZ);
glRotatef (15.0, 1.0, 1.0, 0.0);
glRotatef (30.0, 0.0, 1.0, 0.0);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);
glEnable (GL_BLEND);
glDepthMask (GL_FALSE);
glBlendFunc (GL_SRC_ALPHA, GL_ONE);
glCallList (cubeList);
glDepthMask (GL_TRUE);
glDisable (GL_BLEND);
glPopMatrix ();
glutSwapBuffers();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLint) w, (GLint) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void animate(void)
{
if (solidZ <= MINZ || transparentZ >= MAXZ)
glutIdleFunc(NULL);
else {
solidZ -= ZINC;
transparentZ += ZINC;
glutPostRedisplay();
}
}
/* ARGSUSED1 */
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'a':
case 'A':
solidZ = MAXZ;
transparentZ = MINZ;
glutIdleFunc(animate);
break;
case 'r':
case 'R':
solidZ = MAXZ;
transparentZ = MINZ;
glutPostRedisplay();
break;
case 27:
exit(0);
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
De acordo com os programas descritos acima, responda:
1) A rotina glOrtho do programa disk.c e a rotina gluOrtho2D do programa smooth.c fazem projeções ortogonais. A única diferença está na normalização da coordenada z para o intervalo [-1,1] em uma delas. Qual delas?
2) A rotina glFrustum do programa cube.c e a rotina gluPerspective do programa aim.c fazem projeções perspectivas sobre planos. Compare e estabeleça a correspondência entre a lista de argumentos das duas rotinas.
3) Modifique nos programas disk.c e aim.c os valores dos argumentos w e h da função glViewport. Por exemplo, glViewport(0, 0, w/2, h) e glViewport(0, 0, w, h/2). Justifique os resultados visuais obtidos.
4) Modifique no programa aim.c o valor do argumento theta da função gluPerspective.
Exemplo, gluPerspective(theta/10, (GLfloat) w/(GLfloat) h, 1.0, 20.0) e gluPerspective(10*theata, (GLfloat) w/(GLfloat) h, 1.0, 20.0). Justifique os resultados visuais obtidos. Como se pode estimar um valor razoável para theta a partir da distância do observador e da altura da janela?
5) Modifique no programa cube.c os valores dos quatro primeiros argumentos da função glFrustum (left, right, top e bottom). Observe que é possível definir janelas assimétricas em relação ao eixo do observador. É possível obter esse mesmo efeito com a função gluPerspective? Justifique.
6) OpenGL estabelece um volume de visualização através de seis planos de recorte. Identifique nas funções glOrtho eglFrustum os argumentos que definem estes planos de recorte. 7) Nos programas clip.c e tea.c identifique as funções que estão relacionadas com o recorte 8) Modifique a definição dos planos de recorte no programa clip.c : através da alteração dos valores dos coeficientes dos planos de recorte definidos pelas variáveis eqn e eqn2. Exemplo: eqn[4] = {1.0, 1.0, 1.0, 0.0}; através da inserção e remoção dos planos de recorte. Exemplo: remover o plano de recorte eqn2. Qual é a função de cada argumento da rotina glClipPlane?
9) O OpenGL provê um buffer denimonado stencil para proteger o desenho numa área específica da tela. Ele permite obter alguns efeitos interessantes de recorte bidimensional. Os programas stenciltst.c e stencil.c ilustram o uso deste buffer. Identifique nos programas os trechos de código que
a) define uma área de stencil. Como se altera a forma e a posição desta área? b) limita o desenho dentro da área de stencil. O que acontece quando o desenho é maior que a
área de stencil? c) protege desenhos dentro da área de stencil.
10) Sobre o programa planes.c.
a) Qual é o papel do argumento GL_DEPTH_BUFFER_BIT da função glClear?
b) Qual é o papel do argumento GL_DEPTH_TEST da função glEnable? O que acontecerá se removermos este comando?
c) Modifique o argumento da função glDepthFunc para GL_NEVER, GL_ALWAYS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER e GL_NOTEQUAL. Compare os resultados obtidos.
d) Qual das funções corresponde ao algoritmo z-Buffer? e) Qual das funções corresponde ao algoritmo de pintor?
11) Sobre o programa light.c, responda:
a) O que acontecerá se retirarmos o comando glEnable(GL_LIGHTING)? b) O que acontecerá se retirarmos o comando glEnable(GL_LIGHT0)? c) O que acontecerá se retirarmos o comando glMaterialfv(GL_FRONT, GL_SHININESS,
mat_shininess)? Qual é a função do comando glMaterialfv? d) Qual é o modelo de tonalização utilizado? Altere o parâmetro GL_SMOOTH para GL_FLAT
no comando glShadeModel e anote o que foi constatado.
12) Sobre o programa alpha3D.c, responda (Este programa ilustra uma alternativa para "emular" efeitos de transparência)
a) Qual é o papel do comando glDepthMask? b) Qual é a opacidade do toro? O que acontecerá se alterarmos este valor? c) Qual é a opacidade do cubo? O que acontecerá se alterarmos este valor?