Alrighty - here's a modified copy of the envmap sample app. You can move the viewport around by using the d-pad and the buttons on the right. One side controls X size/position and the other Y size/position.
Useful for visualizing what the values mean, and now that I've seen it, I expect I can get the PSPGL implementation to do what I mean. :)
Code: Select all
/*
* PSP Software Development Kit - http://www.pspdev.org
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* envmap.c - Sample to desmonstrate environment mapping functionality
*
* Copyright (c) 2005 Jesper Svennevid
* Copyright (c) 2005 Renaldas Zioma <[email protected]>
*
*/
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <pspctrl.h>
#include <pspgu.h>
#include <pspgum.h>
PSP_MODULE_INFO("EnvMap Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);
#define printf pspDebugScreenPrintf
static unsigned int __attribute__((aligned(16))) list[262144];
extern unsigned char env0_start[];
typedef struct Vertex
{
float nx,ny,nz;
float x,y,z;
} Vertex;
int SetupCallbacks();
void genTorus( unsigned slices, unsigned rows, float radius, float thickness,
Vertex* dstVertices, unsigned short* dstIndices );
#define BUF_WIDTH (512)
#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)
#define PIXEL_SIZE (4) /* change this if you change to another screenmode */
#define FRAME_SIZE (BUF_WIDTH * SCR_HEIGHT * PIXEL_SIZE)
#define ZBUF_SIZE (BUF_WIDTH SCR_HEIGHT * 2) /* zbuffer seems to be 16-bit? */
#define TORUS_SLICES 48 // numc
#define TORUS_ROWS 48 // numt
#define TORUS_RADIUS 1.0f
#define TORUS_THICKNESS 0.5f
#define LIGHT_DISTANCE 3.0f
Vertex __attribute__((aligned(16))) torus_vertices[TORUS_SLICES*TORUS_ROWS];
unsigned short __attribute__((aligned(16))) torus_indices[TORUS_SLICES*TORUS_ROWS*6];
int main(int argc, char* argv[])
{
pspDebugScreenInit();
SetupCallbacks();
// generate geometry
genTorus( TORUS_ROWS, TORUS_SLICES, TORUS_RADIUS, TORUS_THICKNESS, torus_vertices, torus_indices );
// flush cache so that no stray data remains
sceKernelDcacheWritebackAll();
//setup Pad
SceCtrlData pad;
u32 oldButtons = 0;
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(0);
// setup GU
sceGuInit();
sceGuStart(GU_DIRECT,list);
sceGuDrawBuffer(GU_PSM_8888,(void*)0,BUF_WIDTH);
sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT,(void*)0x88000,BUF_WIDTH);
sceGuDepthBuffer((void*)0x110000,BUF_WIDTH);
sceGuOffset(2048 - (SCR_WIDTH/2),2048 - (SCR_HEIGHT/2));
sceGuViewport(2048,2048,SCR_WIDTH,SCR_HEIGHT);
sceGuDepthRange(0xc350,0x2710);
sceGuScissor(0,0,SCR_WIDTH,SCR_HEIGHT);
sceGuEnable(GU_SCISSOR_TEST);
sceGuDepthFunc(GU_GEQUAL);
sceGuEnable(GU_DEPTH_TEST);
sceGuFrontFace(GU_CW);
sceGuShadeModel(GU_SMOOTH);
sceGuEnable(GU_CULL_FACE);
sceGuEnable(GU_CLIP_PLANES);
sceGuEnable(GU_TEXTURE_2D);
sceGuEnable(GU_LIGHTING);
sceGuEnable(GU_LIGHT0);
sceGuFinish();
sceGuSync(0,0);
sceDisplayWaitVblankStart();
sceGuDisplay(GU_TRUE);
// run sample
int valTorus = 0;
int valEnvMap = 0;
int rotateTorus = 1;
int rotateEnvMap = 1;
int startX = 2048;
int startY = 2048;
int sizeX = 480;
int sizeY = 272;
for(;;)
{
sceGuStart(GU_DIRECT,list);
// Diddle the viewport
sceGuViewport(startX, startY, sizeX, sizeY);
// clear screen
sceGuClearColor(0x554433);
sceGuClearDepth(0);
sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);
// setup a light
ScePspFVector3 dir = { 0, 0, 1 };
sceGuLight(0,GU_DIRECTIONAL,GU_DIFFUSE,&dir);
sceGuLightColor(0,GU_DIFFUSE,0x00ff4040 );
sceGuLightAtt(0,1.0f,0.0f,0.0f);
sceGuAmbient(0x00202020);
// setup proj/view matrices
sceGumMatrixMode(GU_PROJECTION);
sceGumLoadIdentity();
sceGumPerspective(75.0f,16.0/9.0f,1.0f,1000.0f);
sceGumMatrixMode(GU_VIEW);
sceGumLoadIdentity();
// setup envmap texture
sceGuTexMode(GU_PSM_4444,0,0,0);
sceGuTexImage(0,64,64,64,env0_start);
sceGuTexFunc(GU_TFX_MODULATE,GU_TCC_RGB);
sceGuTexFilter(GU_LINEAR,GU_LINEAR);
sceGuAmbientColor(0xffffffff);
// setup envmap matrix (2x3)
// 2d rotation/translation matrix
// [ cosa -sina ]
// [ sina cosa ]
// [ tx ty ]
// envmap matrix is transposed
// since it is passed to GU as columns
float angle = -2.0f * valEnvMap * (GU_PI/180.0f);
float cs = cosf(angle);
float sn = sinf(angle);
ScePspFVector3 envmapMatrixColumns[2] = {
{ cs, sn, 0.0f },
{ -sn, cs, 0.0f }
};
sceGuLight( 2, GU_DIRECTIONAL, GU_DIFFUSE, &envmapMatrixColumns[0] );
sceGuLight( 3, GU_DIRECTIONAL, GU_DIFFUSE, &envmapMatrixColumns[1] );
// setup envmap texture coord generation
sceGuTexMapMode(
GU_ENVIRONMENT_MAP, // envmap mode on
2, // use 2nd light position as an envmap matrix 1st column
3 // use 3rd light position as an envmap matrix 2nd column
);
// draw torus
sceGumMatrixMode(GU_MODEL);
{
ScePspFVector3 pos = {0,0,-2.5f};
ScePspFVector3 rot = {valTorus * 0.79f * (GU_PI/180.0f), valTorus * 0.98f * (GU_PI/180.0f), valTorus * 1.32f * (GU_PI/180.0f)};
sceGumLoadIdentity();
sceGumTranslate(&pos);
sceGumRotateXYZ(&rot);
}
sceGuColor(0xffffff);
sceGumDrawArray(GU_TRIANGLES,GU_NORMAL_32BITF|GU_VERTEX_32BITF|GU_INDEX_16BIT|GU_TRANSFORM_3D,sizeof(torus_indices)/sizeof(unsigned short),torus_indices,torus_vertices);
pspDebugScreenSetXY(0,0);
printf( "Press X to toggle environment map rotation" );
pspDebugScreenSetXY(0,1);
printf( "Press O to toggle torus rotation" );
pspDebugScreenSetXY(0,2);
printf( " rect = %d, %d, %d, %d", startX, startY, sizeX, sizeY );
sceCtrlReadBufferPositive(&pad, 1);
if(oldButtons != pad.Buttons)
{
if(pad.Buttons & PSP_CTRL_CROSS)
rotateTorus ^= 0x01;
if(pad.Buttons & PSP_CTRL_CIRCLE)
rotateEnvMap ^= 0x01;
}
oldButtons = pad.Buttons;
// Increase startX
if(pad.Buttons & PSP_CTRL_UP)
startX++;
// Decrease startX
if(pad.Buttons & PSP_CTRL_DOWN)
startX--;
// Increase sizeX
if(pad.Buttons & PSP_CTRL_RIGHT)
sizeX++;
// Decreate sizeY
if(pad.Buttons & PSP_CTRL_LEFT)
sizeX--;
// Increase startX
if(pad.Buttons & PSP_CTRL_TRIANGLE)
startY++;
// Decrease startX
if(pad.Buttons & PSP_CTRL_CROSS)
startY--;
// Increase sizeX
if(pad.Buttons & PSP_CTRL_CIRCLE)
sizeY++;
// Decreate sizeY
if(pad.Buttons & PSP_CTRL_SQUARE)
sizeY--;
sceGuFinish();
sceGuSync(0,0);
// sceDisplayWaitVblankStart();
sceGuSwapBuffers();
if(rotateTorus) valTorus++;
if(rotateEnvMap) valEnvMap++;
}
sceGuTerm();
sceKernelExitGame();
return 0;
}
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
sceKernelExitGame();
return 0;
}
/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
if(thid >= 0)
{
sceKernelStartThread(thid, 0, 0);
}
return thid;
}
/* usefull geometry functions */
void genTorus( unsigned slices, unsigned rows, float radius, float thickness, Vertex* dstVertices, unsigned short* dstIndices )
{
unsigned int i,j;
// generate torus (TODO: tri-strips)
for (j = 0; j < slices; ++j)
{
for (i = 0; i < rows; ++i)
{
struct Vertex* curr = &dstVertices[i+j*rows];
float s = i + 0.5f;
float t = j;
float cs,ct,ss,st;
cs = cosf(s * (2*GU_PI)/slices);
ct = cosf(t * (2*GU_PI)/rows);
ss = sinf(s * (2*GU_PI)/slices);
st = sinf(t * (2*GU_PI)/rows);
curr->nx = cs * ct;
curr->ny = cs * st;
curr->nz = ss;
curr->x = (radius + thickness * cs) * ct;
curr->y = (radius + thickness * cs) * st;
curr->z = thickness * ss;
}
}
for (j = 0; j < slices; ++j)
{
for (i = 0; i < rows; ++i)
{
unsigned short* curr = &dstIndices[(i+(j*rows))*6];
unsigned int i1 = (i+1)%rows, j1 = (j+1)%slices;
*curr++ = i + j * rows;
*curr++ = i1 + j * rows;
*curr++ = i + j1 * rows;
*curr++ = i1 + j * rows;
*curr++ = i1 + j1 * rows;
*curr++ = i + j1 * rows;
}
}
}