El Bosque se compone de: un cubemap con la textura de cielo y pasto, una pelota anaranjada transparente y 3 arboles generados con figuras solidas y materiales.
Código
#include<stdio.h>
#include<stdlib.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <windows.h>
#include <math.h>
#define PI 3.14159
using namespace std;
int HL=1,angx=0,angy=60, angz=0;
float camy, camz;
unsigned char * datos;
float a;
int an1=512,al1=512;//dim imagenes, ancho, alto
unsigned char * imagen1;
unsigned char * imagen2;
unsigned char * imagen3;
unsigned char * imagen4;
unsigned char * imagen5;
unsigned char * imagen6;
int leerImagen1(){
FILE *imagen;
imagen=fopen("C:/ImagenesC/grass.RAW","r");
imagen1=(unsigned char*)malloc(an1*al1*3);
if(imagen==NULL){
printf("Error: No imagen");
return 0;
}
fread(imagen1,an1*al1*3,1,imagen);
fclose(imagen);
return 1;
}
int leerImagen2(){
FILE *imagen;
imagen=fopen("C:/ImagenesC/azul.RAW","r");
imagen2=(unsigned char*)malloc(an1*al1*3);
if(imagen==NULL){
printf("Error: No imagen");
return 0;
}
fread(imagen2,an1*al1*3,1,imagen);
fclose(imagen);
return 1;
}
int leerImagen3(){
FILE *imagen;
imagen=fopen("C:/ImagenesC/azul.RAW","r");
imagen3=(unsigned char*)malloc(an1*al1*3);
if(imagen==NULL){
printf("Error: No imagen");
return 0;
}
fread(imagen3,an1*al1*3,1,imagen);
fclose(imagen);
return 1;
}
int leerImagen4(){
FILE *imagen;
imagen=fopen("C:/ImagenesC/azul.RAW","r");
imagen4=(unsigned char*)malloc(an1*al1*3);
if(imagen==NULL){
printf("Error: No imagen");
return 0;
}
fread(imagen4,an1*al1*3,1,imagen);
fclose(imagen);
return 1;
}
int leerImagen5(){
FILE *imagen;
imagen=fopen("C:/ImagenesC/azul.RAW","r");
imagen5=(unsigned char*)malloc(an1*al1*3);
if(imagen==NULL){
printf("Error: No imagen");
return 0;
}
fread(imagen5,an1*al1*3,1,imagen);
fclose(imagen);
return 1;
}
int leerImagen6(){
FILE *imagen;
imagen=fopen("C:/ImagenesC/azul.RAW","r");
imagen6=(unsigned char*)malloc(an1*al1*3);
if(imagen==NULL){
printf("Error: No imagen");
return 0;
}
fread(imagen6,an1*al1*3,1,imagen);
fclose(imagen);
return 1;
}
void texturaI1(void)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, an1, al1, 0, GL_RGB, GL_UNSIGNED_BYTE, imagen1);
}
void texturaI2(void)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, an1, al1, 0, GL_RGB, GL_UNSIGNED_BYTE, imagen2);
}
void texturaI3(void)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, an1, al1, 0, GL_RGB, GL_UNSIGNED_BYTE, imagen3);
}
void texturaI4(void)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, an1, al1, 0, GL_RGB, GL_UNSIGNED_BYTE, imagen4);
}
void texturaI5(void)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, an1, al1, 0, GL_RGB, GL_UNSIGNED_BYTE, imagen5);
}
void texturaI6(void)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, an1, al1, 0, GL_RGB, GL_UNSIGNED_BYTE, imagen6);
}
void crearArbol(){
glDisable(GL_BLEND);
//Cobre
GLfloat mat_b_ambient[] = { 0.2125,0.1275,0.054,1};
GLfloat mat_b_diffuse[] = { 0.714,0.4284,0.18144,1};
GLfloat mat_bspecular[] = { 0.393548,0.271906,0.166721,1};
GLfloat mat_bshininess[] = { 26.5};
//Color Esmeralda
GLfloat mat_green_ambient[] = { 0.0215,0.1745,0.0215,0.55};
GLfloat mat_green_diffuse[] = { 0.07568,0.61424,0.07568,0.55};
GLfloat mat_specular[] = { 0.633,0.727811,0.633,0.55};
GLfloat mat_shininess[] = { 26.8};
//Aplicar color cobre
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_bspecular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_bshininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_b_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_b_diffuse);
glPushMatrix();//tronco
glColor3f(0.36, 0.25, 0.20);
glRotatef(270, 1, 0, 0);
GLUquadricObj *quad;
quad = gluNewQuadric();
gluCylinder(quad,1.0f,1.0f,10.0f,128, 128);
//Aplicar el color esmeralda
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_green_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_green_diffuse);
glPushMatrix();
glColor3f(0.196078, 0.8, 0.196078);
glTranslatef(0, 0, 12);
glScalef(2, 2, 2);
glutSolidDodecahedron();
glPopMatrix();
glPushMatrix();//tronkito01
glTranslatef(0, 0, 6);
glRotatef(270, 0, 1, 0);
glRotatef(45, 0, 1, 0);
//Aplicar color cobre
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_bspecular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_bshininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_b_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_b_diffuse);
glPushMatrix();
glColor3f(0.36, 0.25, 0.20);
gluCylinder(quad,0.5f,0.5f,4.0f,32,32);
//Aplicar el color esmeralda
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_green_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_green_diffuse);
glPushMatrix();
glColor3f(0.196078, 0.8, 0.196078);
glTranslatef(0, 0, 4);
glutSolidDodecahedron();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();//tronkito02
glTranslatef(0, 0, 3.5);
glRotatef(270, 0, 1, 0);
glRotatef(135, 0, 1, 0);
//Aplicar color cobre
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_bspecular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_bshininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_b_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_b_diffuse);
glPushMatrix();
glColor3f(0.36, 0.25, 0.20);
gluCylinder(quad,0.5f,0.5f,4.0f,32,32);
//Aplicar el color esmeralda
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_green_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_green_diffuse);
glPushMatrix();
glColor3f(0.196078, 0.8, 0.196078);
glTranslatef(0, 0, 4);
glScalef(0.7, 0.7, 0.7);
glutSolidDodecahedron();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
}
void dibujaI1(void)//PISO
{
glDisable(GL_LIGHTING);
glColor3f(1,1,1);
glBegin(GL_QUADS);
glTexCoord3f(0.0, 1.0,0.0);
glVertex3f(-100,-100,100);
glTexCoord3f(0.0, 0.0,0.0);
glVertex3f(-100,-100,-100);
glTexCoord3f(1.0, 0.0,0.0);
glVertex3f(100,-100,-100);
glTexCoord3f(1.0, 1.0,0.0);
glVertex3f(100,-100,100);
glEnd();
glEnable(GL_LIGHTING);
}
void dibujaI2(void)//negx
{
glDisable(GL_LIGHTING);
glColor3f(1,1,1);
glBegin(GL_QUADS);
glTexCoord3f(0.0, 1.0,0.0);
glVertex3f(-100,-100,100);
glTexCoord3f(0.0, 0.0,0.0);
glVertex3f(-100,100,100);
glTexCoord3f(1.0, 0.0,0.0);
glVertex3f(-100,100,-100);
glTexCoord3f(1.0, 1.0,0.0);
glVertex3f(-100,-100,-100);
glEnd();
glEnable(GL_LIGHTING);
}
void dibujaI3(void)//negz
{
glDisable(GL_LIGHTING);
glColor3f(1,1,1);
glBegin(GL_QUADS);
glTexCoord3f(0.0, 1.0,0.0);
glVertex3f(100,-100,100);
glTexCoord3f(0.0, 0.0,0.0);
glVertex3f(100,100,100);
glTexCoord3f(1.0, 0.0,0.0);
glVertex3f(-100,100,100);
glTexCoord3f(1.0, 1.0,0.0);
glVertex3f(-100,-100,100);
glEnd();
glEnable(GL_LIGHTING);
}
void dibujaI4(void)
{
glDisable(GL_LIGHTING);
glColor3f(1,1,1);
glBegin(GL_QUADS);
glTexCoord3f(0.0, 1.0,0.0);
glVertex3f(-100,-100,-100);
glTexCoord3f(0.0, 0.0,0.0);
glVertex3f(-100,100,-100);
glTexCoord3f(1.0, 0.0,0.0);
glVertex3f(100,100,-100);
glTexCoord3f(1.0, 1.0,0.0);
glVertex3f(100,-100,-100);
glEnd();
glEnable(GL_LIGHTING);
}
void dibujaI5(void)
{
glDisable(GL_LIGHTING);
glColor3f(1,1,1);
glBegin(GL_QUADS);
glTexCoord3f(0.0, 1.0,0.0);
glVertex3f(100,-100,-100);
glTexCoord3f(0.0, 0.0,0.0);
glVertex3f(100,100,-100);
glTexCoord3f(1.0, 0.0,0.0);
glVertex3f(100,100,100);
glTexCoord3f(1.0, 1.0,0.0);
glVertex3f(100,-100,100);
glEnd();
glEnable(GL_LIGHTING);
}
void dibujaI6(void)
{
glDisable(GL_LIGHTING);
glColor3f(1,1,1);
glBegin(GL_QUADS);
glTexCoord3f(0.0, 1.0,0.0);
glVertex3f(-100,100,-100);
glTexCoord3f(0.0, 0.0,0.0);
glVertex3f(-100,100,100);
glTexCoord3f(1.0, 0.0,0.0);
glVertex3f(100,100,100);
glTexCoord3f(1.0, 1.0,0.0);
glVertex3f(100,100,-100);
glEnd();
glEnable(GL_LIGHTING);
}
void display(void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess[]={50.0};
GLfloat mat_diff[]={1.0,0.5,0.0,0.5};
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_diff);
glPushMatrix();
glRotatef(angx, 1, 0, 0);
glRotatef(angy, 0, 1, 0);
glRotatef(angz, 0, 0, 1);
glPushMatrix();
glColor3f(1,1,1);
glDisable(GL_BLEND);
texturaI1();
dibujaI1();
texturaI2();
dibujaI2();
texturaI3();
dibujaI3();
texturaI4();
dibujaI4();
texturaI5();
dibujaI5();
texturaI6();
dibujaI6();
glPopMatrix();
glPushMatrix();
glTranslatef(30.0,-100.0,25.0);
crearArbol();
glPopMatrix();
glPushMatrix();
glTranslatef(4.0,-100.0,3.0);
crearArbol();
glPopMatrix();
glPushMatrix();
glTranslatef(45.0,-100.0,10.0);
crearArbol();
glPopMatrix();
glPushMatrix();
glColor3f(1,0.2,0);
glEnable(GL_BLEND);//habilitar tranparencia
glBlendFunc(GL_ONE, GL_ONE);//Definir la transparencia
GLfloat mat_green_ambient[] = { 1,0.5,0,0.5};
GLfloat mat_green_diffuse[] = { 1,0.5,0,0.5};
GLfloat mat_xxspecular[] = { 1,0.5,0,0.5};
GLfloat mat_xxshininess[] = { 25.8};
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_xxspecular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_xxshininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_green_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_green_diffuse);
//glutSolidTeapot(5);
glTranslatef(0.0,-97.0,0.0);
glutSolidSphere(3,50,50);
glPopMatrix();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void myReshape(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 90.0, (GLdouble)w/(GLdouble)h, 0.1,200.0); //x, aspecto, vista minimo, vista maximo
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, -90.0, 20.0,
0.0, camy, camz,
0.0, 1.0, 0.0);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
}
void myIdleFunc(void) {
glutPostRedisplay();
}
void inicio()
{
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
leerImagen1();
leerImagen2();
leerImagen3();
leerImagen4();
leerImagen5();
leerImagen6();
camy=-90.0;
camz=-70.0;
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("3D");
inicio();
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(myIdleFunc);
glClearColor(1.0, 1.0, 1.0, 1.0);
glutMainLoop();
return 0;
}
Link para descargar
No hay comentarios:
Publicar un comentario