From d352bd354d87630773853facc8369b56b07a4d5a Mon Sep 17 00:00:00 2001 From: N0NameN0 Date: Wed, 6 Jan 2010 13:35:27 +0000 Subject: [PATCH] [ADD] Adding GRRLIB_DrawSphere [CHG] Light and primitive demo to add a sphere --- GRRLIB/GRRLIB/GRRLIB_3D.c | 34 ++++++++++++++++++++++++++ GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h | 1 + examples/3D_LightDiffuse/source/main.c | 9 ++++--- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/GRRLIB/GRRLIB/GRRLIB_3D.c b/GRRLIB/GRRLIB/GRRLIB_3D.c index 813fa12..aa06751 100644 --- a/GRRLIB/GRRLIB/GRRLIB_3D.c +++ b/GRRLIB/GRRLIB/GRRLIB_3D.c @@ -286,3 +286,37 @@ void GRRLIB_DrawTorus(f32 r, f32 R, int nsides, int rings, bool filled){ } } +/** + * Draw a Sphere (with normal). + * @param r Radius of the Sprhere. + * @param lats Number of lattitudes. + * @param longs Number of Longitutes. + * @param filled Wired or not. +*/ +void GRRLIB_DrawSphere(f32 r, int lats, int longs, bool filled) { + int i,j; + + for(i = 0; i <= lats; i++) { + f32 lat0 = M_PI * (-0.5F + (f32) (i - 1) / lats); + f32 z0 = sin(lat0); + f32 zr0 = cos(lat0); + + f32 lat1 = M_PI * (-0.5F + (f32) i / lats); + f32 z1 = sin(lat1); + f32 zr1 = cos(lat1); + if(filled) GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 2*(longs+1)); + else GX_Begin(GX_LINESTRIP, GX_VTXFMT0, 2*(longs+1)); + for(j = 0; j <= longs; j++) { + f32 lng = 2 * M_PI * (f32) (j - 1) / longs; + f32 x = cos(lng); + f32 y = sin(lng); + + GX_Position3f32(x * zr0 * r, y * zr0 * r, z0 * r); + GX_Normal3f32(x * zr0 * r, y * zr0 * r, z0 * r); + GX_Position3f32(x * zr1 * r, y * zr1 * r, z1 * r); + GX_Normal3f32(x * zr1 * r, y * zr1 * r, z1 * r); + } + GX_End(); + } +} + diff --git a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h index e8f8816..a896765 100644 --- a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h +++ b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h @@ -151,6 +151,7 @@ void GRRLIB_InitLight(u8 id, guVector lpos, u32 lcol); void GRRLIB_LightOff(void); void GRRLIB_LightSwitch(u8 id, u32 ambcol, u32 matcol, u8 colsrc); void GRRLIB_DrawTorus(f32 r, f32 R, int nsides, int rings, bool filled); +void GRRLIB_DrawSphere(f32 r, int lats, int longs, bool filled); //------------------------------------------------------------------------------ // GRRLIB_Freetype.c - FreeType function for GRRLIB diff --git a/examples/3D_LightDiffuse/source/main.c b/examples/3D_LightDiffuse/source/main.c index 0815357..12e39d0 100644 --- a/examples/3D_LightDiffuse/source/main.c +++ b/examples/3D_LightDiffuse/source/main.c @@ -50,9 +50,12 @@ int main() { GRRLIB_LightSwitch(GX_LIGHT0|GX_LIGHT1|GX_LIGHT2,RGBA(Amb,Amb,Amb,0xFF),0x808080FF,0); GRRLIB_ObjectView(0,0,0, a,a*2,a*3, 1,1,1); - GRRLIB_DrawTorus(0.4f, 2.0f, 20, 50, 0); - GRRLIB_ObjectView(0,0,0, -a,-a*2,-a*3, 1,1,1); - GRRLIB_DrawTorus(0.8f, 4.0f, 20, 50, 1); + GRRLIB_DrawTorus(0.4f, 3.0f, 20, 50, 1); + GRRLIB_ObjectView(0,0,0, -a,a*2,-a*3, 1,1,1); + GRRLIB_DrawSphere(2.0f, 30, 30, 0); + + + a+=0.5f;