Anybody know how to get rid of border for 2D graphics?

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
nathanGroovy
DCEmu Newbie
DCEmu Newbie
Posts: 1
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Wed Dec 21, 2016 1:53 pm
Has thanked: 0
Been thanked: 0

Anybody know how to get rid of border for 2D graphics?

Post by nathanGroovy »

I'm learning programming with KOS, and I can display texture on screen and move it around with the controller.
But now I'm trying to see if I can get rid of the white border that's around my graphic, so its just the main graphic that
is being displayed. Here is code I'm using for drawing one of the graphics:

Code: Select all

void draw_ground(void) {
    pvr_poly_cxt_t cxt;
    pvr_poly_hdr_t hdr;
    pvr_vertex_t vert;

    pvr_poly_cxt_txr(&cxt, PVR_LIST_OP_POLY, PVR_TXRFMT_RGB565, 512, 512, ground_tex, PVR_FILTER_BILINEAR);
    pvr_poly_compile(&hdr, &cxt);
    pvr_prim(&hdr, sizeof(hdr));

    vert.argb = PVR_PACK_COLOR(1.0f, 1.0f, 1.0f, 1.0f);
    vert.oargb = 0;
    vert.flags = PVR_CMD_VERTEX;

    vert.x = gx;
    vert.y = gy;
    vert.z = 1;
    vert.u = 0.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = gx + size;
    vert.y = gy;
    vert.z = 1;
    vert.u = 1.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = gx;
    vert.y = gy + size;
    vert.z = 1;
    vert.u = 0.0;
    vert.v = 1.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = gx + size;
    vert.y = gy + size;
    vert.z = 1;
    vert.u = 1.0;
    vert.v = 1.0;
    vert.flags = PVR_CMD_VERTEX_EOL;
    pvr_prim(&vert, sizeof(vert));
}
I'm still pretty new at this, so any help getting the right kind of transparency for my images would be great. :grin:
Attachments
The image I'm trying to properly display in my test project.
The image I'm trying to properly display in my test project.
User avatar
bogglez
Moderator
Moderator
Posts: 578
Joined: Sun Apr 20, 2014 9:45 am
Has thanked: 0
Been thanked: 0

Re: Anybody know how to get rid of border for 2D graphics?

Post by bogglez »

You need to change
PVR_LIST_OP_POLY from OP to PT to use the punchthru (alpha mask) polygon list and change
PVR_TXRFMT_RGB565 to ARGB1555.
Also change your image by replacing white with transparency.
More info in the docs, wiki and examples.
Wiki & tutorials: http://dcemulation.org/?title=Development
Wiki feedback: viewtopic.php?f=29&t=103940
My libgl playground (not for production): https://bitbucket.org/bogglez/libgl15
My lxdream fork (with small fixes): https://bitbucket.org/bogglez/lxdream
CSword123
DCEmu Newbie
DCEmu Newbie
Posts: 8
Joined: Sat Sep 22, 2018 11:14 pm
Has thanked: 3 times
Been thanked: 0

Re: Anybody know how to get rid of border for 2D graphics?

Post by CSword123 »

Hi, I'm following the png example and having a similar problem trying to display a png file as a punch through texture. If I try rendering the texture it doesn't appear on the screen and if I render a background first, the node texture just erases the background except for a few pixels on top.
Spoiler!

Code: Select all

void initNode(void)
{
    nodeTex = pvr_mem_malloc(NODE_WIDTH * NODE_HEIGHT * 2);
    png_to_texture("rd/tracknode.png", nodeTex, PNG_NO_ALPHA); // I'm assuming this isn't the right flag?
}

Code: Select all

void drawNode(void) 
{
    pvr_poly_cxt_t context;                 // rendering context for a polygon
    pvr_poly_hdr_t header;                  // header to submit to GPU
    pvr_vertex_t vert;                      // points of a polygon
    // This function fills in a pvr_poly_cxt_t with default parameters appropriate for rendering a textured polygon in the given list
    pvr_poly_cxt_txr(&context, PVR_LIST_PT_POLY, PVR_TXRFMT_ARGB1555, NODE_WIDTH, NODE_HEIGHT,
     nodeTex, PVR_FILTER_BILINEAR);
    pvr_poly_compile(&header, &context);               // compiles a polygon context (source) into a polygon header (destination) 
    pvr_prim(&header, sizeof(header));

    vert.argb = PVR_PACK_COLOR(1.0f, 1.0f, 1.0f, 1.0f);
    vert.oargb = 0;
    vert.flags = PVR_CMD_VERTEX;

    vert.x = 1;
    vert.y = 320;
    vert.z = 1;
    vert.u = 0.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = NODE_WIDTH;
    vert.y = 320;
    vert.z = 1;
    vert.u = 1.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 1;
    vert.y = 448;
    vert.z = 1;
    vert.u = 0.0;
    vert.v = 1.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = NODE_WIDTH;
    vert.y = 448;
    vert.z = 1;
    vert.u = 1.0;
    vert.v = 1.0;
    vert.flags = PVR_CMD_VERTEX_EOL;
    pvr_prim(&vert, sizeof(vert));
}
And in the drawFrame function, I have:

