miércoles, 10 de octubre de 2007

tarea 6

Algoritmo de Recorte de Cohen - Sutherland

http://hkeyland.googlepages.com/tarea6.pdf

The Cohen-Sutherland Line-Clipping Algorithm

The more efficient Cohen-Sutherland Algorithm performs initial tests on a line to determine whether intersection calculations can be avoided.

Steps for Cohen-Sutherland algorithm

    1. End-points pairs are check for trivial acceptance or trivial rejected using the outcode.
    2. If not trivial-accepance or trivial-rejected, divided into two segments at a clip edge.
    3. Iteratively clipped by testing trivial-acceptance or trivial-rejected, and divided into two segments until completely inside or trivial-rejected.

Pseudo-code of Cohen-Sutherland Algorithm.

Trivial acceptance/reject test

To perform trivial accept and reject tests, we extend the edges of the clip rectangle to divide the plane of the clip rectangle into nine regions. Each region is assigned a 4-bit code deteermined by where the region lies with respect to the outside halfplanes of the clip-rectangle edges. Each bit in the outcode is set to either 1 (true) or 0 (false); the 4 bits in the code correspond to the following conditions:

    • Bit 1 : outside halfplane of top edge, above top edge
      Y > Ymax
    • Bit 2 : outside halfplane of bottom edge, below bottom edge
      Y <>
    • Bit 3 : outside halfplane of right edge, to the right of right edge
      X > Xmax
    • Bit 4 : outside halfplane of left edge, to the left of left edge
      X <>

Conclusion

In summary, the C-S algorithm is efficient when outcode testing can be done cheaply (for example, by doing bitwise operations in assembly language) and trivial acceptance or rejection is applicable to the majority of line segments .(For example, large windows - everything is inside , or small windows - everything is outside).

http://www.cc.gatech.edu/grads/h/Hao-wei.Hsieh/Haowei.Hsieh/code1.html


1 Examen CG

Genere un humanoide con posiciones establecidas por default y ademas hacer que reciba los parametros necesarios para poder generar cualquier pose.

http://hkeyland.googlepages.com/examen.zip

martes, 9 de octubre de 2007

Tarea 5

Descargar el archivo pdf

Proyección Perspectiva

La palabra clave perspective determina la cámara perspectiva, que simula la típica cámara con objetivo. El ángulo de visión horizontal es determinado por la proporción entre la longitud del vector direction y la longitud del vector right, o por la palabra clave opcional angle, que es el modo aconsejado. El ángulo de visión ha de ser mayor de 0 y menor de 180 grados.


Proyección Ortográfica

La cámara ortográfica tiene dos modos de operación:

La proyección ortográfica pura. Esta proyección utiliza rayos paralelos para crear una imagen de la escena. El área de visión está determinada por las longitudes de los vectores right y up, que, por cierto, han de ser especificados, ya que con este tipo de proyección no se utilizan los de la cámara por defecto. En caso de ser omitidos, se usará el segundo método de proyección ortográfico de la cámara.

Si, en una cámara perspectiva, reemplazamos la palabra clave perspective por orthographic, y dejamos los demás parámetros igual, obtendremos una vista ortográfica con la misma área de imagen. Se puede conseguir lo mismo añadiendo la palabra clave angle a una cámara ortográfica. El valor de los ángulos es opcional. Ergo, este modo se activa si up y right no están presentes en la declaración de la cámara, o cuando la palabra clave angle sí lo está.

Debemos prestar atención al hecho de que las partes visibles de la escena pueden variar cuando cambiamos entre las vistas perspectiva y ortográfica. Mientras los objetos de interés estén cerca del punto look_at, seguirán visibles cuando cambiemos a la cámara ortográfica. Los objetos más lejanos pueden salir del encuadre mientras los cercanos permanecen a la vista.

Si los objetos están demasiado cerca a la situación de la cámara pueden desparecer. Demasiado cerca quiere decir, detrás del plano de proyección de la cámara ortográfica (el plano que atraviesa el punto look_at).


Tarea 4

Modelar el puerco araña (puerco potter) de manera jerárquica





Codigo fuente

#include
#include "glut.h"
#include "Cubo.h"
using namespace std;

float angleX = 0.0f;
float angleY = 0.0f;
float angleZ = 0.0f;
float transZ = -10.0f;
float transY = 0.0f;
float transX = 0.0f;
float xScale=1;
float yScale=1;
float zScale=1;
int camina=0;
float caminar=0.0f,aux=0;
float codo=0;
float hom=0;
float cintu=0;
float rod=0;
float tor=0;

