faces over vertices, 'smart' sceGumDrawArray?

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

Moderators: cheriff, TyRaNiD

Post Reply
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

faces over vertices, 'smart' sceGumDrawArray?

Post by LuMo »

hello, i am still strugglin' with my code,
what i am thinking about is i got the list of vertices that are in a mesh
and i got the faces (actually an array of short int which holds the position of the vertices for a trianlge)

so one face might look like this:
3 2 4
triangle #3 #2 #4

further i pass those values like

Code: Select all

sceGumDrawArray(GU_TRIANGLES,GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,number_of_faces*3,faces,vertices);
am i trying something that is not possible?

greets
lumo
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
memon
Posts: 63
Joined: Mon Oct 03, 2005 10:51 pm

Post by memon »

You are missing something :) The pspgu.h is at the moment the best documentation there is to this stuff.

sceGumDrawArray(GU_TRIANGLES,GU_INDEX_16BIT|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,number_of_faces*3,faces,vertices);
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

true, forgot that, but still no go...
i pasted my code online now... (will be open source anyway, when its running...)
you can access/read my code here or read it in this thread

here you can download the files:
mymeshtest.cpp
Makefile
test.3db

Code: Select all

#include <pspkernel.h>
#include <pspiofilemgr.h> //new
#include <malloc.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <pspctrl.h>

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include <pspgu.h>
#include <pspgum.h>

PSP_MODULE_INFO&#40;"3D Mesh Test", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;

#define printf	pspDebugScreenPrintf

#define BUF_WIDTH &#40;512&#41;
#define SCR_WIDTH &#40;480&#41;
#define SCR_HEIGHT &#40;272&#41;
#define PIXEL_SIZE &#40;4&#41; /* change this if you change to another screenmode */
#define FRAME_SIZE &#40;BUF_WIDTH * SCR_HEIGHT * PIXEL_SIZE&#41;
#define ZBUF_SIZE &#40;BUF_WIDTH SCR_HEIGHT * 2&#41; /* zbuffer seems to be 16-bit? */


/* PSP-REPRESENTATION OF A POINT */
struct Vertex
&#123;
	//float u, v; // UV-texture
	unsigned int color;// = 0xff7f0000; //static AABBGGRR
	float x,y,z;
&#125;;

struct Point
&#123;
	float x,y,z;
&#125;;

/*
struct Vector
&#123;
	float x,y,z;
&#125;;

inline Vector Normalize&#40; Vector &v &#41;
&#123;
  float temp = 1/Length&#40; v &#41;;
  v.x *= temp;
  v.y *= temp;
  v.z *= temp;
  return&#40; v &#41;;
&#125;

//calculate normal verctor and normalize it
inline Vector CrossProduct&#40; Vector &v1, Vector &v2 &#41; 
&#123;
  Vector v;
  v.x = &#40; v1.y * v2.z &#41; – &#40; v1.z * v2.y &#41;;
  v.y = &#40; v1.z * v2.x &#41; – &#40; v1.x * v2.z &#41;;
  v.z = &#40; v1.x * v2.y &#41; – &#40; v1.y * v2.x &#41;;
  return Normalize&#40; &v &#41;;
&#125;

*/

static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;
unsigned short __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; *faces;
struct Point __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; *points;
struct Vertex __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; *vertices;

struct Vertex __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; *vertex_render_list;
// add coloredpoints at v2
int SetupCallbacks&#40;&#41;;