Code: Select all

void drawFrame(void) 
{
    pvr_wait_ready();
    pvr_scene_begin();

    pvr_list_begin(PVR_LIST_OP_POLY);
    drawBG();
    pvr_list_finish();

    pvr_list_begin(PVR_LIST_PT_POLY);
    drawNode();
    pvr_list_finish();

    pvr_scene_finish(); 
}

Here's the png i'm trying to render as punch through:
tracknode.png
tracknode.png (1.11 KiB) Viewed 342 times
TapamN
DC Developer
DC Developer
Posts: 105
Joined: Sun Oct 04, 2009 11:13 am
Has thanked: 2 times
Been thanked: 90 times

Re: Anybody know how to get rid of border for 2D graphics?

Post by TapamN »

One problem I see is that you're calling png_to_texture with PNG_NO_ALPHA, which results in a RGB565 format texture being stored in video RAM, but you tell pvr_poly_cxt_txr that the texture is ARGB1555. This will have the colors and transparency get messed up. To load the PNG as ARGB1555, you need to pass PNG_MASK_ALPHA instead, or use PNG_FULL_ALPHA for ARGB4444.

It shouldn't normally matter if you send opaque or punchthrough polygons first, but if you find it does matter, it might be because you're sending bad data to the PVR somehow. I'm not immediately seeing anything wrong with drawNode, so the problem might be in drawBG.
These users thanked the author TapamN for the post:
CSword123
Twada
DC Developer
DC Developer
Posts: 42
Joined: Wed Jan 20, 2016 4:55 am
Has thanked: 18 times
Been thanked: 53 times

Re: Anybody know how to get rid of border for 2D graphics?

Post by Twada »

CSword123 wrote: Fri Dec 30, 2022 11:03 am Hi, I'm following the png example and having a similar problem trying to display a png file as a punch through texture. If I try rendering the texture it doesn't appear on the screen and if I render a background first, the node texture just erases the background except for a few pixels on top.
Perhaps the PVR is not initialized correctly.
The png example is initialized with "pvr_init_defaults()", but this function should not enable punch-through.
Try initializing it with "pvr_init()".

Code: Select all

pvr_init_params_t pvr_params = {
    /* Opaque, Opaque modifiers, Translucent, Translucent modifiers, Punch-through */
    { PVR_BINSIZE_16, PVR_BINSIZE_0, PVR_BINSIZE_16, PVR_BINSIZE_0, PVR_BINSIZE_16},
    /* Vertex buffer size 512K */
    512 * 1024,

    /* No DMA */
    0,

    /* No FSAA */
    0,

    /* Translucent Autosort enabled. */
    0};

/* Init kos  */
pvr_init(&pvr_params);
These users thanked the author Twada for the post:
CSword123
CSword123
DCEmu Newbie
DCEmu Newbie
Posts: 8
Joined: Sat Sep 22, 2018 11:14 pm
Has thanked: 3 times
Been thanked: 0

Re: Anybody know how to get rid of border for 2D graphics?

Post by CSword123 »

TapamN wrote: Fri Dec 30, 2022 10:25 pm One problem I see is that you're calling png_to_texture with PNG_NO_ALPHA, which results in a RGB565 format texture being stored in video RAM, but you tell pvr_poly_cxt_txr that the texture is ARGB1555. This will have the colors and transparency get messed up. To load the PNG as ARGB1555, you need to pass PNG_MASK_ALPHA instead, or use PNG_FULL_ALPHA for ARGB4444.

It shouldn't normally matter if you send opaque or punchthrough polygons first, but if you find it does matter, it might be because you're sending bad data to the PVR somehow. I'm not immediately seeing anything wrong with drawNode, so the problem might be in drawBG.
OK, good to know. it's set to PNG_MASK_ALPHA now.
Twada wrote: Sat Dec 31, 2022 5:20 am
CSword123 wrote: Fri Dec 30, 2022 11:03 am Hi, I'm following the png example and having a similar problem trying to display a png file as a punch through texture. If I try rendering the texture it doesn't appear on the screen and if I render a background first, the node texture just erases the background except for a few pixels on top.
Perhaps the PVR is not initialized correctly.
The png example is initialized with "pvr_init_defaults()", but this function should not enable punch-through.
Try initializing it with "pvr_init()".

