lunes, 16 de abril de 2007

Previo Practica 7

#include string.h
#include stdio.h
#include GL/glut.h
#include GL/gl.h
#include windows.h
#include math.h
#include stdarg.h
#include malloc.h
/* Windows parameters */
#define WIDTH 600
#define HEIGHT 600
#define TITLE "GLUT Demo: Using Subwindows"
void conv(void);
void onMouse(int button, int state, int x, int y);
void mano(void);
void Humanoide(void);
void Movimiento(void);
void onMouse2(int button, int state, int x, int y);
void onMotion2(int x, int y) ;
void onMotion(int x, int y);
void idle2(void);
int winIdMain;
int winIdSub;
int xo,im,yo,alpha,beta,ven,ven2,inif,aux;
int winIdSub2;
float xf,yf;
char xm[3]="000";
char ym[3]="000";
float r1,r2,r3,r4,r5,r6,rmu1,xmu,ymu,zmu,zp,xp;
float r7,r8,r9,r10,r11,r12,r13,r14,r15;
float x,y,z,brazod, brazoi, pied, piei;
int k=0;
xf=150;
yf=200;
ven=0;
ven2=0;
inif=0;
void init(void)
{
glClear (GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT);
}
void text(GLuint x, GLuint y, GLfloat scale,char *format,char *format2) { //Funcion que escribe texto
va_list args;
char buffer[255], *p;
GLfloat font_scale = 119.05f + 33.33f;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, glutGet(GLUT_WINDOW_WIDTH), 0,
glutGet(GLUT_WINDOW_HEIGHT));
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glTranslatef(x, y, 0.0);
glScalef(scale/font_scale, scale/font_scale, scale/font_scale);
//len = (int) strlen(format);
glutStrokeCharacter(GLUT_STROKE_ROMAN, 40);
for(p = buffer; *p; p++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
glutStrokeCharacter(GLUT_STROKE_ROMAN, 44);
va_start(args, format2);
vsprintf(buffer, format2, args);
va_end(args);
//len = (int) strlen(format2);
for(p = buffer; *p; p++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
glutStrokeCharacter(GLUT_STROKE_ROMAN, 41);
glPopAttrib();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void fractal(void){ //Funcion que calcula el fractal
int ran;
ran = rand()%4;
if(ran==1){
xf=(50+xf)/2;
yf=(200+yf)/2;
}
if(ran==2){
xf=(250+xf)/2;
yf=(200+yf)/2;
}
if(ran==3){
xf=(150+xf)/2;
yf=(50+yf)/2;
}
}
void pinta(void){ //Funcion que pinta fractal
int xe,ze;
float x2,y2;
glColor3f(1.0,1.0,0.0);
glBegin(GL_POINTS);
glVertex3f(50.0f,200.0f,0.0f);
glVertex3f(250.0f,200.0f,0.0f);
glVertex3f(150.0f,50.0f,0.0f);
glEnd();
if(k==0){
glBegin(GL_POINTS);
glVertex3f(xf,yf,0);
glEnd();
k=1;
}
fractal();
glBegin(GL_POINTS);
glVertex3f(xf,yf,0);
glEnd();
}
void Display (void) //Funcion que muestra panatalla principal
{
/* Clean drawing board */
glutSetWindow(winIdMain);
//glLoadIdentity ();
pinta();
glFlush();
}
/* Another display function, this one will be
used to update the graphic subwindow */
void subDisplay (){ //Funcion que muestra contenido de las subventanas
/* Clear subwindow */
if(im==1){
glutSetWindow (winIdSub);
glutSetCursor (GLUT_CURSOR_CYCLE);
glutPositionWindow (20, 20);
glClearColor (0.0, 0.0, 1.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT);
itoa(xo,xm,10);
itoa(yo,ym,10);
glColor3f(1.0,1.,0.0) ;
glutPassiveMotionFunc(onMotion2);
text(xo,(500-yo),20,xm,ym);
}
if(im==2){
glutSetWindow (winIdSub2);
glutSetCursor (GLUT_CURSOR_SPRAY);
glutPositionWindow (100, 100);
glClearColor (1.0, 0.0,0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,1.0,0.0);
glutMouseFunc(onMouse2);
glPushMatrix();
glScalef(0.2,.2,.2);
//glTranslatef(0,0,0.0f);
glPushMatrix();
glScalef(0.4,2.4,2.0);
glTranslatef(-21,2.75f,0.0f);
glRotatef(90,0,0,1);
mano( );
glPopMatrix();
glPushMatrix();
glScalef(0.4,2.4,2.0);
glTranslatef(21,2.75f,0.0f);
glRotatef(-90,0,0,1);
mano();
glPopMatrix();
Humanoide();
glPopMatrix();
glutSwapBuffers ();
}
}
/* Callback function for reshaping the main window */
void Reshape (int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, h, 0);
glMatrixMode(GL_MODELVIEW);
}
/* Callback function for reshaping the subwindow */
void subReshape (int w, int h)
{
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0.0F, 1.0F, 0.0F, 1.0F);
}
void idle2(void) //maneja a la subventana
{
Movimiento();
subDisplay();
}
void idle (void) //maneja ventana principal
{
glutSetWindow (winIdMain);
glutPostRedisplay ();
glutSetWindow (winIdSub);
glutPostRedisplay ();
glutSetWindow (winIdSub2);
glutPostRedisplay ();
}
void onMouse(int button, int state, int x, int y) //Funcion de mouse ventana principal
{
if ( (button == GLUT_LEFT_BUTTON) & (state == GLUT_DOWN) ) {
if(ven2==1){
ven2=0;
glutDestroyWindow (winIdSub2);
goto yy;
}
if(ven==0){
winIdSub = glutCreateSubWindow (winIdMain, 500,500, 500 , 500);
glutMouseFunc(onMouse);
im=1;
glutDisplayFunc (subDisplay);
ven=1;
xo=x;
yo=y;
}
else ven=1;
yy: aux=0;
}
if ( (button == GLUT_RIGHT_BUTTON) & (state == GLUT_DOWN) ) {
if(ven==1){
ven=0;
glutDestroyWindow(winIdSub);
goto xx;
}
if(ven2==0){
winIdSub2 = glutCreateSubWindow (winIdMain, 500,500, 500 , 500);
glutMouseFunc(onMouse2);
im=2;
glutDisplayFunc (subDisplay);
glutIdleFunc (idle2);
ven2=1;
xo=x;
yo=y;
}
else ven2=1;
}
xx: aux=1;
}
void onMouse2(int button, int state, int x, int y) //Funcion mouse subventana
{
if ( (button == GLUT_LEFT_BUTTON) & (state == GLUT_DOWN) ) {
if(ven2==1){
ven2=0;
glutSetWindow (winIdMain);
main();
glutDestroyWindow (winIdSub2);
goto yy;
}
if(ven==0){
winIdSub = glutCreateSubWindow (winIdMain, 500,500, 500 , 500);
glutMouseFunc(onMouse);
im=1;
glutDisplayFunc (subDisplay);
ven=1;
xo=x;
yo=y;
}
else ven=1;
yy: aux=0;
}
}
void onMotion(int x, int y) { //Funcion de movimiento del mouse ventana principal
alpha = (alpha + (y - yo));
beta = (beta + (x - xo));
xo = x; yo = y;
glutPostRedisplay();
}
void onMotion2(int x, int y) { //Funcion de movimiento del mouse subventana
alpha = (alpha + (y - yo));
beta = (beta + (x - xo));
xo = x; yo = y;
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y) //Evento de teclado
{
switch (key) {
case 27:
exit(0);
break;
}
}
int main (int argc, char **argv)
{
/* Glut initializations */
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE GLUT_RGBA GLUT_DEPTH);
glutInitWindowPosition (20,20);
glutInitWindowSize (WIDTH, HEIGHT);
/* Main window creation and setup */
winIdMain = glutCreateWindow (TITLE);
// glutSetWindow (winIdMain);
init ();
glutDisplayFunc (Display);
glutReshapeFunc (Reshape);
glutMouseFunc(onMouse);
glutPassiveMotionFunc(onMotion);
glutKeyboardFunc(keyboard);
glutIdleFunc (idle);
glutMainLoop ();
return 0;
}
void Movimiento(void) //Movimiento aleatorio del humanoide
{
x=rand() %2;
y=rand() %2;
z=rand() %2;
brazod=rand()%80;
brazoi=rand()%80;
pied=rand()%80;
piei=rand()%80;
//ejes de rotación de la muñeca
xmu=rand() %2;
ymu=rand() %2;
zmu=rand() %2;
//àngulo de la muñeca
rmu1=rand()%90;
//angulos de los grados de libertad del dedo medio
r1=rand()%180;
r2=rand()%180;
r3=rand()%180;
//angulos de los grados de libertad del dedo indice
r4=rand()%180;
r5=rand()%180;
r6=rand()%180;
//angulos de los grados de libertad del dedo anular
r7=rand()%180;
r8=rand()%180;
r9=rand()%180;
//angulos de los grados de libertad del dedo meñique
r10=rand()%180;
r11=rand()%180;
r12=rand()%180;
//angulos de los grados de libertad del dedo pulgar
r13=rand()%180;
r14=rand()%180;
r15=rand()%180;
//ejes de rotación del dedo pulgar
zp=1;
xp=0;
}
void Humanoide(void) //Funcion del Humanoide
{
glPushMatrix();
//cabeza
glTranslatef(0.0f,2.5f,0.0f);
glutWireCube (0.5);
//tronco
glScalef(2.5,2.5,2.0);
glTranslatef(0.0f,-0.45f,0.0f);
glutWireCube (0.5);
//piernas
glPushMatrix();
glPushMatrix();
//pierna izquierda
glRotatef(piei,x,y,z);
glTranslatef(-0.1f,-0.55f,0.0f);
glScalef(-.3,1.2,1.0);
glutWireCube (0.5);
glPushMatrix();
glTranslatef(0.0f,-0.7f,0.0f);
glutWireCube (0.5);
glPushMatrix();
glTranslatef(0.0f,.35f,0.0f);
glScalef(1.1,.4,1.0);
glutWireCube (0.5);
glPopMatrix();
glPopMatrix();
//pies
glPushMatrix();
glTranslatef(.3f,-1.0f,0.0f);
glScalef(1.6,.2,1.0);
glutWireCube (0.5);
glPopMatrix();
glPopMatrix();
//pierna derecha
glRotatef(pied,x,y,z);
glTranslatef(0.1f,-0.55f,0.0f);
glScalef(-.3,1.2,1.0);
glutWireCube (0.5);
glPushMatrix();
glTranslatef(0.0f,-0.7f,0.0f);
glutWireCube (0.5);
glPushMatrix();
glTranslatef(0.0f,.35f,0.0f);
glScalef(1.1,.4,1.0);
glutWireCube (0.5);
glPopMatrix();
glPopMatrix();
//pies
glTranslatef(-.3f,-1.0f,0.0f);
glScalef(1.6,.2,1.0);
glutWireCube (0.5);

glPopMatrix();
//brazo derecho
glPushMatrix();
glRotatef(brazod,x,y,z);
glTranslatef(0.35f,0.1f,0.0f);
glScalef(0.5,0.4,.5);
glutWireCube (0.5);
glPushMatrix();
glTranslatef(0.4f,0.0f,0.0f);
glScalef(0.5,1.2,1.0);
glutWireCube (0.5);
glPopMatrix();
glTranslatef(0.7f,0.0f,0.0f);
glScalef(.7,1,1.0);
glutWireCube (0.5);
//brazo izquierdo
glPopMatrix();
glRotatef(brazoi,x,y,z);
glTranslatef(-0.35f,0.1f,0.0f);
glScalef(0.5,0.4,.5);
glutWireCube (0.5);
glPushMatrix();
glTranslatef(-0.4f,0.0f,0.0f);
glScalef(0.5,1.2,1.0);
glutWireCube (0.5);
glPopMatrix();
glTranslatef(-0.7f,0.0f,0.0f);
glScalef(.7,1,1.0);
glutWireCube (0.5);
glPopMatrix();

}
void mano(void) //Funcion de la mano
{
glRotatef(rmu1,xmu,ymu,zmu);
glPushMatrix();
glTranslatef(-0.1,0.0,0.0f);
glScalef(1.7,2.5,1.0);
glutWireCube (0.5);
glPopMatrix();
//dedo medio
glPushMatrix();
glTranslatef(0.0,0.9,0.0f);
glRotatef(r1,1,0,0);
glScalef(.3,1.2,1.0);
glutWireCube (0.5);
glTranslatef(0.0,.45,0.0f);
glRotatef(r2,1,0,0);
glScalef(1.0,0.7,1.0);
glutWireCube (0.5);
glTranslatef(0.0,.47,0.0f);
glRotatef(r3,1,0,0);
glScalef(1.0,0.7,1.0);
glutWireCube (0.5);
glPopMatrix();
//dedo indice
glPushMatrix();
glTranslatef(0.2,0.0,0.0f);
glTranslatef(0.0,0.9,0.0f);
glRotatef(r4,1,0,0);
glScalef(.3,1.2,1.0);
glutWireCube (0.5);
glTranslatef(0.0,.45,0.0f);
glRotatef(r5,1,0,0);
glScalef(1.0,0.7,1.0);
glutWireCube (0.5);
glTranslatef(0.0,.47,0.0f);
glRotatef(r6,1,0,0);
glScalef(1.0,0.7,1.0);
glutWireCube (0.5);
glPopMatrix();
//dedo anular
glPushMatrix();
glTranslatef(-0.2,0.0,0.0f);
glTranslatef(0.0,0.9,0.0f);
glRotatef(r7,1,0,0);
glScalef(.3,1.2,1.0);
glutWireCube (0.5);
glTranslatef(0.0,.45,0.0f);
glRotatef(r8,1,0,0);
glScalef(1.0,0.7,1.0);
glutWireCube (0.5);
glTranslatef(0.0,.47,0.0f);
glRotatef(r9,1,0,0);
glScalef(1.0,0.7,1.0);
glutWireCube (0.5);
glPopMatrix();
//dedo meñique
glPushMatrix();
glTranslatef(-0.4,0.0,0.0f);
glTranslatef(0.0,0.9,0.0f);
glRotatef(r10,1,0,0);
glScalef(.3,1.2,1.0);
glutWireCube (0.5);
glTranslatef(0.0,.45,0.0f);
glRotatef(r11,1,0,0);
glScalef(1.0,0.7,1.0);
glutWireCube (0.5);
glTranslatef(0.0,.47,0.0f);
glRotatef(r12,1,0,0);
glScalef(1.0,0.7,1.0);
glutWireCube (0.5);
glPopMatrix();
//dedo pulgar
glPushMatrix();
glTranslatef(0.45,-0.8,0.0f);
glTranslatef(0.0,0.9,0.0f);
glRotatef(r13,xp,0,zp);
glScalef(.3,1.2,1.0);
glutWireCube (0.5);
glTranslatef(0.0,.45,0.0f);
glRotatef(r14,1,0,0);
glScalef(1.0,0.7,1.0);
glutWireCube (0.5);
glTranslatef(0.0,.47,0.0f);
glRotatef(r15,1,0,0);
glScalef(1.0,0.7,1.0);
glutWireCube (0.5);
glPopMatrix();
}

No hay comentarios: