c++ - 加载 .dae 文件时出现问题,索引和顶点加载正确

标签 c++ opengl mesh collada indices

所以看完this我认为最好更改为我加载文件的格式并决定使用 .dae 文件。在写完这个非常简单的 xml-loader 之后:

void Model::loadModelFromDae(std::string source)
{
    using namespace std;
    string name = source.substr(0,source.find("."));
    cout<<"+++LOADING::"<<name<<"+++"<<endl;

    ifstream file;
    file.open(source);
    string line;
    size =0;
    indexSize=0;

    while(getline(file,line))
    {
        /*possible float_array's: vertices and normal*/
        if(std::string::npos != line.find("<float_array"))
        {
            if(std::string::npos != line.find("positions"))
            {
                //get the size of the vertices float_array
                int loc_count = line.find("count");
                // to explain the hardcoded values: count=" is 7 char's
                string cout_substr = line.substr(line.find("count"),line.find(">"));
                cout_substr = cout_substr.substr(7,cout_substr.find("\"")); 
                size = atoi(cout_substr.c_str());
                cout<<"\tSIZE:"<<size<<endl;
                vertices = new float[size];
                memset(vertices,0.0f,sizeof(float)*size);

                string values = line.substr(line.find("count")+11,line.size());
                values = values.substr(0,values.size()-14);
                std::stringstream converter(values);
                vector<float> temp;
                // Iterate over the istream, using >> to grab floats
                // and push_back to store them in the vector
                std::copy(std::istream_iterator<float>(converter),
                    std::istream_iterator<float>(),
                    std::back_inserter(temp));
                for(int i=0;i<temp.size();i++) vertices[i] = temp.at(i);
            }
            //TODO: handle normal array
        }
        /*Handling indices*/
        if(std::string::npos != line.find("<p>"))
        {
            string values = line.substr(line.find(">")+1,line.length());
            values = values.substr(0,values.length()-4);
            std::stringstream converter(values);
            vector<short> temp;
            // Iterate over the istream, using >> to grab shorts
            // and push_back to store them in the vector
            std::copy(std::istream_iterator<short>(converter),
                std::istream_iterator<short>(),
                std::back_inserter(temp));
            indices = new short[temp.size()];
            indexSize = temp.size();
            cout<<"\tINDEXSIZE:"<<indexSize<<endl;
            for(int i=0;i<temp.size();i++) indices[i] = temp.at(i);
        }
    }

    cout<<"+++ENDED LOADING +DAE+ FILE:"<<name<<"+++"<<endl;
}

我仍然有一些问题:我正在尝试加载一个立方体,并检查每个顶点/索引,它们与 XML 文件完全匹配,但输出完全不是我所期望的:立方体呈现三角形,但不是按照他们应该的顺序。所以我有几个问题:

  • .dae 是否以不同于 openGL 的格式保存它的索引 是吗?
  • (可选,因为它需要大量调试)你能发现我的错误吗,我最初的想法是顶点的朝向是 错误但放置 glFrontFace(GL_CCW);glFrontFace(GL_CW); 没有任何改变。

最佳答案

在弄乱了索引之后,我得出了这个结论:索引不仅是顶点的索引,而且是法线和 UV 的索引。因此,在加载索引时,您应该注意哪个索引属于哪个元素。

关于c++ - 加载 .dae 文件时出现问题,索引和顶点加载正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18298887/

相关文章:

c++ - 静态链接 GLU?

c++ - 如何使用glCreateShaderProgram?

java - 在 openGL Java 中为球体对象的三角网格生成索引

mesh - 使用来自点和法线的 vtk 对曲面进行三角测量

c - 网格的翼状边缘结构如何工作?

c++ - 在模板函数中提示祖先类型

c++ - C++文件之间的区别

c++ - GLSL需要什么数据?

非对象类型的 C++ 智能指针?

opengl - 为什么在交换缓冲区后调用 GL.Finish 可以防止低端硬件出现大量卡顿,但在现代 GPU 上却不能?