float al,an,pro;
GLint venAncho=600;
GLint venAlto=600;
float hum_camina(float cam);
void humanoide();
void reshape(int width, int height);
void keyboard(unsigned char key, int x, int y);
void init();


void cilindro(float r1,float r2,float an){
GLUquadricObj *qobj;
qobj = gluNewQuadric ();
gluQuadricDrawStyle(qobj,GLU_FILL);
gluCylinder(qobj, r1, r2,an, 20, 20);
glScalef(1,1,0.01f);
glutSolidSphere(r1,20,20);
glTranslatef(0,0,100*an);
glutSolidSphere(r2,20,20);
glTranslatef(0,0,-100*an);
glScalef(1,1,100);

};

int main(int argc, char **argv) {
al=3;
an=3;
pro=3;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(20,20);
glutInitWindowSize( 600, 600);
glutCreateWindow("Puerco Araña");
init();
glutDisplayFunc(humanoide);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(humanoide);
glutMainLoop();
return 0;
}
void humanoide(void){
glClearColor(0,.4,.5,0);
GLUquadricObj *qobj;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(transX, transY, transZ);
glRotatef(angleX, 1.0f, 0.0f, 0.0f);
glRotatef(angleY, 0.0f, 1.0f, 0.0f);
glRotatef(angleZ, 0.0f, 0.0f, 1.0f);
glScalef(xScale, yScale, zScale);

////
//tronco
glPushMatrix();
glRotatef(90,0,1,0);
glColor3f(1.0f, 0.7f, 0.8f);
cilindro(1.5*an, 1.2*al, 5*pro);
glPushMatrix();//Pompis
//glColor3f(0.0f, 0.7f, 0.0f);
glTranslatef(-.05*an,0.1*al,4.0f*pro);
glutSolidSphere(1.4*an, 20, 20);
glPushMatrix();//cola
glTranslatef(0.0f*an,.8*al,1.0*pro);
glRotatef(-50,1,0,0);
cilindro(.2*an, .1*al, 1*pro);
glPopMatrix();//fin cola
glPopMatrix();//Pompis
//cabeza
glColor3f(1.0f, 0.7f, 0.8f);
glPushMatrix();
glTranslatef(0.0f,0.7*al,-1.0f);
glutSolidSphere(1.7*an, 20, 20);
//orejas
glPushMatrix();
glColor3f(1.0f, 0.6f, 0.6f);
glTranslatef(1.2*an,0.8*al,0*pro);
glPushMatrix();
glRotatef(90,0,1,0);
glRotatef(-45,1,0,0);
glutSolidCone(.5*an, .75*al, 50,5);
glPopMatrix();//cierre de oreja1
//oreja2
glTranslatef(-1.2*an,0.8*al,0*pro);
glPushMatrix();
glRotatef(-90,0,1,0);
glRotatef(-45,1,0,0);
glutSolidCone(.5*an, 1*al, 50,5);
glPopMatrix();//cierred e oreja2
//fleco
glPushMatrix();
glColor3f(1.0f, 0.8f, 0.0f);
glTranslatef(1.25*an,.9*al,0*pro);
//drawCube(.9*an, .4*al, 1*pro);
glRotatef(90,1,0,0);
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_FILL);
gluCylinder(qobj, .7*an, .6*al, .4*pro,7,200);
glTranslatef(-.2*an,0*al,.3*pro);
glutSolidSphere(.6*an, 10, 3);
glTranslatef(.4*an,.2*al,.0*pro);
glutSolidSphere(.6*an, 10, 3);
glPopMatrix();
glPopMatrix();//cierred e orejas
glColor3f(1.0f, 0.7f, 0.8f);
//Trompa
glPushMatrix();
glTranslatef(0,-0.5f,-1.0*pro);
glColor3f(1.0f, 0.6f, 0.6f);
glPushMatrix();
glRotatef(180,0,1,0);
glRotatef(10,1,0,0);
cilindro(.4*an, .9*al, 1.25*pro);
glPopMatrix();//cierra trompas
glPopMatrix();//cerramso trompa
//ojos si salen
glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(0.8*an,1*al,-1.5*pro);
glutSolidSphere(.5*an, 20*al, 20*pro);
glPushMatrix();
glColor3f(0.0f, 0.0f, 0.0f);
glTranslatef(0*an,0*al,-0.5*pro);
glutSolidSphere(.07*an, 20*al, 20*pro);
glColor3f(0,0,0);

//Lentes
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_FILL);
glTranslatef(0*an,0*al,-.01*pro);
glutSolidTorus (.3, 1.7, 12, 24);
glTranslatef(-1.8*an,0*al,0*pro);
glutSolidTorus (.3, 1.7, 12, 24);
glRotatef(-60,1,0,0);
glTranslatef(.9*an,0*al,.1*pro);
glutSolidTorus (.3, 1.2, 12, 24);

