简体   繁体   中英

Texture Mapping issue OpenGL GLUT

I have a simple object (box.obj) which is essentially a cube, but made up of 50 or so vectors, normals and texture coordinates.

Then I have a brick jpeg texture that I want to map to it:

I'm using the jpeglib to import the image into a texture, and that's all working fine, my problem comes with how the texture is mapping on to the cube. This is what it ends up looking like:

Notice the weird curly sides and large bricks on the front and top. I'm doing the same with the 'table' beneath the cube, which should have a wood texture, but it's blurry/block.

Here's the parser I'm using to get the objects/textures. This gets called in the display function.

for (int i = 0; i < m_nNumPolygon; i++) {
    glBegin(GL_TRIANGLES);
    glTexCoord2f(m_pUVArray[m_pTriangles[i].v1].u, m_pUVArray[m_pTriangles[i].v1].v);
    glNormal3f(m_pNormalArray[m_pTriangles[i].n1].x,
            m_pNormalArray[m_pTriangles[i].n1].y,
            m_pNormalArray[m_pTriangles[i].n1].z);
    glVertex3f(m_pVertexArray[m_pTriangles[i].v1].x,
            m_pVertexArray[m_pTriangles[i].v1].y,
            m_pVertexArray[m_pTriangles[i].v1].z);
    glTexCoord2f(m_pUVArray[m_pTriangles[i].v2].u, m_pUVArray[m_pTriangles[i].v2].v);
    glNormal3f(m_pNormalArray[m_pTriangles[i].n2].x,
            m_pNormalArray[m_pTriangles[i].n2].y,
            m_pNormalArray[m_pTriangles[i].n2].z);
    glVertex3f(m_pVertexArray[m_pTriangles[i].v2].x,
            m_pVertexArray[m_pTriangles[i].v2].y,
            m_pVertexArray[m_pTriangles[i].v2].z);
    glTexCoord2f(m_pUVArray[m_pTriangles[i].v3].u, m_pUVArray[m_pTriangles[i].v3].v);
    glNormal3f(m_pNormalArray[m_pTriangles[i].n3].x,
            m_pNormalArray[m_pTriangles[i].n3].y,
            m_pNormalArray[m_pTriangles[i].n3].z);
    glVertex3f(m_pVertexArray[m_pTriangles[i].v3].x,
            m_pVertexArray[m_pTriangles[i].v3].y,
            m_pVertexArray[m_pTriangles[i].v3].z);
    glEnd();
}

Let me know if I need to post any more code eg. how the objects are being read in or whatnot!

I Think you are giving wrong texture coordinates.

You can share vertices among adjacent faces but not texture coordinates. To visualize this problem look at this image


(source: geeks3d at www.ozone3d.net )


(source: thedarkmod.com )

Try to assign the vertices by understanding this image. It basically unwraps the cube object on to a texture plane.

So Think of the case where you are assigning the texture coordinated to the back face. If you are giving the texture coordinated which you gave to the left, right, bottom and top face, it would endup pasting the entire texture of all the four faces on that single face.

So it would endup pasting that curly texture which you have in your cube in the left face. Take a look at the image and try to understand why the texture coordinates assigned to the left edge of the first face in middle row cannot be assinged to right edge of the fourth face.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM