Skip to content

Instantly share code, notes, and snippets.

@abuiles
Created November 20, 2009 15:11
Show Gist options
  • Save abuiles/239554 to your computer and use it in GitHub Desktop.
Save abuiles/239554 to your computer and use it in GitHub Desktop.
// robot.c: Adaptado de OpenGL Programming Guide (Neider, Davis, Woo)
#include <GL/glut.h>
#include <stdio.h>
GLfloat angle, fAspect;
static int shouldery = 0, shoulder = 0, elbow = 0,wrist=0,finger1 = 0,finger2 = 0;
GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position[] = { 5.0, 5.0, 5.0, 0.0 }; // x, y, z, 1:directional 0:point
void elbowAdd() {
elbow = (elbow + 5) % 360;
}
void elbowSubtract() {
elbow = (elbow - 5) % 360;
}
void shoulderAdd() {
shoulder = (shoulder + 5) % 360;
}
void shoulderSubtract() {
shoulder = (shoulder - 5) % 360;
}
void shoulderAddy() {
shouldery = (shouldery + 5) % 360;
}
void shoulderSubtracty() {
shouldery = (shouldery - 5) % 360;
}
void wSubtractz()
{
wrist = (wrist - 5) % 360;
}
void wAddz()
{
wrist = (wrist + 5) % 360;
}
void fSubtract()
{
finger1 = (finger1 - 5) % 360 ;
finger2 = (finger2 + 5) % 360 ;
}
void fAdd()
{
finger1 = (finger1 + 5) % 360;
finger2 = (finger2 - 5) % 360 ;
}
// Desplegar la escena 3D
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 1.0f);
GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_ambient[] = { 0.2, 0.2, 0.2, 0.1};
GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
GLfloat mat_blue[] = { 0.1, 0.2, 0.8, 1.0 };
GLfloat mat_red[] = { 0.8, 0.2, 0.1, 1.0 };
GLfloat mat_green[] = { 0.1, 0.8, 0.1, 1.0 };
GLfloat mat_specular[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat no_shininess[] = { 0.0 };
GLfloat low_shininess[] = { 5.0 };
GLfloat high_shininess[] = { 100.0 };
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_green);
//glTranslatef(-1., 0., 0.);
glRotatef((GLfloat) shoulder, 0., 0., 1.);
glRotatef((GLfloat) shouldery, 1., 0., 0.);
glTranslatef(1., 0., 0.);
glPushMatrix();
glScalef(2.0, 0.4, 1.0);
glutSolidCube(1.); // shoulder
glPopMatrix();
glTranslatef(1, 0., 0.);
glRotatef((GLfloat) elbow, 0., 1., 0.);
glTranslatef(1, 0., 0.);
glPushMatrix();
glScalef(2.0, 0.4, 1.0);
glutSolidCube(1); // elbow
glPopMatrix();
glTranslatef(1., 0., 0.);
glRotatef((GLfloat)wrist, 0., 0., 1.);
glTranslatef(1., 0., 0.);
glPushMatrix();
glScalef(2.0, 0.4, 1.0);
glutSolidCube(1); // elbow
glPopMatrix();
glTranslatef(0., 0., 0.3);
glTranslatef(.7, 0., 0.0);
glPushMatrix();
glRotatef((GLfloat) finger1, 0., 1., 0.);
glTranslatef(.7, 0., 0.);
glScalef(1., 0.1, 0.1);
glutSolidCube(1); // elbow
glPopMatrix();
glTranslatef(0.0, 0., -0.5);
glTranslatef(-.7, 0., 0.);
glTranslatef(.7,0.,0.);
glPushMatrix();
glRotatef((GLfloat) (finger2), 0., 1., 0.);
glTranslatef(.7, 0., 0.);
glScalef(1., 0.1, 0.1);
glutSolidCube(1); // elbow
glPopMatrix();
glPopMatrix();
// Ejecutar los comandos
glutSwapBuffers();
}
// Inicializar los parametros de despliegue
void init (void)
{
// glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
angle=45;
glClearColor(0.0, 0.1, 0.1, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
// Especificar los parametros de visualizacion
void visualizationParams(void)
{
// Sistema de coordenadas de proyeccion
glMatrixMode(GL_PROJECTION);
// Inicializa el sistema de coordenadas de la proyeccion
glLoadIdentity();
// Especifica proyeccion perspectiva
gluPerspective(angle,fAspect,0.1,500);
// Especifica el sistema de coordenadas del modelo
glMatrixMode(GL_MODELVIEW);
// Inicializa el sistema de coordenadas del modelo
glLoadIdentity();
// Especifica la posicion del observador
gluLookAt(0,0,15, 0,0,0, 0,1,0);
}
// Se llama cuando cambia el tamanyo de la ventana
void reshape(GLsizei w, GLsizei h)
{
// Para prevenir division por 0
if ( h == 0 ) h = 1;
// Tamanyo del viewport
glViewport(0, 0, w, h);
// Aspect Ration
fAspect = (GLfloat)w/(GLfloat)h;
visualizationParams();
}
// Responder a los eventos de teclas especiales
void specialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP) {
elbowAdd();
}
if(key == GLUT_KEY_DOWN) {
elbowSubtract();
}
if(key == GLUT_KEY_LEFT) {
shoulderAddy();
}
if(key == GLUT_KEY_RIGHT) {
shoulderSubtracty();
}
glutPostRedisplay();
}
void normalKeys(unsigned char key, int x, int y)
{
if (key == 'q')
{
fSubtract ();
}
if (key == 'w')
{
fAdd ();
}
if(key == 'a') {
shoulderSubtract();
}
if(key == 's') {
shoulderAdd();
}
if(key == 'z') {
wSubtractz();
}
if(key == 'x'){
wAddz();
}
glutPostRedisplay();
}
// Programa Principal
int main(int argc,char* argv[])
{
glutInit (&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("Brazo Robot");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(normalKeys);
glutSpecialFunc(specialKeys);
init();
glutMainLoop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment