Programa usando figuras como Toroides y Esferas.
El programa permite visualizar los gráficos 3D con luz, profundidad y sombras.
Imagen al compilar
Imagen con desplazamiento
Código del Gráfico
#include <vcl.h>
//#include <vcl\condefs.h>
#include <windows.h>
#include <GL/glut.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
//---------------------------------------------------------------------------
//USELIB("gl\GLUT32.LIB");
//---------------------------------------------------------------------------
GLfloat elevacion = 0.0f;
GLfloat azimitud = 0.0f;
GLfloat giro = 0.0f;
//parametros para construir la grilla
#define DEF_floorGridScale 0.5
#define DEF_floorGridXSteps 11.0
#define DEF_floorGridZSteps 11.0
void polarView(GLfloat distance, GLfloat twist, GLfloat elevation, GLfloat azimuth)
{
glTranslated(0.0, -0.7, distance);
glRotated(twist, 0.0, 0.0, 1.0);
glRotated(elevation, 1.0, 0.0, 0.0);
glRotated(azimuth, 0.0, 0.0, 1.0);
}
//Inicializa las propiedades de la fuente de luz
static void init(void)
{
GLfloat light_ambient[] = { 0.75, 0.75, 0.75, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0.0, 0.0, 1.0, 1.0 };
glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}
void display (void)
{
//Defino un material Rojo
GLfloat mat_ambient_toro[] = {0.05, 0.05, 0.05, 1.0f};
GLfloat mat_diffuse_toro[] = { 0.8, 0.0, 0.0, 1.0f};
GLfloat mat_specular_toro[] = {0.9, 0.8, 0.8, 1.0f};
//Defino un material Azul
GLfloat mat_ambient_cubo[] = {0.1, 0.1, 0.1, 1.0f};
GLfloat mat_diffuse_cubo[] = {0.0, 0.0, 0.8, 1.0f};
GLfloat mat_specular_cubo[] = {0.9, 0.9, 0.9, 1.0f};
//Defino un material Verde
GLfloat mat_ambient_esfera[] = {0.1, 0.1, 0.1, 1.0f};
GLfloat mat_diffuse_esfera[] = {0.7, 0.7, 0.0, 1.0f};
GLfloat mat_specular_esfera[] = {0.8, 0.8, 0.8, 1.0f};
GLfloat zExtent, xExtent, xLocal, zLocal;
int loopX, loopZ;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW_MATRIX );
glLoadIdentity();
polarView(-20.0f, giro, elevacion,azimitud);
//Dibujo la Grilla
glPushMatrix();
glPushAttrib( GL_LIGHTING_BIT );
glDisable( GL_LIGHTING );
glColor3f( 0.0, 0.7, 0.7 );
glPopAttrib();
glPopMatrix();
//Dibuja el Toro
glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient_toro);
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse_toro);
glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular_toro);
glMaterialf (GL_FRONT, GL_SHININESS, 50.0f);
glPushMatrix ();
glTranslatef (-1.2, 1.4, 2.2);
glRotatef(-45.0, 0.0, 1.0, 0.0);
glRotatef(-45.0, 1.0, 0.0, 0.0);
glutSolidTorus (0.1, 5, 5, 100);
glPopMatrix ();
glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient_toro);
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse_toro);
glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular_toro);
glMaterialf (GL_FRONT, GL_SHININESS, 50.0f);
glPushMatrix ();
glTranslatef (-1.2, 1.4, 2.2);
glRotatef(-45.0, 0.0, 1.0, 0.0);
glRotatef(-45.0, 1.0, 0.0, 0.0);
glutSolidTorus (0.12, 8, 8, 300);
glPushMatrix ();
glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient_esfera);
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse_esfera);
glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular_esfera);
glMaterialf (GL_FRONT, GL_SHININESS, 100.0f);
glPushMatrix ();
glTranslatef (0.0, 0.0, 0.0);
glutSolidSphere (1.0, 16, 16);
glPopMatrix ();
glFlush ();
glutSwapBuffers ( );
}
//------------------------------------------------------------------------------------
void displaySfera (void)
{
//Defino un material Verde
GLfloat mat_ambient_esfera[] = {0.1, 0.1, 0.1, 1.0f};
GLfloat mat_diffuse_esfera[] = {0.7, 0.7, 0.0, 1.0f};
GLfloat mat_specular_esfera[] = {0.8, 0.8, 0.8, 1.0f};
GLfloat zExtent, xExtent, xLocal, zLocal;
int loopX, loopZ;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW_MATRIX );
glLoadIdentity();
polarView(-20.0f, giro, elevacion,azimitud);
//Dibujo la Grilla
glPushMatrix();
glPushAttrib( GL_LIGHTING_BIT );
glDisable( GL_LIGHTING );
glColor3f( 0.0, 0.7, 0.7 );
glPopAttrib();
glPopMatrix();
//Dibuja la esfera de radio 2.5
glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient_esfera);
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse_esfera);
glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular_esfera);
glMaterialf (GL_FRONT, GL_SHININESS, 100.0f);
glPushMatrix ();
glTranslatef (-1.0, 1.0, 0.0);
glutSolidSphere (1.0, 16, 16);
glPopMatrix ();
glFlush ();
glutSwapBuffers ( );
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(30, (GLfloat) w/(GLfloat) h, 1.0, 200.0);
glMatrixMode (GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y)
{ elevacion=(elevacion==-360||elevacion==360)?0.0f:elevacion;
azimitud=(azimitud==-360||azimitud==360)?0.0f:azimitud;
giro=(giro==-360||giro==360)?0.0f:giro;
clrscr();
printf("%f\n",elevacion);
printf("%f\n",azimitud);
printf("%f\n",giro);
switch (key) {
case 'x':
case 'X': elevacion++;
//elevacion = elevacion + 1.0f;
glutPostRedisplay();
break;
case 'v':
case 'V':
Sleep(5);
elevacion-100;
//elevacion = elevacion - 1.0f;
glutPostRedisplay();
break;
case 'y':
case 'Y':azimitud++;
//azimitud = azimitud + 1.1f;
glutPostRedisplay();
break;
case 'u':
case 'U':azimitud--;
//azimitud = azimitud - 1.2f;
glutPostRedisplay();
break;
case 'z':
case 'Z':giro++;
//giro = giro + 1.0f;
glutPostRedisplay();
break;
case 'c':
case 'C':giro--;
//giro = giro - 1.0f;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
}
}
int main(int argc, char **argv)
{
glutInitWindowSize(800, 500);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
//glutDisplayFunc(displaySfera);
glutMainLoop();
return 0;
}
No puedo correrlo que es el archivo
ResponderEliminar?
Yo utilizo el entorno Borland C++ Builder 6
Eliminar