Sonicadvance1
Joined: 13 Oct 2005 Posts: 10
|
Posted: Mon Apr 03, 2006 3:21 pm Post subject: 3D Rendering Problem |
|
|
My current program I'm working is running into some problems rendering my triangles. The ones in front are invisible and then the ones in the back are not. Not quite sure what is wrong so I will post the source. The rendering is mostly just the cube examples.
| Code: | #include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <stdio.h>
#include <string.h>
#include <pspgu.h>
#include <pspgum.h>
#include <psptypes.h>
#include <pspkerneltypes.h>
#include <pspiofilemgr.h>
#include <pspiofilemgr_fcntl.h>
#include <stdlib.h>
//#include <pspmoduleinfo.h>
#include <pspwlan.h>
#include <math.h>
#define BUF_WIDTH (512)
#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)
PSP_MODULE_INFO("Project 4", 0, 1, 1);
extern unsigned char logo_start[];
#define printf pspDebugScreenPrintf
int done=0;
struct Vertex
{
float u, v;
unsigned int color;
float x,y,z;
};
struct Vertex __attribute__((aligned(16))) Vertices[10000];
static unsigned int __attribute__((aligned(16))) list[262144];
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, THREAD_ATTR_USER, 0);
if (thid >= 0)
sceKernelStartThread(thid, 0, 0);
return thid;
}
static unsigned int staticOffset = 0;
static unsigned int getMemorySize(unsigned int width, unsigned int height, unsigned int psm)
{
switch (psm)
{
case GU_PSM_T4:
return (width * height) >> 1;
case GU_PSM_T8:
return width * height;
case GU_PSM_5650:
case GU_PSM_5551:
case GU_PSM_4444:
case GU_PSM_T16:
return 2 * width * height;
case GU_PSM_8888:
case GU_PSM_T32:
return 4 * width * height;
default:
return 0;
}
}
void* getStaticVramBuffer(unsigned int width, unsigned int height, unsigned int psm)
{
unsigned int memSize = getMemorySize(width,height,psm);
void* result = (void*)staticOffset;
staticOffset += memSize;
return result;
}
int
Random (int low, int high)
{
return (rand() % (high + 1));
}
int main(int argc, char **argv)
{
SetupCallbacks();
pspDebugScreenInit();
int am=0;
int selected=0;
char str[1028];
FILE *fp;
fp=fopen("ms0:/Test.raw", "r");
while(fgets(str, 1024, fp) != NULL)
{
sscanf(str,"%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n",\
&Vertices[am].x,&Vertices[am].y,&Vertices[am].z,&Vertices[am].u,&Vertices[am].v,\
&Vertices[am+1].x,&Vertices[am+1].y,&Vertices[am+1].z,&Vertices[am+1].u,&Vertices[am+1].v,\
&Vertices[am+2].x,&Vertices[am+2].y,&Vertices[am+2].z,&Vertices[am+2].u,&Vertices[am+2].v);
Vertices[am].color=Random(1,0xFFFFFF);
Vertices[am+1].color=Random(1,0xFFFFFF);
Vertices[am+2].color=Random(1,0xFFFFFF);
am+=3;
}
am-=3;
printf("%s",str);
SceCtrlData pad;
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
void* fbp0 = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888);
void* fbp1 = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888);
void* zbp = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444);
sceGuInit();
sceGuStart(GU_DIRECT,list);
sceGuDrawBuffer(GU_PSM_8888,fbp0,BUF_WIDTH);
sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT,fbp1,BUF_WIDTH);
sceGuDepthBuffer(zbp,BUF_WIDTH);
sceGuOffset(2048 - (SCR_WIDTH/2),2048 - (SCR_HEIGHT/2));
sceGuViewport(2048,2048,SCR_WIDTH,SCR_HEIGHT);
sceGuDepthRange(65535,0);
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_TEXTURE_2D);
sceGuEnable(GU_CLIP_PLANES);
sceGuFinish();
sceGuSync(0,0);
sceDisplayWaitVblankStart();
sceGuDisplay(GU_TRUE);
int val = 0;
float z=0;
while(done==0){
sceCtrlReadBufferPositive(&pad, 1);
if(pad.Buttons & PSP_CTRL_UP)
z+=0.1f;
if(pad.Buttons & PSP_CTRL_DOWN)
z-=0.1f;
sceGuStart(GU_DIRECT,list);
// clear screen
sceGuClearColor(0xff554433);
sceGuClearDepth(0);
sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);
// setup matrices for cube
sceGumMatrixMode(GU_PROJECTION);
sceGumLoadIdentity();
sceGumPerspective(75.0f,16.0f/9.0f,0.5f,1000.0f);
sceGumMatrixMode(GU_VIEW);
sceGumLoadIdentity();
sceGumMatrixMode(GU_MODEL);
sceGumLoadIdentity();
{
ScePspFVector3 pos = { 0, 0, z };
ScePspFVector3 rot = { val * 0.79f * (GU_PI/180.0f), val * 0.98f * (GU_PI/180.0f), val * 1.32f * (GU_PI/180.0f) };
sceGumTranslate(&pos);
sceGumRotateXYZ(&rot);
}
// setup texture
sceGuTexMode(GU_PSM_4444,0,0,0);
sceGuTexImage(0,64,64,64,logo_start);
sceGuTexFunc(GU_TFX_ADD,GU_TCC_RGB);
sceGuTexEnvColor(0xffff00);
sceGuTexFilter(GU_LINEAR,GU_LINEAR);
sceGuTexScale(1.0f,1.0f);
sceGuTexOffset(0.0f,0.0f);
sceGuAmbientColor(0xffffffff);
// draw cube
sceGumDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,am*3,0,Vertices);
sceGuFinish();
sceGuSync(0,0);
sceDisplayWaitVblankStart();
sceGuSwapBuffers();
val++;
}
sceGuTerm();
return 0;
}
|
Heres the raw file I'm using for testing , the format is Vertex vertex vertex u v Vertex Vertex Vertex u v Vertex Vertex Vertex u v
| Code: | -3.250000 3.250000 3.750000 0.000000 0.000000 -3.250000 -3.250000 3.750000 0.000000 1.000000 3.250000 3.250000 3.750000 1.000000 0.000000
-3.250000 -3.250000 3.750000 0.000000 1.000000 3.250000 -3.250000 3.750000 1.000000 1.000000 3.250000 3.250000 3.750000 1.000000 0.000000
3.250000 3.250000 3.750000 0.000000 0.000000 3.250000 -3.250000 3.750000 0.000000 1.000000 3.250000 3.250000 -2.500000 1.000000 0.000000
3.250000 -3.250000 3.750000 0.000000 1.000000 3.250000 -3.250000 -2.500000 1.000000 1.000000 3.250000 3.250000 -2.500000 1.000000 0.000000
3.250000 3.250000 -2.500000 0.000000 0.000000 3.250000 -3.250000 -2.500000 0.000000 1.000000 -3.250000 3.250000 -2.500000 1.000000 0.000000
3.250000 -3.250000 -2.500000 0.000000 1.000000 -3.250000 -3.250000 -2.500000 1.000000 1.000000 -3.250000 3.250000 -2.500000 1.000000 0.000000
-3.250000 3.250000 -2.500000 0.000000 0.000000 -3.250000 -3.250000 -2.500000 0.000000 1.000000 -3.250000 3.250000 3.750000 1.000000 0.000000
-3.250000 -3.250000 -2.500000 0.000000 1.000000 -3.250000 -3.250000 3.750000 1.000000 1.000000 -3.250000 3.250000 3.750000 1.000000 0.000000
-3.250000 3.250000 -2.500000 0.000000 0.000000 -3.250000 3.250000 3.750000 0.000000 1.000000 3.250000 3.250000 -2.500000 1.000000 0.000000
-3.250000 3.250000 3.750000 0.000000 1.000000 3.250000 3.250000 3.750000 1.000000 1.000000 3.250000 3.250000 -2.500000 1.000000 0.000000
-3.250000 -3.250000 3.750000 0.000000 0.000000 -3.250000 -3.250000 -2.500000 0.000000 1.000000 3.250000 -3.250000 3.750000 1.000000 0.000000
-3.250000 -3.250000 -2.500000 0.000000 1.000000 3.250000 -3.250000 -2.500000 1.000000 1.000000 3.250000 -3.250000 3.750000 1.000000 0.000000
|
|
|