-1

I have been attempting to get an .obj file loaded into my OpenGl application and I thought it had finally done it, but on closer inspection the model appears to have been either turned into a super low poly version or the entire model has been flipped inside out resulting in the faces looking weirdly bumpy and the normals/textures yield unexpected results as well.

I am unsure whether this is caused by the loader or something else but to save posting a wall of text ill just post the loader code, if more code is needed let me know and I will update my question.

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;
  }

Update:

adding some pictures of what it should look like and my drawing code.

this is what it should look like:

enter image description here

this is what it actually looks like:

enter image description here

Update 2: adding my shader code due to a request in the comments sorry about this becoming a massive wall of text also in_Color is treated as a uv I just haven't renamed it yet. vertex shader:

#version 150 core

uniform mat4 M;
uniform mat4 MVP;
uniform vec3 LightPosition_worldspace;
uniform mat4 V;



in vec3 in_Position;
in vec2 in_Color;
in vec3 in_Normals;

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

void main(void)
{
     gl_Position = MVP* vec4(in_Position, 1.0);
     Position_worldspace = (M * vec4(in_Position,1)).xyz;
     vec3 vertexPosition_cameraspace = ( V * M * vec4(in_Position,1)).xyz;
     EyeDirection_cameraspace = vec3(0,0,0) - vertexPosition_cameraspace;
     vec3 LightPosition_cameraspace = ( V * vec4(LightPosition_worldspace,1)).xyz;
     Normal_cameraspace = ( V * M * vec4(in_Normals,0)).xyz;
     pass_Color = in_Color;
}

fragment shader:

#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);


}
I Phantasm I
  • 1,571
  • 5
  • 20
  • 27
  • have any side by side screenshot of the broken model and how it should look like? You are also sure that the .obj file was exported with correct settings? – Grimmy Jun 10 '13 at 13:12
  • possible duplicate of [Rendering meshes with multiple indices](http://stackoverflow.com/questions/11148567/rendering-meshes-with-multiple-indices) – Nicol Bolas Jun 10 '13 at 13:39
  • Where's the OBJ that's rendering incorrectly? Where's the render code? – genpfault Jun 10 '13 at 14:34
  • @NicolBolas Don't think so. Looks like he's properly converting the multi-indices into a bunch of non-indexed vertex arrays. – Christian Rau Jun 10 '13 at 14:41
  • 3
    The vertices looking inside out is normally caused by a winding issue. Make sure it's rendering with the correct cull ordering. – slugonamission Jun 10 '13 at 14:53
  • @Grimmy added some pictures and yes im using the correct model export settings. – I Phantasm I Jun 11 '13 at 08:09
  • @genpfault added the render code and a picture which will hopfully make it easier to see the issue. – I Phantasm I Jun 11 '13 at 08:10
  • @IPhantasmI Well, the most interesting would probably be the shaders. – Christian Rau Jun 11 '13 at 08:29
  • @ChristianRau added the shaders, hopfully this will be enough info to get this running properly! – I Phantasm I Jun 11 '13 at 11:23
  • 1
    I don't see you set `LightDirection_cameraspace`, only a local `LightPosition_cameraspace` that is then not used anymore. – Christian Rau Jun 11 '13 at 11:58
  • @ChristianRau wow, how i never saw that in my hours of trying to fix it, i think its time for some better variable names, thanks a lot for that! ill accept it if you make it an answer. – I Phantasm I Jun 11 '13 at 13:35

1 Answers1

1

Turning comment into answer since it turned out to be the solution:

In your shader code you have a varying variable called LightDirection_cameraspace, which you use in your fragment shader computations. But you never assign a value to it in the vertex shader (instead you compute a local variable LightPosition_cameraspace which isn't used further, though). So it probably has some undefined value inside the fragment shader, causing your lighting issues.

Christian Rau
  • 43,206
  • 10
  • 106
  • 177