//armazon de lentes
glTranslatef(1.5*an,0*al,-.01*pro);
glRotatef(20,1,0,0);
glRotatef(-10,0,1,0);
cilindro(.1*an,.1*al,2*pro);
glRotatef(20,0,1,0);
glTranslatef(-3*an,0*al,-.6*pro);
cilindro(.1*an,.1*al,2*pro);
glPopMatrix();
glPopMatrix();//cierre ojo1
//OJO2
glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
glTranslatef(-0.8*an,1*al,-1.5*pro);
glutSolidSphere(.5*an, 20*al, 20*pro);
glPushMatrix();
glColor3f(0.0f, 0.0f, 0.0f);
glTranslatef(0*an,0*al,-0.5*pro);
glutSolidSphere(.07*an, 20*al, 20*pro);
glPopMatrix();
glPopMatrix();//cierre ojo2
glPopMatrix();//cierre de cabeza

//manos
glColor3f(1.0f, 0.7f, 0.8f);

glPushMatrix();//mano1
glTranslatef(0.4f*an,-0.2f*al,1*pro);
glPushMatrix();//mano1
glRotatef(60,1,0,0);
cilindro(.5*an, .3*al, 1*pro);


//Pata Izquierda
glPushMatrix();//
glRotatef(30,1,0,0);
glTranslatef(0.0f*an,0.5f*al,.8*pro);
cilindro(.3*an, .1*al, 2*pro);
glPopMatrix();//
glPopMatrix();//mano1
glTranslatef(0.4f*an,-0.1f*al,-2*pro);


//Pata Derecha
glPushMatrix();//mano2
glRotatef(90,1,0,0);
glRotatef(30,1,0,0);
cilindro(.5*an, .3*al, 1*pro);
glPushMatrix();//
glRotatef(-30,1,0,0);
glTranslatef(-0.2f*an,-0.4f*al,0.7*pro);
cilindro(.3*an, .1*al, 2*pro);
glPopMatrix();//
glPopMatrix();//mano2
glPopMatrix();

//Patata Izquierda
glTranslatef(3.8*al,0,0);
glPushMatrix();//mano1
glTranslatef(0.4f*an,-0.2f*al,.7*pro);
glPushMatrix();//mano1
glRotatef(60,1,0,0);
cilindro(.7*an, .5*al, 1.4*pro);
glPushMatrix();//
glRotatef(30,1,0,0);
glTranslatef(0.0f*an,0.5f*al,.8*pro);
cilindro(.6*an, .2*al, 2*pro);
glPopMatrix();//
glPopMatrix();//mano1


//Pata Derecha
glTranslatef(0.4f*an,0*al,-1.4*pro);
glPushMatrix();
glRotatef(90,1,0,0);
glRotatef(30,1,0,0);
cilindro(.7*an, .5*al, 1.4*pro);
glPushMatrix();//
glRotatef(-30,1,0,0);
glTranslatef(0.0f*an,-0.5f*al,0.8*pro);
cilindro(.6*an, .2*al, 2*pro);
glPopMatrix();//
glPopMatrix();//mano2
glPopMatrix();//cierra manos

glPopMatrix();//cierre de tronco

//rabo

glPushMatrix();
glTranslatef(-0.1f,0.1f,0.0f);
glColor3f(1.0f, 0.6f, 0.6f);
glPushMatrix();
glRotatef(90,0,1,0);
cilindro( 0.02, 0.02, 0.1);
glPopMatrix();
glPopMatrix();


glFlush();
glutSwapBuffers();
}

void reshape(int width, int height){
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-20,20,-20,20,-40,40);
//gluPerspective(110,(GLfloat)width/(GLfloat)height, 0.1, 50);
glMatrixMode(GL_MODELVIEW);
venAncho = width;
venAlto = height;
}

