Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ .obj模型加载器在opengl中意外工作_C++_Opengl - Fatal编程技术网

C++ .obj模型加载器在opengl中意外工作

C++ .obj模型加载器在opengl中意外工作,c++,opengl,C++,Opengl,我一直在尝试将一个.obj文件加载到我的OpenGl应用程序中,我认为它终于做到了,但仔细观察,该模型似乎已经变成了超低多边形版本,或者整个模型被翻过来,导致面看起来异常凹凸不平,法线/纹理也产生了意想不到的结果 我不确定这是否是由加载器或其他原因造成的,但为了避免张贴一堵墙的文本,我只需发布加载器代码,如果需要更多代码,请告诉我,我将更新我的问题 bool ObjLoader::loadOBJ( const char * path, std::vector<glm::ve

我一直在尝试将一个.obj文件加载到我的OpenGl应用程序中,我认为它终于做到了,但仔细观察,该模型似乎已经变成了超低多边形版本,或者整个模型被翻过来,导致面看起来异常凹凸不平,法线/纹理也产生了意想不到的结果

我不确定这是否是由加载器或其他原因造成的,但为了避免张贴一堵墙的文本,我只需发布加载器代码,如果需要更多代码,请告诉我,我将更新我的问题

bool ObjLoader::loadOBJ(
    const char * path,
    std::vector<glm::vec3> & out_vertices,
    std::vector<glm::vec2> & out_uvs,
    std::vector<glm::vec3> & out_normals
    )
    {
    printf("Loading OBJ file %s...\n", path);

    std::vector<unsigned int> vertexIndices, uvIndices, normalIndices;
    std::vector<glm::vec3> temp_vertices;
    std::vector<glm::vec2> temp_uvs;
    std::vector<glm::vec3> temp_normals;


    FILE * file = fopen(path, "r");
    if( file == NULL ){
            printf("Cant find the file!!\n");
            return false;
    }

    while( 1 ){

            char lineHeader[128];
            int res = fscanf(file, "%s", lineHeader);
            if (res == EOF)
                    break;

            if ( strcmp( lineHeader, "v" ) == 0 ){
                    glm::vec3 vertex;
                    fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z );
                    temp_vertices.push_back(vertex);
            }else if ( strcmp( lineHeader, "vt" ) == 0 ){
                    glm::vec2 uv;
                    fscanf(file, "%f %f\n", &uv.x, &uv.y );
                    temp_uvs.push_back(uv);
            }else if ( strcmp( lineHeader, "vn" ) == 0 ){
                    glm::vec3 normal;
                    fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z );
                    temp_normals.push_back(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] );

                    vertexIndices.push_back(vertexIndex[0]);
                    vertexIndices.push_back(vertexIndex[1]);
                    vertexIndices.push_back(vertexIndex[2]);
                    uvIndices    .push_back(uvIndex[0]);
                    uvIndices    .push_back(uvIndex[1]);
                    uvIndices    .push_back(uvIndex[2]);
                    normalIndices.push_back(normalIndex[0]);
                    normalIndices.push_back(normalIndex[1]);
                    normalIndices.push_back(normalIndex[2]);
            }else{

                    char stupidBuffer[1000];
                    fgets(stupidBuffer, 1000, file);
            }
    }


    for( unsigned int i=0; i<vertexIndices.size(); i++ ){
            unsigned int vertexIndex = vertexIndices[i];
            unsigned int uvIndex = uvIndices[i];
            unsigned int normalIndex = normalIndices[i];

            glm::vec3 vertex = temp_vertices[ vertexIndex-1 ];
            glm::vec2 uv = temp_uvs[ uvIndex-1 ];
            glm::vec3 normal = temp_normals[ normalIndex-1 ];

            out_vertices.push_back(vertex);
            out_uvs     .push_back(uv);
            out_normals .push_back(normal);
    }

    return true;
  }
片段着色器:

#version 150 core

in vec2 pass_Color;
in vec3 Position_worldspace;
in vec3 Normal_cameraspace;
in vec3 EyeDirection_cameraspace;
in vec3 LightDirection_cameraspace;

out vec3 out_Color;
uniform sampler2D myTextureSampler;
uniform mat4 MV;
uniform vec3 LightPosition_worldspace;

void main(void)
{

        vec3 LightColor = vec3(1,1,1);
        float LightPower = 200.0f;
        vec3 MaterialDiffuseColor = texture2D( myTextureSampler, pass_Color ).rgb;
        vec3 MaterialAmbientColor = vec3(0.1,0.1,0.1) * MaterialDiffuseColor;
        vec3 MaterialSpecularColor = vec3(0.3,0.3,0.3);

        float distance = length( LightPosition_worldspace - Position_worldspace );
        vec3 n = normalize( Normal_cameraspace );
        vec3 l = normalize( LightDirection_cameraspace );
        float cosTheta = clamp( dot( n,l ), 0,1 );
        vec3 E = normalize(EyeDirection_cameraspace);
        vec3 R = reflect(-l,n);
        float cosAlpha = clamp( dot( E,R ), 0,1 );


        out_Color = MaterialAmbientColor
        + MaterialDiffuseColor * LightColor * LightPower
         * cosTheta / (distance*distance) +MaterialSpecularColor
          * LightColor * LightPower * pow(cosAlpha,5) / (distance*distance);


}

将评论转化为答案,因为这是解决方案:


在着色器代码中,有一个名为
LightDirection\u cameraspace
的变量,可用于片段着色器计算。但您从未在顶点着色器中为其指定值(而是计算一个局部变量
LightPosition\u cameraspace
,但该变量不再使用)。因此,它可能在片段着色器中有一些未定义的值,从而导致照明问题。

是否有损坏模型的并排屏幕截图,以及它应该是什么样子?您还确定.obj文件是以正确的设置导出的吗?可能与渲染不正确的obj的位置重复?渲染代码在哪里?顶点向外看通常是由缠绕问题引起的。确保以正确的消隐顺序进行渲染。我看不到您设置了
LightDirection\u cameraspace
,只有一个本地
LightPosition\u cameraspace
,然后不再使用。
#version 150 core

in vec2 pass_Color;
in vec3 Position_worldspace;
in vec3 Normal_cameraspace;
in vec3 EyeDirection_cameraspace;
in vec3 LightDirection_cameraspace;

out vec3 out_Color;
uniform sampler2D myTextureSampler;
uniform mat4 MV;
uniform vec3 LightPosition_worldspace;

void main(void)
{

        vec3 LightColor = vec3(1,1,1);
        float LightPower = 200.0f;
        vec3 MaterialDiffuseColor = texture2D( myTextureSampler, pass_Color ).rgb;
        vec3 MaterialAmbientColor = vec3(0.1,0.1,0.1) * MaterialDiffuseColor;
        vec3 MaterialSpecularColor = vec3(0.3,0.3,0.3);

        float distance = length( LightPosition_worldspace - Position_worldspace );
        vec3 n = normalize( Normal_cameraspace );
        vec3 l = normalize( LightDirection_cameraspace );
        float cosTheta = clamp( dot( n,l ), 0,1 );
        vec3 E = normalize(EyeDirection_cameraspace);
        vec3 R = reflect(-l,n);
        float cosAlpha = clamp( dot( E,R ), 0,1 );


        out_Color = MaterialAmbientColor
        + MaterialDiffuseColor * LightColor * LightPower
         * cosTheta / (distance*distance) +MaterialSpecularColor
          * LightColor * LightPower * pow(cosAlpha,5) / (distance*distance);


}