Advertisement

Problem with textures when loading 3ds files

Started by February 01, 2006 10:27 AM
7 comments, last by The Wazaa 18 years, 9 months ago
Hi I've written a 3Ds file loader that currently supports models and textures. Or it should support textures, they don't seem to want to appear ;) . I've tried lots of stuff and I don't think that the problem is in my loader, not sure though. The source code can be found at: http://e43.sag.karlstad.se/it18/GLengine.zip . I'm sorry if it's a bit ugly, after all it's one of my first C++ programs :-) . Thanks in advance The Wazaa EDIT: After some debugging I'm sure that the error isn't in the loader. In "Init.cpp" I have this function:

void createTextures(UINT textureArray[], LPSTR fileName, int textureID)
{
	AUX_RGBImageRec *pBitmap = NULL;

	if (!fileName) return;
	pBitmap = auxDIBImageLoad(fileName);

	if (pBitmap == 0) exit(0);

	glGenTextures(1, &textureArray[textureID]);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glBindTexture(GL_TEXTURE_2D, textureArray[textureID]);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);

    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitmap->sizeX, pBitmap->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pBitmap->data);
	if (pBitmap)
	{
		if (pBitmap->data)
		{
			free(pBitmap->data);
		}
        free(pBitmap);
	}
}

I think that the error is here, maybe. [Edited by - The Wazaa on February 1, 2006 12:05:20 PM]
wow that's awesome that you wrote that 3ds importer! Good job!
Advertisement
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);

Valid parameters are GL_LINEAR or GL_NEAREST
I'm trying to post my comments, but for some reason my browser will not post.
Looking at your code, I found several problems.

In the file Main.cpp

//******************************************
//********* Begin Move Text ****************
//******************************************
modelLoader.load3Ds("face.3DS", &model2);
//delete &modelLoader

for (int i=0; i < model2.numOfMaterials; i++)
{
if (strlen(model2.pMaterials.fileName) > 0)
{
createTextures(textureArray, model2.pMaterials.fileName, i);

}
model2.pMaterials.textureId = i;
}

//******************************************
//*********** End Move Text ****************
//******************************************

RegisterClassEx(initWindowClass(className));

createWindow(className, windName, SCREEN_WIDTH, SCREEN_HEIGHT);

hDC = GetDC(hWnd);

setPiFD(SCREEN_HEIGHT, SCREEN_WIDTH, 16);

hRC = wglCreateContext(hDC);

wglMakeCurrent(hDC, hRC);

//**********************************
//*** Move Code From Above Here ****
//**********************************

initGL();

The code between the begin and end comments need to be moved to after you setup the opegl context. You are trying to load a texture into a rendering context that has not been setup yet.

My previous comment about the GL_TEXTURE_MAG_FILTER still holds true.

In file 3DSRenderer.cpp

void TdsLoader::render_3ds(tDmodel *model)
{
glEnable(GL_TEXTURE_2D);
for (int i=0; i < model->numOfObjects; i++)
{
if(model->pObject.size() <= 0) break;
tDobj *pObject = &model->pObject;


if (pObject->hasTexture)
{

glBindTexture(GL_TEXTURE_2D, 0/*textureArray[pObject->textureId]*/);
}

You need to replace the bind texture with a valid texture unit. Setting the texture unit to 0 is basically telling OpenGL to disable an texture unit calls.

This should correct your problems. Once I made the changes, everything worked fine.

Keep up the good work.

This seemed to work when I took out the source tags.
Quote: Original post by Deception666
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);

Valid parameters are GL_LINEAR or GL_NEAREST


This is valid as well - > GL_LINEAR_MIPMAP_LINEAR
For more info on glTexParameter options have a look at the followings site.
http://www.mevis.de/opengl/glTexParameter.html

The more applications I write, more I find out how less I know
Advertisement
Quote: Original post by CRACK123
Quote: Original post by Deception666
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);

Valid parameters are GL_LINEAR or GL_NEAREST


This is valid as well - > GL_LINEAR_MIPMAP_LINEAR
For more info on glTexParameter options have a look at the followings site.
http://www.mevis.de/opengl/glTexParameter.html


You might want to reread the website you quoted.

Quote:
GL_TEXTURE_MAG_FILTER
The texture magnification function is used when the pixel being textured maps to an area less than or equal to one texture element. It sets the texture magnification function to any of the following:

GL_NEAREST
Returns the value of the texture element that is nearest (in Manhattan distance) to the center of the pixel being textured.

GL_LINEAR
Returns the weighted average of the four texture elements that are closest to the center of the pixel being textured. These can include border texture elements, depending on the values of GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T, and on the exact mapping.

GL_NEAREST is generally faster than GL_LINEAR, but it can produce textured images with sharper edges because the transition between texture elements is not as smooth. The default value of GL_TEXTURE_MAG_FILTER is GL_LINEAR.
You are right, its only for GL_TEXTURE_MIN_FILTER
The more applications I write, more I find out how less I know
Thanks a lot, it works very well now. As for a new challenge I shall soon with a MD5 loader (the Doom 3 format). Right now I'm fighting with a heightmap thing, which almost works ;) I can't seem to get it shown by triangles, so lines have to do. When I first started it I was amazed, I used around 16000 points which I drew lines from, in 70 fps!


Thanks again everyone

The Wazaa

This topic is closed to new replies.

Advertisement