//
// Generated by dtk
// Translation Unit: J3DMaterial.cpp
//

#include "JSystem/J3DGraphBase/J3DMaterial.h"
#include "JSystem/J3DGraphBase/J3DGD.h"
#include "dolphin/types.h"

/* 802DDBC4-802DDDC4       .text createColorBlock__11J3DMaterialFUl */
J3DColorBlock * J3DMaterial::createColorBlock(u32 param_0) {
    J3DColorBlock* rv = NULL;
    switch (param_0) {
        case 0:
            rv = new J3DColorBlockLightOff();
            break;
        case 0x40000000:
            rv = new J3DColorBlockLightOn();
            break;
        case 0x80000000:
            rv = new J3DColorBlockAmbientOn();
            break;
    }

    return rv;
}

/* 802DDDC4-802DDF28       .text createTexGenBlock__11J3DMaterialFUl */
J3DTexGenBlock * J3DMaterial::createTexGenBlock(u32 param_0) {
    switch (param_0) {
        case 0x8000000:
            return new J3DTexGenBlock4();
        case 0:
        default:
            return new J3DTexGenBlockBasic();
    }
}

/* 802DDF28-802DE29C       .text createTevBlock__11J3DMaterialFi */
J3DTevBlock * J3DMaterial::createTevBlock(int param_0) {
    J3DTevBlock* rv = NULL;
    if (param_0 <= 1) {
        rv = new J3DTevBlock1();
    } else if (param_0 == 2) {
        rv = new J3DTevBlock2();
    } else if (param_0 <= 4) {
        rv = new J3DTevBlock4();
    } else if (param_0 <= 16) {
        rv = new J3DTevBlock16();
    }
    return rv;
}

/* 802DE29C-802DE384       .text createIndBlock__11J3DMaterialFi */
J3DIndBlock * J3DMaterial::createIndBlock(int param_0) {
    if (param_0 != 0) {
        return new J3DIndBlockFull();
    }

    return new J3DIndBlockNull();
}

/* 802DE384-802DE548       .text createPEBlock__11J3DMaterialFUlUl */
J3DPEBlock * J3DMaterial::createPEBlock(u32 createFlags, u32 materialMode) {
    J3DPEBlock* rv = NULL;

    if (createFlags == 0) {
        if (materialMode & 1) {
            rv = new J3DPEBlockOpa();
            return rv;
        } else if (materialMode & 2) {
            rv = new J3DPEBlockTexEdge();
            return rv;
        } else if (materialMode & 4) {
            rv = new J3DPEBlockXlu();
            return rv;
        }
    }

    if (createFlags == 0x10000000) {
        rv = new J3DPEBlockFull();
    } else if (createFlags == 0x20000000) {
        rv = new J3DPEBlockFogOff();
    }

    return rv;
}

/* 802DE548-802DE598       .text calcSizeColorBlock__11J3DMaterialFUl */
u32 J3DMaterial::calcSizeColorBlock(u32 param_0) {
    u32 rv = 0;
    switch (param_0) {
        case 0:
            rv = sizeof(J3DColorBlockLightOff);
            break;
        case 0x40000000:
            rv = sizeof(J3DColorBlockLightOn);
            break;
        case 0x80000000:
            rv = sizeof(J3DColorBlockAmbientOn);
            break;
    }

    return rv;
}

/* 802DE598-802DE5C4       .text calcSizeTexGenBlock__11J3DMaterialFUl */
u32 J3DMaterial::calcSizeTexGenBlock(u32 param_0) {
    switch (param_0) {
        case 0x8000000:
            return sizeof(J3DTexGenBlock4);
        case 0:
        default:
            return sizeof(J3DTexGenBlockBasic);
    }
}

/* 802DE5C4-802DE60C       .text calcSizeTevBlock__11J3DMaterialFi */
u32 J3DMaterial::calcSizeTevBlock(int param_0) {
    u32 rv = 0;
    if (param_0 <= 1) {
        rv = sizeof(J3DTevBlock1);
    } else if (param_0 == 2) {
        rv = sizeof(J3DTevBlock2);
    } else if (param_0 <= 4) {
        rv = sizeof(J3DTevBlock4);
    } else if (param_0 <= 16) {
        rv = sizeof(J3DTevBlock16);
    }
    return rv;
}