int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	SceCtrlData pad;
	unsigned long number_of_vertices;
	unsigned long number_of_faces;
	int version;
	SetupCallbacks&#40;&#41;;


	// load model
	//printf&#40;"Loading 3D-Model...\n", number_of_vertices&#41;;
	FILE *fd = fopen&#40;"ms0&#58;/PSP/test.3db","rb"&#41;;
	fseek&#40;fd, 3, SEEK_CUR&#41;; // '3', 'D', 'B'
	fread&#40;&#40;void*&#41;&number_of_vertices, sizeof&#40;unsigned long&#41;,1,fd&#41;; // vertices count
	fread&#40;&#40;void*&#41;&number_of_faces, sizeof&#40;unsigned long&#41;,1,fd&#41;;		 // faces count
	fread&#40;&#40;void*&#41;&version, sizeof&#40;int&#41;,1,fd&#41;;		 									 // version of 3D-Binary mesh
	
	//points=memalign&#40;16,sizeof&#40;float&#41;*number_of_vertices*3&#41;;
	points=&#40;struct Point*&#41;memalign&#40;16,sizeof&#40;struct Point*&#41;*number_of_vertices/3&#41;;
	
	vertices=&#40;struct Vertex*&#41;memalign&#40;16,sizeof&#40;struct Vertex&#41;*number_of_vertices&#41;;
	fread&#40;&#40;void*&#41;points, sizeof&#40;struct Point&#41;,number_of_vertices,fd&#41;;
	//printf&#40;"number_of_vertices = %i\n", number_of_vertices&#41;;
	//printf&#40;"number_of_faces = %i\n", number_of_faces&#41;;
	
	// load the points to the vertices &#40;u,v is set to zero, color is fixed in v1&#41;
	for &#40;unsigned int i=0; i <= number_of_vertices; i++&#41; 
	&#123;
		//vertices&#91;i&#93;.u = 0.0;
		//vertices&#91;i&#93;.v = 0.0;
		vertices&#91;i&#93;.color = 0xff7f0000;
		vertices&#91;i&#93;.x = points&#91;i&#93;.x;
		vertices&#91;i&#93;.y = points&#91;i&#93;.y;
		vertices&#91;i&#93;.z = points&#91;i&#93;.z;
	&#125;
	free&#40;points&#41;;
	sceKernelDcacheWritebackAll&#40;&#41;;
	// load the faces
	faces=&#40;unsigned short*&#41;memalign&#40;16,sizeof&#40;unsigned short&#41;*number_of_faces*3&#41;;
	fread&#40;&#40;void*&#41;faces, sizeof&#40;unsigned short&#41;,number_of_faces*3,fd&#41;;


	// faces hold the number of vertices...
	sceKernelDcacheWritebackAll&#40;&#41;;
	fclose&#40;fd&#41;;


	// setup ctrl
	sceCtrlSetSamplingCycle&#40;0&#41;;
	sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;

	float rx = 0;
	float ry = 0;
	float rz = 0;
	float pz = -20;
	float px = 0;

	// setup GU
	sceGuInit&#40;&#41;;
	sceGuStart&#40;GU_DIRECT,list&#41;;
	sceGuDrawBuffer&#40;GU_PSM_8888,&#40;void*&#41;0,BUF_WIDTH&#41;;
	sceGuDispBuffer&#40;SCR_WIDTH,SCR_HEIGHT,&#40;void*&#41;0x88000,BUF_WIDTH&#41;;
	sceGuDepthBuffer&#40;&#40;void*&#41;0x110000,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;0xc350,0x2710&#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_CCW&#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;;

	// run sample

	for&#40;;;&#41;
	&#123;
		sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
		if&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41; break;

		sceGuStart&#40;GU_DIRECT,list&#41;;

		// clear screen
		sceGuClearColor&#40;0xff000000&#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;;

		if &#40;127-pad.Ly>50&#41; rx+=&#40;&#40;127-pad.Ly&#41;-50&#41; * 0.04f;
		if &#40;127-pad.Ly<-50&#41; rx+=&#40;&#40;127-pad.Ly&#41;+50&#41; * 0.04f;
		if &#40;127-pad.Lx>50&#41; ry+=&#40;&#40;127-pad.Lx&#41;-50&#41; * 0.04f;
		if &#40;127-pad.Lx<-50&#41; ry+=&#40;&#40;127-pad.Lx&#41;+50&#41; * 0.04f;
		if&#40;pad.Buttons & PSP_CTRL_SQUARE&#41;
		&#123;
			//other cmds
		&#125;
		else
		&#123;
			if&#40;pad.Buttons & PSP_CTRL_LTRIGGER&#41; rz-=1.0f;
			if&#40;pad.Buttons & PSP_CTRL_RTRIGGER&#41; rz+=1.0f;
			if&#40;pad.Buttons & PSP_CTRL_UP&#41; pz/=1.05f;
			if&#40;pad.Buttons & PSP_CTRL_DOWN&#41; pz*=1.05f;
			if&#40;pad.Buttons & PSP_CTRL_LEFT&#41; px-=0.1f;
			if&#40;pad.Buttons & PSP_CTRL_RIGHT&#41; px+=0.1f;
		&#125;
		ScePspFVector3 pos = &#123; px, 0, pz &#125;;
		ScePspFVector3 rot = &#123; rx * 1.00f * &#40;M_PI/180.0f&#41;, ry * 1.00f * &#40;M_PI/180.0f&#41;, rz * &#40;M_PI/180.0f&#41;&#125;;
		sceGumRotateXYZ&#40;&rot&#41;;
		sceGumTranslate&#40;&pos&#41;;

		// draw cube
		//sceGumDrawArray&#40;GU_TRIANGLES,GU_INDEX_16BIT|GU_TEXTURE_32BITF|GU_NORMAL_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_3D,number_of_faces*3,faces,vertices&#41;;

		sceGumDrawArray&#40;GU_TRIANGLES,
        GU_INDEX_16BIT|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,
        number_of_faces*3,
        faces,
        vertices&#41;;
		
		sceGuFinish&#40;&#41;;

		sceGuSync&#40;0,0&#41;;
		sceDisplayWaitVblankStart&#40;&#41;;
		sceGuSwapBuffers&#40;&#41;;
	&#125;

	sceGuTerm&#40;&#41;;

	sceKernelExitGame&#40;&#41;;
	return 0;
