|
| 1 | +// ____ ___ | / _____ _____ |
| 2 | +// | __ | |___/ | | |
| 3 | +// |___| ___| | \ __|__ | gsKit Open Source Project. |
| 4 | +// ---------------------------------------------------------------------- |
| 5 | +// Copyright 2004 - Chris "Neovanglist" Gilbert <[email protected]> |
| 6 | +// Licenced under Academic Free License version 2.0 |
| 7 | +// Review gsKit README & LICENSE files for further details. |
| 8 | +// |
| 9 | +// renderTexture.c - Example demonstrating render to texture operation. |
| 10 | +// First we are rendering a triangle to a texture, |
| 11 | +// then we are rendering the texture to the screen. |
| 12 | +// |
| 13 | + |
| 14 | +#include <stdio.h> |
| 15 | +#include <malloc.h> |
| 16 | + |
| 17 | +#include <gsKit.h> |
| 18 | +#include <dmaKit.h> |
| 19 | +#include <gsToolkit.h> |
| 20 | +#include <gsInline.h> |
| 21 | +#include <gsTexture.h> |
| 22 | + |
| 23 | +#define TEXTURE_WIDTH 256 |
| 24 | +#define TEXTURE_HEIGHT 256 |
| 25 | + |
| 26 | +int main(int argc, char *argv[]) |
| 27 | +{ |
| 28 | + GSGLOBAL *gsGlobal; |
| 29 | + GSTEXTURE renderTexture; |
| 30 | + uint32_t i, j, offset; |
| 31 | + uint32_t totalVertices = 3; |
| 32 | + uint32_t textureTotalVertices = 2; |
| 33 | + uint64_t White = GS_SETREG_RGBAQ(0xFF, 0xFF, 0xFF, 0x00, 0x00); |
| 34 | + gs_rgbaq color = color_to_RGBAQ(0x80, 0x80, 0x80, 0x80, 0); |
| 35 | + u32 render_target_ptr; |
| 36 | + |
| 37 | + gsGlobal = gsKit_init_global(); |
| 38 | + |
| 39 | + gsGlobal->PSM = GS_PSM_CT24; |
| 40 | + gsGlobal->PSMZ = GS_PSMZ_16S; |
| 41 | + |
| 42 | + dmaKit_init(D_CTRL_RELE_OFF, D_CTRL_MFD_OFF, D_CTRL_STS_UNSPEC, |
| 43 | + D_CTRL_STD_OFF, D_CTRL_RCYC_8, 1 << DMA_CHANNEL_GIF); |
| 44 | + |
| 45 | + // Initialize the DMAC |
| 46 | + dmaKit_chan_init(DMA_CHANNEL_GIF); |
| 47 | + |
| 48 | + gsKit_init_screen(gsGlobal); |
| 49 | + gsKit_mode_switch(gsGlobal, GS_ONESHOT); |
| 50 | + |
| 51 | + renderTexture.Width = TEXTURE_WIDTH; |
| 52 | + renderTexture.Height = TEXTURE_HEIGHT; |
| 53 | + renderTexture.PSM = GS_PSM_CT16; |
| 54 | + renderTexture.Mem = 0; // NOT NEEDED |
| 55 | + renderTexture.Vram = gsKit_vram_alloc(gsGlobal, gsKit_texture_size(renderTexture.Width, renderTexture.Height, renderTexture.PSM), GSKIT_ALLOC_USERBUFFER); |
| 56 | + |
| 57 | + gsKit_set_clamp(gsGlobal, GS_CMODE_CLAMP); |
| 58 | + |
| 59 | + GSPRIMPOINT *verts = (GSPRIMPOINT *)malloc(sizeof(GSPRIMPOINT) * totalVertices); |
| 60 | + verts[0].xyz2 = vertex_to_XYZ2(gsGlobal, 0, 0, 0); |
| 61 | + verts[0].rgbaq = color_to_RGBAQ(0xFF, 0x00, 0x00, 0xFF, 0); |
| 62 | + |
| 63 | + verts[1].xyz2 = vertex_to_XYZ2(gsGlobal, renderTexture.Width, 0, 0); |
| 64 | + verts[1].rgbaq = color_to_RGBAQ(0x00, 0xFF, 0x00, 0xFF, 0); |
| 65 | + |
| 66 | + verts[2].xyz2 = vertex_to_XYZ2(gsGlobal, renderTexture.Width / 2, renderTexture.Height, 0); |
| 67 | + verts[2].rgbaq = color_to_RGBAQ(0x00, 0x00, 0xFF, 0xFF, 0); |
| 68 | + |
| 69 | + GSPRIMUVPOINTFLAT *textureVertex = (GSPRIMUVPOINTFLAT *)malloc(sizeof(GSPRIMUVPOINTFLAT) * textureTotalVertices); |
| 70 | + textureVertex[0].xyz2 = vertex_to_XYZ2(gsGlobal, 0, 0, 0); |
| 71 | + textureVertex[0].uv = vertex_to_UV(&renderTexture, 0, 0); |
| 72 | + |
| 73 | + textureVertex[1].xyz2 = vertex_to_XYZ2(gsGlobal, gsGlobal->Width, gsGlobal->Height, 0); |
| 74 | + textureVertex[1].uv = vertex_to_UV(&renderTexture, renderTexture.Width, renderTexture.Height); |
| 75 | + |
| 76 | + while (1) |
| 77 | + { |
| 78 | + gsKit_clear(gsGlobal, White); |
| 79 | + |
| 80 | + // set render target to the texture |
| 81 | + gsKit_renderToTexture(gsGlobal, &renderTexture); |
| 82 | + |
| 83 | + gsKit_prim_list_triangle_gouraud_3d(gsGlobal, totalVertices, verts); |
| 84 | + |
| 85 | + // set render target back to the screen |
| 86 | + gsKit_renderToScreen(gsGlobal); |
| 87 | + |
| 88 | + gskit_prim_list_sprite_texture_uv_flat_color(gsGlobal, &renderTexture, color, textureTotalVertices, textureVertex); |
| 89 | + |
| 90 | + gsKit_queue_exec(gsGlobal); |
| 91 | + gsKit_sync_flip(gsGlobal); |
| 92 | + } |
| 93 | + |
| 94 | + free(verts); |
| 95 | + |
| 96 | + return 0; |
| 97 | +} |
0 commit comments