void keyboard(unsigned char key, int x, int y){
switch(key)
{
case 27:
exit(0);
case 'w':
angleX += 1.0f;
break;
case 'W':
angleX -= 1.0f;
break;
case 's':
angleZ += 1.0f;
break;
case 'S':
angleZ -= 1.0f;
break;
case 'a':
angleY += 1.0f;
break;
case 'A':
angleY -= 1.0f;
break;
case 'z':
transZ += 0.1f;
break;
case 'Z':
transZ -= 0.1f;
break;
case 'x':
transX += 0.1f;
break;
case 'X':
transX -= 0.1f;
break;
case 'y':
transY += 0.1f;
break;
case 'Y':
transY -= 0.1f;
break;
case 'b':
xScale += 0.1f;
break;
case 'B':
xScale -= 0.1f;
break;
case 'n':
yScale += 0.1f;
break;
case 'N':
yScale -= 0.1f;
break;
case 'm':
zScale += 0.1f;
break;
case 'M':
zScale -= 0.1f;
break;
case 'c':
camina=0;
break;
case 'C':
camina=1;
break;
case 'V':
if(codo>=0)
codo-=1.0;
break;
case 'v':
if(codo<=135)
codo+=1;
break;
case 'G':
if(hom>=0)
hom-=1.0;
break;
case 'g':
if(hom<90)
hom+=1;
break;
case 'H':
if(cintu>=-20)
cintu-=1.0;
break;
case 'h':
if(cintu<=45)
cintu+=1;
break;
case 'R':
if(rod>=0)
rod-=1.0;
break;
case 'r':
if(rod<=45)
rod+=1;
break;
case 'F':
if(tor>=0)
tor-=1.0;
break;
case 'f':
if(tor<=45)
tor+=1;
break;
default:
break;
}
}
void init(){
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
}


Tarea 3

Modelar jerárquicamente, construir el árbol del modelo y escribir el código que lo construye

El codigo completo lo puedes bajar aqui:


#include
#include "glut.h"
#include "Cubo.h"

using namespace std;

float angleX = 0.0f;
float angleY = 0.0f;
float angleZ = 0.0f;
float transZ = -10.0f;

float al,an,pro;
GLint venAncho=600;
GLint venAlto=600;
void HORMIGA();
void reshape(int width, int height);
void keyboard(unsigned char key, int x, int y);
int main(int argc, char **argv) {
al=1;
an=1;
pro=1;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(20,20);
glutInitWindowSize( 600, 600);
glClearColor(0,0,0,0);
glEnable(GL_DEPTH_TEST);
glutCreateWindow("HORMIGA 3D");
glutDisplayFunc(HORMIGA);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(HORMIGA);
glutMainLoop();
return 0;
}

void keyboard(unsigned char key, int x, int y){
switch(key)
{
case 27:
exit(0);
case 'w':
angleX += 5.0f;
break;
case 'W':
angleX -= 5.0f;
break;
case 's':
angleZ += 5.0f;
break;
case 'S':
angleZ -= 5.0f;
break;
case 'a':
angleY += 5.0f;
break;
case 'A':
angleY -= 5.0f;
break;
case 'z':
transZ += 0.1f;
break;
case 'Z':
transZ -= 0.1f;
break;
default:
break;
}
}
void HORMIGA(void){
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0f, 0.0f, transZ);
glRotatef(angleX, 1.0f, 0.0f, 0.0f);
glRotatef(angleY, 0.0f, 1.0f, 0.0f);
glRotatef(angleZ, 0.0f, 0.0f, 1.0f);

//cola
glPushMatrix();
glColor4f(.9f,0.40f,0.12f,.5f);
glScalef(4,4,4);
glTranslatef(0.0f,0.0f,0.0f);
drawCube(an,al,pro);
glPopMatrix();

//Tronco
glTranslatef(0.0f,4,0.0f);
glPushMatrix();
glColor4f(1.0f,0.40f,0.12f,.5f);
glScalef(3,2,2);
drawCube(an,al,pro);
glPopMatrix();

//cabeza
glPushMatrix();
glColor4f(.9f,0.60f,0.12f,.5f);
glTranslatef(0.0f,1.5*al,0.0f);
drawCube(an,al,pro);
glPopMatrix();

//cintura
glPushMatrix();
glColor4f(.7f,0.50f,0.12f,.5f);
glScalef(1,1.5,1);
glTranslatef(0.0f,-(7*al)/6,0.0f);
drawCube(an,al,pro);
glPopMatrix();

