PDA

View Full Version : how to draw a sphere?

chen
03-21-2006, 01:00 PM
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 &amp;#0124;&amp;#0124; stacks < 2 &amp;#0124;&amp;#0124; radius <= 0.0) {
cout<<"MyGluSphere() - bad parameters";
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&amp; n = N[0][i];
myglNormal3f(n.x, n.y, n.z);
}
myglEnd();

// middle quad strips
for (k=0; k<stacks-2; ++k) {
myglBegin(MYGL_POLYGON);
for (s=slices; s>=0; --s) {
i = (s == slices) ? 0 : s;
const Vector3&amp; n1 = N[k][i];
myglNormal3f(n1.x, n1.y, n1.z);
const Vector3&amp; 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&amp; 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

RigidBody
03-21-2006, 01:04 PM
hm...you do clear the depth buffer, don't you?

chen
03-21-2006, 01:14 PM
i'm not using depth buffer

RigidBody
03-21-2006, 01:35 PM
i think you should.

chen
03-21-2006, 02:14 PM
Do you have experience in implementation of openGL function?

RigidBody
03-21-2006, 03:09 PM
well...yes, i would say so.

chen
03-21-2006, 03:12 PM
can i e-mail you my code?

RigidBody
03-21-2006, 03:19 PM
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.

knackered
03-21-2006, 04:44 PM
well a depth buffer isn't necessary for a simple sphere, just enable back face culling.

ioquan
03-21-2006, 07:12 PM
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?

chen
03-22-2006, 01:05 AM
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 myglLoadMatrix(const GLfloat *m);
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 baseRadius, float topRadius, float height, 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

RigidBody
03-22-2006, 02:19 AM
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?

chen
03-22-2006, 03:04 AM
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.

Mikkel Gjoel
03-24-2006, 08:04 PM
You might want to write a working OpenGL-program for reference, and then test it with your own implementation afterwards.

chen
03-25-2006, 12:48 AM
I wrote a working OpenGL program, but it doesn't work with my own implementation

Mikkel Gjoel
03-25-2006, 07:06 AM
But then it's really not an OpenGL-question now is it? :p 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.