OpenGL en Ubuntu
Para compilar programas que utilicen openGL, necesité instalar primero
sudo apt-get install build-essential freeglut3-dev
El build-essential instalará las cosas que necesitas para compilar c++ y usar los makefiles. Por otro lado freeglut3-dev te proporcionará las bibliotecas que necesitas para crear programas openGL.
Inspiración

Este ejemplo crea un humanoide utilizando paralelepipedos que puedes girar y escalar con las letras ‘w’, ‘a’, ’s’ y ‘z’, la combinación de cualquiera de estas letras con la tecla
#include <iostream>
//#include <Windows.h>
#include <GL/glut.h>
#include <cstdlib>
//sample input:
// {1, 1, -1},{1, 0, -1},{1, 0, 0},{1, 1, 0}
// {0, 1, -1},{0, 0, -1},{0, 0, 0},{1, 0, 0}
// We are using the Standard Template Library namespace
using namespace std;
#define VERTICES 8
#define FACES 6
float angleX = 0.0f;
float angleY = 0.0f;
float angleZ = 0.0f;
float transZ = -10.0f;
float g_fCubeCoords[8][3];
void initGraphics(int argc, char* argv[]);
void startRendering();
void reshape(int w, int h);
void keyboard(unsigned char key, int x, int y);
void display();
void dibujaCubof(float width,float height, float depth){
//calcula 8 vertices
double mid_w= width/2.0, mid_h=height/2.0, mid_d=depth/2.0;
g_fCubeCoords[0][0]= mid_w;g_fCubeCoords[0][1]= mid_h;g_fCubeCoords[0][2]= mid_d;
g_fCubeCoords[1][0]= mid_w;g_fCubeCoords[1][1]= mid_h;g_fCubeCoords[1][2]=-mid_d;
g_fCubeCoords[2][0]= mid_w;g_fCubeCoords[2][1]=-mid_h;g_fCubeCoords[2][2]=-mid_d;
g_fCubeCoords[3][0]= mid_w;g_fCubeCoords[3][1]=-mid_h;g_fCubeCoords[3][2]= mid_d;
g_fCubeCoords[4][0]= -mid_w;g_fCubeCoords[4][1]= mid_h;g_fCubeCoords[4][2]= mid_d;
g_fCubeCoords[5][0]= -mid_w;g_fCubeCoords[5][1]= mid_h;g_fCubeCoords[5][2]=-mid_d;
g_fCubeCoords[6][0]= -mid_w;g_fCubeCoords[6][1]=-mid_h;g_fCubeCoords[6][2]=-mid_d;
g_fCubeCoords[7][0]= -mid_w;g_fCubeCoords[7][1]=-mid_h;g_fCubeCoords[7][2]= mid_d;
glBegin(GL_QUADS);
/// Front face
glVertex3fv(g_fCubeCoords[0]); glVertex3fv(g_fCubeCoords[1]);
glVertex3fv(g_fCubeCoords[2]); glVertex3fv(g_fCubeCoords[3]);
/// Back face
glVertex3fv(g_fCubeCoords[4]); glVertex3fv(g_fCubeCoords[5]);
glVertex3fv(g_fCubeCoords[6]); glVertex3fv(g_fCubeCoords[7]);
/// Left face
glVertex3fv(g_fCubeCoords[0]); glVertex3fv(g_fCubeCoords[3]);
glVertex3fv(g_fCubeCoords[7]); glVertex3fv(g_fCubeCoords[4]);
/// Right face
glVertex3fv(g_fCubeCoords[1]); glVertex3fv(g_fCubeCoords[5]);
glVertex3fv(g_fCubeCoords[6]); glVertex3fv(g_fCubeCoords[2]);
/// Top face
glVertex3fv(g_fCubeCoords[0]); glVertex3fv(g_fCubeCoords[4]);
glVertex3fv(g_fCubeCoords[5]); glVertex3fv(g_fCubeCoords[1]);
/// Bottom face
glVertex3fv(g_fCubeCoords[3]); glVertex3fv(g_fCubeCoords[2]);
glVertex3fv(g_fCubeCoords[6]); glVertex3fv(g_fCubeCoords[7]);
glEnd();
}
int main(int argc, char* argv[])
{
initGraphics(argc, argv);
startRendering();
return 0;
}
void initGraphics(int argc, char* argv[])
{
glutInit( &argc, argv ); // Start GLUT with command line arguments
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); // Set up the display mode
glutInitWindowSize( 500, 500 ); // Set window init size
glutInitWindowPosition( 0, 0 ); // Set window init position
glutCreateWindow( "Humanoide con cubos" ); // Create the window with the string as its name
// GLUT rendering functions
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc( display ); // Set up pointer functions
}
void startRendering()
{
glEnable(GL_DEPTH_TEST);
// Start rendering
glutMainLoop();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(100.0f, (GLfloat)w/(GLfloat)h, 0.1, 100);
}
void display()
{
static int i = 0;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
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);
//torso
glColor3f(.3,.9,0);
dibujaCubof(3,4,1);
//pierna izquierda
glColor3f(0.0f,0.5f,0.1f);
glTranslatef (1,-3.5,0);
dibujaCubof(1,3,1);
//pierna derecha
glTranslatef(-2,0,0);
dibujaCubof(1,3,1);
//espinillas
glTranslatef(0,-3,0);
dibujaCubof(1.5,3.5,1.5);
glTranslatef(2,0,0);
dibujaCubof(1.5,3.5,1.5);
//PIES
glColor3f(1,1,1);
glTranslatef(.25,-2,.25);
dibujaCubof(2,.5,2);
glTranslatef(-2.5,0,0);
dibujaCubof(2,.5,2);
//cabeza
glTranslatef(1.25,11.5,0);
dibujaCubof(2,2,2);
//brazos
glColor3f(0.0f,0.5f,0.1f);
glTranslatef(2.75,-2,-.25);
dibujaCubof(2.5,1,1);
glTranslatef(-5.5,0,0);
dibujaCubof(2.5,1,1);
//codos
glColor3f(1,1,1);
glTranslatef(-1.75,0,0);
dibujaCubof(1,1.4,1.4);
glTranslatef(9,0,0);
dibujaCubof(1,1.4,1.4);
//manos
glColor3f(0.0f,0.5f,0.1f);
glTranslatef(1,0,0);
dibujaCubof(1,1,1);
glTranslatef(-11,0,0);
dibujaCubof(1,1,1);
glPopMatrix();
glutSwapBuffers();
}
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;
}
glutPostRedisplay();
}
//#include <Windows.h>
#include <GL/glut.h>
#include <cstdlib>
//sample input:
// {1, 1, -1},{1, 0, -1},{1, 0, 0},{1, 1, 0}
// {0, 1, -1},{0, 0, -1},{0, 0, 0},{1, 0, 0}
// We are using the Standard Template Library namespace
using namespace std;
#define VERTICES 8
#define FACES 6
float angleX = 0.0f;
float angleY = 0.0f;
float angleZ = 0.0f;
float transZ = -10.0f;
float g_fCubeCoords[8][3];
void initGraphics(int argc, char* argv[]);
void startRendering();
void reshape(int w, int h);
void keyboard(unsigned char key, int x, int y);
void display();
void dibujaCubof(float width,float height, float depth){
//calcula 8 vertices
double mid_w= width/2.0, mid_h=height/2.0, mid_d=depth/2.0;
g_fCubeCoords[0][0]= mid_w;g_fCubeCoords[0][1]= mid_h;g_fCubeCoords[0][2]= mid_d;
g_fCubeCoords[1][0]= mid_w;g_fCubeCoords[1][1]= mid_h;g_fCubeCoords[1][2]=-mid_d;
g_fCubeCoords[2][0]= mid_w;g_fCubeCoords[2][1]=-mid_h;g_fCubeCoords[2][2]=-mid_d;
g_fCubeCoords[3][0]= mid_w;g_fCubeCoords[3][1]=-mid_h;g_fCubeCoords[3][2]= mid_d;
g_fCubeCoords[4][0]= -mid_w;g_fCubeCoords[4][1]= mid_h;g_fCubeCoords[4][2]= mid_d;
g_fCubeCoords[5][0]= -mid_w;g_fCubeCoords[5][1]= mid_h;g_fCubeCoords[5][2]=-mid_d;
g_fCubeCoords[6][0]= -mid_w;g_fCubeCoords[6][1]=-mid_h;g_fCubeCoords[6][2]=-mid_d;
g_fCubeCoords[7][0]= -mid_w;g_fCubeCoords[7][1]=-mid_h;g_fCubeCoords[7][2]= mid_d;
glBegin(GL_QUADS);
/// Front face
glVertex3fv(g_fCubeCoords[0]); glVertex3fv(g_fCubeCoords[1]);
glVertex3fv(g_fCubeCoords[2]); glVertex3fv(g_fCubeCoords[3]);
/// Back face
glVertex3fv(g_fCubeCoords[4]); glVertex3fv(g_fCubeCoords[5]);
glVertex3fv(g_fCubeCoords[6]); glVertex3fv(g_fCubeCoords[7]);
/// Left face
glVertex3fv(g_fCubeCoords[0]); glVertex3fv(g_fCubeCoords[3]);
glVertex3fv(g_fCubeCoords[7]); glVertex3fv(g_fCubeCoords[4]);
/// Right face
glVertex3fv(g_fCubeCoords[1]); glVertex3fv(g_fCubeCoords[5]);
glVertex3fv(g_fCubeCoords[6]); glVertex3fv(g_fCubeCoords[2]);
/// Top face
glVertex3fv(g_fCubeCoords[0]); glVertex3fv(g_fCubeCoords[4]);
glVertex3fv(g_fCubeCoords[5]); glVertex3fv(g_fCubeCoords[1]);
/// Bottom face
glVertex3fv(g_fCubeCoords[3]); glVertex3fv(g_fCubeCoords[2]);
glVertex3fv(g_fCubeCoords[6]); glVertex3fv(g_fCubeCoords[7]);
glEnd();
}
int main(int argc, char* argv[])
{
initGraphics(argc, argv);
startRendering();
return 0;
}
void initGraphics(int argc, char* argv[])
{
glutInit( &argc, argv ); // Start GLUT with command line arguments
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); // Set up the display mode
glutInitWindowSize( 500, 500 ); // Set window init size
glutInitWindowPosition( 0, 0 ); // Set window init position
glutCreateWindow( "Humanoide con cubos" ); // Create the window with the string as its name
// GLUT rendering functions
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc( display ); // Set up pointer functions
}
void startRendering()
{
glEnable(GL_DEPTH_TEST);
// Start rendering
glutMainLoop();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(100.0f, (GLfloat)w/(GLfloat)h, 0.1, 100);
}
void display()
{
static int i = 0;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
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);
//torso
glColor3f(.3,.9,0);
dibujaCubof(3,4,1);
//pierna izquierda
glColor3f(0.0f,0.5f,0.1f);
glTranslatef (1,-3.5,0);
dibujaCubof(1,3,1);
//pierna derecha
glTranslatef(-2,0,0);
dibujaCubof(1,3,1);
//espinillas
glTranslatef(0,-3,0);
dibujaCubof(1.5,3.5,1.5);
glTranslatef(2,0,0);
dibujaCubof(1.5,3.5,1.5);
//PIES
glColor3f(1,1,1);
glTranslatef(.25,-2,.25);
dibujaCubof(2,.5,2);
glTranslatef(-2.5,0,0);
dibujaCubof(2,.5,2);
//cabeza
glTranslatef(1.25,11.5,0);
dibujaCubof(2,2,2);
//brazos
glColor3f(0.0f,0.5f,0.1f);
glTranslatef(2.75,-2,-.25);
dibujaCubof(2.5,1,1);
glTranslatef(-5.5,0,0);
dibujaCubof(2.5,1,1);
//codos
glColor3f(1,1,1);
glTranslatef(-1.75,0,0);
dibujaCubof(1,1.4,1.4);
glTranslatef(9,0,0);
dibujaCubof(1,1.4,1.4);
//manos
glColor3f(0.0f,0.5f,0.1f);
glTranslatef(1,0,0);
dibujaCubof(1,1,1);
glTranslatef(-11,0,0);
dibujaCubof(1,1,1);
glPopMatrix();
glutSwapBuffers();
}
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;
}
glutPostRedisplay();
}
Para compilar y ejecutar el ejemplo ‘humanoide.cpp’ utilizamos este par de línea.
g++ -o humanoide humanoide.cpp -lglut -lGLU -lG
./humanoide
./humanoide
1 Comment
