diff options
author | Treeki <treeki@gmail.com> | 2011-03-12 23:17:12 +0100 |
---|---|---|
committer | Treeki <treeki@gmail.com> | 2011-03-12 23:17:12 +0100 |
commit | 7d4e4c0b34a613dd3c0220475ae4e448197522c1 (patch) | |
tree | 4f5cee367de3fdef4f9a7c84af59ffe76a2bb1c3 /oldStuff/3dlib/treeki3d.cpp | |
download | kamek-7d4e4c0b34a613dd3c0220475ae4e448197522c1.tar.gz kamek-7d4e4c0b34a613dd3c0220475ae4e448197522c1.zip |
initial commit. now I can start playing with stuff!
Diffstat (limited to 'oldStuff/3dlib/treeki3d.cpp')
-rw-r--r-- | oldStuff/3dlib/treeki3d.cpp | 287 |
1 files changed, 287 insertions, 0 deletions
diff --git a/oldStuff/3dlib/treeki3d.cpp b/oldStuff/3dlib/treeki3d.cpp new file mode 100644 index 0000000..d244b98 --- /dev/null +++ b/oldStuff/3dlib/treeki3d.cpp @@ -0,0 +1,287 @@ +/* Treeki's 3D Library for Wii */
+
+#include "3dlib/treeki3d.h"
+
+namespace T3D {
+
+const GXColor Black = {0,0,0,255};
+const GXColor White = {255,255,255,255};
+
+u32 HasBeenInited;
+
+RenderInfo *RenderQueue;
+int RenderQueuePos;
+
+CameraInfo Camera;
+
+tmdl_material *LastMaterial;
+
+
+bool Init(bool test) {
+ if (HasBeenInited == 'T3Df')
+ return test;
+
+ HasBeenInited = 'T3Df';
+
+ OSReport("T3D Engine revision 1 -- created by Treeki\n");
+ OSReport("Setting up the render queue for %d model(s).\n", OBJ_COUNT);
+
+ RenderQueue = new RenderInfo[OBJ_COUNT];
+ RenderQueuePos = 0;
+
+ // Setup camera
+ Camera.projection = GX_PERSPECTIVE;
+
+ GXRenderModeObj *rm = Hook_GetGXRenderModeObj();
+ f32 aspect = (f32)rm->viWidth / (f32)rm->viHeight;
+
+ OSReport("Dump of GXRenderModeObj settings:\n");
+ OSReport("viTVmode => %d; fbWidth => %d; efbHeight => %d; xfbHeight => %d\n", rm->viTVmode, rm->fbWidth, rm->efbHeight, rm->xfbHeight);
+ OSReport("viXOrigin => %d; viYOrigin => %d; viWidth => %d; viHeight => %d\n", rm->viXOrigin, rm->viYOrigin, rm->viWidth, rm->viHeight);
+ OSReport("xFBmode => %d; field_rendering => %d; aa => %d\n", rm->xFBmode, rm->field_rendering, rm->aa);
+
+ OSReport("Calculated VI aspect: %f\n", aspect);
+
+ MTXPerspective(Camera.projection_matrix, 45, aspect, 0.1f, 1800.0f);
+
+ Point3d camPos = {0,0,-150};
+ Vec camUp = {0,1,0};
+ Point3d target = {0,0,0};
+ MTXLookAt(Camera.view_matrix, &camPos, &camUp, &target);
+
+ return test;
+}
+
+bool Model::bind(void *data, const char *mdlname) {
+ ARCHandle arc;
+ ARCFileInfo model_info, texture_info;
+ tmdl_header *model = 0;
+ TPLPalette *texture = 0;
+
+ OSReport("[T3D] Loading model %s\n", mdlname);
+
+ // obtain the files from the arc
+ char model_name[64], texture_name[64];
+ snprintf(model_name, 64, "t3d/mdl_%s.tmdl", mdlname);
+ snprintf(texture_name, 64, "t3d/tex_%s.tpl", mdlname);
+
+ if (!ARCInitHandle(data, &arc)) {
+ OSReport("[T3D] CANNOT LOAD ARC!!\n");
+ OSReport("ARCInitHandle returned false\n");
+ return false;
+ }
+
+ if (ARCOpen(&arc, model_name, &model_info)) {
+ model = (tmdl_header*)ARCGetStartAddrInMem(&model_info);
+ ARCClose(&model_info);
+ } else {
+ OSReport("[T3D] CANNOT GET MODEL!!\n");
+ OSReport("ARCOpen could not get %s\n", model_name);
+ return false;
+ }
+
+ if (ARCOpen(&arc, texture_name, &texture_info)) {
+ texture = (TPLPalette*)ARCGetStartAddrInMem(&texture_info);
+ ARCClose(&texture_info);
+ }
+
+ // bind it
+ if (model->version != 2) {
+ OSReport("[T3D] INVALID VERSION!!\n");
+ OSReport("this revision of T3D expects a v2 model file, not %d\n", model->version);
+ return false;
+ }
+
+ if (texture) {
+ TPLBind(texture);
+ }
+
+ // fix up every shape's GX arrays
+ for (int shape_id = 0; shape_id < model->shape_count; shape_id++) {
+ u32 shape_offset = *(u32*)((u32)model + sizeof(tmdl_header) + (4*shape_id));
+ tmdl_shape *shape = (tmdl_shape*)((u32)model + shape_offset);
+
+ void *pos_arr = (void*)((u32)model + shape->pos_arr_offset);
+ void *nrm_arr = (void*)((u32)model + shape->nrm_arr_offset);
+ void *uv_arr = (void*)((u32)model + shape->uv_arr_offset);
+
+ DCStoreRangeNoSync(pos_arr, shape->pos_arr_size);
+ DCStoreRangeNoSync(nrm_arr, shape->nrm_arr_size);
+ DCStoreRangeNoSync(uv_arr, shape->uv_arr_size);
+ }
+
+ // done!
+ this->model_res = model;
+ this->texture_res = texture;
+
+ return true;
+}
+
+void Model::addToDrawList(Mtx mtx) {
+ RenderInfo *info = &RenderQueue[RenderQueuePos++];
+ MTXCopy(mtx, info->matrix);
+ info->model = this;
+
+ if (RenderQueuePos > OBJ_COUNT) {
+ OSReport("FATAL ERROR!!\n");
+ OSReport("T3D object limit reached, render queue will overflow\n");
+ }
+}
+
+void Model::draw(RenderInfo *info) {
+ Mtx matrix;
+ MTXCopy(Camera.view_matrix, matrix);
+ MTXConcat(info->matrix, matrix, matrix);
+
+ GXLoadPosMtxImm(matrix, GX_PNMTX0);
+
+ for (int i = 0; i < this->model_res->shape_count; i++)
+ this->drawShape(info, i);
+}
+
+void Model::drawShape(RenderInfo *info, int shape_id) {
+ u32 shape_offset = *(u32*)((u32)this->model_res + sizeof(tmdl_header) + (4*shape_id));
+ tmdl_shape *shape = (tmdl_shape*)((u32)this->model_res + shape_offset);
+
+ tmdl_material *material = (tmdl_material*)((u32)this->model_res + shape->mat_offset);
+ this->setupMaterialGX(material);
+
+ void *pos_arr = (void*)((u32)this->model_res + shape->pos_arr_offset);
+ void *nrm_arr = (void*)((u32)this->model_res + shape->nrm_arr_offset);
+ void *uv_arr = (void*)((u32)this->model_res + shape->uv_arr_offset);
+
+ GXSetArray(GX_VA_POS, pos_arr, 12);
+ GXSetArray(GX_VA_NRM, nrm_arr, 12);
+ GXSetArray(GX_VA_TEX0, uv_arr, 8);
+
+ void *dl_ptr = (void*)((u32)this->model_res + shape->dl_offset);
+ GXCallDisplayList(dl_ptr, shape->dl_size);
+}
+
+void Model::setupMaterialGX(tmdl_material *material) {
+ if (material == LastMaterial)
+ return;
+
+ LastMaterial = material; // small optimisation
+
+ if (material->texture_id != 0xFFFFFFFF) {
+ GXSetChanMatColor(GX_COLOR0A0, White);
+ GXSetChanMatColor(GX_COLOR1A1, White);
+
+ GXTexObj tex;
+
+ TPLGetGXTexObjFromPalette(this->texture_res, &tex, material->texture_id);
+ GXLoadTexObj(&tex, GX_TEXMAP0);
+
+ GXSetNumTexGens(1);
+ GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
+
+ GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
+ GXSetTevOrder(GX_TEVSTAGE1, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR0A0);
+
+ // TEV blending ops:
+ // First off, use the equivalent of GXSetTevOp(GX_TEVSTAGE0, GX_MODULATE)
+ //GXSetTevOp(GX_TEVSTAGE0, GX_MODULATE);
+ GXSetTevColorIn(GX_TEVSTAGE0, GX_CC_ZERO, GX_CC_TEXC, GX_CC_RASC, GX_CC_ZERO);
+ GXSetTevAlphaIn(GX_TEVSTAGE0, GX_CA_ZERO, GX_CA_TEXA, GX_CA_RASA, GX_CA_ZERO);
+ GXSetTevColorOp(GX_TEVSTAGE0, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE, GX_TEVPREV);
+ GXSetTevAlphaOp(GX_TEVSTAGE0, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE, GX_TEVPREV);
+
+ // Then do the same for stage1, but blend TEVPREV with RASC/RASA
+ GXSetTevColorIn(GX_TEVSTAGE1, GX_CC_ZERO, GX_CC_RASC, GX_CC_CPREV, GX_CC_ZERO);
+ GXSetTevAlphaIn(GX_TEVSTAGE1, GX_CA_ZERO, GX_CA_RASA, GX_CA_APREV, GX_CA_ZERO);
+ GXSetTevColorOp(GX_TEVSTAGE1, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE, GX_TEVPREV);
+ GXSetTevAlphaOp(GX_TEVSTAGE1, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE, GX_TEVPREV);
+
+ } else {
+ GXSetChanMatColor(GX_COLOR0A0, material->mat_colour);
+ GXSetChanMatColor(GX_COLOR1A1, White);
+
+ GXSetNumTexGens(0);
+
+ GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR0A0);
+ GXSetTevOrder(GX_TEVSTAGE1, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR1A1);
+
+ // TEV blending ops:
+ // First stage becomes GX_PASSCLR
+ GXSetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
+
+ // Then blend this colour with RASA/RASC in the second stage
+ GXSetTevColorIn(GX_TEVSTAGE1, GX_CC_ZERO, GX_CC_RASC, GX_CC_CPREV, GX_CC_ZERO);
+ GXSetTevAlphaIn(GX_TEVSTAGE1, GX_CA_ZERO, GX_CA_RASA, GX_CA_APREV, GX_CA_ZERO);
+ GXSetTevColorOp(GX_TEVSTAGE1, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE, GX_TEVPREV);
+ GXSetTevAlphaOp(GX_TEVSTAGE1, GX_TEV_ADD, GX_TB_ZERO, GX_CS_SCALE_1, GX_TRUE, GX_TEVPREV);
+ }
+}
+
+void DrawQueue() {
+ LastMaterial = NULL;
+
+ GXInvalidateVtxCache();
+ GXInvalidateTexAll();
+ GXClearVtxDesc();
+
+ GXSetVtxDesc(GX_VA_POS, GX_INDEX16);
+ GXSetVtxDesc(GX_VA_NRM, GX_INDEX16);
+ GXSetVtxDesc(GX_VA_TEX0, GX_INDEX16);
+
+ GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
+ GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_NRM, GX_NRM_XYZ, GX_F32, 0);
+ GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
+
+ GXSetCurrentMtx(GX_PNMTX0);
+
+ GXRenderModeObj *rm = Hook_GetGXRenderModeObj();
+ if (rm->field_rendering)
+ GXSetViewportJitter(0, 0, rm->fbWidth, rm->efbHeight, 0, 1, VIGetNextField());
+ else
+ GXSetViewport(0, 0, rm->fbWidth, rm->efbHeight, 0, 1);
+
+ GXSetProjection(Camera.projection_matrix, Camera.projection);
+
+ GXSetBlendMode(GX_BM_NONE, GX_BL_ONE, GX_BL_ONE, GX_LO_CLEAR);
+ GXSetCullMode(GX_CULL_NONE);
+ GXSetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
+ GXSetZCompLoc(GX_TRUE);
+
+ GXSetNumTevStages(2);
+ GXSetTevDirect(GX_TEVSTAGE0);
+ GXSetTevDirect(GX_TEVSTAGE1);
+
+ GXSetTevSwapModeTable(GX_TEV_SWAP0, GX_CH_RED, GX_CH_GREEN, GX_CH_BLUE, GX_CH_ALPHA);
+
+ GXSetTevSwapMode(GX_TEVSTAGE0, GX_TEV_SWAP0, GX_TEV_SWAP0);
+ GXSetTevSwapMode(GX_TEVSTAGE1, GX_TEV_SWAP0, GX_TEV_SWAP0);
+
+ /*GXInitLightDir(&light, 0, -1, 0);
+ GXInitLightSpot(&light, 45, GX_SP_COS);
+ GXInitLightDistAttn(&light, 30, 0.7, GX_DA_GENTLE);*/
+ GXLightObj light, light2;
+ GXInitLightColor(&light, White);
+ GXInitLightPos(&light, 0,-500,0);
+ GXLoadLightObjImm(&light, GX_LIGHT0);
+
+ GXInitLightColor(&light2, White);
+ GXInitSpecularDir(&light2, 0, 0, 0.5);
+ GXInitLightShininess(&light2, 4.0f);
+ GXLoadLightObjImm(&light2, GX_LIGHT1);
+
+ GXSetNumChans(2);
+ GXSetChanAmbColor(GX_COLOR0A0, (GXColor){128,128,128,255});
+ GXSetChanAmbColor(GX_COLOR1A1, (GXColor){128,128,128,255});
+ //GXSetChanMatColor(GX_COLOR0A0, {255,255,255,255});
+
+ GXSetChanCtrl(GX_COLOR0A0, GX_ENABLE, GX_SRC_REG, GX_SRC_REG, GX_LIGHT0, GX_DF_CLAMP, GX_AF_NONE);
+ GXSetChanCtrl(GX_COLOR1A1, GX_ENABLE, GX_SRC_REG, GX_SRC_REG, GX_LIGHT1, GX_DF_CLAMP, GX_AF_SPEC);
+
+ GXSetFog(GX_FOG_NONE, 0, 0, 0, 0, (GXColor){0,0,0,0});
+
+
+ for (int idx = 0; idx < RenderQueuePos; idx++) {
+ RenderQueue[idx].model->draw(&RenderQueue[idx]);
+ }
+
+ RenderQueuePos = 0;
+}
+
+};
|