PDA

View Full Version : Textures in GLUT



hessayon
12-23-2008, 08:18 AM
Hi, I'm a Spanish student, sorry about my poor English.

I've done one aplication with GLUT, but I can't see textures in this.

My texturas.c flie contents this:

#include<GL/glut.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include"texturas.h"


/************************************************** *******************/
/* Lee una imagen de textura en formato tga */
/* Parametros --> char *fichero : Cadena de caracteres con el nombre */
/* del fichero */
/* int num: Numero que identifica la textura */
/* Salida --> Ninguna */
/************************************************** *******************/
void leeTextura ( char *fichero, int num) {
int i, j;
char r, g, b, c;
FILE *tga;

/* Apertura del fichero TGA */
if ((tga = fopen(fichero, "rb" )) == NULL)
printf ( "Error abriendo el fichero: %s\n" , fichero);
else {
/* Lee los 18 primeros caracteres de la cabecera */
for (j=1; j<=18; j++)
fscanf (tga, "%c" , &c);

/* Lee la imagen */
for (j=ALTOTEXTURA-1; j>=0; j--) {
for (i=ANCHOTEXTURA-1; i>=0; i--) {
fscanf(tga, "%c%c%c" , &b, &g, &r);
texturas[num][j][i][0] = (GLubyte)r;
texturas[num][j][i][1] = (GLubyte)g;
texturas[num][j][i][2] = (GLubyte)b;
}
}
fclose(tga); /* Cierre del fichero TGA */
}
}

void cargaTextura( char *fichero, int num) {
leeTextura(fichero, num);
glBindTexture(GL_TEXTURE_2D, nombretexturas[num]);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, 3, ANCHOTEXTURA, ALTOTEXTURA, 0, GL_RGB, GL_UNSIGNED_BYTE, texturas[num]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
}

void InicializaTexturas(void){
glGenTextures(NUMTEXTURAS, nombretexturas);
cargaTextura( "textura.tga" , 0 );
}

And my texturas.h has this content:

#ifndef TEXTURAS_H
#define TEXTURAS_H

#define ANCHOTEXTURA 256
#define ALTOTEXTURA 256
#define NUMTEXTURAS 1

GLubyte texturas[NUMTEXTURAS][ALTOTEXTURA][ANCHOTEXTURA][3];
GLuint nombretexturas[NUMTEXTURAS];

void leeTextura(char *fichero, int num);
void cargaTextura(char *fichero, int num);
void InicializaTexturas(void);

#endif

when I put a texture in an object I do this:

glEnable (GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,nombretexturas[0]);
object();
glDisable(GL_TEXTURE_2D);

I can't see any texture. What could be the matter??

Thanks

dletozeun
12-23-2008, 11:08 AM
Do you generate texture coordinates in the "object()" function?
At first sight, I don't see any opengl mistake in the above piece of code.

hessayon
12-23-2008, 01:02 PM
Yes, I create the coordinates with this method:

void tira( float R1, float R2, float R3, float y1, float y2, float y3, int N) {
int i;
Punto3D p;
Vector3D n;

glBegin(GL_QUAD_STRIP);
for (i = 0 ; i <= N; i++) {
n = normal(R1, R2, y1, y2, N, i);
glNormal3f(n.a, n.b, n.c);
glTexCoord2f((float)(float)i/(float)N,y1+0.5);
p = vertice(R1, y1, N, i);
glVertex3f(p.x, p.y, p.z);
n = normal(R2, R3, y2, y3, N, i);
glNormal3f(n.a, n.b, n.c);
glTexCoord2f((float)(float)i/(float)N,y2+0.5);
p = vertice(R2, y2, N, i);
glVertex3f(p.x, p.y, p.z);
}
glEnd();
}

This function is to create a solid object.

dletozeun
12-24-2008, 03:56 AM
I do not see either any opengl mistake in the tira function. Try to simplify your code as much as you can to isolate each potential part of the code that could lead to this problem. You can try to render just one quad with simple texture coordinates and apply the loaded texture on it. If the texture is still not drawn the problem might come from the texture loading part.
Check that you call all functions in the right order, (initialization before loading the texture...), that you bind the texture at the right time, etc..