[ADD] Added GRRLIB_DrawTorus, our first primitive !!!

This commit is contained in:
N0NameN0 2010-01-06 09:25:28 +00:00
parent 42d7917d0d
commit 864f2610e9
4 changed files with 67 additions and 28157 deletions

View file

@ -237,3 +237,52 @@ void GRRLIB_LightSwitch(u8 id, u32 ambcol, u32 matcol, u8 colsrc) {
GX_SetChanAmbColor(GX_COLOR0A0, (GXColor) { R(ambcol), G(ambcol), B(ambcol), A(ambcol)}); GX_SetChanAmbColor(GX_COLOR0A0, (GXColor) { R(ambcol), G(ambcol), B(ambcol), A(ambcol)});
GX_SetChanMatColor(GX_COLOR0A0, (GXColor) { R(matcol), G(matcol), B(matcol), A(matcol)}); GX_SetChanMatColor(GX_COLOR0A0, (GXColor) { R(matcol), G(matcol), B(matcol), A(matcol)});
} }
/**
* Draw a Torus (with normal).
* @param r Radius of the ring.
* @param R Radius of the torus.
* @param nsides Number of faces per ring.
* @param rings Number of rings.
* @param filled Wired or not.
*/
void GRRLIB_DrawTorus(f32 r, f32 R, int nsides, int rings, bool filled){
int i, j;
f32 theta, phi, theta1;
f32 cosTheta, sinTheta;
f32 cosTheta1, sinTheta1;
f32 ringDelta, sideDelta;
ringDelta = 2.0 * M_PI / rings;
sideDelta = 2.0 * M_PI / nsides;
theta = 0.0;
cosTheta = 1.0;
sinTheta = 0.0;
for (i = rings - 1; i >= 0; i--) {
theta1 = theta + ringDelta;
cosTheta1 = cos(theta1);
sinTheta1 = sin(theta1);
if(filled) GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 2*(nsides+1));
else GX_Begin(GX_LINESTRIP, GX_VTXFMT0, 2*(nsides+1));
phi = 0.0;
for (j = nsides; j >= 0; j--) {
f32 cosPhi, sinPhi, dist;
phi += sideDelta;
cosPhi = cos(phi);
sinPhi = sin(phi);
dist = R + r * cosPhi;
GX_Position3f32(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi);
GX_Normal3f32(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
GX_Position3f32(cosTheta * dist, -sinTheta * dist, r * sinPhi);
GX_Normal3f32(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
}
GX_End();
theta = theta1;
cosTheta = cosTheta1;
sinTheta = sinTheta1;
}
}

View file

@ -150,6 +150,7 @@ void GRRLIB_SetTexture(GRRLIB_texImg *tex, bool rep);
void GRRLIB_InitLight(u8 id, guVector lpos, u32 lcol); void GRRLIB_InitLight(u8 id, guVector lpos, u32 lcol);
void GRRLIB_LightOff(void); void GRRLIB_LightOff(void);
void GRRLIB_LightSwitch(u8 id, u32 ambcol, u32 matcol, u8 colsrc); void GRRLIB_LightSwitch(u8 id, u32 ambcol, u32 matcol, u8 colsrc);
void GRRLIB_DrawTorus(f32 r, f32 R, int nsides, int rings, bool filled);
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// GRRLIB_Freetype.c - FreeType function for GRRLIB // GRRLIB_Freetype.c - FreeType function for GRRLIB

View file

@ -1,10 +1,6 @@
/*=========================================== /*===========================================
NoNameNo NoNameNo
Simple Diffuse Light Sample Code Simple Flat 3D cube
With 3 Light sources.
Note : This torus hardcoded obj will be removed
when we will have nice primitive functions.
============================================*/ ============================================*/
#include <grrlib.h> #include <grrlib.h>
@ -18,41 +14,6 @@ when we will have nice primitive functions.
extern Mtx _GRR_view; extern Mtx _GRR_view;
typedef struct GRRLIB_3dObj {
int NbFace;
guVector *Pos;
guVector *Tex;
guVector *Nrm;
int *FacPos;
int *FacTex;
int *FacNrm;
} GRRLIB_3dObj;
#include "torus.h"
void GRRLIB_Draw3dObj(GRRLIB_3dObj obj, u32 col, bool tex, bool norm) {
int i;
GX_Begin(GX_TRIANGLES, GX_VTXFMT0, obj.NbFace*3);
for(i=0;i<obj.NbFace*3;i+=3) {
GX_Position3f32(obj.Pos[obj.FacPos[i]-1].x,obj.Pos[obj.FacPos[i]-1].y,-obj.Pos[obj.FacPos[i]-1].z);
if(col) GX_Color1u32 (col);
if(norm) GX_Normal3f32(obj.Nrm[obj.FacNrm[i]-1].x,obj.Nrm[obj.FacNrm[i]-1].y,-obj.Nrm[obj.FacNrm[i]-1].z);
if(tex) GX_TexCoord2f32(obj.Tex[obj.FacTex[i]-1].x, obj.Tex[obj.FacTex[i]-1].y);
GX_Position3f32(obj.Pos[obj.FacPos[i+1]-1].x,obj.Pos[obj.FacPos[i+1]-1].y,-obj.Pos[obj.FacPos[i+1]-1].z);
if(col) GX_Color1u32 (col);
if(norm) GX_Normal3f32(obj.Nrm[obj.FacNrm[i+1]-1].x,obj.Nrm[obj.FacNrm[i+1]-1].y,-obj.Nrm[obj.FacNrm[i+1]-1].z);
if(tex) GX_TexCoord2f32(obj.Tex[obj.FacTex[i+1]-1].x, obj.Tex[obj.FacTex[i+1]-1].y);
GX_Position3f32(obj.Pos[obj.FacPos[i+2]-1].x,obj.Pos[obj.FacPos[i+2]-1].y,-obj.Pos[obj.FacPos[i+2]-1].z);
if(col) GX_Color1u32 (col);
if(norm) GX_Normal3f32(obj.Nrm[obj.FacNrm[i+2]-1].x,obj.Nrm[obj.FacNrm[i+2]-1].y,-obj.Nrm[obj.FacNrm[i+2]-1].z);
if(tex) GX_TexCoord2f32(obj.Tex[obj.FacTex[i+2]-1].x, obj.Tex[obj.FacTex[i+2]-1].y);
}
GX_End();
}
int main() { int main() {
float a=0; float a=0;
u8 Amb=0x80; u8 Amb=0x80;
@ -61,7 +22,6 @@ int main() {
GRRLIB_Init(); GRRLIB_Init();
WPAD_Init(); WPAD_Init();
Obj3dInittorus();
GRRLIB_texImg *tex_font = GRRLIB_LoadTexture(font); GRRLIB_texImg *tex_font = GRRLIB_LoadTexture(font);
GRRLIB_InitTileSet(tex_font, 16, 16, 32); GRRLIB_InitTileSet(tex_font, 16, 16, 32);
@ -82,7 +42,6 @@ int main() {
GRRLIB_3dMode(0.1,1000,45,0,0,1); GRRLIB_3dMode(0.1,1000,45,0,0,1);
GRRLIB_ObjectView(0,0,0, a,a*2,a*3, 3,3,3);
GRRLIB_InitLight(GX_LIGHT0, (guVector){-6, 0, zlight}, 0xFF0000FF); GRRLIB_InitLight(GX_LIGHT0, (guVector){-6, 0, zlight}, 0xFF0000FF);
GRRLIB_InitLight(GX_LIGHT1, (guVector){6, 0, zlight}, 0x00FF00FF); GRRLIB_InitLight(GX_LIGHT1, (guVector){6, 0, zlight}, 0x00FF00FF);
@ -90,15 +49,19 @@ int main() {
GRRLIB_LightSwitch(GX_LIGHT0|GX_LIGHT1|GX_LIGHT2,RGBA(Amb,Amb,Amb,0xFF),0x808080FF,0); GRRLIB_LightSwitch(GX_LIGHT0|GX_LIGHT1|GX_LIGHT2,RGBA(Amb,Amb,Amb,0xFF),0x808080FF,0);
GRRLIB_Draw3dObj(torus,0,0,1); 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);
a+=0.5f; a+=0.5f;
GRRLIB_LightOff(); GRRLIB_LightOff();
// Switch To 2D Mode to display text // Switch To 2D Mode to display text
GRRLIB_2dMode(); GRRLIB_2dMode();
GRRLIB_Printf((640-(16*35))/2, 20, tex_font, 0xFFFFFFFF, 1, "(PLUS / MINUS) AMBIENT = 0X%02X%02X%02XFF",Amb,Amb,Amb,Amb); GRRLIB_Printf((640-(16*40))/2, 20, tex_font, 0xFFFFFFFF, 1, "PRESS PLUS OR MINUS TO PLAY WITH AMBIENT");
GRRLIB_Printf((640-(16*35))/2, 36, tex_font, 0xFFFFFFFF, 1, " (A / B) ZLIGHT = %f ",zlight); GRRLIB_Printf((640-(16*15))/2, 36, tex_font, 0xFFFFFFFF, 1, "AMBIENT = 0X%02X%02X%02XFF",Amb,Amb,Amb,Amb);
GRRLIB_Printf((640-(16*15))/2, 52, tex_font, 0xFFFFFFFF, 1, "ZLIGHT = %f",zlight);
GRRLIB_Render(); GRRLIB_Render();
} }

File diff suppressed because it is too large Load diff