&#125;

/* Exit callback */
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, 0, 0&#41;;
	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, 0, 0&#41;;
	&#125;

	return thid;
&#125;

the makefile is like:

Code: Select all

TARGET = mymeshtest
OBJS = mymeshtest.o

INCDIR =
CFLAGS = -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

LIBDIR =
LDFLAGS =
LIBS= -lpspgum -lpspgu -lm

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = 3dloadtest

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak

"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
ufoz
Posts: 86
Joined: Thu Nov 10, 2005 2:36 am
Location: Tokyo
Contact:

Post by ufoz »

You should disable GU_TEXTURE_2D if you're not using a texture.
As for the cache writeback, since your mesh data doesn't change each frame, theoretically it should be enough to flush the cache only once, after you're done loading your model. (but maybe someone can correct me if I'm wrong)
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

ok i removed the 2d-texture and the chachewritebacks...
still the same... app crashes...

hum possibly i found the bug... (omg, yes! its in the loadin' part *outch*)
it crashes in line: free(points);
but WHY?

greets
lumo
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

Usually happens because you've overwritten the end of the allocation by mistake...

Code: Select all

points=&#40;struct Point*&#41;memalign&#40;16,sizeof&#40;struct Point*&#41;*number_of_vertices/3&#41;;
...
fread&#40;&#40;void*&#41;points, sizeof&#40;struct Point&#41;,number_of_vertices,fd&#41;; 
Aye Caramba!

Are you sure that shouldn't be

Code: Select all

points=&#40;struct Point*&#41;memalign&#40;16,sizeof&#40;struct Point&#41;*number_of_vertices&#41;;
Jim
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

you are right, thats another mistake by me :)
but still the same, fucks up at free(points);

note: even if i remove the free line program fucks up at psp...

greets
lumo
PS: i changed it so i can execute it on my computer and just printf the stuff...

Code: Select all

	printf&#40;"freein' points... "&#41;;
	free&#40;points&#41;; //FUCKS UP THE PROGRAM, WHY?
	printf&#40;"done.\n"&#41;;
program output at this part is:
$ testload.exe
:number_of_vertices = 8
:number_of_faces = 12
freein' points...
Home@lumo ~/
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

Code: Select all

vertices=&#40;struct Vertex*&#41;memalign&#40;16,sizeof&#40;struct Vertex&#41;*number_of_vertices&#41;; 
...
for &#40;unsigned int i=0; i <= number_of_vertices; i++&#41; 
One out here too...

Jim
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

ok i now took out the loading part to have a look at it alone...
after some testing i found something strange...

first:
load the points to memory -> fine (test per output)

Code: Select all

	for &#40;unsigned int i=0; i < number_of_vertices; i++&#41; 
	&#123;
		printf&#40;"&#123;%f, %f, %f&#125;,\n", points&#91;i&#93;.x, points&#91;i&#93;.y, points&#91;i&#93;.z&#41;;
	&#125;
the line below (note: NO code between...)

Code: Select all

	for &#40;unsigned int i=0; i < number_of_vertices; i++&#41; 
	&#123;
		vertices&#91;i&#93;.u = 0.0;
		vertices&#91;i&#93;.v = 0.0;
		vertices&#91;i&#93;.color = 0xff7f0000;
		vertices&#91;i&#93;.x = points&#91;i&#93;.x;
		vertices&#91;i&#93;.y = points&#91;i&#93;.y;
		vertices&#91;i&#93;.z = points&#91;i&#93;.z;
	&#125;
	for &#40;unsigned int i=0; i < number_of_vertices; i++&#41; 
	&#123;
		printf&#40;"&#123;%f, %f, %u, %f, %f, %f&#125;,\n", vertices&#91;i&#93;.u, vertices&#91;i&#93;.v, vertices&#91;i&#93;.color, vertices&#91;i&#93;.x, vertices&#91;i&#93;.y, vertices&#91;i&#93;.z&#41;;
	&#125;
and now the misterious part!
vertices do NOT hold the values from above!
ok they do for the first 12values but after those, they are fucked up...
code and output, displayed on cmd
last but not least the src, that should be in memory
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

This is STILL wrong

Code: Select all

points=&#40;struct Point*&#41;memalign&#40;16,sizeof&#40;struct Point*&#41;*number_of_vertices&#41;;
It should be

Code: Select all

points=&#40;struct Point*&#41;memalign&#40;16,sizeof&#40;struct Point&#41;*number_of_vertices&#41;;
Jim
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

great!
its no more crashing, although the result is not what i expected :D

the loaded mesh is no cube...
possibly the index starts at 0 not at 1 as i did it...
going to figure that out...
if some one knows for sure, let me know

EDIT: i was right, had to decrease the indices per one, so starts at zero, works fine now although it looks bit stretched...
values are proper so it must be correct :D

THANKS for your great support!

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