/* 802DE60C-802DE620       .text calcSizeIndBlock__11J3DMaterialFi */
u32 J3DMaterial::calcSizeIndBlock(int param_0) {
    if (param_0 != 0) {
        return sizeof(J3DIndBlockFull);
    }

    return sizeof(J3DIndBlockNull);
}

/* 802DE620-802DE688       .text calcSizePEBlock__11J3DMaterialFUlUl */
u32 J3DMaterial::calcSizePEBlock(u32 param_0, u32 param_1) {
    u32 rv = 0;
    if (param_0 == 0) {
        if (param_1 & 1) {
            rv = sizeof(J3DPEBlockOpa);
        } else if (param_1 & 2) {
            rv = sizeof(J3DPEBlockTexEdge);
        } else if (param_1 & 4) {
            rv = sizeof(J3DPEBlockXlu);
        }
    }
    else if (param_0 == 0x10000000) {
        rv = sizeof(J3DPEBlockFull);
    } else if (param_0 == 0x20000000) {
        rv = sizeof(J3DPEBlockFogOff);
    }
    return rv;
}

/* 802DE688-802DE6D8       .text initialize__11J3DMaterialFv */
void J3DMaterial::initialize() {
    mShape = NULL;
    mNext = NULL;
    mJoint = NULL;
    mMaterialMode = 1;
    mIndex = -1;
    mInvalid = 0;
    mDiffFlag = 0;
    mColorBlock = NULL;
    mTexGenBlock = NULL;
    mTevBlock = NULL;
    mIndBlock = NULL;
    mPEBlock = NULL;
    mpOrigMaterial = NULL;
    mMaterialAnm = NULL;
    mSharedDLObj = NULL;
}

/* 802DE6D8-802DE78C       .text countDLSize__11J3DMaterialFv */
u32 J3DMaterial::countDLSize() {
    return ALIGN_NEXT(mColorBlock->countDLSize() + mTexGenBlock->countDLSize() + mTevBlock->countDLSize() + mIndBlock->countDLSize() + mPEBlock->countDLSize(), 32);
}

/* 802DE78C-802DEA68       .text makeDisplayList_private__11J3DMaterialFP17J3DDisplayListObj */
void J3DMaterial::makeDisplayList_private(J3DDisplayListObj* param_0) {
    param_0->beginDL();
    mTevBlock->load();
    mIndBlock->load();
    mPEBlock->load();
    J3DGDSetGenMode(mTexGenBlock->getTexGenNum(), mColorBlock->getColorChanNum(), mTevBlock->getTevStageNum(), mIndBlock->getIndTexStageNum(), (GXCullMode)(u8)mColorBlock->getCullMode());
    mTexGenBlock->load();
    mColorBlock->load();
    J3DGDSetNumChans(mColorBlock->getColorChanNum());
    J3DGDSetNumTexGens(mTexGenBlock->getTexGenNum());
    param_0->endDL();
}

/* 802DEA68-802DEAB0       .text makeDisplayList__11J3DMaterialFv */
void J3DMaterial::makeDisplayList() {
    if (!j3dSys.getMatPacket()->isLocked()) {
        j3dSys.getMatPacket()->mDiffFlag = mDiffFlag;
        makeDisplayList_private(j3dSys.getMatPacket()->getDisplayListObj());
    }
}

/* 802DEAB0-802DEAD4       .text makeSharedDisplayList__11J3DMaterialFv */
void J3DMaterial::makeSharedDisplayList() {
    makeDisplayList_private(mSharedDLObj);
}

/* 802DEAD4-802DEB3C       .text load__11J3DMaterialFv */
void J3DMaterial::load() {
    j3dSys.setMaterialMode(mMaterialMode);
    if (!j3dSys.checkFlag(2)) {
        j3dSys.mMatPacket->callDL();
        loadNBTScale(*mTexGenBlock->getNBTScale());
    }
}

/* 802DEB3C-802DEBA0       .text loadSharedDL__11J3DMaterialFv */
void J3DMaterial::loadSharedDL() {
    j3dSys.setMaterialMode(mMaterialMode);
    if (!j3dSys.checkFlag(2)) {
        mSharedDLObj->callDL();
        loadNBTScale(*mTexGenBlock->getNBTScale());
    }
}

