首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >移动uv坐标

移动uv坐标
EN

Stack Overflow用户
提问于 2018-01-15 03:50:58
回答 1查看 55关注 0票数 0

我正在尝试为我的游戏引擎创建一个模型加载器,它接受一个.obj对象文件(从blender),并将其转换为一个带有索引和顶点的模型,但uv坐标似乎发生了移动,并且非常不正确。

以下是模型加载器的代码:

代码语言:javascript
复制
bool RawModel::LoadModel(std::string fileName)
{

    m_name = fileName;

    int pos = m_name.find_last_of("\\");
    if (pos >= 0)
    {
        m_name = m_name.substr(pos + 1, m_name.length());
    }

    //Load vertices:

    m_vertices.clear();
    m_indices.clear();

    std::ifstream objFile(fileName);
    if (!objFile.is_open())
    {
        std::cout << "Could not open file: " << fileName << std::endl;
        return false;
    }

    std::string currentLine;

    std::vector<Vector3> vertices;
    std::vector<Vector2> uvs;
    std::vector<Vector3> normals;

    while (objFile.good())
    {
        std::getline(objFile, currentLine);
        if (currentLine.find("v ") != std::string::npos) //Vertex
        {
            std::stringstream streamedLine(currentLine);
            float x, y, z;
            std::string temps;
            streamedLine >> temps;
            streamedLine >> x;
            streamedLine >> y;
            streamedLine >> z;
            vertices.push_back(Vector3(x, y, z));
        }
        else if (currentLine.find("vt ") != std::string::npos) //Uv
        {
            std::stringstream streamedLine(currentLine);
            float x, y;
            std::string temps;
            streamedLine >> temps;
            streamedLine >> x;
            streamedLine >> y;
            uvs.push_back(Vector2(x, 1-y)); //-y because top is 0y not 1y as in normal coordinate space
        }
        else if (currentLine.find("vn ") != std::string::npos) //Normal
        {
            std::stringstream streamedLine(currentLine);
            float x, y, z;
            std::string temps;
            streamedLine >> temps;
            streamedLine >> x;
            streamedLine >> y;
            streamedLine >> z;
            vertices.push_back(Vector3(x, y, z));
        }
        else if (currentLine.find("f ") != std::string::npos)
        {
            m_vertices.resize(vertices.size());

            std::stringstream streamedLine(currentLine);
            std::string segment;
            streamedLine >> segment;
            std::vector<std::string> indices;

            indices.reserve(3);

            //getting the triangulated face to a array
            while (streamedLine >> segment)
                indices.push_back(segment);

            for (int i = 0; i < indices.size(); i++) //Loop all indices in the face
            {
                std::stringstream streamedIndex(indices[i]);
                std::vector<int> vertex;//0 = vertexPos, 1 = uv, 2 = normal
                indices.reserve(3);

                while (std::getline(streamedIndex, segment, '/')) //x3
                {
                    vertex.push_back(stoi(segment)-1);
                }
                m_vertices[vertex[0]] = VertexType(vertices[vertex[0]], uvs[vertex[1]]);
                m_indices.emplace_back(vertex[0]);

            }

        }
    }
    return true;
}

VertexType是一个结构,它是顶点的结构。顶点的构建和定义如下所示:

代码语言:javascript
复制
struct VertexType
{
    D3DXVECTOR3 position;
    D3DXVECTOR2 uv;
    D3DXVECTOR3 normal;
    VertexType() : position(0, 0, 0), uv(0, 0) {};
    VertexType(D3DXVECTOR3 position, D3DXVECTOR2 uv, D3DXVECTOR3 normal) :position(position), uv(uv), normal(normal) {};
    VertexType(Vector3 position, Vector2 uv) :position(position.D3DXVECTOR()), uv(uv.D3DXVECTOR()) {};
};

这就是它正常工作时应该是什么样子

这是它在我的游戏引擎中的样子:

我怀疑它的模型是错误的,因为它的工作方式是统一的,搅拌器具有相同的纹理(根据uv布局,绿色和红色在uv坐标之外,透明部分通常应该是绿色和红色,但为了方便起见,它们被设置为绿色和红色)。(纹理底部为红色,顶部为绿色)

这是在这两种情况下使用的纹理。

EN

回答 1

Stack Overflow用户

发布于 2018-02-14 08:47:21

在加载OBJ文件后,我记得这个问题。下面是我在C#中的修复方法,我相信你可以在这里应用它。

代码语言:javascript
复制
vp.SetTextureUV(result.Textures[TextureIndex].X, -result.Textures[TextureIndex].Y);

当你设置你的UV的V分量时,出于某种原因,我不得不在格式中取消这个值,以使它与我的纹理一起工作。我希望这也能解决你的问题。从截图上看,我想它会的。

你可以在Y轴上翻转你的纹理,这也应该可以做到这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48253539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档