Sign in to follow this  
Mexx Cuter

Creating a sphere directly and with a list problem

Recommended Posts

Hi, What I am trying to do is create a sphere with a triangle_strip or quad_strip. I would like to store my vertices in a list and call it when needed. This is where i have a problem: when i create the sphere on the fly it works but when i use to load it from a list it seems to draw extra vertices. (Here is the code which draws a half sphere of 8*8) on the fly code:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
int i,j;
double theta1,theta2,theta3;
pPoint3D e,p,c; 
double r = 8;
int n = 8;
c.x = 0.0f;
c.y = 0.0f;
c.z = 0.0f;

   for (j=0;j<n/4;j++) {
      theta1 = j * c2Pi / n - cPiOver2;
      theta2 = (j + 1) * c2Pi / n - cPiOver2;

      glBegin(GL_TRIANGLE_STRIP);
      for (i=0;i<=n;i++) {
         theta3 = i * c2Pi / n;

         e.x = cos(theta2) * cos(theta3);
         e.y = sin(theta2);
         e.z = cos(theta2) * sin(theta3);
         p.x = c.x + r * e.x;
         p.y = c.y + r * e.y;
         p.z = c.z + r * e.z;

         glNormal3f(e.x,e.y,e.z);
         glVertex3f(p.x,p.y,p.z);

         e.x = cos(theta1) * cos(theta3);
         e.y = sin(theta1);
         e.z = cos(theta1) * sin(theta3);
         p.x = c.x + r * e.x;
         p.y = c.y + r * e.y;
         p.z = c.z + r * e.z;

         glNormal3f(e.x,e.y,e.z);
         glVertex3f(p.x,p.y,p.z);

      }
      glEnd();
   }

code with list:
void pSkyDome::CreateSphere(pPoint3D c,double r,int n)
{
int i,j;
double theta1,theta2,theta3;
pPoint3D e,p;   

if (Vertices) delete []Vertices;

NumVertices = (n*n + n) / 2;
Vertices = new pVertex[NumVertices];
int vertCount = 0;


   for (j=0;j<n/4;j++) {		//radials
      theta1 = j * c2Pi / n - cPiOver2;
      theta2 = (j + 1) * c2Pi / n - cPiOver2;
      
       for (i=0;i<=n;i++) {		//vertical
         theta3 = i * c2Pi / n;

	 //first
         e.x = cos(theta2) * cos(theta3);
         e.y = sin(theta2);
         e.z = cos(theta2) * sin(theta3);
         p.x = c.x + r * e.x;
         p.y = c.y + r * e.y;
         p.z = c.z + r * e.z;

		 Vertices[vertCount].normal = e;
		 Vertices[vertCount].position = p;
		 vertCount++;

	 //second
         e.x = cos(theta1) * cos(theta3);
         e.y = sin(theta1);
         e.z = cos(theta1) * sin(theta3);
         p.x = c.x + r * e.x;
         p.y = c.y + r * e.y;
         p.z = c.z + r * e.z;

		 Vertices[vertCount].normal = e;
		 Vertices[vertCount].position = p;
		 vertCount++;
      }
   }

}


glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_TRIANGLE_STRIP); //GL_QUAD_STRIP
	
	for (int i=0; i<NumVertices; i++)
	{		
		 glNormal3f( Vertices[i].normal.x,
					 Vertices[i].normal.y,
					 Vertices[i].normal.z);
		
		if(i==17)//Vertices[i].position.y < 0)
		{glColor3f(1.0,0.0,0.0);}
		else
		{glColor3f(1.0,1.0,1.0);}
		 glVertex3f( Vertices[i].position.x,
					 Vertices[i].position.y,
					 Vertices[i].position.z);		
	}

	glEnd();


Graphical illustration( the red point is the 17th vertice): sphere prob Hope some-one can clear this up for me. Thanks

Share this post


Link to post
Share on other sites
For anybody stepping on this problem.
It was a stupid mistake by me :)

The first example creates multiple triangle strips.
While the second only creates one and off course more triangle are created.(all on the side)

I’ll update this post later with the fixed version of my code.

sorry for any inconvenience

Share this post


Link to post
Share on other sites
my solution:

[source lan=c++]
void pSkyDome::CreateSphere(pPoint3D c,double r,int n)
{

int i,j;
double theta1,theta2,theta3;
pPoint3D e,p;

if (Vertices) delete []Vertices;
NumVertices = (n*n + n) + n;
Vertices = new pVertex[NumVertices];
int vertCount = 0;

for (j=0;j<n/2;j++) { //radials
theta1 = j * c2Pi / n - cPiOver2;
theta2 = (j + 1) * c2Pi / n - cPiOver2;

// create a point which tells there is a strip comming
Vertices[vertCount] = NULL;
vertCount++;

//create a new strip
for (i=0;i<=n;i++) { //vertical
theta3 = i * c2Pi / n;

//first
e.x = cos(theta2) * cos(theta3);
e.y = sin(theta2);
e.z = cos(theta2) * sin(theta3);
p.x = c.x + r * e.x;
p.y = c.y + r * e.y;
p.z = c.z + r * e.z;

Vertices[vertCount].normal = e;
Vertices[vertCount].position = p;
vertCount++;

//second
e.x = cos(theta1) * cos(theta3);
e.y = sin(theta1);
e.z = cos(theta1) * sin(theta3);
p.x = c.x + r * e.x;
p.y = c.y + r * e.y;
p.z = c.z + r * e.z;

Vertices[vertCount].normal = e;
Vertices[vertCount].position = p;
vertCount++;
}
}
}

void pSkyDome::pDraw()
{
glBegin(GL_TRIANGLE_STRIP);//GL_QUAD_STRIP
for (int i=0; i<NumVertices; i++)
{
if (Vertices[i] == NULL) {
glEnd();
glBegin(GL_TRIANGLE_STRIP);
}
else {
glNormal3f( Vertices[i].normal.x,
Vertices[i].normal.y,
Vertices[i].normal.z);
glVertex3f( Vertices[i].position.x,
Vertices[i].position.y,
Vertices[i].position.z);
}
}
glEnd();
}
[/source lan=c++]

Maybe it is usefull to any one, maybe not :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this