You are not logged in.

#1 2011-08-01 21:39:36

owen
Member

printf_3d

Based on GRRLIB_printf().  I'm not sure if this is in there already or if it is of any use but here it is in its unoptimized form.  I will make another version that only uses one GX if anybody is interested.  It has some alpha issues with the transparency in the font texture - not sure why though.

This function allows you to do a printf with a Z coordinate.  Works just like GRRLIB_printf() but in 3d mode.

Code:

void  printf_3d (const f32 xpos, const f32 ypos, const f32 zpos,
                     GRRLIB_texImg *tex, const u32 color,
                     const f32 zoom, const char *text, ...) {
    if (tex == NULL || tex->data == NULL) {
        return;
    }

    int i, size;
    char tmp[1024];
//    f32 offset = tex->tilew * zoom;

    va_list argp;
    va_start(argp, text);
    size = vsnprintf(tmp, sizeof(tmp), text, argp);
    va_end(argp);
    
    f32 s1, s2, t1, t2;
    //u16 width, height;
    int frame;
    //width  = tex->w * 0.5;
    //height = tex->h * 0.5;

    for (i = 0; i < size; i++) {
        
    GRRLIB_3dMode(1, 10000, 45, 1, 0);

        //GRRLIB_DrawTile(xpos+i*offset, ypos, tex, 0, zoom, zoom, color, tmp[i] - tex->tilestart);
        GRRLIB_SetTexture( tex, false);
        
        GRRLIB_ObjectView( xpos+(i*(zoom*2))+(zoom*0.1), ypos, zpos, 180,0,0, zoom,zoom,zoom );        
        
        frame=tmp[i] - tex->tilestart;

        // The 0.001f/x is the frame correction formula by spiffen
        s1 = (frame % tex->nbtilew) * tex->ofnormaltexx;
        s2 = s1 + tex->ofnormaltexx;
        t1 = (int)(frame/tex->nbtilew) * tex->ofnormaltexy;
        t2 = t1 + tex->ofnormaltexy;

        GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
            GX_Position3f32(-1, -1, 0);
            GX_Color1u32   (color);
            GX_TexCoord2f32(s1, t1);

            GX_Position3f32(1, -1,  0);
            GX_Color1u32   (color);
            GX_TexCoord2f32(s2, t1);

            GX_Position3f32(1, 1,  0);
            GX_Color1u32   (color);
            GX_TexCoord2f32(s2, t2);

            GX_Position3f32(-1, 1,  0);
            GX_Color1u32   (color);
            GX_TexCoord2f32(s1, t2);
        GX_End();
    
    }
}

Offline

 

#2 2012-10-05 04:08:37

owen
Member

Re: printf_3d

finally spent some time this past weekend and got it working in one gx block.  Might be bugs are functions that need to be removed.  Also note that it works best with square fonts.  And you may need to adjust the normals or rotate the object to face the correct direction.  This method is better than the above since the whole string of letters rotates together.

Code:

    void  printf_3d (const f32 xpos, const f32 ypos, const f32 zpos,
                         GRRLIB_texImg *tex, float rx, float ry, float rz, const u32 color,
                         const f32 zoom, const char *text, ...) {

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

        int i, size;
        char tmp[1024];

        va_list argp;
        va_start(argp, text);
        size = vsnprintf(tmp, sizeof(tmp), text, argp);
        va_end(argp);
        
        f32 s1, s2, t1, t2;
        int frame;

        engine_3dnt(); // 3d with normal and texture mode

        GRRLIB_SetTexture( tex, false);
        GRRLIB_ObjectView( xpos, ypos, zpos, rx,ry,rz, zoom,zoom,zoom );        
        GX_Begin(GX_QUADS, GX_VTXFMT0, size*4);
        
        float chr_w, chr_p;//, chr_y;
        
        chr_w=2.0f / (float)size;  //with of one character
        //chr_y=-0.0f; //center the characters across the zero y-axis.  depends on which dir is up
    
        for (i = 0; i < size; i++) {
            frame=tmp[i] - tex->tilestart;

            // The 0.001f/x is the frame correction formula by spiffen
            s1 = (frame % tex->nbtilew) * tex->ofnormaltexx;
            s2 = s1 + tex->ofnormaltexx;
            t1 = (int)(frame/tex->nbtilew) * tex->ofnormaltexy;
            t2 = t1 + tex->ofnormaltexy ;
            
            chr_p=((float)i*chr_w);  //this char's position

            GX_Position3f32(-1.0f+chr_p, -0.0f, 0);            
            GX_Normal3f32(0.0f,0.0f,-1.0f); GX_Color1u32(color); GX_TexCoord2f32(s1, t1);

            GX_Position3f32(-1.0f+chr_p+chr_w, -0.0f,  0); 
            GX_Normal3f32(0.0f,0.0f,-1.0f); GX_Color1u32(color); GX_TexCoord2f32(s2, t1);

            GX_Position3f32(-1.0f+chr_p+chr_w, -0.0f+chr_w,  0);
            GX_Normal3f32(0.0f,0.0f,-1.0f); GX_Color1u32(color); GX_TexCoord2f32(s2, t2);

            GX_Position3f32(-1.0f+chr_p, -0.0f+chr_w,  0);
            GX_Normal3f32(0.0f,0.0f,-1.0f); GX_Color1u32(color); GX_TexCoord2f32(s1, t2);
        }
        GX_End();
            
        if(debug) do_draw_lines(  xpos, ypos, zpos, rx,ry,rz, color, zoom,zoom,zoom );
    }

Last edited by owen (2012-10-05 04:10:03)

Offline

 

Board footer

Powered by FluxBB