Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading an OBJ file, how to use normals (#vertices < #normals)
    primarykey
    data
    text
    <p>I have an obj file and have succesfully loaded the object to opengl without using the normals given.<br> This is how it looks:<br> <img src="https://i.stack.imgur.com/Z08uO.png" alt="tree without normals"><br> The format of the file is:</p> <pre><code>v x y z vn x y z f x//x' y//y' z//z' </code></pre> <p>The displaying function of the mesh is like that:</p> <pre><code>glBegin(GL_TRIANGLES); for all faces { glVertex3f(.., .., ..); glVertex3f(.., .., ..); glVertex3f(.., .., ..); } glEnd(); </code></pre> <p>And the result is this:<br> <img src="https://i.stack.imgur.com/iMFCk.png" alt="enter image description here"></p> <p>I've read that the object might look flat due to the default normal vector that OpenGL uses for lighting equations.<br> This could be solved with normals. The normals given are 780 and the vertices 155. </p> <p>I've tried using glNorm before each glVertex3f call but the obj looks completely weird (lines, etc).</p> <p>What should I do?</p> <p><strong>EDIT #1:</strong> This is how I read the file: </p> <pre><code>void loader(class mesh &amp;tree) { int vx1, vx2, vx3, vn1, vn2, vn3; ifstream file; string line; point vec, norm; face tempface; file.open("tree.obj"); if (file.is_open() == true) { while(getline(file, line) ) { if (line.substr(0,2) == "") continue; else if (line.substr(0, 2) == "v ") { istringstream numbers(line.substr(2)); numbers &gt;&gt; vec.x &gt;&gt; vec.y &gt;&gt; vec.z; tree.vectors.push_back(vec); } else if (line.substr(0,2) == "vn") { istringstream numbers(line.substr(2)); numbers &gt;&gt; norm.x &gt;&gt; norm.y &gt;&gt; norm.z; tree.normals.push_back(norm); } else if (line.substr(0,2) == "f ") { face f; line = line.substr(2,line.length()); for (string::iterator it = line.begin(); it != line.end(); ++it) { if (*it == '/') { //erase both of the "//" line.erase(it); line.erase(it); //add a space between the numbers line.insert(it, ' '); } } istringstream inp(line); inp &gt;&gt; f.vert_indices[0] &gt;&gt; f.norm_indices[0] &gt;&gt; f.vert_indices[1] &gt;&gt; f.norm_indices[1] &gt;&gt; f.vert_indices[2] &gt;&gt; f.norm_indices[2]; tree.faces.push_back(f); } else continue; } file.close(); } } </code></pre> <p>And this is how I am displaying it:</p> <pre><code> void mesh::displayMesh() { glPushMatrix(); glBegin(GL_TRIANGLES); for(vector&lt;face&gt;::const_iterator it = faces.begin(); it != faces.end(); ++it) { //glVertex3f(normals[it-&gt;norm_indices[0] -1 ].x, normals[it-&gt;norm_indices[0] -1 ].y, normals[it-&gt;norm_indices[0] -1 ].z); glVertex3f(vectors[it-&gt;vert_indices[0] -1 ].x, vectors[it-&gt;vert_indices[0] -1 ].y, vectors[it-&gt;vert_indices[0] -1 ].z); //glVertex3f(normals[it-&gt;norm_indices[1] -1 ].x, normals[it-&gt;norm_indices[1] -1 ].y, normals[it-&gt;norm_indices[1] -1 ].z); glVertex3f(vectors[it-&gt;vert_indices[1] -1 ].x, vectors[it-&gt;vert_indices[1] -1 ].y, vectors[it-&gt;vert_indices[1] -1 ].z); //glVertex3f(normals[it-&gt;norm_indices[2] -1 ].x, normals[it-&gt;norm_indices[2] -1 ].y, normals[it-&gt;norm_indices[2] -1 ].z); glVertex3f(vectors[it-&gt;vert_indices[2] -1 ].x, vectors[it-&gt;vert_indices[2] -1 ].y, vectors[it-&gt;vert_indices[2] -1 ].z); } glEnd(); glPopMatrix(); } </code></pre> <p>The function displayMesh is called within Render function of openGL and the loader function is called into Setup OpenGL function and the mesh tree object is a global variable.</p> <p><strong>EDIT #2:</strong> This how the tree looks with the normals:<br> <img src="https://i.stack.imgur.com/sOXRv.png" alt="tree with normals"> Also <a href="http://pastebin.com/zp7gGc7h" rel="nofollow noreferrer">this is the code</a> of the setup function of OpenGL.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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