3D Rendering Problem

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Sonicadvance1
Posts: 10
Joined: Thu Oct 13, 2005 8:35 am

3D Rendering Problem

Post by Sonicadvance1 »

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: Select all

#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 &#40;512&#41;
#define SCR_WIDTH &#40;480&#41;
#define SCR_HEIGHT &#40;272&#41;
PSP_MODULE_INFO&#40;"Project 4", 0, 1, 1&#41;;
extern unsigned char logo_start&#91;&#93;;
#define printf	pspDebugScreenPrintf
int done=0;
struct Vertex
&#123;
	float u, v;
	unsigned int color;
	float x,y,z;
&#125;;
	struct Vertex __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; Vertices&#91;10000&#93;;
	static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
    sceKernelExitGame&#40;&#41;;
	return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
    int cbid;
    cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
    sceKernelRegisterExitCallback&#40;cbid&#41;;
    sceKernelSleepThreadCB&#40;&#41;;

	return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41;
&#123;
    int thid = 0;
    thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, THREAD_ATTR_USER, 0&#41;;
    if &#40;thid >= 0&#41;
	sceKernelStartThread&#40;thid, 0, 0&#41;;
    return thid;
&#125;
static unsigned int staticOffset = 0;

static unsigned int getMemorySize&#40;unsigned int width, unsigned int height, unsigned int psm&#41;
&#123;
	switch &#40;psm&#41;
	&#123;
		case GU_PSM_T4&#58;
			return &#40;width * height&#41; >> 1;

		case GU_PSM_T8&#58;
			return width * height;

		case GU_PSM_5650&#58;
		case GU_PSM_5551&#58;
		case GU_PSM_4444&#58;
		case GU_PSM_T16&#58;
			return 2 * width * height;

		case GU_PSM_8888&#58;
		case GU_PSM_T32&#58;
			return 4 * width * height;

		default&#58;
			return 0;
	&#125;
&#125;
void* getStaticVramBuffer&#40;unsigned int width, unsigned int height, unsigned int psm&#41;
&#123;
	unsigned int memSize = getMemorySize&#40;width,height,psm&#41;;
	void* result = &#40;void*&#41;staticOffset;
	staticOffset += memSize;

	return result;
&#125;
int
Random &#40;int low, int high&#41;
&#123;
  return &#40;rand&#40;&#41; % &#40;high + 1&#41;&#41;;
