Texturing problem

I am trying to bake texture in 3DS Max and use it in OpenGL, but texture looks weird.
I was looking on google for few hours, but cant find any solution. :frowning:

Here is my code:

public void RenderObject(CObject obj)
	{
		CMesh m; MFace f; MVertex v; MNormal n; MTexCords t; CTexture tx;
		
		tx = obj.GetTexture(); m = obj.GetMesh();
		
		if(tx.hasTexture()) 
		{
			tx.apply2Object(gl.getGL());
		}
		
		gl.glTranslatef(obj.location.x, obj.location.y, obj.location.z);
		gl.glRotatef(obj.rotation.x, 1, 0, 0);
		gl.glRotatef(obj.rotation.y, 0, 1, 0);
		gl.glRotatef(obj.rotation.z, 0, 0, 1);
		
		for(int i = 0; i < m.getFaceList().size();i++)
		{
			f = m.getFaceList().get(i);
			
			gl.glColor3f(0.5f, 0.5f, 0.5f);
			gl.glBegin(GL2.GL_POLYGON);
					
			for(int j = 0;j < f.GetPolygonCount();j++)
			{
				v = m.getVertexList().get(f.GetPolygonByIndex(j).vertexIndex - 1);
				t = m.getTexCordsList().get(f.GetPolygonByIndex(j).texcordIndex - 1);
				n = m.getNormalList().get(f.GetPolygonByIndex(j).normalIndex - 1);
				
				gl.glNormal3f(n.x, n.y, n.z);
				gl.glTexCoord3f(t.u,t.v, t.w);
				gl.glVertex3f(v.x, v.y, v.z);
				
			}

			gl.glEnd();
		}

Code is based on open source OBJ loader in c++.

What i want:
http://img521.imageshack.us/img521/5181/wantp.jpg

What i get:
http://img171.imageshack.us/img171/645/getor.jpg

Baked texture:
http://img205.imageshack.us/img205/8562/bakedi.jpg

help!?

It looks like your texture coordinates are not being loaded correctly. I would print out or debug the texture coordinates that were loaded in to make sure you’re passing in the expected values. You could try testing a simpler model, such as a textured cube to see if there are problems with your RenderObject() or your parser.

A couple of other suggestions:

  1. Subtract 1 from the polygon indices in the parser, so you don’t have to remember to do it every time you need to access the vertex, normal or texcoords lists.
  2. In MFace, you have getPolygonCount() and getPolygonByIndex() but the MFace is the polygon and those methods appear to return the vertex count and vertex information. If that’s not what they are doing, then you should look carefully at your RenderObject() method.

some things are complicated too much, somewhere used wrong names…
but anyway i have checked parsed data, everything is correct.

tested with simple object, result:
http://img7.imageshack.us/img7/5130/omgwtfqj.jpg

looks like texture is applied on every side, instead of whole object.

here is my CTexture class:

public class CTexture 
{
	private Texture texture;
	private boolean loaded;
	
	public CTexture()
	{
		loaded = false;
	}
	
	public void LoadTextureFromResource(String res,String type) throws IOException
	{
        InputStream stream = getClass().getResourceAsStream(res);
        TextureData data = TextureIO.newTextureData(GLProfile.getDefault(),stream, false, type);
        this.texture = TextureIO.newTexture(data);
        this.loaded = true;
	}
	
	public boolean hasTexture()
	{
		return loaded;
	}
	
	public void apply2Object(GL gl)
	{
		texture.enable(gl);
		texture.bind(gl);
	}
	
}

Well in the cube example, it might be that the correct texture coordinates would display the entire image on each cube face.

Try experimenting in the cube obj file and change the 1’s in the texture coordinates to 0.5’s. This should then display the bottom quarter of the image stretched over each face.

texture coords were wrong,
i needed to use UVW Unwrap modifier in 3DS max.

after that works perfectly :slight_smile: