c++ - OpenGL Vertex Array sphere stray vertex -


i've been trying code sphere in opengl using various code snippets online, after running code theres stray vertex , i'm not sure code has missed it:

enter image description here

code:

    float lats = 1/(float)(longitude-1);     float longs = 1/(float)(latitude-1);      int r,s;      vector<glfloat> vertices;     vector<glfloat> normals;     vector<glfloat> texcoords;     vector<glushort> indices;      for(r = 0; r < longitude; r++)     {         for(s = 0; s < latitude; s++)         {             float const x = cos(2*m_pi * s * longs) * sin( m_pi * r * lats );             float const y = sin( -m_pi_2 + m_pi * r * lats );             float const z = sin(2*m_pi * s * longs) * sin( m_pi * r * lats );              vertices.push_back(x * getr());             vertices.push_back(y * getr());             vertices.push_back(z * getr());              normals.push_back(x);             normals.push_back(y);             normals.push_back(z);              texcoords.push_back(s*lats);             texcoords.push_back(r*longs);         }     }      for(r = 0; r < longitude; r++)      {         for(s = 0; s < latitude; s++)         {             indices.push_back(r * latitude + s);             indices.push_back(r * latitude + (s+1));             indices.push_back((r+1) * latitude + (s+1));             indices.push_back((r+1) * latitude + s);         }     } 

can see have gone wrong?

you computing,

float lats = 1/(float)(longitude-1); float longs = 1/(float)(latitude-1); 

the north pole of sphere causing division 0.

updated

after looking @ code again think issue may bit more subtle.

you assuming

2*m_pi/* double */ * (latitude - 1)/*int*/  * 1/(float)(latitude - 1)/*float*/ == 2*m_pi 

because of floating point issues may not true. applies other expressions in sin() & cos()

probably dealing loss of precision.

since deterministic fix manually @ end. still applies though.

interestingly front-face, back-face color coding indicates problem, there "knot" @ top


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -