# Creating a sphere directly and with a list problem

This topic is 4377 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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

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

for (int i=0; i<NumVertices; i++)
{
glNormal3f( Vertices.normal.x,
Vertices.normal.y,
Vertices.normal.z);

if(i==17)//Vertices.position.y < 0)
{glColor3f(1.0,0.0,0.0);}
else
{glColor3f(1.0,1.0,1.0);}
glVertex3f( Vertices.position.x,
Vertices.position.y,
Vertices.position.z);
}

glEnd();


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

##### 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 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 == NULL)	{		        glEnd();			glBegin(GL_TRIANGLE_STRIP);		}		else {		 glNormal3f( Vertices.normal.x,					 Vertices.normal.y,					 Vertices.normal.z);			 glVertex3f( Vertices.position.x,					 Vertices.position.y,					 Vertices.position.z);			}	}	glEnd();}[/source lan=c++]Maybe it is usefull to any one, maybe not :)

1. 1
2. 2
Rutin
21
3. 3
4. 4
5. 5
frob
13

• 9
• 13
• 10
• 9
• 17
• ### Forum Statistics

• Total Topics
632601
• Total Posts
3007349

×