Advertisement

UV planar mapping

Started by June 15, 2013 11:24 AM
-1 comments, last by belfegor 11 years, 8 months ago

I have list of clipped vertices by some bounding box, now i wish to generate UV coordinate for those vertices by using planar mapping.

I have used information from DevMaster thread how it should be done, but there is something wrong and i don't see what have i missed as on some surfaces UV coords are fine but on some there is streched.

Here is relevant code:

D3DXVECTOR3 axis[3]; //plane axes
            axis[0] = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
            axis[1] = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
            axis[2] = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
D3DXVECTOR3 bbCenter = (bbMax + bbMin) * 0.5f;
            D3DXVECTOR3 bbExtent = (bbMax - bbMin) * 0.5f;
            //Loop through vertices in mesh and calculate their UV coordinates
            for(std::size_t i = 0; i < vTriPoints.size(); ++i)
            {
                float dot = 0.0f;
                for(std::size_t j = 0; j < 3; ++j)
                {
                    float currDot = std::fabs(D3DXVec3Dot(&vVertsNormals[i], &axis[j]));
                    if(dot < currDot)
                    {
                        dot = currDot;
                        D3DXVECTOR3 toCenter =  pVerts[i].pos - bbCenter;
                        float dist           = D3DXVec3Length(&bbExtent);
                        D3DXVECTOR3 coord    = toCenter / dist;
                        pVerts[i].texcoord   = D3DXVECTOR2((coord.x + 1.0f) * 0.5f, (coord.y + 1.0f) * 0.5f);
                    }
                }
            }

This topic is closed to new replies.

Advertisement