//patas
glPushMatrix();
glColor4f(.9f,0.40f,0.12f,.5f);
glRotatef(40.0f,0.0f,1.0f, 0.0f);
glRotatef(-20.0f,0.0f,0.0f, 1.0f);
glScalef(2,0.5,0.5);
glTranslatef(-1.2*an,1.3*al,-2.5*pro);
drawCube(an,al,pro);
glPushMatrix();
glScalef(0.5f,2,2);
glRotatef(-70.0f,0.0f,1.0f, 0.0f);
glScalef(2.5,0.5,0.5);
glTranslatef(-0.73*an,0*al,2.4*pro);
drawCube(an,al,pro);
glPushMatrix();
glScalef(1/2.5,2,2);
glRotatef(20.0f,0.0f,1.0f, 0.0f);
glScalef(1,0.5,0.5);
glTranslatef(-1.8*an,0.0f,-0.8*pro);
drawCube(an,al,pro);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glColor4f(.9f,0.40f,0.12f,.5f);
glRotatef(30.0f,0.0f,1.0f, 0.0f);
glScalef(2,0.5,0.5);
glTranslatef(-1.2*an,0.0,-2.5*pro);
drawCube(an,al,pro);
glPushMatrix();
glScalef(0.5f,2,2);
glRotatef(-70.0f,0.0f,1.0f, 0.0f);
glScalef(2.5,0.5,0.5);
glTranslatef(-0.73*an,0*al,2.4*pro);
drawCube(an,al,pro);
glPushMatrix();
glScalef(1/2.5,2,2);
glRotatef(20.0f,0.0f,1.0f, 0.0f);
glScalef(1,0.5,0.5);
glTranslatef(-1.8*an,0.0f,-0.8*pro);
drawCube(an,al,pro);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glColor4f(.9f,0.40f,0.12f,.5f);
glRotatef(40.0f,0.0f,1.0f, 0.0f);
glRotatef(20.0f,0.0f,0.0f, 1.0f);
glScalef(2,0.5,0.5);
glTranslatef(-1.2*an,-1.3*al,-2.5*pro);
drawCube(an,al,pro);
glPushMatrix();
glScalef(0.5f,2,2);
glRotatef(-70.0f,0.0f,1.0f, 0.0f);
glScalef(2.5,0.5,0.5);
glTranslatef(-0.73*an,0*al,2.4*pro);
drawCube(an,al,pro);
glPushMatrix();
glScalef(1/2.5,2,2);
glRotatef(20.0f,0.0f,1.0f, 0.0f);
glScalef(1,0.5,0.5);
glTranslatef(-1.8*an,0.0f,-0.8*pro);
drawCube(an,al,pro);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glColor4f(.9f,0.40f,0.12f,.5f);
glRotatef(-40.0f,0.0f,1.0f, 0.0f);
glRotatef(20.0f,0.0f,0.0f, 1.0f);
glScalef(2,0.5,0.5);
glTranslatef(1.2*an,1.3*al,-2.5*pro);
drawCube(an,al,pro);
glPushMatrix();
glScalef(0.5f,2,2);
glRotatef(70.0f,0.0f,1.0f, 0.0f);
glScalef(2.5,0.5,0.5);
glTranslatef(0.73*an,0*al,2.4*pro);
drawCube(an,al,pro);
glPushMatrix();
glScalef(1/2.5,2,2);
glRotatef(-20.0f,0.0f,1.0f, 0.0f);
glScalef(1,0.5,0.5);
glTranslatef(1.8*an,0.0f,-0.8*pro);
drawCube(an,al,pro);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glColor4f(.9f,0.40f,0.12f,.5f);
glRotatef(-30.0f,0.0f,1.0f, 0.0f);
glScalef(2,0.5,0.5);
glTranslatef(1.2*an,0.0,-2.5*pro);
drawCube(an,al,pro);
glPushMatrix();
glScalef(0.5f,2,2);
glRotatef(70.0f,0.0f,1.0f, 0.0f);
glScalef(2.5,0.5,0.5);
glTranslatef(0.73*an,0*al,2.4*pro);
drawCube(an,al,pro);
glPushMatrix();
glScalef(1/2.5,2,2);
glRotatef(-20.0f,0.0f,1.0f, 0.0f);
glScalef(1,0.5,0.5);
glTranslatef(1.8*an,0.0f,-0.8*pro);
drawCube(an,al,pro);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glColor4f(.9f,0.40f,0.12f,.5f);
glRotatef(-40.0f,0.0f,1.0f, 0.0f);
glRotatef(-20.0f,0.0f,0.0f, 1.0f);
glScalef(2,0.5,0.5);
glTranslatef(1.2*an,-1.3*al,-2.5*pro);
drawCube(an,al,pro);
glPushMatrix();
glScalef(0.5f,2,2);
glRotatef(70.0f,0.0f,1.0f, 0.0f);
glScalef(2.5,0.5,0.5);
glTranslatef(0.73*an,0*al,2.4*pro);
drawCube(an,al,pro);
glPushMatrix();
glScalef(1/2.5,2,2);
glRotatef(-20.0f,0.0f,1.0f, 0.0f);
glScalef(1,0.5,0.5);
glTranslatef(1.8*an,0.0f,-0.8*pro);
drawCube(an,al,pro);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}

void reshape(int width, int height){
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10,10,-10,10, 0.1, 20);
glMatrixMode(GL_MODELVIEW);
venAncho = width;
venAlto = height;
}