saludos muchachos , debo crear un poligono en forma de casa y encima de el ,debo crear una esfera a la que debo ponerle una textura de luna y hacerla girar ya tengo la casa y la luna pero no he podido poner las 2 en la misma ventana,
alguien sabe como realizar esto?
Si trabajas con glut la esfera la puedes hacer con un glutSphere.
Como realizas el pintado? Es imposible que no puedas poner las dos cosas en la misma ventana..
glu también te da la funcionalidad para dibujar una esfera (y otras primitivas). En la web de nehe tienes algunos ejemplos:
http://nehe.gamedev.net/data/lessons/lesso...n.asp?lesson=18
la esfera ya la tengo y la textura la pongo asi:
#include
#include
#include
#include
#include
// Angulos
float angle, angle2;
int moving, startx, starty;
GLfloat ambient_light[] = {0.3, 0.3, 0.0, 1.0};
GLfloat source_light[] = {0.9, 0.8, 0.8, 1.0};
GLfloat light_pos[] = {0.0, 0.0, 0.0, 1.0};
void load_texture ( char *file_name, int width, int height, int depth,
GLenum colour_type, GLenum filter_type )
{
GLubyte *raw_bitmap ;
FILE *file;
if ((file = fopen(file_name, "rb"))==NULL )
{
printf ( "No se encontro archivo: %s\n", file_name );
exit ( 1 );
}
raw_bitmap = (GLubyte *) malloc ( width * height * depth * ( sizeof(GLubyte)) );
if (raw_bitmap == NULL)
{
printf ( "No puede asignarse memoria para la textura" );
fclose ( file);
exit ( 1 );
}
fread ( raw_bitmap , width * height * depth, 1 , file );
fclose ( file);
// Set del "Filtering type"
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_type );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_type );
// Set del "Texture Evironment"
glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// Construye mapeo -> Mipmaps
gluBuild2DMipmaps ( GL_TEXTURE_2D, colour_type, width, height,
colour_type, GL_UNSIGNED_BYTE, raw_bitmap );
// Free up the array
free ( raw_bitmap );
}
void init ( void )
{
// Set up de modelo de iluminación sencillo
glEnable ( GL_LIGHTING );
glLightModelfv ( GL_LIGHT_MODEL_AMBIENT, ambient_light );
glLightfv (GL_LIGHT0,GL_DIFFUSE, source_light );
glLightfv ( GL_LIGHT0,GL_POSITION, light_pos );
glEnable ( GL_LIGHT0 );
// Enable de las propiedades del material para la iluminación
glEnable ( GL_COLOR_MATERIAL );
glColorMaterial ( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
glEnable ( GL_TEXTURE_2D );
glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );
load_texture ( "moon_r.raw", 500,200, 3, GL_RGB, GL_LINEAR );
// Dejamos la comparación de profundidad "apagada", pues no se necesita...
// glEnable (GL_DEPTH_TEST);
glEnable ( GL_CULL_FACE );
//glClearColor (0.0, 0.0, 0.0, 0.0);
}
void draw_moon ( void )
{
glPushMatrix ( );
glRotatef ( 90.0, 1.0, 0.0, 0.0 );
glutSolidSphere (1.0, 16, 16);
glEnd();
glPopMatrix ( );
/*glBegin(GL_POLYGON);
glTranslatef(-40.0,25.0,-150);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0f, 0.0f, 0.0f); // glvertex3d para 3 coordenadas que sean double.
glVertex3f(0.4f,-0.5f,0.0f);
glVertex3f(0.4f,-0.80f,0.0f);
glVertex3f(-0.4f,-0.8f,0.0f);
glVertex3f(-0.4f,-0.5f,0.0f);
glEnd();
glFlush();*/
}
void display ( void )
{
glClear ( GL_COLOR_BUFFER_BIT );
glLightfv ( GL_LIGHT0,GL_POSITION, light_pos );
glPushMatrix ( );
glTranslatef ( -3.0, 1.0, -15.0 );
glRotatef ( angle2, 1.0, 0.0, 0.0 );
glRotatef ( angle, 0.0, 1.0, 0.0 );
draw_moon ( );
// draw_house ( );
glPopMatrix ( );
glutSwapBuffers( );
}
void reshape ( int w, int h )
{
glViewport ( 0, 0, (GLint) w, (GLint) h );
glMatrixMode ( GL_PROJECTION);
glLoadIdentity( );
if ( h==0 )
gluPerspective ( 45, (GLdouble)w, 1.0, 2000.0 );
else
gluPerspective ( 45, (GLdouble)w / (GLdouble)h,1.0, 2000.0 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );
}
void idle_func ( void )
{
angle = angle + 0.1;
if ( angle == 360 )
angle = 0;
glutPostRedisplay ( );
}
int main ( int argc, char** argv )
{
glutInit ( &argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowPosition(10,10);
glutInitWindowSize ( 700, 700);
glutCreateWindow ("CASALUNA");
init();
glutReshapeFunc ( reshape );
glutDisplayFunc ( display );
glutIdleFunc ( idle_func );
glutMainLoop ( );
return 0;
}
EL POLIGONO EN FORMA DE CASA LO TENGO ASI:
PERO NO HE PODIDO PONER ESTA CASA CON LA LUNA JUNTAS ALGUIEN SABE COMO UNIRLAS? LE AGRADECERIA ME AYUDARA
#include
#include
#include
#include
#include
float angle, angle2;;
void changeSize(int w, int h)
{
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
float ratio = 1.0* w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,
0.0,0.0,-1.0,
0.0f,1.0f,0.0f);
}
void dibujar()
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); // Se borra el buffer de la pantalla
glBegin(GL_POLYGON); // Se va a empezar una secuencia de triángulos.
glVertex3f(0.0f, 0.0f, 0.0f); // glvertex3d para 3 coordenadas que sean double.
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.4f,-0.5f,0.0f);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.4f,-0.80f,0.0f);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-0.4f,-0.8f,0.0f);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.4f,-0.5f,0.0f);
glEnd();
glFlush();
glPushMatrix();
//glRotatef(angle,0.0,0.0,0.0);
glRotatef ( angle2, 1.0, 0.0, 0.0 );
glRotatef ( angle, 0.0, 1.0, 0.0 );
glTranslatef(-40.0,25.0,-150);
glColor3f(1.0,0.0,1.0);//Dibuja la bola amarilla
glutSolidSphere(6,20,10);
//glPopMatrix();
glPopMatrix();
angle++;
// Se termina de definir los triángulos.
}
/*void bola()
{
glPushMatrix();
glTranslatef(0,0,-150);
glColor4f(0.0,0.8,0.0,1.0);//Dibuja la bola amarilla
glutSolidSphere(6,20,10);
glPopMatrix();
}*/
int main( int argc, char **argv)
{
glutInit(&argc, argv);//inicializamos glut usamos la función glutInit pasándole como argumentos, los parámetros de la línea de comandos como punteros:
glutInitWindowPosition(10,10);//posicion de la ventana
glutInitWindowSize(700,500);//tamaño de la ventana
glutInitDisplayMode(GLUT_RGB |GLUT_SINGLE | GLUT_DEPTH);//inicilizamos el modo de desplegado de la pantalla usando la función
glutCreateWindow("Ventana de la casa");
glutDisplayFunc(dibujar);
// glutDisplayFunc(bola);
glutReshapeFunc(changeSize);
//glutIdleFunc(dibujar);
glutIdleFunc ( idle_func );
glutMainLoop();
}
void idle_func ( void )
{
angle = angle + 0.1;
if ( angle == 360 )
angle = 0;
glutPostRedisplay ( );
}