为什么fscanf()将数字四舍五入为整数?

时间:2019-04-01 08:18:55

标签: c++

我正在使用教程a link中的示例函数,该函数读取OBJ文件以供OpenGL渲染。

我在Qt 5.9.3中编码我的应用程序,并使用QML制作GUI。应用程序从.bt文件(OctoMap)中读取数据,并在子窗口中删除它们。不幸的是,当我尝试从OBJ读取同一模型时,我的应用程序未显示任何内容。当我使用调试模式并检查矢量时,两者(来自bt和obj)是相同的。下面显示了来自OBJ文件的变量:

v -2.0868 -1.6868 4.680000
v -2.0868 -1.9132 4.680000
v -2.3132 -1.6868 4.680000
v -2.3132 -1.9132 4.680000
v -2.0868 -1.6868 4.520000
v -2.3132 -1.6868 4.520000
v -2.0868 -1.9132 4.520000
v -2.3132 -1.9132 4.520000

和调试模式下的信息:

vertices2   <36 elementów>  QVector<QVector3D>
    [0] @0x3257148  QVector3D
        xp  -2.0868000984191895 float
        yp  -1.6868000030517578 float
        zp  4.679999828338623   float
    [1] @0x3257154  QVector3D
        xp  -2.0868000984191895 float
        yp  -1.9132000207901    float
        zp  4.679999828338623   float
    [2] @0x3257160  QVector3D
        xp  -2.313199996948242  float
        yp  -1.6868000030517578 float
        zp  4.679999828338623   float

来自Octomap的向量是相同的,因此它应该可以正常工作。 绘制渲染模型的QOpenglShaderProgram通过指针获取矢量变量:

program1.setAttributeArray(vertexAttr1, vertices.constData());
program1.setAttributeArray(vertexAttr1, vertices2.constData());

即使我不使用第一个向量,第二个向量的数据也无法显示。 但是,我发现了问题的可能原因之一。当我尝试通过命令std :: cout打印出变量时,我得到以下输出:

one: -2.0868 two: -2
one: -2.3132 two: -2
one: -2.0868 two: -2
one: -2.0868 two: -2

其中一个是OctoMap矢量,两个是OBJ矢量。 我从turorial按功能创建OBJ向量

bool openGLrenderer::loadOBJ(
    const char * path

){
    //vertices2.clear();
    //normals2.clear();
    //uves2.clear();
    printf("Loading OBJ file %s...\n", path);

    std::vector<unsigned int> vertexIndices, uvIndices, normalIndices;
    QVector<QVector3D> temp_vertices;
    QVector<QVector2D> temp_uvs;
    QVector<QVector3D> temp_normals;


    FILE * file = fopen(path, "r");
    if( file == NULL ){
        printf("Impossible to open the file ! Are you in the right path ? See Tutorial 1 for details\n");
        return false;
    }

    while( 1 ){

        char lineHeader[128];
        // read the first word of the line
        int res = fscanf(file, "%s", lineHeader);
        if (res == EOF)
            break; // EOF = End Of File. Quit the loop.

        // else : parse lineHeader

        if ( strcmp( lineHeader, "v" ) == 0 ){
            glm::vec3 vertexGLM;
            QVector3D vertex;
            fscanf(file, "%g %g %g\n", &vertexGLM.x, &vertexGLM.y, &vertexGLM.z );
            vertex.setX(vertexGLM.x);
            vertex.setY(vertexGLM.y);
            vertex.setZ(vertexGLM.z);
            temp_vertices << vertex;

        }else if ( strcmp( lineHeader, "vn" ) == 0 ){
            glm::vec3 normalGLM;
            QVector3D normal;
            fscanf(file, "%f %f %f\n", &normalGLM.x, &normalGLM.y, &normalGLM.z );
            normal.setX(normalGLM.x);
            normal.setY(normalGLM.y);
            normal.setZ(normalGLM.z);
            temp_normals << normal;
        }else if ( strcmp( lineHeader, "f" ) == 0 ){
            std::string vertex1, vertex2, vertex3;
            unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
            int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2] );
            if (matches != 9){
                printf("File can't be read by our simple parser :-( Try exporting with other options\n");
                return false;
            }
            vertexIndices.push_back(vertexIndex[0]);
            vertexIndices.push_back(vertexIndex[1]);
            vertexIndices.push_back(vertexIndex[2]);

            normalIndices.push_back(normalIndex[0]);
            normalIndices.push_back(normalIndex[1]);
            normalIndices.push_back(normalIndex[2]);
        }else{
            // Probably a comment, eat up the rest of the line
            char stupidBuffer[1000];
            fgets(stupidBuffer, 1000, file);
        }

    }

    // For each vertex of each triangle
    for( unsigned int i=0; i<vertexIndices.size(); i++ ){

        vertices2 << temp_vertices[ vertexIndices[i]-1 ];
        normals2 << temp_normals[ normalIndices[i]-1 ];

        // Put the attributes in buffers


    }

    return true;
}

在某些情况下,fscanf方法是否可以从float变量中获取整数?

glm::vec3 vertexGLM;
QVector3D vertex;
fscanf(file, "%g %g %g\n", &vertexGLM.x, &vertexGLM.y, &vertexGLM.z );
      vertex.setX(vertexGLM.x);
      vertex.setY(vertexGLM.y);
      vertex.setZ(vertexGLM.z);

当我使用值1.2345代替vertexGLM.x时,应用程序不会将其取整。

我可以尝试使用fscanf以外的其他方法,但是您知道此功能以这种方式工作的原因吗?

0 个答案:

没有答案