missing texture

the story so far:

texture image loads
texparameters set
glBindTexture
glEnable
glTex…;
glVertex;
blah blah

so why doesn’t it show up? it definitly loads to the texture object, and the dimensions are a power of 2. glBind… is called outside glBegin. i can’t think of any other reason it won’t ‘show’.

cheers
I

Enable before binding… maybe? And you do bind again before drawing the poly… and specify tex coordinates

yeah i tried enable before binding, but it doesn’t help. tex coords are correct too.
thx anyway. any other ideas?

cheers
I

do you check and see that your texture is valid… (do you create the texture after you have created a rendering context…

hmmm…this may be something closer to the truth but i’m not sure what you mean by ‘rendering context’ - is the texture created before entering glutMainLoop? someone said it might be something like this.

what I mean is you should create the window with glutCreateWindow before making the texture…

(at least you must do glutINit)

try this …
before using the texture…
that is binding it for the second time
issue a

glIsTexture(GLuint textureName)

with param as the texture number…

and could you post the code how you init your texture

This works fine for me:

char TEXTURE_FILE = “Texture.BMP”; /* Texture file’s name /
int TEX_LOAD_ERROR = 0; /
Texture loading error code /
int INIT_OK = 1; /
Code returned when initialisation is OK */

/* Texture structure type definition /
typedef struct{
char
filename;
GLubyte *image;
GLint width;
GLint height;
GLuint ID;
} TextureStruct;

GLubyte* LoadBMP(char file_name, int &width, int &height) / bitmap loader */
{

}

int init(void) /* initialisation function /
{
/
Declaration of a texture structure pointer /
TextureStruct texture;
/
Allocate texture structure /
texture = (TextureStruct
)malloc(sizeof(TextureStruct));
/
Make texture->filename point to texture file’s name /
texture->filename = TEXTURE_FILE;
/
Load the texture in texture structure /
texture->image = LoadBMP(texture->filename, texture->width, texture->height);
/
Return error code if texture can’t be loaded */
if(texture->image == NULL)
return TEX_LOAD_ERROR;

glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

/* Create texture object and bind texture */
glGenTextures(1, &texture->ID);
glBindTexture(GL_TEXTURE_2D, texture->ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
               GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
               GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture->width, texture->height, 0,
            GL_RGB, GL_UNSIGNED_BYTE, texture->image);

/* Once the texture object is created, you can free the texture structure to clean memory */
free(texture->image);
free(texture);

/* enable 2D texturing */
glEnable(GL_TEXTURE_2D);

/* Set clear color */
glClearColor(0.0, 0.0, 0.0, 0.0);

return INIT_OK;

}

And as mr x suggested I call glutCreateWindow before init.

void main()
{

glutCreateWindow (“Texture demo”);
/* initialisation */
if(init () == TEX_LOAD_ERROR)
{
printf("Can’t load texture
");
exit(0);
}

}

you may be on to something here. i included

if (!glIsTexture(sh_tex.texture_object[0]))
exit(-1);

before trying to bind the texture (2nd time), and the program exited, so i assume the tex isn’t registering properly. but i’ve used this code b4 for textures, so it must be something to do with the operation/structure of the code…mustn’t it?

the shape (multi-triangled sphere) has attribute sh_tex, which is of type texture. each object of class texture has a texture_object attribute, containing one texture (i may need to expand, so i’m using tex objs). the Texture constructor has the following code to initialise the texture, i’m pretty sure it’s ok.

(64X64 .PPM file)

GLubyte *tex_ptr;
int i;
FILE *tex_file_ptr;
char line[256];

glGenTextures(1, texture_object);

// load the image (.PPM file)
tex_ptr = (GLubyte )malloc(6464*3);

tex_file_ptr = fopen(“image01.ppm”, “rb”);
if(tex_file_ptr == (FILE *)0)
{
fprintf(stderr, "Error opening texture file
");
exit(-1);
}

fgets(line, 256, tex_file_ptr);

for(i=0;i<64643;i++)
tex_ptr[i]=(GLubyte)getc(tex_file_ptr);
fclose(tex_file_ptr);

// install the texture into texture_object
glBindTexture(GL_TEXTURE_2D, texture_object[0]);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, tex_ptr);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}

the basic structure is

class shape{
texture obj_tex;



}
.
.
class texture{
GLuint texture_object[1];
texture();
.
.
texture::texture(){
// initialise texture code as above


}

i’m also sure that the texture coords have been calulated properly to texture a multiple-triangle sphere.

i’m sure you’re sufficiently confused now,

cheers
I

where is your glEnable(GL_TEXTURE_2D); ?

put glEnable(GL_TEXTURE_2D) before this line

glGenTextures(1, texture_object);

(if it isn’t already)

otherwise I will have to think harder …

hth

class shape has another function, display. this draws the shape. glBindTexture & glEnable are called here, just before glBegin(GL_TRIANGLES).

the way the program works is that the sphere is altered regularly (position, size etc), so the actual drawing of it is separated. it’s a pretty complicated program (i didn’t write it but i have to add texture to it).

if you can’t offer anything else, thanks anyway - i appreciate your help.

cheers
I

I think you need glEnable before you first bind the texture…

Originally posted by mr x:
I think you need glEnable before you first bind the texture…

The code I posted doesn’t have a glEnable(GL_TEXTURE_2D) before glBind, and I can assure you that it works (just tried it actually ).