h1

I made it work zomg happy :3

September 17, 2008

I got this sucka working!  That’s right, a sphere NOT MADE WITH LATITUDE AND LONGITUDE.  Great pwn4ge, what is this miracle?  I’ll tell you.  Basically, what I’ve done is taken an icosahedron and recursively subdivided with level of detail.  You may have seen something like this in the OpenGL red book.  But I swear I didn’t see it in there.  I later found out it was in there.  It turned out their method was better than mine, so I went over to optimizing theirs.  I have my result here, and with a precalculated icosahedron mesh for the tesselating.

Here: (sphere.c)

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/gl.h>

#define _A 0.525731112119133606f
#define _B 0.850650808352039932f

static unsigned int icosa_indices[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}
};

static float icosa_verts[12][3]=
{
{-_A,0.0,_B},{_A,0.0,_B},{-_A,0.0,-_B},{_A,0.0,-_B},
{0.0,_B,_A},{0.0,_B,-_A},{0.0,-_B,_A},{0.0,-_B,-_A},
{_B,_A,0.0},{-_B,_A,0.0},{_B,-_A,0.0},{-_B,-_A,0.0}
};

static void normalize_vert(float *a)
{
float d=sqrtf(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
a[0]/=d; a[1]/=d; a[2]/=d;
}

static void draw_recursive_tri(float *a,float *b,float *c,unsigned 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
{
register unsigned int i;
float ab[3],ac[3],bc[3];
for (i=0; i<3; i++)
{
ab[i]=(a[i]+b[i])/2.0f;
ac[i]=(a[i]+c[i])/2.0f;
bc[i]=(b[i]+c[i])/2.0f;
}

normalize_vert(ab);
normalize_vert(ac);
normalize_vert(bc);

draw_recursive_tri(a,ab,ac,div-1,r);
draw_recursive_tri(b,bc,ab,div-1,r);
draw_recursive_tri(c,ac,bc,div-1,r);
draw_recursive_tri(ab,bc,ac,div-1,r);
}
}

void rat_draw_sphere(unsigned int detail,float radius)
{
register unsigned int i;
glBegin(GL_TRIANGLES);
for (i=0; i<20; i++)
draw_recursive_tri
(
icosa_verts[icosa_indices[i][0]],
icosa_verts[icosa_indices[i][1]],
icosa_verts[icosa_indices[i][2]],
detail,radius
);
glEnd();
}

Advertisements

One comment

  1. This is awesome man!!!



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: