Jump to content
  • Advertisement
Sign in to follow this  
Endar

OpenGL Drawing a sphere

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
For your sphere its simple using quadric :

GLUquadricObj *Quadric1 = gluNewQuadric();
gluSphere(Quadric1, 3, 50, 50);

3 = radius
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
You aren't converting the angles into radians; use sin(i*PI/180) etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!