/* 802DEBA0-802DEC38       .text patch__11J3DMaterialFv */
void J3DMaterial::patch() {
    j3dSys.getMatPacket()->mDiffFlag = mDiffFlag;
    j3dSys.getMatPacket()->beginPatch();
    mTevBlock->patch();
    mColorBlock->patch();
    mTexGenBlock->patch();
    j3dSys.getMatPacket()->endPatch();
}

/* 802DEC38-802DEE3C       .text diff__11J3DMaterialFUl */
void J3DMaterial::diff(u32 param_0) {
    if (j3dSys.getMatPacket()->isEnabled_Diff()) {
        j3dSys.getMatPacket()->beginDiff();
        mTevBlock->diff(param_0);
        mIndBlock->diff(param_0);
        mPEBlock->diff(param_0);
        if (param_0 & 0x2000000) {
            J3DGDSetGenMode_3Param(mTexGenBlock->getTexGenNum(), mTevBlock->getTevStageNum(), mIndBlock->getIndTexStageNum());
            J3DGDSetNumTexGens(mTexGenBlock->getTexGenNum());
        }
        mTexGenBlock->diff(param_0);
        mColorBlock->diff(param_0);
        j3dSys.getMatPacket()->endDiff();
    }
}

/* 802DEE3C-802DEE88       .text calc__11J3DMaterialFPA4_Cf */
void J3DMaterial::calc(const Mtx param_0) {
    mTexGenBlock->calc(param_0);

    calcCurrentMtx();
    setCurrentMtx();
}

/* 802DEE88-802DEEA0       .text setCurrentMtx__11J3DMaterialFv */
void J3DMaterial::setCurrentMtx() {
    mShape->setCurrentMtx(mCurrentMtx);
}

/* 802DEEA0-802DEFF0       .text calcCurrentMtx__11J3DMaterialFv */
void J3DMaterial::calcCurrentMtx() {
    mCurrentMtx.setCurrentTexMtx(
        mTexGenBlock->getTexCoord(0)->getTexGenMtx(),
        mTexGenBlock->getTexCoord(1)->getTexGenMtx(),
        mTexGenBlock->getTexCoord(2)->getTexGenMtx(),
        mTexGenBlock->getTexCoord(3)->getTexGenMtx(),
        mTexGenBlock->getTexCoord(4)->getTexGenMtx(),
        mTexGenBlock->getTexCoord(5)->getTexGenMtx(),
        mTexGenBlock->getTexCoord(6)->getTexGenMtx(),
        mTexGenBlock->getTexCoord(7)->getTexGenMtx()
    );
}

/* 802DEFF0-802DF09C       .text copy__11J3DMaterialFP11J3DMaterial */
void J3DMaterial::copy(J3DMaterial* param_0) {
    mColorBlock->reset(param_0->mColorBlock);
    mTexGenBlock->reset(param_0->mTexGenBlock);
    mTevBlock->reset(param_0->mTevBlock);
    mIndBlock->reset(param_0->mIndBlock);
    mPEBlock->reset(param_0->mPEBlock);
}

/* 802DF09C-802DF0F8       .text reset__11J3DMaterialFv */
void J3DMaterial::reset() {
    if ((~mDiffFlag & 0x80000000) == 0) {
        mDiffFlag &= ~0x80000000;
        mMaterialMode = mpOrigMaterial->mMaterialMode;
        mInvalid = mpOrigMaterial->mInvalid;
        mMaterialAnm = NULL;
        copy(mpOrigMaterial);
    }
}

/* 802DF0F8-802DF118       .text change__11J3DMaterialFv */
void J3DMaterial::change() {
    if ((mDiffFlag & 0xc0000000) == 0) {
        mDiffFlag |= 0x80000000;
        mMaterialAnm = NULL;
    }
}

/* 802DF118-802DF1AC       .text newSharedDisplayList__11J3DMaterialFUl */
s32 J3DMaterial::newSharedDisplayList(u32 param_0) {
    if (mSharedDLObj == NULL) {
        mSharedDLObj = new J3DDisplayListObj();
        if (mSharedDLObj == NULL) {
            return 4;
        }
        s32 res = mSharedDLObj->newDisplayList(param_0);
        switch (res) {
        case kJ3DError_Success:
            break;
        default:
            return res;
        }
    }
    return 0;
}

