Not Yet Answered # create a sphere using triangles

VernonDozier 2,218 Discussion Starter cool_zephyr 7 firstPerson 761 VernonDozier 2,218 Discussion Starter cool_zephyr 7 VernonDozier 2,218

1

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.

0

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??

0

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.

0

>> 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.

0

@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

0

@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.

*Edited 4 Years Ago by VernonDozier*: typo

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`

I don’t want at this stage work on a big separate project as I've already got plenty ...

I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:

```
try
{
Runtime run = Runtime.getRuntime();
Process pr = ...
```