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

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[12][3] = {    
    {-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[20][3] = { 
    {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[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
    a[0]/=d; a[1]/=d; a[2]/=d;
}

void drawtri(GLfloat *a, GLfloat *b, GLfloat *c, int div, float r) {
    if (div<=0) {
        glNormal3fv(a); glVertex3f(a[0]*r, a[1]*r, a[2]*r);
        glNormal3fv(b); glVertex3f(b[0]*r, b[1]*r, b[2]*r);
        glNormal3fv(c); glVertex3f(c[0]*r, c[1]*r, c[2]*r);
    } else {
        GLfloat ab[3], ac[3], bc[3];
        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++)
        drawtri(vdata[tindices[i][0]], vdata[tindices[i][1]], vdata[tindices[i][2]], ndiv, radius);
    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

The first few sections of this page may be relevant.

@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, networking, learning, and sharing knowledge.