/* 802DF1AC-802DF240       .text newSingleSharedDisplayList__11J3DMaterialFUl */
s32 J3DMaterial::newSingleSharedDisplayList(u32 param_0) {
    if (mSharedDLObj == NULL) {
        mSharedDLObj = new J3DDisplayListObj();
        if (mSharedDLObj == NULL) {
            return 4;
        }
        s32 res = mSharedDLObj->newSingleDisplayList(param_0);
        switch (res) {
        case kJ3DError_Success:
            break;
        default:
            return res;
        }
    }
    return 0;
}

/* 802DF240-802DF260       .text initialize__18J3DPatchedMaterialFv */
void J3DPatchedMaterial::initialize() {
    J3DMaterial::initialize();
}

/* 802DF260-802DF264       .text makeDisplayList__18J3DPatchedMaterialFv */
void J3DPatchedMaterial::makeDisplayList() {}

/* 802DF264-802DF268       .text makeSharedDisplayList__18J3DPatchedMaterialFv */
void J3DPatchedMaterial::makeSharedDisplayList() {}

/* 802DF268-802DF2AC       .text load__18J3DPatchedMaterialFv */
void J3DPatchedMaterial::load() {
    j3dSys.setMaterialMode(mMaterialMode);
    if (j3dSys.checkFlag(2)) {
        return;
    }
    j3dSys.mMatPacket->callDL();
}

/* 802DF2AC-802DF2EC       .text loadSharedDL__18J3DPatchedMaterialFv */
void J3DPatchedMaterial::loadSharedDL() {
    j3dSys.setMaterialMode(mMaterialMode);
    if (!j3dSys.checkFlag(0x02)) {
        mSharedDLObj->callDL();
    }
}

/* 802DF2EC-802DF338       .text calc__18J3DPatchedMaterialFPA4_Cf */
void J3DPatchedMaterial::calc(const Mtx param_0) {
    mTexGenBlock->calc(param_0);

    calcCurrentMtx();
    setCurrentMtx();
}

/* 802DF338-802DF33C       .text reset__18J3DPatchedMaterialFv */
void J3DPatchedMaterial::reset() {}

/* 802DF33C-802DF340       .text change__18J3DPatchedMaterialFv */
void J3DPatchedMaterial::change() {}

/* 802DF340-802DF360       .text initialize__17J3DLockedMaterialFv */
void J3DLockedMaterial::initialize() {
    J3DMaterial::initialize();
}

/* 802DF360-802DF364       .text makeDisplayList__17J3DLockedMaterialFv */
void J3DLockedMaterial::makeDisplayList() {}

/* 802DF364-802DF368       .text makeSharedDisplayList__17J3DLockedMaterialFv */
void J3DLockedMaterial::makeSharedDisplayList() {}

/* 802DF368-802DF3AC       .text load__17J3DLockedMaterialFv */
void J3DLockedMaterial::load() {
    j3dSys.setMaterialMode(mMaterialMode);
    if (j3dSys.checkFlag(2)) {
        return;
    }
    j3dSys.mMatPacket->callDL();
}

/* 802DF3AC-802DF3EC       .text loadSharedDL__17J3DLockedMaterialFv */
void J3DLockedMaterial::loadSharedDL() {
    j3dSys.setMaterialMode(mMaterialMode);
    if (!j3dSys.checkFlag(0x02)) {
        mSharedDLObj->callDL();
    }
}

/* 802DF3EC-802DF3F0       .text patch__17J3DLockedMaterialFv */
void J3DLockedMaterial::patch() {}

/* 802DF3F0-802DF3F4       .text diff__17J3DLockedMaterialFUl */
void J3DLockedMaterial::diff(u32) {}

/* 802DF3F4-802DF3F8       .text calc__17J3DLockedMaterialFPA4_Cf */
void J3DLockedMaterial::calc(const Mtx) {}

/* 802DF3F8-802DF3FC       .text reset__17J3DLockedMaterialFv */
void J3DLockedMaterial::reset() {}

/* 802DF3FC-802DF400       .text change__17J3DLockedMaterialFv */
void J3DLockedMaterial::change() {}
