spheremap | diana v
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
//int alto=1440,ancho=2880;//
//int alto=1024,ancho=1024;
int alto=1000,ancho=2000;//dim imagen
static float rotar=0;
GLfloat ang1 = 0,ang2 = 0;
int moving, startx, starty;
static GLdouble tamanio = 10.0;
unsigned char * textura;
GLUquadric *figura;
int leerImagen(){
FILE *imagen;
imagen=fopen("C:\\Users\\hp\\Documents\\7 SEMESTRE\\carrito2000x1000.raw","r");
textura=(unsigned char*)malloc(ancho*alto*3);
if(imagen==NULL){
printf("Error: No imagen");
return 0;
}
fread(textura,ancho*alto*3,1,imagen);
fclose(imagen);
return 1;
}
void iluminacion (int encendido){
if(encendido==1) {
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
const GLfloat pos[4]={1, 1, 1};//poner afuera
glLightfv(GL_LIGHT0, GL_POSITION, pos);
}else{
glDisable(GL_LIGHTING);}
}
void usarTextura(int encendido)
{
if(encendido==1) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ancho, alto, 0, GL_RGB, GL_UNSIGNED_BYTE, textura);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, *textura);
gluQuadricTexture(figura,1);
}else{glDisable(GL_TEXTURE_2D);}
}
void usarMaterial(int encendido)
{
if(encendido==1) {
GLfloat ambiente[] = { 0.5, 0.5, 0.5, 1.0 };//gris
GLfloat diffusa[] = { 0.5, 0.5, 0.5, 1.0 };
GLfloat especular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat shininess[] = { 128.0 };
GLfloat mat_emission[] = {0.4, .4, .4, 0.0};
glMaterialfv(GL_FRONT, GL_AMBIENT, ambiente);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffusa);
glMaterialfv(GL_FRONT, GL_SPECULAR, especular);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
}
}
void dibujaObjeto(void)
{
//glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);//BLEND SIVE PARA LA TRANSPATECIA
glColor4f(1, 1, 1, 0.2);
glutSolidTeapot(20);
//gluCylinder(figura,tamanio/2,200,300);
gluSphere(figura,tamanio*100,128,128); //ESFEROTA
//gluSphere(figura,tamanio,128,128); //ESFERita
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
iluminacion(0);
usarTextura(1);
usarMaterial(0);
glPushMatrix();
glRotatef(ang1, 0.0, 1.0, 0.0);
glRotatef(ang2, 1.0, 0.0, 0.0);
glRotatef(rotar, 0,0,1);//PARA QUE GIRE SOLITO
//dibujaObjeto1();
dibujaObjeto();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) {
moving = 1;
startx = x;
starty = y;
}
if (state == GLUT_UP) {
moving = 0;
}
}
}
void mover(int x, int y)
{
if (moving) {
ang1 = ang1 + (x - startx);
ang2 = ang2 + (y - starty);
startx = x;
starty = y;
glutPostRedisplay();
}
}
void idle(void)
{
rotar=rotar+0.01;
glutPostRedisplay();
}
void init(void){
glClearColor(0.5, 0.6, 0.7, 1);
glEnable(GL_DEPTH_TEST);
leerImagen();
figura = gluNewQuadric(); //crear un objeto de open gl
glMatrixMode(GL_PROJECTION);
gluPerspective(90.0,1.0,20.0,2000.0);//campo de vision
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 10.0, 60.0, /* vista en (0,8,60) */
0.0, 0.0, 0.0, /* centro en (0,8,0) */
0.0, 1.0, 0.); /* altura en Y */
//mapear entorno sphere en la tetera
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
}
int
main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL | GLUT_MULTISAMPLE);//buffers
glutInitWindowSize(800, 800);
glutCreateWindow("SPHEREMAP,MATERIAL Y QUADRICS");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(mover);
glutIdleFunc(idle);
init();
glutMainLoop();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
//int alto=1440,ancho=2880;//
//int alto=1024,ancho=1024;
int alto=1000,ancho=2000;//dim imagen
static float rotar=0;
GLfloat ang1 = 0,ang2 = 0;
int moving, startx, starty;
static GLdouble tamanio = 10.0;
unsigned char * textura;
GLUquadric *figura;
int leerImagen(){
FILE *imagen;
imagen=fopen("C:\\Users\\hp\\Documents\\7 SEMESTRE\\carrito2000x1000.raw","r");
textura=(unsigned char*)malloc(ancho*alto*3);
if(imagen==NULL){
printf("Error: No imagen");
return 0;
}
fread(textura,ancho*alto*3,1,imagen);
fclose(imagen);
return 1;
}
void iluminacion (int encendido){
if(encendido==1) {
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
const GLfloat pos[4]={1, 1, 1};//poner afuera
glLightfv(GL_LIGHT0, GL_POSITION, pos);
}else{
glDisable(GL_LIGHTING);}
}
void usarTextura(int encendido)
{
if(encendido==1) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ancho, alto, 0, GL_RGB, GL_UNSIGNED_BYTE, textura);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, *textura);
gluQuadricTexture(figura,1);
}else{glDisable(GL_TEXTURE_2D);}
}
void usarMaterial(int encendido)
{
if(encendido==1) {
GLfloat ambiente[] = { 0.5, 0.5, 0.5, 1.0 };//gris
GLfloat diffusa[] = { 0.5, 0.5, 0.5, 1.0 };
GLfloat especular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat shininess[] = { 128.0 };
GLfloat mat_emission[] = {0.4, .4, .4, 0.0};
glMaterialfv(GL_FRONT, GL_AMBIENT, ambiente);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffusa);
glMaterialfv(GL_FRONT, GL_SPECULAR, especular);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
}
}
void dibujaObjeto(void)
{
//glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);//BLEND SIVE PARA LA TRANSPATECIA
glColor4f(1, 1, 1, 0.2);
glutSolidTeapot(20);
//gluCylinder(figura,tamanio/2,200,300);
gluSphere(figura,tamanio*100,128,128); //ESFEROTA
//gluSphere(figura,tamanio,128,128); //ESFERita
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
iluminacion(0);
usarTextura(1);
usarMaterial(0);
glPushMatrix();
glRotatef(ang1, 0.0, 1.0, 0.0);
glRotatef(ang2, 1.0, 0.0, 0.0);
glRotatef(rotar, 0,0,1);//PARA QUE GIRE SOLITO
//dibujaObjeto1();
dibujaObjeto();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) {
moving = 1;
startx = x;
starty = y;
}
if (state == GLUT_UP) {
moving = 0;
}
}
}
void mover(int x, int y)
{
if (moving) {
ang1 = ang1 + (x - startx);
ang2 = ang2 + (y - starty);
startx = x;
starty = y;
glutPostRedisplay();
}
}
void idle(void)
{
rotar=rotar+0.01;
glutPostRedisplay();
}
void init(void){
glClearColor(0.5, 0.6, 0.7, 1);
glEnable(GL_DEPTH_TEST);
leerImagen();
figura = gluNewQuadric(); //crear un objeto de open gl
glMatrixMode(GL_PROJECTION);
gluPerspective(90.0,1.0,20.0,2000.0);//campo de vision
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 10.0, 60.0, /* vista en (0,8,60) */
0.0, 0.0, 0.0, /* centro en (0,8,0) */
0.0, 1.0, 0.); /* altura en Y */
//mapear entorno sphere en la tetera
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
}
int
main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL | GLUT_MULTISAMPLE);//buffers
glutInitWindowSize(800, 800);
glutCreateWindow("SPHEREMAP,MATERIAL Y QUADRICS");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(mover);
glutIdleFunc(idle);
init();
glutMainLoop();
return 0;
}

Comentarios
Publicar un comentario