Open GL - Ayuda con codigo de texturizacion en opengl dev C++

<<>>
 
Vista:

Ayuda con codigo de texturizacion en opengl dev C++

Publicado por marck (1 intervención) el 13/06/2023 04:02:58
hola necesito ayuda con programacion en dev c++,intento realizar una esfera 3d con la textura del planeta tierra ,lo logre solo que la textura no sale,solo se ponde de color verde o azul dependiendo de la imagen formato .tga,necesito ayuda por favor,me desespero:
#include <GL/glut.h>
#include <stdio.h>

GLfloat angulo=0.0;


typedef struct{
GLubyte *dibujo;
GLuint bpp;
GLuint largo;
GLuint ancho;
GLuint ID;
}textura;



textura tplanetatierra;


int cargaTGA(char *nombre,textura *imagen){
GLubyte cabeceraTGA[12]={0,0,2,0,0,0,0,0,0,0,0,0};
GLubyte compararTGA[12];
GLubyte cabecera[6];
GLuint bytesporpunto;
GLuint tamanoimagen;
GLuint temp,i;
GLuint tipo=GL_RGBA;



FILE *archivo=fopen(nombre,"rb");
if(
archivo==NULL ||
fread(compararTGA,1,sizeof(compararTGA),archivo)!=sizeof(compararTGA) ||
memcmp(cabeceraTGA,compararTGA,sizeof(compararTGA))!= 0||
fread(cabecera,1,sizeof(cabecera),archivo)!=sizeof(cabecera)
){
if(archivo==NULL){
printf("No se encontro el archivo\n",nombre);
return 0;
}else{
fclose(archivo);
return 0;
}
}
imagen->largo=256*cabecera[1] +cabecera[0];
imagen->ancho=256*cabecera[3] +cabecera[2];
if(
imagen->largo <=0 ||
imagen->ancho <=0 ||
(cabecera[4]!=24 && cabecera[4]!=32)
){
printf("Datos invalidos\n");
fclose(archivo);
return 0;

}
imagen->bpp=cabecera[4];
bytesporpunto=cabecera[4]/8;
tamanoimagen=imagen->largo * imagen->ancho *bytesporpunto;

imagen->dibujo=(GLubyte*)malloc(tamanoimagen);
if(
imagen->dibujo==NULL ||
fread(imagen->dibujo,1,tamanoimagen,archivo) !=tamanoimagen
){
if(imagen->dibujo!=NULL){
printf("Error leyendo imagen \n");
free(imagen->dibujo);
}else {
printf("Error asignando memoria \n");
}
fclose(archivo);
return 0;
}

//TGA:BGRA RGBA
for(i=0;i<(int)tamanoimagen;i+=bytesporpunto){
temp=imagen->dibujo[i];
imagen->dibujo[i]=imagen->dibujo[i+2];
imagen->dibujo[i+2]=temp;
}

fclose(archivo);

glGenTextures(1,&imagen[0].ID);
glBindTexture(GL_TEXTURE_2D,imagen[0].ID);

if(imagen->bpp==24 ) tipo=GL_RGB;
else if (imagen->bpp == 32) tipo = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D,0,tipo,imagen[0].ancho,imagen[0].largo,0,tipo,GL_UNSIGNED_BYTE,imagen[0].dibujo);

glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
return 1;
}







void init(){
glShadeModel(GL_SMOOTH);
glClearColor(0.0,0.0,0.0,0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);

glDepthFunc(GL_LEQUAL);
glDepthFunc(GL_LESS);

glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glEnable(GL_TEXTURE_2D);

////
GLfloat light_position[] = { 1, 1, 1, 0 };
GLfloat light_ambient[] = { 0.2, 0.2, 0.2, 5.0 };
GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 5.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 5.0 };
GLfloat mat_shininess[] = { 100.0 };

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);

glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

if(!cargaTGA("malla-tierra.tga",&tplanetatierra)){
printf("Error cargando textura \n");
exit(0);
}


}

void fondo(){

}
void display(){


glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D,tplanetatierra.ID);


glRotatef(0,0,1,0);
glRotatef(angulo,0,1,0);
glTranslated(-0,-400,0);
glutSolidSphere(400,50,50);

glFlush();
glLoadIdentity();
glutSwapBuffers();





}

void reshape(int largo , int ancho){
glViewport(0,0,(GLsizei)largo,(GLsizei)ancho);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
int t=500;
glOrtho(-t,t,-t,t,-t,t);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

void idle(){
angulo +=0.1;
display();
}

int main(int argc, char** argv) {
glutInit(&argc,argv);

glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE|GLUT_DOUBLE |GLUT_DEPTH|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("SEMANA09");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMainLoop();

return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder