From 899f7f1d47cc205a9b25883edf0c2ddbc87efd11 Mon Sep 17 00:00:00 2001 From: N0NameN0 Date: Wed, 6 Jan 2010 14:08:06 +0000 Subject: [PATCH] [ADD] Adding GRRLIB_DrawCube [CHG] Adding a cube to the primitive/light demo code --- GRRLIB/GRRLIB/GRRLIB_3D.c | 54 ++++++++++++++++++++++++++ GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h | 1 + examples/3D_LightDiffuse/source/main.c | 4 ++ 3 files changed, 59 insertions(+) diff --git a/GRRLIB/GRRLIB/GRRLIB_3D.c b/GRRLIB/GRRLIB/GRRLIB_3D.c index aa06751..dcd5561 100644 --- a/GRRLIB/GRRLIB/GRRLIB_3D.c +++ b/GRRLIB/GRRLIB/GRRLIB_3D.c @@ -320,3 +320,57 @@ void GRRLIB_DrawSphere(f32 r, int lats, int longs, bool filled) { } } +/** + * Draw a Cube (with normal). + * @param size Size of the cube edge. + * @param filled Wired or not. +*/ +void GRRLIB_DrawCube(f32 size, bool filled) +{ + static f32 n[6][3] = + { + {-1.0, 0.0, 0.0}, + {0.0, 1.0, 0.0}, + {1.0, 0.0, 0.0}, + {0.0, -1.0, 0.0}, + {0.0, 0.0, 1.0}, + {0.0, 0.0, -1.0} + }; + static int faces[6][4] = + { + {0, 1, 2, 3}, + {3, 2, 6, 7}, + {7, 6, 5, 4}, + {4, 5, 1, 0}, + {5, 6, 2, 1}, + {7, 4, 0, 3} + }; + f32 v[8][3]; + int i; + + v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2; + v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2; + v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2; + v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2; + v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2; + v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2; + + for (i = 5; i >= 0; i--) { + if(filled) GX_Begin(GX_QUADS, GX_VTXFMT0,4); + else GX_Begin(GX_LINESTRIP, GX_VTXFMT0,5); + GX_Position3f32(v[faces[i][0]][0],v[faces[i][0]][1],v[faces[i][0]][2] ); + GX_Normal3f32(n[i][0], n[i][1], n[i][2]); + GX_Position3f32(v[faces[i][1]][0],v[faces[i][1]][1],v[faces[i][1]][2]); + GX_Normal3f32(n[i][0], n[i][1], n[i][2]); + GX_Position3f32(v[faces[i][2]][0],v[faces[i][2]][1],v[faces[i][2]][2]); + GX_Normal3f32(n[i][0], n[i][1], n[i][2]); + GX_Position3f32(v[faces[i][3]][0],v[faces[i][3]][1],v[faces[i][3]][2]); + GX_Normal3f32(n[i][0], n[i][1], n[i][2]); + if(!filled) { + GX_Position3f32(v[faces[i][0]][0],v[faces[i][0]][1],v[faces[i][0]][2]); + GX_Normal3f32(n[i][0], n[i][1], n[i][2]); + } + GX_End(); + } +} + diff --git a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h index a896765..9e668f3 100644 --- a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h +++ b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h @@ -152,6 +152,7 @@ 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); +void GRRLIB_DrawCube(f32 size, 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 12e39d0..8af4894 100644 --- a/examples/3D_LightDiffuse/source/main.c +++ b/examples/3D_LightDiffuse/source/main.c @@ -53,6 +53,10 @@ int main() { 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); + GRRLIB_ObjectView(-4,-4,0, a,a*2,-a*3, 1,1,1); + GRRLIB_DrawCube(2.0f, 0); + GRRLIB_ObjectView(4,-4,0, a,-a*2,-a*3, 1,1,1); + GRRLIB_DrawCube(2.0f, 1);