# how to draw a sphere?

I need to draw a sphere in my project, without using the openGL function, because my project is to implement the openGL graphics pipe-line.
I use this code for drawing the sphere:

``````
typedef vector<Vector3> vectors;
void mygluSphere(float radius, int slices, int stacks)
{
if (slices < 3 &#0124;&#0124; stacks < 2 &#0124;&#0124; radius <= 0.0) {
return;
}

// calculate all the x,y coordinates (at radius 1)
vector<vectors> N(stacks - 1);
int k, s, i;
double z, a, r;
for (k=0; k<stacks-1; ++k) {
N[k].resize(slices);
a = PI * (-0.5 + (1.0 + k) / stacks);
z = sin(a);
r = cos(a);
for (s=0; s<slices; ++s) {
a = PI * 2 * s / slices;
N[k][s].set(cos(a) * r, sin(a) * r, z);
}
}

// bottom triangle fan
myglBegin(MYGL_POLYGON);
myglNormal3f(0.0, 0.0, -1.0);
for (s=slices; s>=0; --s) {
i = (s == slices) ? 0 : s;
const Vector3& n = N[0][i];
myglNormal3f(n.x, n.y, n.z);
}
myglEnd();

for (k=0; k<stacks-2; ++k) {
myglBegin(MYGL_POLYGON);
for (s=slices; s>=0; --s) {
i = (s == slices) ? 0 : s;
const Vector3& n1 = N[k][i];
myglNormal3f(n1.x, n1.y, n1.z);
const Vector3& n2 = N[k + 1][i];
myglNormal3f(n2.x, n2.y, n2.z);
}
myglEnd();
}

// top triangle fan
myglBegin(MYGL_POLYGON);
for (s=0; s<=slices; ++s) {
i = (s == slices) ? 0 : s;
const Vector3& n = N[stacks - 2][i];
myglNormal3f(n.x, n.y, n.z);
}
myglEnd();
}

``````

But when i’m using it, after some rotation operation, some of the sphere’s polygons, are hidden.

Can some one check this code and found what is wrong, or perhaps suggest a better one?

Chen

hm…you do clear the depth buffer, don’t you?

i’m not using depth buffer

i think you should.

Do you have experience in implementation of openGL function?

well…yes, i would say so.

can i e-mail you my code?

as i pointed out, the problem seems to be that you do not use a depth buffer.

to learn how a depth buffer works, draw a simple cube with each side in a different color. compare the results that you get with and without using depth buffer.

well a depth buffer isn’t necessary for a simple sphere, just enable back face culling.

Dear god, save all of the vertex and index data ahead of time, and just use glDrawElements or whetever when you want to draw it. Why recompute all of the vertex data every frame?

As i wrote before, i have to implement the openGL graphics pipe-line. this is part of my header file:

``````
void myglMatrixMode(int ModeChoice);
void myglPushMatrix();
void myglPopMatrix();

void myglClearColor(float color1, float color2, float color3, float color4);

void myglOrtho(float fleft, float fright, float fbottom, float ftop, float fnear, float ffar);

void myglRotatef(float angle, float ux, float uy, float uz);
void myglTranslatef(float dx, float dy, float dz);
void myglScaled(float sx, float sy, float sz);

void myglColor3f(float r, float g, float b);
void myglNormal3f(float x, float y, float z);
void myglVertex3f(float tx, float ty, float tz);
void myglVertex2f(float tx, float ty);

void myglBegin(int tempiDraw);
void myglEnd();

void myglFlush();
void myglClear(int clearval);

void disableClip();
void enableClip();

void myglEnable(int enableType);
void myglLightfv(int lightNum, int component, float *value);
void myglLightf(int lightNum, int component, float value);
void myglMaterialfv(int faceType, int property, float* values);
void myglMaterialf(int faceType, int property, float value);

// GLU and GLUT functions
void mygluPerspective(float viewAngle, float aspect, float N, float F);
void mygluLookAt(float eyex, float eyey, float eyez,
float lookx, float looky, float lookz,
float upx, float upy, float upz);

void mygluSphere(float radius, int slices, int stacks);
void mygluCylinder(float s, int c, float h);

void myglutSwapBuffers();
void myglutPostRedisplay();
``````

SO i can’t use regular openGL functions, like glCullFace, because it wont work.
This is the reason, i’m asking if there is some one that i can send him my code, so he could look at it, and tell me what is wrong with it.

Chen

so why don’t you make a myglCullFace function in addition to your other mygl* functions?

what exactly do you intend with your own gl implementation, and why doesn’t it have a depth buffer?

My mistake, i’m using depth buffer (sorry, but i’m not the only one that write this project), that implemented in a class that compatible with OpenGL’s glDrawPixels.

You might want to write a working OpenGL-program for reference, and then test it with your own implementation afterwards.

I wrote a working OpenGL program, but it doesn’t work with my own implementation

But then it’s really not an OpenGL-question now is it? I do know a couple of guys able to bugfix code without having ever seen it, but they all bought medium-sized tropical islands to live on…

I’m aware that you offer to send your code to people but I doubt you’ll get anyone to bugfix your entire OpenGL-implementation for you. If you have questions as to the way OpenGL is working, you’re more than welcome to ask them though.