Sphere

6475908f9503cf68fb9e462214ca4d73
0
elijah 101 Oct 13, 2009 at 08:18

I’m trying to do a Sphere through revolve. I creating a slice of points from 90 to -90 and revolve around an axis resulting in creating N number of slice through a rotation.

Then I join the slices by taking 4 points each time from a two slice. Example

Point 1 = SLICE[ SLICE_LOOP ][ VERTEX_LOOP ] ;
Point 2 = SLICE[ SLICE_LOOP ][ VERTEX_LOOP+ 1 ];
Point 3 = SLICE[ SLICE_LOOP + 1 ][ VERTEX_LOOP ]
Point 4 = SLICE[ SLICE_LOOP + 1 ][ VERTEX_LOOP + 1 ];

Using the 4 points I create two triangles with anticlockwise winding rule. The Sphere is created correctly except

“at the VERTEX_LOOP = 0 where the winding rule is clockwise as the Normal is facing inside of the sphere. This only happens for only one of the triangles. The other triangle is created with anticlokwise.

All the other triangles the normals are facing outside the sphere.”

I notice when the distance between tip is closed , the normal becomes clockwise.

My question is this cause by Maths rounding off when calculating Normals? I’m using java. Has anybody face this problem before ? And how do I solve this.

4 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Oct 13, 2009 at 16:49

Sounds like a bug in your logic or math somewhere. I doubt this a roundoff issue. Anywhere, here is some working (but old and kind of horrible) code that generates a sphere; you can compare to your algorithm.

// Code to generate sphere model
#define U_TESSELATION 48
#define V_TESSELATION 24
int GenerateSphere ()
{
    // Create a display list containing the sphere vertices and normals

    // Setup vertices
    const int vertex_size = 3;                                  // 3 floats for location (which equals normal)
    float *fbuf = new float[vertex_size * (U_TESSELATION*(V_TESSELATION-1)+2)];     // +2 for the pole vertices
    const float pole_vertices[2 * vertex_size] = { /* south pole */ 0, 0, -1, /* north pole */ 0, 0, 1 };
    memcpy(fbuf, pole_vertices, 2*vertex_size*sizeof(float));
    for (int i = 0; i < U_TESSELATION; ++i)
    {
        float theta = 2 * pi * i / U_TESSELATION;
        for (int j = 0; j < V_TESSELATION-1; ++j)
        {
            float phi = pi * (j+1) / V_TESSELATION;
            // Calculate location
            vec3 location;
            location.x = cosf(theta) * sinf(phi);
            location.y = sinf(theta) * sinf(phi);
            location.z = -cosf(phi);
            // Copy vertex into the f-buffer
            int offset = vertex_size * (i*(V_TESSELATION-1) + j + 2);               // +2 for the pole vertices
            fbuf[offset] = location.x;
            fbuf[offset+1] = location.y;
            fbuf[offset+2] = location.z;
        }
    }
    // Set vertex pointer
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(float)*vertex_size, fbuf);
    // Set normal pointer -- uses exactly the same data as the locations
    glEnableClientState(GL_NORMAL_ARRAY);
    glNormalPointer(GL_FLOAT, sizeof(float)*vertex_size, fbuf);

    // Setup indices for tri list
    int strip_size = 6*(V_TESSELATION-2) + 6;                                       // # of indices in one pole-to-pole strip
    unsigned long *ulbuf = new unsigned long[U_TESSELATION*strip_size];
    for (i = 0; i < U_TESSELATION; ++i)
    {
        int offset = i * strip_size;
        int iplus1 = (i == U_TESSELATION - 1) ? 0 : i+1;
        // Fill in the first and last triangles (which connect to the pole)
        ulbuf[offset] = 0;
        ulbuf[offset+1] = 2 + (V_TESSELATION-1)*iplus1;
        ulbuf[offset+2] = 2 + (V_TESSELATION-1)*i;
        ulbuf[offset+3] = 1;
        ulbuf[offset+4] = 2 + (V_TESSELATION-1)*i + V_TESSELATION-2;
        ulbuf[offset+5] = 2 + (V_TESSELATION-1)*iplus1 + V_TESSELATION-2;
        offset += 6;
        // Fill in the other triangles
        for (int j = 0; j < V_TESSELATION-2; ++j)
        {
            ulbuf[offset] = 2 + (V_TESSELATION-1)*i + j + 1;
            ulbuf[offset+1] = 2 + (V_TESSELATION-1)*i + j;
            ulbuf[offset+2] = 2 + (V_TESSELATION-1)*iplus1 + j + 1;
            ulbuf[offset+3] = ulbuf[offset+2];
            ulbuf[offset+4] = ulbuf[offset+1];
            ulbuf[offset+5] = 2 + (V_TESSELATION-1)*iplus1 + j;
            offset += 6;
        }
    }

    // Create the display list
    int list = glGenLists(1);
    glNewList(list, GL_COMPILE);
    glDrawElements(GL_TRIANGLES, U_TESSELATION * strip_size, GL_UNSIGNED_INT, ulbuf);
    glEndList();

    delete []fbuf;
    delete []ulbuf;
    
    return list;
}
5225bc0c3bf66f4c275c332de6388d1f
0
SyntaxError 101 Oct 13, 2009 at 17:09

If I understand you correctly, at the polls you will have a single triangle instead of two in each slice. Did you allow for this?

6475908f9503cf68fb9e462214ca4d73
0
elijah 101 Oct 14, 2009 at 04:48

Hi SyntaxError

I have cater for that at the polls at both ends. It is the second index from the last poll that is causing the problem

6475908f9503cf68fb9e462214ca4d73
0
elijah 101 Oct 16, 2009 at 09:26

I have solved the problem. It is cause by condition like -0.0f and NAN when calculating the Normal in Java.