Jump to content


- - - - -

Sphere


4 replies to this topic

#1 elijah

    New Member

  • Members
  • PipPip
  • 21 posts

Posted 13 October 2009 - 08:18 AM

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.

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 5307 posts
  • LocationBellevue, WA

Posted 13 October 2009 - 04:49 PM

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

reedbeta.com - developer blog, OpenGL demos, and other projects

#3 SyntaxError

    Valued Member

  • Members
  • PipPipPip
  • 139 posts

Posted 13 October 2009 - 05:09 PM

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?

#4 elijah

    New Member

  • Members
  • PipPip
  • 21 posts

Posted 14 October 2009 - 04:48 AM

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

#5 elijah

    New Member

  • Members
  • PipPip
  • 21 posts

Posted 16 October 2009 - 09:26 AM

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users