Jump to content
  • Advertisement
Sign in to follow this  

Texturing Problem

This topic is 3717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Sorry if this has been posted before but everything I have found has not worked. I'm trying to write some functions that can load a model and texture from files and then draw them on the screen. At the moment just to get it working I'm using a cube, but hopefully more complex models will work with the same code. I've already sorted loaded the model and a BMP image into memory. I can draw the model but the texture doesn't appear.
loadBMP(textureFilename,obj->image);

glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, 14);
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,obj->image->width,obj->image->height,0,
		GL_RGB,GL_UNSIGNED_BYTE,obj->image->imageData);

free (obj->image);

To draw the model I have the following code. This draws the model just doesn't map the texture onto it.
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,14);
for(i=0;i<obj->facetCount;i++) {
	glBegin(GL_TRIANGLES);
	glTexCoord2f(obj->texture[obj->facetT.a].u,
		obj->texture[obj->facetT.a].v);
	glNormal3f(obj->normal[obj->facetN.a].x,
		obj->normal[obj->facetN.a].y,
		obj->normal[obj->facetN.a].z);
	glVertex3f(obj->vertex[obj->facetV.a].x,
		obj->vertex[obj->facetV.a].y,
		obj->vertex[obj->facetV.a].z);

	glTexCoord2f(obj->texture[obj->facetT.b].u,
		obj->texture[obj->facetT.b].v);
	glNormal3f(obj->normal[obj->facetN.b].x,
		obj->normal[obj->facetN.b].y,
		obj->normal[obj->facetN.b].z);
	glVertex3f(obj->vertex[obj->facetV.b].x,
		obj->vertex[obj->facetV.b].y,
		obj->vertex[obj->facetV.b].z);

	glTexCoord2f(obj->texture[obj->facetT.c].u,
		obj->texture[obj->facetT.c].v);
	glNormal3f(obj->normal[obj->facetN.c].x,
		obj->normal[obj->facetN.c].y,
		obj->normal[obj->facetN.c].z);
	glVertex3f(obj->vertex[obj->facetV.c].x,
		obj->vertex[obj->facetV.c].y,
		obj->vertex[obj->facetV.c].z);
	glEnd();
}

I have used breakpoints within the code and the image is loaded correctly, it is a 24bit bitmap 256x256 and it is all in obj->image->imageData which is an 3D array 256x256x3 the texture coordinates are set properly. Sometimes I am able to get it to texture the cube with something similar to the static you would get on an untuned TV. Other times it is just white. I'm not sure what I changed to get that the work. I have also tried using
glGenTextures(1,&obj->textureID);
but it doesn't appear to do anything obj->textureID is the same afterwards as it was. I would really appreciate any help anyone can give me. This has been bugging me for a couple of weeks now.

Share this post


Link to post
Share on other sites
Advertisement
What do you mean its a 3D array? You mean this?

unsigned char imageData[][][];

Don't use that format as it's a headache.

Use

unsigned char* imageData = new unsigned char[width * height * colormode];

Share this post


Link to post
Share on other sites
Thanks this is in C rather than C++ but I have used used something similar to that using
unsigned char *imageData = malloc(width * height * depth);
Had to rewrite my loadBMP method but it all seems to be working. :D

Also not sure why but glGenTextures seems to be working now :S

Now that it loads the texture correctly I'm finding that it isnt mapping it properly. I think that might be a problem with the way I've done it in Max though, I'm exporting the model as a .obj file the model loads OK but the texture isn't mapping correctly. It worked when I tested it with a cube but then moved onto a helicopter and that caused some problems. Will just have to play around with that for a while.

Could someone explain what
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
is doing. I put them there as the tutorials I was using had them but I don't really get what they are doing.

Thanks.

Share this post


Link to post
Share on other sites
They setup the filter modes for the texture object you setup. As of now you aren't using mipmapping with the setup you have. I would recommend moving to mipmapping.


//the call to gluBuild2DMipmaps uses the CPU to process mipmaps
loadBMP(textureFilename,obj->image);

glGenTextures(1,&obj->textureID);
glBindTexture (GL_TEXTURE_2D, obj->textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
//other types of filter for min filter are GL_LINEAR_MIPMAP_LINEAR, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST, and GL_NEAREST_MIPMAP_NEAREST

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, obj->image->width,obj->image->height,
GL_RGB, GL_UNSIGNED_BYTE, obj->image->imageData);
free (obj->image);

//or you can use the GPU if you have support for it with a call to
loadBMP(textureFilename,obj->image);

glGenTextures(1,&obj->textureID);
glBindTexture (GL_TEXTURE_2D, obj->textureID);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,obj->image->width,obj->image->height,0,
GL_RGB,GL_UNSIGNED_BYTE,obj->image->imageData);
free (obj->image);






Your choice

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!