&#125;
int main&#40;int argc, char **argv&#41;
&#123;
	SetupCallbacks&#40;&#41;;
	pspDebugScreenInit&#40;&#41;;
	int am=0;
	int selected=0;
	char str&#91;1028&#93;;
	FILE *fp;
	fp=fopen&#40;"ms0&#58;/Test.raw", "r"&#41;;
	while&#40;fgets&#40;str, 1024, fp&#41; != NULL&#41;
	&#123;	
	sscanf&#40;str,"%f %f %f %f %f   %f %f %f %f %f   %f %f %f %f %f\n",\
	&Vertices&#91;am&#93;.x,&Vertices&#91;am&#93;.y,&Vertices&#91;am&#93;.z,&Vertices&#91;am&#93;.u,&Vertices&#91;am&#93;.v,\
	&Vertices&#91;am+1&#93;.x,&Vertices&#91;am+1&#93;.y,&Vertices&#91;am+1&#93;.z,&Vertices&#91;am+1&#93;.u,&Vertices&#91;am+1&#93;.v,\
	&Vertices&#91;am+2&#93;.x,&Vertices&#91;am+2&#93;.y,&Vertices&#91;am+2&#93;.z,&Vertices&#91;am+2&#93;.u,&Vertices&#91;am+2&#93;.v&#41;;
	Vertices&#91;am&#93;.color=Random&#40;1,0xFFFFFF&#41;;
	Vertices&#91;am+1&#93;.color=Random&#40;1,0xFFFFFF&#41;;
	Vertices&#91;am+2&#93;.color=Random&#40;1,0xFFFFFF&#41;;
	am+=3;
	&#125;
	am-=3;
	printf&#40;"%s",str&#41;;
	SceCtrlData pad;

	sceCtrlSetSamplingCycle&#40;0&#41;;
	sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;
			void* fbp0 = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888&#41;;
	void* fbp1 = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888&#41;;
	void* zbp = getStaticVramBuffer&#40;BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444&#41;;

	sceGuInit&#40;&#41;;

	sceGuStart&#40;GU_DIRECT,list&#41;;
	sceGuDrawBuffer&#40;GU_PSM_8888,fbp0,BUF_WIDTH&#41;;
	sceGuDispBuffer&#40;SCR_WIDTH,SCR_HEIGHT,fbp1,BUF_WIDTH&#41;;
	sceGuDepthBuffer&#40;zbp,BUF_WIDTH&#41;;
	sceGuOffset&#40;2048 - &#40;SCR_WIDTH/2&#41;,2048 - &#40;SCR_HEIGHT/2&#41;&#41;;
	sceGuViewport&#40;2048,2048,SCR_WIDTH,SCR_HEIGHT&#41;;
	sceGuDepthRange&#40;65535,0&#41;;
	sceGuScissor&#40;0,0,SCR_WIDTH,SCR_HEIGHT&#41;;
	sceGuEnable&#40;GU_SCISSOR_TEST&#41;;
	sceGuDepthFunc&#40;GU_GEQUAL&#41;;
	sceGuEnable&#40;GU_DEPTH_TEST&#41;;
	sceGuFrontFace&#40;GU_CW&#41;;
	sceGuShadeModel&#40;GU_SMOOTH&#41;;
	sceGuEnable&#40;GU_CULL_FACE&#41;;
	sceGuEnable&#40;GU_TEXTURE_2D&#41;;
	sceGuEnable&#40;GU_CLIP_PLANES&#41;;
	sceGuFinish&#40;&#41;;
	sceGuSync&#40;0,0&#41;;

	sceDisplayWaitVblankStart&#40;&#41;;
	sceGuDisplay&#40;GU_TRUE&#41;;
	int val = 0;
	float z=0;
	while&#40;done==0&#41;&#123;
	sceCtrlReadBufferPositive&#40;&pad, 1&#41;; 
	if&#40;pad.Buttons & PSP_CTRL_UP&#41;
		z+=0.1f;
	if&#40;pad.Buttons & PSP_CTRL_DOWN&#41;
		z-=0.1f;
		
		sceGuStart&#40;GU_DIRECT,list&#41;;

		// clear screen

		sceGuClearColor&#40;0xff554433&#41;;
		sceGuClearDepth&#40;0&#41;;
		sceGuClear&#40;GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT&#41;;

		// setup matrices for cube

		sceGumMatrixMode&#40;GU_PROJECTION&#41;;
		sceGumLoadIdentity&#40;&#41;;
		sceGumPerspective&#40;75.0f,16.0f/9.0f,0.5f,1000.0f&#41;;

		sceGumMatrixMode&#40;GU_VIEW&#41;;
		sceGumLoadIdentity&#40;&#41;;

		sceGumMatrixMode&#40;GU_MODEL&#41;;
		sceGumLoadIdentity&#40;&#41;;
		&#123;
			ScePspFVector3 pos = &#123; 0, 0, z &#125;;
			ScePspFVector3 rot = &#123; val * 0.79f * &#40;GU_PI/180.0f&#41;, val * 0.98f * &#40;GU_PI/180.0f&#41;, val * 1.32f * &#40;GU_PI/180.0f&#41; &#125;;
			sceGumTranslate&#40;&pos&#41;;
			sceGumRotateXYZ&#40;&rot&#41;;
		&#125;

		// setup texture

		sceGuTexMode&#40;GU_PSM_4444,0,0,0&#41;;
		sceGuTexImage&#40;0,64,64,64,logo_start&#41;;
		sceGuTexFunc&#40;GU_TFX_ADD,GU_TCC_RGB&#41;;
		sceGuTexEnvColor&#40;0xffff00&#41;;
		sceGuTexFilter&#40;GU_LINEAR,GU_LINEAR&#41;;
		sceGuTexScale&#40;1.0f,1.0f&#41;;
		sceGuTexOffset&#40;0.0f,0.0f&#41;;
		sceGuAmbientColor&#40;0xffffffff&#41;;
		
		// draw cube

		sceGumDrawArray&#40;GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,am*3,0,Vertices&#41;;

		sceGuFinish&#40;&#41;;
		sceGuSync&#40;0,0&#41;;

		sceDisplayWaitVblankStart&#40;&#41;;
		sceGuSwapBuffers&#40;&#41;;
		val++;

	&#125;
	sceGuTerm&#40;&#41;;
	return 0;
&#125;
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: Select all

-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
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

sceGuFrontFace(GU_CW);
sceGuEnable(GU_CULL_FACE);
it won't show the back-triangles cause you are culling em

greets
lumo
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Post Reply