简体   繁体   中英

UV Mapping issue artifact on Sphere OpenGl

经度上的神器

I am UV mapping a 2D Texture on a 3d sphere X, Y, Z coordinates, by using the formula

u = (0.5 + atan2(X, Y) / (2 * glm::pi<double>()));
v = (0.5 - asin(Z) / glm::pi<double>());

in modern openGL C++.

I dont know why there is this artifact in the sphere. Cant figure it out.

Ok, I have figured and corrected this out, thought I will answer here finally now.

Big thanks to BDL and Rabbid76.

Whenever u == 0, I added the same vertex position (XYZ) to the vertices vector (or array) and also increased the index, but hardcoding the texture u to be 1.0f this time.

No issues now, the seam looks perfect now.

This is detail of a textured sphere geometry which is indexed. You should use index for better performance: m_meridians and m_latitudes are detail level of sphere.

for (size_t i = 0; i < m_meridians + 1; i++)
{
    for (size_t j = 0; j < m_latitudes + 2; j++)
    {
        // texCoord in the range [(0,0), (1,1)]
       QVector2D texCoord((float)i / m_meridians, (float)j / (m_latitudes+1));
        // theta = longitude from 0 to 2pi
        // phi = latitude from -pi/2 to pi/2
        double theta, phi;
        theta = 2*M_PI * texCoord.x();
        phi = M_PI * texCoord.y() - M_PI_2;
        QVector3D pos;
        pos.setY((float)std::sin(phi));
        pos.setX((float)std::cos(phi) * std::cos(theta));
        pos.setZ((float)std::cos(phi) * std::sin(theta));

        m_vertices.push_back({pos, texCoord});
    }
}

// Calculate triangle indices

for (size_t i = 0; i < m_meridians; i++)
{
    // Construct triangles between successive meridians
    for (size_t j = 0; j < m_latitudes + 1; j++)
    {
        m_indices.push_back(i * (m_latitudes+2) + j);
        m_indices.push_back(i * (m_latitudes+2) + j+1);
        m_indices.push_back((i+1) * (m_latitudes+2) + j+1);

        m_triangleCount++;

        m_indices.push_back((i+1) * (m_latitudes+2) + j+1);
        m_indices.push_back((i+1) * (m_latitudes+2) + j);
        m_indices.push_back(i * (m_latitudes+2) + j);

        m_triangleCount++;
    }
}

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