Spoiler!

Code: Select all

pvr_init_params_t pvr_params = {
    /* Opaque, Opaque modifiers, Translucent, Translucent modifiers, Punch-through */
    { PVR_BINSIZE_16, PVR_BINSIZE_0, PVR_BINSIZE_16, PVR_BINSIZE_0, PVR_BINSIZE_16},
    /* Vertex buffer size 512K */
    512 * 1024,

    /* No DMA */
    0,

    /* No FSAA */
    0,

    /* Translucent Autosort enabled. */
    0};

/* Init kos  */
pvr_init(&pvr_params);
I copied pvr_params exactly and placed the new function call where pvr_init_defaults() used to be in main, and now I get the background but the blue polygon still doesn't appear.
A few questions:
1. Why does the pvr_mem_malloc call need to be multiplied by 2?
2. Does the z-coordinate vertex of the background need to be messed with in order to overlay the blue polygon on top of the background?
3. Do any of the pvr_params have to be modified in my case?

Here's my drawBG function and main functions:
Spoiler!

Code: Select all

void drawBG(void) 
{
    pvr_poly_cxt_t context;                 // rendering context for a polygon
    pvr_poly_hdr_t header;                  // header to submit to GPU
    pvr_vertex_t vert;                      // points of a polygon
    // This function fills in a pvr_poly_cxt_t with default parameters appropriate for rendering a textured polygon in the given list
    pvr_poly_cxt_txr(&context, PVR_LIST_OP_POLY, PVR_TXRFMT_RGB565, BACKGROUND_WIDTH, BACKGROUND_HEIGHT,
     backTex, PVR_FILTER_BILINEAR);
    pvr_poly_compile(&header, &context);               // compiles a polygon context (source) into a polygon header (destination) 
    pvr_prim(&header, sizeof(header));

    vert.argb = PVR_PACK_COLOR(1.0f, 1.0f, 1.0f, 1.0f);
    vert.oargb = 0;
    vert.flags = PVR_CMD_VERTEX;

    vert.x = 1;
    vert.y = 1;
    vert.z = 1;
    vert.u = 0.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = SCREEN_WIDTH;
    vert.y = 1;
    vert.z = 1;
    vert.u = 1.0;
    vert.v = 0.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = 1;
    vert.y = SCREEN_HEIGHT;
    vert.z = 1;
    vert.u = 0.0;
    vert.v = 1.0;
    pvr_prim(&vert, sizeof(vert));

    vert.x = SCREEN_WIDTH;
    vert.y = SCREEN_HEIGHT;
    vert.z = 1;
    vert.u = 1.0;
    vert.v = 1.0;
    vert.flags = PVR_CMD_VERTEX_EOL;
    pvr_prim(&vert, sizeof(vert));
}

Code: Select all


int main(int argc, char* argv[])
{
    maple_device_t* controller;
    cont_state_t* state;
    // set video mode to 640 by 480 pixels
    vid_set_mode(DM_640x480, PM_RGB565);
    // initialize PVR chip
    pvr_init(&pvr_params);
    //initialize background
    initBG();
    //initialize node
    initNode();
    // initialize sound streaming
    initSound();
    playOggTrack(SONG_FILE);
    //initSerial();
    while(1)
    {    
        controller = maple_enum_type(0, MAPLE_FUNC_CONTROLLER);

        if(controller) 
        {
            state = (cont_state_t*) maple_dev_status(controller);

            if(!state)
                break;

            if(state->buttons & CONT_START)                     // detect if Start was pressed to exit
                break;

            if(state->buttons & CONT_A)                         // detect if A button was pressed to restart music
            {                       
                printf("main: Restarting Ogg Vorbis\n");
                stopOggTrack();
                playOggTrack(SONG_FILE);
            }
        }
        drawFrame();                                            // render an image to the frame buffer
    }

    destroy();
    return 0;
}

EDIT: It works! So I forgot had messed with the alpha value in the blue polygon's PVR_PACK_COLOR function call(vert.argb). I had changed it to 0.0f just to see if it would help only the corners be invisible(desperation) but that resulted in the entire thing being invisible.
TapamN
DC Developer
DC Developer
Posts: 105
Joined: Sun Oct 04, 2009 11:13 am
Has thanked: 2 times
Been thanked: 90 times

Re: Anybody know how to get rid of border for 2D graphics?

Post by TapamN »

CSword123 wrote: Sat Dec 31, 2022 10:58 am1. Why does the pvr_mem_malloc call need to be multiplied by 2?
Because the texture is loaded as 16-bit color (2 bytes per pixel).
These users thanked the author TapamN for the post:
CSword123
Post Reply