Sign in to follow this  
oenda

Texture problem on a 3d model

Recommended Posts

oenda    147

This is very strange error.I have an ms3d(milkshape 3d) model.I am rendering it but it doesnt appear correctly.It is covered by repeated white textures.
 
I load textures with sfml.My texture loading code:
 

bool CImage::Load(char * szFilename, bool bMipMap)
{
	m_bMipMap = bMipMap;
	m_szFilename = szFilename;

    sf::Image img_data;
    img_data.loadFromFile(m_szFilename);

    m_uiWidth = img_data.getSize().x;
	m_uiHeight = img_data.getSize().y;


    m_uiImage = 0;

	glGenTextures(1, &m_uiImage);
	glBindTexture(GL_TEXTURE_2D, m_uiImage);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	if(m_bMipMap)
	{
		gluBuild2DMipmaps(GL_TEXTURE_2D, 3, m_uiWidth, m_uiHeight, GL_RGB, GL_UNSIGNED_BYTE,  img_data.getPixelsPtr());
	}
	else
	{
		glTexImage2D(GL_TEXTURE_2D, 0, 3, m_uiWidth, m_uiHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,  img_data.getPixelsPtr());
	}

	return true;
}

 

Any help will be appreciated.

Share this post


Link to post
Share on other sites
C0lumbo    4411

Is it possible the UVs need to be normalised?

 

DirectX and OpenGL use normalised texture coordinates. That is, regardless of the texture's width and height, (0,0) and (1,1) will always represent opposite corners of the texture image. It's possible that the ms3d model format (or the loading code you use) uses non-normalised texture coordinates, so that a 512x512 texture would have (0,0) and (512,512) as it's corners.

 

Try dividing the UV coordinates by the texture dimensions and see if that gets you anywhere.

Share this post


Link to post
Share on other sites
lask1    1144

Is it possible the UVs need to be normalised?

 

DirectX and OpenGL use normalised texture coordinates. That is, regardless of the texture's width and height, (0,0) and (1,1) will always represent opposite corners of the texture image. It's possible that the ms3d model format (or the loading code you use) uses non-normalised texture coordinates, so that a 512x512 texture would have (0,0) and (512,512) as it's corners.

 

Try dividing the UV coordinates by the texture dimensions and see if that gets you anywhere.

Possible but not sure. You are definitely on the right track with the UV coordinates. Also @oenda your texture loading code appears to be fine it's probably something like C0lumbo said.

Share this post


Link to post
Share on other sites
oenda    147

I didn't change anything except texture loading code.I read examples.I found solution at Opengl example.Problem is not related to UV coordinates.It is related to texture loading.The solution is:

    GLuint id = 0;
    {
        sf::Image image;
        if (image.loadFromFile(szFilename))
            {
        glGenTextures(1, &id);
        glBindTexture(GL_TEXTURE_2D, id);
        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.getSize().x, image.getSize().y, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

            }
    }

m_uiImage = id;

I don't know why but problem has been solved.Thanks for your help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this