Còdigo en c++:
Libreria que dibuja un cubo dimensiones pasadas por parametros lo alto, ancho y profundidad:
#include "glut.h"
void drawCube(float A,float B,float C){
float ancho=A/2;
float alto=B/2;
float prof=C/2;
//Caras del cubo
//frontal
glBegin(GL_QUADS);
glVertex3f(-ancho,-alto,prof);
glVertex3f(ancho,-alto,prof);
glVertex3f(ancho,alto,prof);
glVertex3f(-ancho,alto,prof);
glEnd();
glBegin(GL_QUADS); //cara trasera
glVertex3f( ancho, -alto, -prof);
glVertex3f(-ancho, -alto, -prof);
glVertex3f(-ancho, alto, -prof);
glVertex3f( ancho, alto, -prof);
glEnd();
glBegin(GL_QUADS); //cara lateral izq
glVertex3f(-ancho,-alto, -prof);
glVertex3f(-ancho,-alto, prof);
glVertex3f(-ancho, alto, prof);
glVertex3f(-ancho, alto, -prof);
glEnd();
glBegin(GL_QUADS); //cara lateral dcha
glVertex3f(ancho, -alto, prof);
glVertex3f(ancho, -alto, -prof);
glVertex3f(ancho, alto, -prof);
glVertex3f(ancho, alto, prof);
glEnd();
glBegin(GL_QUADS); //cara arriba
glVertex3f(-ancho, alto, prof);
glVertex3f( ancho, alto, prof);
glVertex3f( ancho, alto, -prof);
glVertex3f(-ancho, alto, -prof);
glEnd();
glBegin(GL_QUADS); //cara abajo
glVertex3f( ancho,-alto, -prof);
glVertex3f( ancho,-alto, prof);
glVertex3f(-ancho,-alto, prof);
glVertex3f(-ancho,-alto, -prof);
glEnd();
}
Codigo del programa principal que contiene el "main":
#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 humanoide();
void reshape(int width, int height);
void keyboard(unsigned char key, int x, int y);
int main(int argc, char **argv) {
cout<<"dimensiones del cubo\nAlto: ";
cin>>al;
cout<<"Ancho: ";
cin >>an;
cout<<"Profundidad: ";
cin>>pro;
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("Humanoide");
glutDisplayFunc(humanoide);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(humanoide);
glutMainLoop();
return 0;
}
void humanoide(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);
//Tronco
glTranslatef(0.0f,4,0.0f);
glPushMatrix();
glColor4f(0.0f,0.0f,2.0f,1.0f);
glScalef(3,2,2);
drawCube(an,al,pro);
glPopMatrix();
//cabeza
glPushMatrix();
glColor4f(0.0f,1.0f,.0f,1.0f);
glTranslatef(0.0f,1.5*al,0.0f);
drawCube(an,al,pro);
glPopMatrix();
//cintura
glPushMatrix();
glColor4f(0.0f,0.9f,0.5f,1.0f);
glScalef(1,1.5,1);
glTranslatef(0.0f,-(7*al)/6,0.0f);
drawCube(an,al,pro);
glPopMatrix();
//pata IZQ 1
glPushMatrix();
glColor4f(1.0f,2.0f,0.0f,1.0f);
glRotatef(30.0f,0.0f,1.0f, 0.0f);
glRotatef(-10.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();
//pata IZQ 2
glPushMatrix();
glColor4f(1.0f,2.0f,0.0f,1.0f);
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();
//pata IZQ 3
glPushMatrix();
glColor4f(1.0f,2.0f,0.0f,1.0f);
glRotatef(30.0f,0.0f,1.0f, 0.0f);
glRotatef(10.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();
//pata der 1
glPushMatrix();
glColor4f(1.0f,2.0f,0.0f,1.0f);
glRotatef(-30.0f,0.0f,1.0f, 0.0f);
glRotatef(10.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();
//pata der 2
glPushMatrix();
glColor4f(1.0f,2.0f,0.0f,1.0f);
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();
//pata der 3
glPushMatrix();
glColor4f(1.0f,2.0f,0.0f,1.0f);
glRotatef(-30.0f,0.0f,1.0f, 0.0f);
glRotatef(-10.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();
//cola
glPushMatrix();
glColor4f(1.0f,0.0f,0.5f,0.9f);
glScalef(4,4,4);
glTranslatef(0.0f,-1.125*al,0.0f);
drawCube(an,al,pro);
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;
}
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;
default:
break;
}
}
No hay comentarios:
Publicar un comentario