# OpenGL Drawing a sphere

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

## Recommended Posts

First question: Does anyone have any tips or a way to get started with drawing a shpere in open gl? Second: With my Shape and Cube class so far, the objects are made up of vertices and I just use opengl to draw the quads or whatever is needed using the vertices. Should I change this, so, just say a cube is made up of 6 object of a different class, representing the sides, but have their own normal? Because right now, I'm not taking the normal into account. Does this seem like a large failing?

##### Share on other sites

50 and 50 = Number of slides in longitude and altitude

second : Your cube object is perfect. In your object you have your vertex array, normal array, etc...
Dont split each faces in 6 object. The cube is one object.

##### Share on other sites
Daivuk: You said a vertex array and a normal array. My vertex array is size 8, one for each vertex (obviously :D). Does that mean that I should have another array of size 8 of Vectors for the normals? Eg. One for each vertex? Or should I have an array of 6? One for each side of the cube?

Also, with the sphere, I'm attempting a 3d editor (prob never going to go anywhere, but I'm learning stuff, so its definatly not a loss :D), so is there a way to access the vertices of the sphere, or is there another way to draw a good enough one manually?

##### Share on other sites
No one has any ideas?

##### Share on other sites
First : No, each vertex should have a Normal vector :P.

Second : There is no way to access the vertice of a quadric object. So I understand you want to create your onw sphere in the core way :P

So there it is, using cos and sin :

void DrawSphere( float Radius, float Altitude, float Longitude){    for (float i=0; i<=360; i+=360/Longitude)    {        for (float j=-90; j<=90; j+=180/Altitude)        {            Vertex.x = cos(i)*Radius *cos(j);            Vertex.y = sin(i)*Radius *cos(j);            Vertex.z = sin(j)*radius;        }    }}

It should look like something like this :P
chear

##### Share on other sites
Its looking pretty good, except for one problem: it looks like the sphere is complete, except that it is divided up into 4 parts that are seperated.

Imagine you have an orange. From the top, cut it in half. Then cut the halves into half, so you end up with 4 vertical quarters. Then attempt to stand them up and place them 5 centimetres from each other and rotate them all according to the invisible axis at point 0,0,0 between them :D. That's whats happening to the sphere.

It definatly looks like it has all the points, but the quarters are just seperated like that.

Here's the source, although I very much doubt the beginning and end of the function have anything to do with it.

/** * Draw the vertices of the polygon. */void drawVertices(){	// Store the current point size	float p_size;	glGetFloatv(GL_POINT_SIZE, &p_size);	// Change the point size	glPointSize(vertex_size);	// Store the previous color	CColor temp;	temp.setToOpenGLColor();	// Set vertex color	vertex_color.setOpenGLColor();	// Draw the vertices	glBegin(GL_POINTS);					//for(int i=0; i < vertex_buffer.size(); i++)	//		glVertex3f(vertex_buffer.point.x, vertex_buffer.point.y, vertex_buffer.point.z);	CVertex3d Vertex;	float Radius = 2.0f;	float Longitude = 5.0f;	float Altitude = 5.0f;	for (float i=0; i<=360; i+=360/Longitude)	{		for (float j=-90; j<=90; j+=180/Altitude)		{	        	Vertex.x = cos(i)*Radius *cos(j);	        	Vertex.y = sin(i)*Radius *cos(j);	        	Vertex.z = sin(j)*Radius;							glVertex3f(Vertex.x, Vertex.y, Vertex.z);		}	}	glEnd();	// Finish drawing	// Restore the point size	glPointSize(p_size);	// Restore previous color	temp.setOpenGLColor();}

##### Share on other sites
You aren't converting the angles into radians; use sin(i*PI/180) etc.

##### Share on other sites
Thanks very much! Working perfectly!!!

Thanks everyone!

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 14
• 9
• 10
• 12
• 17
• ### Forum Statistics

• Total Topics
632910
• Total Posts
3009177
• ### Who's Online (See full list)

There are no registered users currently online

×