Note that there are some explanatory texts on larger screens.

plurals
  1. PO.obj model loader working unexpectedly in opengl
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <pre><code>bool ObjLoader::loadOBJ( const char * path, std::vector&lt;glm::vec3&gt; &amp; out_vertices, std::vector&lt;glm::vec2&gt; &amp; out_uvs, std::vector&lt;glm::vec3&gt; &amp; out_normals ) { printf("Loading OBJ file %s...\n", path); std::vector&lt;unsigned int&gt; vertexIndices, uvIndices, normalIndices; std::vector&lt;glm::vec3&gt; temp_vertices; std::vector&lt;glm::vec2&gt; temp_uvs; std::vector&lt;glm::vec3&gt; 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", &amp;vertex.x, &amp;vertex.y, &amp;vertex.z ); temp_vertices.push_back(vertex); }else if ( strcmp( lineHeader, "vt" ) == 0 ){ glm::vec2 uv; fscanf(file, "%f %f\n", &amp;uv.x, &amp;uv.y ); temp_uvs.push_back(uv); }else if ( strcmp( lineHeader, "vn" ) == 0 ){ glm::vec3 normal; fscanf(file, "%f %f %f\n", &amp;normal.x, &amp;normal.y, &amp;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", &amp;vertexIndex[0], &amp;uvIndex[0], &amp;normalIndex[0], &amp;vertexIndex[1], &amp;uvIndex[1], &amp;normalIndex[1], &amp;vertexIndex[2], &amp;uvIndex[2], &amp;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&lt;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; } </code></pre> <p>Update:</p> <p>adding some pictures of what it should look like and my drawing code.</p> <p>this is what it should look like:</p> <p><img src="https://i.stack.imgur.com/xhwo1.png" alt="enter image description here"></p> <p>this is what it actually looks like:</p> <p><img src="https://i.stack.imgur.com/qBVFY.png" alt="enter image description here"></p> <p>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:</p> <pre><code>#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; } </code></pre> <p>fragment shader:</p> <pre><code>#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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload