I would like to create my own function for sphere. I already now how to create particular points for sphere, but how to join them in polygons?
glBegin(GL_TRIANGLES);
glNormal3f(...); // put your coordinates here
glVertex3f(...);
glEnd();
Or you can use GL_TRIANGLE_STRIP. Refer to the “Red Book” or other OpenGL litterature - this is pretty basic stuff
Originally posted by Lelik:
[b]glBegin(GL_TRIANGLES); glNormal3f(...); // put your coordinates here glVertex3f(...); glEnd();
Or you can use GL_TRIANGLE_STRIP. Refer to the “Red Book” or other OpenGL litterature - this is pretty basic stuff [/b]
I know what you mean (I have tried this way), but this is not so easy because fundamental meaning has a sequence of creating points…
this is pretty basic stuff
All things are basic, once you understand them.
// This is sloppy and bad form, but
// I hope you get the idea. You can stuff
// all this neatly away in a class, for example.
// I hope I didn't make any mistakes, but please
// correct me if I did.
const int numStacks = 16;
const int numSlices = 32;
struct Vector3 { float x,y,z; };
Vector3 verts[numStacks + 1][numSlices + 1];
// I repeated a creation function here for
// convenience. This creates a sphere of unit
// radius.
void createTheSphere()
{
// Set the sphere positions.
for( int i = 0; i <= numStacks; i++ )
{
float phi = ((2.0*i)/numStacks - 1)*PI/2;
for( int j = 0; j < numSlices; j++ )
{
float theta = (j*2.0*PI)/numSlices;
verts[i][j] = Vector3(cos(theta)*cos(phi), sin(theta)*cos(phi), sin(phi));
}
// Wrap around.
verts[i][numSlices] = verts[i][0];
}
}
void drawTheSphere( Vector3 scale )
{
// Apply an arbitrary scale to the modelview
// matrix for kicks.
// This can be omitted if a unit radius will
// do.
glPushMatrix();
glScalef( scale.x, scale.y, scale.z );
// Draw the tri-strips for each stack.
for( int i = 0; i < numStacks; i++ )
{
// Next stack index.
int i2 = i + 1;
// Draw a single tri-strip.
glBegin( GL_TRIANGLE_STRIP );
for( int j = 0; j <= numSlices; j++ )
{
// You can reverse the order here
// for different winding orders.
glVertex3fv( &verts[i2][j].x );
glVertex3fv( &verts[i][j].x );
}
glEnd();
}
// Back to the previous modelview matrix.
glPopMatrix();
}
edit:
Made a correction in the create sphere function.
Thanks Q, I will try it. I didn’t want ready-to-use code, but I am thankful
yeah, You’re right, all things are basic, once understood…
Greets,
Ray
Most people prefer code to words, so I thought it would illustrate the concept better. You might experiment with an index list for the tri-strips, and use glDrawElements to draw (if speed is important). But for a single sphere, it’s no biggie.
yeah, sometimes code is better than words description, especially if presented code is possibly short and simply. Personally, I avoid hard to understand examples and that’s why I prefer normal- words description. Of course, your example is bright as sun well, we will see it in practice
For words, you would do well to follow Lelik’s advice and read the Red Book, chapter 2 specifically.
http://fly.cc.fer.hr/~unreal/theredbook/chapter02.html
And I have since tested the code, so I know it works. If you have trouble with your setup, don’t hesitate to ask.
I have looked at “Red book”, but I think so slow, that it wasn’t evident for me. I need more imagination…
Hi Q, your code works great! Thanks again