could anyone please help me to create a sphere using triangles?? i've searched a lot but all of them only describe creating a sphere using some library packages

Answered by VernonDozier 2,218 in a post from

Forget about the programming part for a second. How do you propose to create something curved(sphere) with something that only has straight edges(triangle)? It seems geometrically impossible. Perhaps you are trying to create something "close to" a sphere?

>> all of them only describe creating a sphere using …

Answered by mrnutty 761 in a post from

Just from a little googling, here is the code in the OpenGL Red Book.

#include <math.h>

#define X .525731112119133606
#define Z .850650808352039932

static GLfloat vdata = {
{-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
{0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, …
Answered by gusano79 247 in a post from

All 9 Replies

Forget about the programming part for a second. How do you propose to create something curved(sphere) with something that only has straight edges(triangle)? It seems geometrically impossible. Perhaps you are trying to create something "close to" a sphere?

>> all of them only describe creating a sphere using some library packages

Which is to be expected. I imagine you're going to have to elaborate of the what / where / why of the question, particularly if this is a graphics program rather than a console program.

we are studying graphics under C, so I'm thinking of creating a sphere as an assignment..could you help please to get me the algorithm and the math part??

Just from a little googling, here is the code in the OpenGL Red Book.

#include <math.h>

#define X .525731112119133606
#define Z .850650808352039932

static GLfloat vdata = {
{-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
{0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
{Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
};
static GLuint tindices = {
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };

void normalize(GLfloat *a) {
GLfloat d=sqrt(a*a+a*a+a*a);
a/=d; a/=d; a/=d;
}

void drawtri(GLfloat *a, GLfloat *b, GLfloat *c, int div, float r) {
if (div<=0) {
glNormal3fv(a); glVertex3f(a*r, a*r, a*r);
glNormal3fv(b); glVertex3f(b*r, b*r, b*r);
glNormal3fv(c); glVertex3f(c*r, c*r, c*r);
} else {
GLfloat ab, ac, bc;
for (int i=0;i<3;i++) {
ab[i]=(a[i]+b[i])/2;
ac[i]=(a[i]+c[i])/2;
bc[i]=(b[i]+c[i])/2;
}
normalize(ab); normalize(ac); normalize(bc);
drawtri(a, ab, ac, div-1, r);
drawtri(b, bc, ab, div-1, r);
drawtri(c, ac, bc, div-1, r);
drawtri(ab, bc, ac, div-1, r);  //<--Comment this line and sphere looks really cool!
}
}

void drawsphere(int ndiv, float radius=1.0) {
glBegin(GL_TRIANGLES);
for (int i=0;i<20;i++)
glEnd();
}

You can study it.

>> could you help please to get me the algorithm and the math part??

I gave you the math part. It's impossible to create a sphere using triangles mathematically. I think I know what you mean, but I'm not about to assume I'm right, write up a long post, then find out my assumption was wrong. You're going to have to ask a more precise question.

Then again... someone just posted some stuff he Googled. Whether it's what you're looking for is something I don't know.

@firstperson could you please explain the code too?? i'm not able to grasp what the code is doing
@Vernon i have to create a sphere using only the primitives like lines, circles, triangles etc. so i can't use any library..
thank you guys for your time..i think it's too complicated for me to do it

@firstperson could you please explain the code too?? i'm not able to grasp what the code is doing
@Vernon i have to create a sphere using only the primitives like lines, circles, triangles etc. so i can't use any library..
thank you guys for your time..i think it's too complicated for me to do it

You can't understand the code he posted if you are unfamiliar with OpenGL. You mentioned "graphics". You didn't mention anything about OpenGL.

As far as not using any libraries, OpenGL IS a library (the "L" stands for "library"). You have to use a library in order to draw the graphics, and that includes the "primitives" like straight lines, triangles, etc. The very fact that you are using C++ means you'll be using libraries. It's unavoidable. Assuming you are a student, I suggest sitting down with your instructor and getting a better understanding of the assignment.

yeah thanks may be i should do that

@gusano79 hey thanks a lot..i was searching for this thing

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.