You are not logged in.

#1 2011-07-21 16:40:27

z buffer in 2d mode

hello,
first of all i want to thank you for all the effort you put in this library.

back to topic: i'd like to be able to enable the z buffer in 2d mode and pass a z value to the 2d drawing functions.

to give you an example where this feature would be helpful:
i'm currently working on a game that uses a 2d isometric tilemap. without z-buffer i have to figure out the correct drawing order for the tiles manually (and that can be very painful). with z-buffer enabled i just have to assign a z value to each tile and let the z-buffer do all the magic.


I think this can easiliy be achieved, e.g:

Code:

void  GRRLIB_DrawImg (const f32 xpos, const f32 ypos, const f32 zpos, const GRRLIB_texImg *tex, const f32 degrees, const f32 scaleX, const f32 scaleY, const u32 color) {
 //draw img with given z value instead of z=0.0
}

void  GRRLIB_DrawImg (const f32 xpos, const f32 ypos, const GRRLIB_texImg *tex, const f32 degrees, const f32 scaleX, const f32 scaleY, const u32 color) {
  GRRLIB_DrawImg (xpos, ypos, 0.0, tex, degrees, scaleX, scaleY, color);
}

cheers,
al

Offline

 

#2 2011-07-28 12:49:37

EvilTroopa
Member

Re: z buffer in 2d mode

Hoy !

That would be pretty easy to do. I think you could have found it by yourself wink
I haven't tested that code, but tell me if you have any problem with it.
I haven't checked how the ZBuffer was initialized in 2D mode, but let's just hope it'll work !

First go to /Path/to/GRRLIB/grrlib/GRRLIB__lib.h, then below void GRRLIB_DrawImg (around line 110) add :

Code:

void GRRLIB_DrawImgZ (const f32 xpos, const f32 ypos, const f32 zpos, const GRRLIB_texImg *tex,
                    const f32 degrees, const f32 scaleX, const f32 scaleY,
                    const u32 color);

Then go to GRRLIB_render.c, and add (around line 105) :

Code:

void  GRRLIB_DrawImgZ (const f32 xpos, const f32 ypos, const f32 zpos, const GRRLIB_texImg *tex, const f32 degrees, const f32 scaleX, const f32 scaleY, const u32 color) {
    GXTexObj  texObj;
    u16       width, height;
    Mtx       m, m1, m2, mv;

    if (tex == NULL || tex->data == NULL)  return;

    GX_InitTexObj(&texObj, tex->data, tex->w, tex->h,
                  GX_TF_RGBA8, GX_CLAMP, GX_CLAMP, GX_FALSE);

    if (GRRLIB_Settings.antialias == false) {
        GX_InitTexObjLOD(&texObj, GX_NEAR, GX_NEAR,
                         0.0f, 0.0f, 0.0f, 0, 0, GX_ANISO_1);
        GX_SetCopyFilter(GX_FALSE, rmode->sample_pattern, GX_FALSE, rmode->vfilter);
    }
    else {
        GX_SetCopyFilter(rmode->aa, rmode->sample_pattern, GX_TRUE, rmode->vfilter);
    }

    GX_LoadTexObj(&texObj,      GX_TEXMAP0);
    GX_SetTevOp  (GX_TEVSTAGE0, GX_MODULATE);
    GX_SetVtxDesc(GX_VA_TEX0,   GX_DIRECT);

    guMtxIdentity  (m1);
    guMtxScaleApply(m1, m1, scaleX, scaleY, 1.0);
    guMtxRotAxisDeg(m2, &axis, degrees);
    guMtxConcat    (m2, m1, m);

    width  = tex->w * 0.5;
    height = tex->h * 0.5;

    guMtxTransApply(m, m,
        xpos +width  +tex->handlex
            -tex->offsetx +( scaleX *(-tex->handley *sin(-DegToRad(degrees))
                                      -tex->handlex *cos(-DegToRad(degrees))) ),
        ypos +height +tex->handley
            -tex->offsety +( scaleY *(-tex->handley *cos(-DegToRad(degrees))
                                      +tex->handlex *sin(-DegToRad(degrees))) ),
        0);
    guMtxConcat(GXmodelView2D, m, mv);

    GX_LoadPosMtxImm(mv, GX_PNMTX0);
    GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
        GX_Position3f32(-width, -height, zpos);
        GX_Color1u32   (color);
        GX_TexCoord2f32(0, 0);

        GX_Position3f32(width, -height, zpos);
        GX_Color1u32   (color);
        GX_TexCoord2f32(1, 0);

        GX_Position3f32(width, height, zpos);
        GX_Color1u32   (color);
        GX_TexCoord2f32(1, 1);

        GX_Position3f32(-width, height, zpos);
        GX_Color1u32   (color);
        GX_TexCoord2f32(0, 1);
    GX_End();
    GX_LoadPosMtxImm(GXmodelView2D, GX_PNMTX0);

    GX_SetTevOp  (GX_TEVSTAGE0, GX_PASSCLR);
    GX_SetVtxDesc(GX_VA_TEX0,   GX_NONE);
}

Then re-build GRRLIB !

I think you just have to specifiy a zpos as the third parameter in the functions called GX_Position3f32(...).
You can also change the functions GRRLIB_DrawTile, GRRLIB_DrawPart and GRRLIB_DrawTileQuad

Offline

 

Board footer

Powered by FluxBB