Custom model viewer based off cube sdk sample - help

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

Moderators: cheriff, TyRaNiD

neuro.sys
Posts: 12
Joined: Mon Oct 03, 2005 7:44 am
Contact:

Custom model viewer based off cube sdk sample - help

Post by neuro.sys »

Hello, I am trying to use the cube example in the sdk as a base to create a viewer on the PSP. I eventually want to export out models from 3dsmax to the PSP. First, I am trying to get a basic understanding of what is happening in the sample:

I am assuming the cube is constructed with this:

struct Vertex __attribute__((aligned(16))) vertices[12*3] =
{
{0, 0, 0xff7f0000,-1,-1, 1}, // 0
{1, 0, 0xff7f0000,-1, 1, 1}, // 4
{1, 1, 0xff7f0000, 1, 1, 1}, // 5

{0, 0, 0xff7f0000,-1,-1, 1}, // 0
{1, 1, 0xff7f0000, 1, 1, 1}, // 5
{0, 1, 0xff7f0000, 1,-1, 1}, // 1

{0, 0, 0xff7f0000,-1,-1,-1}, // 3
{1, 0, 0xff7f0000, 1,-1,-1}, // 2
{1, 1, 0xff7f0000, 1, 1,-1}, // 6

{0, 0, 0xff7f0000,-1,-1,-1}, // 3
{1, 1, 0xff7f0000, 1, 1,-1}, // 6
{0, 1, 0xff7f0000,-1, 1,-1}, // 7

{0, 0, 0xff007f00, 1,-1,-1}, // 0
{1, 0, 0xff007f00, 1,-1, 1}, // 3
{1, 1, 0xff007f00, 1, 1, 1}, // 7

{0, 0, 0xff007f00, 1,-1,-1}, // 0
{1, 1, 0xff007f00, 1, 1, 1}, // 7
{0, 1, 0xff007f00, 1, 1,-1}, // 4

{0, 0, 0xff007f00,-1,-1,-1}, // 0
{1, 0, 0xff007f00,-1, 1,-1}, // 3
{1, 1, 0xff007f00,-1, 1, 1}, // 7

{0, 0, 0xff007f00,-1,-1,-1}, // 0
{1, 1, 0xff007f00,-1, 1, 1}, // 7
{0, 1, 0xff007f00,-1,-1, 1}, // 4

{0, 0, 0xff00007f,-1, 1,-1}, // 0
{1, 0, 0xff00007f, 1, 1,-1}, // 1
{1, 1, 0xff00007f, 1, 1, 1}, // 2

{0, 0, 0xff00007f,-1, 1,-1}, // 0
{1, 1, 0xff00007f, 1, 1, 1}, // 2
{0, 1, 0xff00007f,-1, 1, 1}, // 3

{0, 0, 0xff00007f,-1,-1,-1}, // 4
{1, 0, 0xff00007f,-1,-1, 1}, // 7
{1, 1, 0xff00007f, 1,-1, 1}, // 6

{0, 0, 0xff00007f,-1,-1,-1}, // 4
{1, 1, 0xff00007f, 1,-1, 1}, // 6
{0, 1, 0xff00007f, 1,-1,-1}, // 5
};

Since there are twelve faces in a cube, and I am guessing that each block of three lines is setting up the 3 verts for each face. My first question is what does each block mean? Are they x,y,x coordinates or some kind of matrix?

Also, I am not sure what the first line is doing, exactly. Is 16 the number of unique verts and 12 the number of faces? Why multiply them?

Thanks for any help!
holger
Posts: 204
Joined: Thu Aug 18, 2005 10:57 am

Post by holger »

see the "struct Vertex" declaration some lines above the array. The libgu include file could also be helpful to understand the layout.

The array defines 12 triangle faces, each containing 3 vertices.
Kro
Posts: 10
Joined: Thu Sep 15, 2005 1:43 pm

Post by Kro »

Just a wild guess, but...
Box has 6 square shaped faces. You have 12 triangles defined (2 for each square)

Code: Select all

+----+
|   /|
|  / |
| /  |
|/   |
+----+
Vertex table holds points (vertices) of each triangle corner (three points per triangle, 12 triangles = 12*3 vertices).
Values may be:
{u, v, color, x, y, z}
u, v: texture coordinates
color - color (0xAARRGGBB - alpha, red, green, blue)
x, y, z - position

Since I can understand this by seeing data only, it is highly possible you have no experience with 3D programming at all. Please read some stuff on 3D data representation before going any further. DirectX of OpenGL manuals would be nice.
neuro.sys
Posts: 12
Joined: Mon Oct 03, 2005 7:44 am
Contact:

thanks for the quick reply

Post by neuro.sys »

I am amazind you guys respond so quickly. Thanks! I have a lot of experience with creating 3d content, and am in the process of learning 3d programming. I figure asking questions on an open forum is a good way to learn 3d programming, but if you know of a book especially applicable to this task I, shoot me the ISBN.
Kro
Posts: 10
Joined: Thu Sep 15, 2005 1:43 pm

Post by Kro »

If you want to get some basic info on OpenGL programming, take a look at NeHe tutorials.

Assuming you're using Windows, get WinAPI documentation as well (Windows API documentation link at the bottom). Starting with 3D programming on Windows/Linux would make it easier (it's better documented, lots of examples, bigger communities). When you know what and how (very important!) you want to do - go back to homebrew creation. :)
neuro.sys
Posts: 12
Joined: Mon Oct 03, 2005 7:44 am
Contact:

ok, some progress

Post by neuro.sys »

So, I wrote a script in 3DS Max that gives me all the vert info off my own model. I kept the vert number the same to make the test simple. Copied the new vert info over the old in the sample file, compiled and popped onto the PSP and it works. So, my pipeline (even though rough) works. I can get a 12 face model from 3DS Max onto the PSP. So, now the question is, how do I make this general? Can I make an exteral file that has all my vert info on it and read in that data? What are the first steps to lead me to be able to do that?

Thanks!
ashleydb
Posts: 26
Joined: Mon Oct 03, 2005 2:06 am
Location: USA
Contact:

Post by ashleydb »

How did you get the new data to copy/paste in the first place? You just use that to write out a text file with that data in it. Just make a .h file and write out the text so it looks just like the cube data in that example, then include that file in your project. Simple.
www.PSP-Files.com - PSP News, Hacks etc.

www.HiAsh.com - My work and stuff
neuro.sys
Posts: 12
Joined: Mon Oct 03, 2005 7:44 am
Contact:

Post by neuro.sys »

My MaxScript spits out data from 3DS Max, then in Dev-C++ I overwrote the original data in the cube.c sample file with my own data. That was just for a test. Now, I want to make the program read in another file so I can eventually have a menu offering choices as to which model to view.


This is the sample cube code that I pasted my own data into:

Code: Select all

/*
 * PSP Software Development Kit - http://www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * Copyright (c) 2005 Jesper Svennevid
 */

#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

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

PSP_MODULE_INFO&#40;"Tri Sample", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;

#define printf	pspDebugScreenPrintf

static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;
extern unsigned char logo_start&#91;&#93;;

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;12*3&#93; =
&#123;
&#123;0.5,1.0, 0xff7f0000, 2.15068e-007, 0.035955, 0.0 &#125;, //1
&#123;0.0,0.0, 0xff7f0000, 0.634508, 1.30497, -0.634508 &#125;, //2
&#123;1.0,0.0, 0xff7f0000, -0.634508, 1.30497, -0.634508 &#125;, //3
&#123;0.5,1.0, 0xff7f0000, 2.15068e-007, 0.035955, 0.0 &#125;, //1
&#123;1.0,0.0, 0xff7f0000, -0.634508, 1.30497, -0.634508 &#125;, //3
&#123;0.0,0.0, 0xff7f0000, -0.634508, 1.30497, 0.634508 &#125;, //4
&#123;0.5,1.0, 0xff7f0000, 2.15068e-007, 0.035955, 0.0 &#125;, //1
&#123;0.0,0.0, 0xff7f0000, -0.634508, 1.30497, 0.634508 &#125;, //4
&#123;1.0,0.0, 0xff7f0000, 0.634508, 1.30497, 0.634508 &#125;, //5
&#123;0.5,1.0, 0xff7f0000, 2.15068e-007, 0.035955, 0.0 &#125;, //1
&#123;1.0,0.0, 0xff7f0000, 0.634508, 1.30497, 0.634508 &#125;, //5
&#123;0.0,0.0, 0xff7f0000, 0.634508, 1.30497, -0.634508 &#125;, //2
&#123;0.0,0.0, 0xff7f0000, 0.634508, 1.30497, -0.634508 &#125;, //2
&#123;0.0,1.0, 0xff7f0000, 0.0, 1.30497, 0.0 &#125;, //6
&#123;1.0,0.0, 0xff7f0000, -0.634508, 1.30497, -0.634508 &#125;, //3
&#123;1.0,0.0, 0xff7f0000, -0.634508, 1.30497, -0.634508 &#125;, //3
&#123;0.0,1.0, 0xff7f0000, 0.0, 1.30497, 0.0 &#125;, //6
&#123;0.0,0.0, 0xff7f0000, -0.634508, 1.30497, 0.634508 &#125;, //4
&#123;0.0,0.0, 0xff7f0000, -0.634508, 1.30497, 0.634508 &#125;, //4
&#123;0.0,1.0, 0xff7f0000, 0.0, 1.30497, 0.0 &#125;, //6
&#123;1.0,0.0, 0xff7f0000, 0.634508, 1.30497, 0.634508 &#125;, //5
&#123;1.0,0.0, 0xff7f0000, 0.634508, 1.30497, 0.634508 &#125;, //5
&#123;0.0,1.0, 0xff7f0000, 0.0, 1.30497, 0.0 &#125;, //6
&#123;0.0,0.0, 0xff7f0000, 0.634508, 1.30497, -0.634508 &#125;, //2
&#123;1.0,1.0, 0xff7f0000, 0.0, -0.035955, 0.0 &#125;, //7
&#123;1.0,0.0, 0xff7f0000, -0.634508, -1.30497, -0.634508 &#125;, //8
&#123;0.0,0.0, 0xff7f0000, 0.634508, -1.30497, -0.634508 &#125;, //9
&#123;1.0,1.0, 0xff7f0000, 0.0, -0.035955, 0.0 &#125;, //7
&#123;0.0,0.0, 0xff7f0000, 0.634508, -1.30497, -0.634508 &#125;, //9
&#123;0.5,0.5, 0xff7f0000, 0.634508, -1.30497, 0.634508 &#125;, //10
&#123;1.0,1.0, 0xff7f0000, 0.0, -0.035955, 0.0 &#125;, //7
&#123;0.5,0.5, 0xff7f0000, 0.634508, -1.30497, 0.634508 &#125;, //10
&#123;0.5,1.0, 0xff7f0000, -0.634508, -1.30497, 0.634508 &#125;, //11
&#123;1.0,1.0, 0xff7f0000, 0.0, -0.035955, 0.0 &#125;, //7
&#123;0.5,1.0, 0xff7f0000, -0.634508, -1.30497, 0.634508 &#125;, //11
&#123;1.0,0.0, 0xff7f0000, -0.634508, -1.30497, -0.634508 &#125;, //8
&#123;1.0,0.0, 0xff7f0000, -0.634508, -1.30497, -0.634508 &#125;, //8
&#123;0.5,1.0, 0xff7f0000, 0.0, -1.30497, 0.0 &#125;, //12
&#123;0.0,0.0, 0xff7f0000, 0.634508, -1.30497, -0.634508 &#125;, //9
&#123;0.0,0.0, 0xff7f0000, 0.634508, -1.30497, -0.634508 &#125;, //9
&#123;0.5,1.0, 0xff7f0000, 0.0, -1.30497, 0.0 &#125;, //12
&#123;0.5,0.5, 0xff7f0000, 0.634508, -1.30497, 0.634508 &#125;, //10
&#123;0.5,0.5, 0xff7f0000, 0.634508, -1.30497, 0.634508 &#125;, //10
&#123;0.5,1.0, 0xff7f0000, 0.0, -1.30497, 0.0 &#125;, //12
&#123;0.5,1.0, 0xff7f0000, -0.634508, -1.30497, 0.634508 &#125;, //11
&#123;0.5,1.0, 0xff7f0000, -0.634508, -1.30497, 0.634508 &#125;, //11
&#123;0.5,1.0, 0xff7f0000, 0.0, -1.30497, 0.0 &#125;, //12
&#123;1.0,0.0, 0xff7f0000, -0.634508, -1.30497, -0.634508 &#125;, //8
&#125;;

int SetupCallbacks&#40;&#41;;

#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? */

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	SetupCallbacks&#40;&#41;;

	// 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_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;;

	// run sample

	int val = 0;

	for&#40;;;&#41;
	&#123;
		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, -2.5f &#125;;
			ScePspFVector3 rot = &#123; val * 0.79f * &#40;M_PI/180.0f&#41;, val * 0.98f * &#40;M_PI/180.0f&#41;, val * 1.32f * &#40;M_PI/180.0f&#41; &#125;;
			sceGumRotateXYZ&#40;&rot&#41;;
			sceGumTranslate&#40;&pos&#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,12*3,0,vertices&#41;;

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

		sceDisplayWaitVblankStart&#40;&#41;;
		sceGuSwapBuffers&#40;&#41;;

		val++;
	&#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;
In case you were wondering what Maxscript looks like, this is the code:
Max has z up, so I swapped y and z.

Code: Select all

for a = 1 to &#40;polyop.getNumFaces $&#41; do
&#40;
	-- PSP Face Format
	-- &#123;u, v, color, x, y, z&#125; 
	-- u, v&#58; texture coordinates 
	-- color - color &#40;0xAARRGGBB - alpha, red, green, blue&#41; 
	-- x, y, z - position 

	vList = polyop.getFaceVerts $ a
	
	for vFace = 1 to 3 do
	&#40;
		vTemp = polyop.getVert $ vList&#91;vFace&#93;
		UV = polyOp.getMapVert $ 1 vList&#91;vFace&#93;

		format "&#123;%,%, 0xff7f0000, %, %, % &#125;, //%\n" UV.x UV.y &#40;vTemp.x/100&#41; &#40;vTemp.z/100&#41; &#40;vTemp.y/100&#41; &#40;vList&#91;vFace&#93;&#41;
	&#41;
&#41;

So, any links or ideas on how to do file I/O on psp?
ashleydb
Posts: 26
Joined: Mon Oct 03, 2005 2:06 am
Location: USA
Contact:

Post by ashleydb »

Can't you do fileIO from MaxScript? That would make things more general.
www.PSP-Files.com - PSP News, Hacks etc.

www.HiAsh.com - My work and stuff
neuro.sys
Posts: 12
Joined: Mon Oct 03, 2005 7:44 am
Contact:

not exactly

Post by neuro.sys »

What I am trying to do is make a general viewer program that runs on the PSP, and anyone can then use my maxscript exporter to create a file. They would then copy the file to their psp and the model viewer would see that new file and load it up. So, I need to understand code that will see files in a psp folder, and then do a read file on the data.

I hope that makes sense...
Kro
Posts: 10
Joined: Thu Sep 15, 2005 1:43 pm

Post by Kro »

Play with kernel/fileio sample application. You can use it to read your own file format (if needed). Modify MAX script so it saves it in easier to read format, e.g.:

Code: Select all

N
x,y,z,c,u,v
x,y,z,c,u,v
x,y,z,c,u,v
...
N = number of polygons. Read one line from file (N value), allocate vertices table of N*3 size, load following rows and store data inside allocated table. Modify

Code: Select all

sceGumDrawArray&#40;GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices&#41;;
to

Code: Select all

sceGumDrawArray&#40;GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,N*3,0,vertices&#41;;
and voila! Simple loader created. Probably you want to read about MAX model file format and read it directly from PSP instead of using MAX script (few of us have enough money for 3DSMAX for file conversion purposes only ;]).
ashleydb
Posts: 26
Joined: Mon Oct 03, 2005 2:06 am
Location: USA
Contact:

Post by ashleydb »

OK. I thought your problem was saving out the file from Max in the first place. PSP FileIO is documented here: http://psp.jim.sh/pspsdk-doc/group__FileIO.html

Searching on the forum brings some interesting discussions too.
www.PSP-Files.com - PSP News, Hacks etc.

www.HiAsh.com - My work and stuff
neuro.sys
Posts: 12
Joined: Mon Oct 03, 2005 7:44 am
Contact:

Thanks for the feedback

Post by neuro.sys »

Thanks Kro and Ashley, I will take a look at your suggestions. I really appreciate it. Currently, I can paste in an arbitary numer of polys into the code and get it to display with uvs on the PSP, so this next step will get me even closer to final!
neuro.sys
Posts: 12
Joined: Mon Oct 03, 2005 7:44 am
Contact:

Why max?

Post by neuro.sys »

Probably you want to read about MAX model file format and read it directly from PSP instead of using MAX script (few of us have enough money for 3DSMAX for file conversion purposes only ;]).
Well, I create all my 3d models in Max. In fact, most games are developed with 3DS max (60% of all console games) so this will be useful to a lot of developers. The cool thing about this program is that any 3d package that supports scripting can spit out a file similar to what I am doing in Max. Anyway, the Max file format is not something you can read (closed format.)

Just some info...
Tim
Posts: 38
Joined: Tue Jul 12, 2005 4:40 am

Post by Tim »

There's a bunch of 3DS loaders floating around the net. In fact, I'm pretty sure I even saw one for QBasic once. A quick google search reveals there's plenty of information on loading the format, though it may not be the ideal format for storing on the psp.

~Tim
ashleydb
Posts: 26
Joined: Mon Oct 03, 2005 2:06 am
Location: USA
Contact:

Post by ashleydb »

You are better off saving out indexed vertex arrays into a binary file ready to read into your PSP app. Thats what I've been doing with a Maya plugin I wrote to export models for games.
www.PSP-Files.com - PSP News, Hacks etc.

www.HiAsh.com - My work and stuff
holger
Posts: 204
Joined: Thu Aug 18, 2005 10:57 am

Post by holger »

while you're on it... simply convert on-thy-fly all textures into a PSP native format (power-of-two width, ABGR or BGR component ordering, 5650, 5551, 4444 or 8888 component depth). Then you can pass the loaded textures and vertex data directly to the GE without doing any expensive conversion on the PSP.
neuro.sys
Posts: 12
Joined: Mon Oct 03, 2005 7:44 am
Contact:

User ineraction added!

Post by neuro.sys »

Okay, so I am stuck on the file i/o part. All the reference you guys supplied is great, but I need a stupid simple example to really undertand how it works. But, since I was stuck in that part, I want ahead and added button interaction. So, now you can rotate the model with the direction pad, and reset it with the x button.

Code: Select all

/*
 * PSP Software Development Kit - http&#58;//www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * Copyright &#40;c&#41; 2005 Jesper Svennevid
 */

#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

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

#include <pspctrl.h> 

PSP_MODULE_INFO&#40;"Tri Sample", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;

#define printf	pspDebugScreenPrintf

static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;
extern unsigned char logo_start&#91;&#93;;

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;764*3&#93; =
&#123;
// Face&#58; 1
&#123;0.0853705,0.602419, 0xff7f0000, -0.773378, 0.243777, 0.391428 &#125;, //45
&#123;0.104532,0.611974, 0xff7f0000, -0.73747, 0.22865, 0.138037 &#125;, //79
&#123;0.126037,0.667144, 0xff7f0000, -0.697381, 0.333651, 0.147721 &#125;, //24

// Face&#58; 2
&#123;0.126037,0.667144, 0xff7f0000, -0.697381, 0.333651, 0.147721 &#125;, //24
&#123;0.0944046,0.693024, 0xff7f0000, -0.756532, 0.414006, 0.390349 &#125;, //23
&#123;0.0853705,0.602419, 0xff7f0000, -0.773378, 0.243777, 0.391428 &#125;, //45


---  LOTS OF FACES!!!! ---


// Face&#58; 764
&#123;0.643987,0.746828, 0xff7f0000, 0.268526, 0.454134, -0.0763672 &#125;, //403
&#123;0.646995,0.708613, 0xff7f0000, 0.274128, 0.383504, -0.0669522 &#125;, //382
&#123;0.710616,0.701221, 0xff7f0000, 0.392752, 0.36966, -0.0664474 &#125;, //381
&#125;;

int SetupCallbacks&#40;&#41;;

#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? */

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	SetupCallbacks&#40;&#41;;

	// 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_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;;

    SceCtrlData pad; 

	// run sample

	float xval = 0;
    float yval = 0;
    float zval = 0;   

    for&#40;;;&#41;
	&#123;
       sceCtrlReadBufferPositive&#40;&pad, 1&#41;; 
   
       if&#40;pad.Buttons & PSP_CTRL_CROSS&#41; 
       &#123;
        xval=0; yval=0; zval=0;               
       &#125;
       
       if&#40;pad.Buttons & PSP_CTRL_DOWN&#41; 
       &#123;
       	xval = xval + .25;               
       &#125;
       
       if&#40;pad.Buttons & PSP_CTRL_UP&#41; 
       &#123;
       	xval = xval - .25;               
       &#125;
       
       if&#40;pad.Buttons & PSP_CTRL_LEFT&#41; 
       &#123;
       	yval = yval + .25;               
       &#125;
       
       if&#40;pad.Buttons & PSP_CTRL_RIGHT&#41; 
       &#123;
       	yval = yval - .25;               
       &#125;
    

           
		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, -2.5f &#125;;
			ScePspFVector3 rot = &#123;xval, yval, zval&#125;;
			sceGumRotateXYZ&#40;&rot&#41;;
			sceGumTranslate&#40;&pos&#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,764*3,0,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;
So, my question is there a really simple example of file i/o ? I know it must be pretty easy, but I am having trouble wrapping my head around it...
cheriff
Regular
Posts: 258
Joined: Wed Jun 23, 2004 5:35 pm
Location: Sydney.au

Post by cheriff »

first, add in your current code something like this:

Code: Select all

FILE *fp = fopen&#40;filename,"w"&#41;;
int numVerts = whatever;
fwrite&#40;&numVerts,1,sizeof&#40;int&#41;,fp&#41;;
fwrite&#40;vertices, numVerts, sizeof&#40;struct Vertex&#41;, fp&#41;;
fclose&#40;fp&#41;;
EDIT: actually, you could just tweak your max exported script to write out to a similar file format.

which dumps your data to a file, then instead of having the static array:

Code: Select all

FILE *fp = fopen&#40;filename,"r"&#41;;
fread&#40;&numVerts,1,sizeof&#40;int&#41;,fp&#41;;
vertices = memalign&#40;128,numverts * sizeof&#40;struct vertex&#41;&#41;; /* check syntax on this */
fread&#40;vertices, numVerts, sizeof&#40;struct Vertex&#41;, fp&#41;;
fclose&#40;fp&#41;;
which just reads it back to a dynamically allocaed array, all things being good, everything else should just work. Then you could prolly take the the creation code and run it on ur pc to create the data files. (on x86 you shouldnt have to worry about endianness)
Damn, I need a decent signature!
neuro.sys
Posts: 12
Joined: Mon Oct 03, 2005 7:44 am
Contact:

thanks cheriff

Post by neuro.sys »

Thanks for the feedback Cheriff.

I guess I need a little remedial programming help. Lets say to make things simple, I want to read from an asci file I wrote out of 3DSmax. Do I need to include any special .h files for file IO? I am getting a syntax error here:

Code: Select all

FILE *file = fopen&#40;"test.txt", "r" &#41;;
I just want to open a test text file and I can't get it to compile now.

I see pspiofilemgr_kernel.h, and a lot of other files that might be needed, but non of them seem to help my problem when i include them.

Thanks for any help...
cheriff
Regular
Posts: 258
Joined: Wed Jun 23, 2004 5:35 pm
Location: Sydney.au

Post by cheriff »

like most systems (that i've ever seen) the FILE struct is defined in <stdio.h>
The screenshot code could be an example for simple file access (it only writes, but man fwrite or fread on a *nix system/online man pages and you'll see they are kinda similar)
Damn, I need a decent signature!
neuro.sys
Posts: 12
Joined: Mon Oct 03, 2005 7:44 am
Contact:

okay that was right

Post by neuro.sys »

So, that fixed one error, but now I am getting "initializer element is not constant" I googled the problem and got a lot of hits, but none made sense in a file io specific way. Any ideas?
cheriff
Regular
Posts: 258
Joined: Wed Jun 23, 2004 5:35 pm
Location: Sydney.au

Post by cheriff »

you dont have that line outside a function (at topmost level) do you? If you need file to be global:

Code: Select all

FILE *file;
int main&#40;&#41;&#123;
     file = fpen&#40;"data.txt","r"&#41;;
&#125;
This are pretty standard generic c problems not limited to pspdev - perhaps you should brush up on the standary library a little? :)
Damn, I need a decent signature!
neuro.sys
Posts: 12
Joined: Mon Oct 03, 2005 7:44 am
Contact:

yup

Post by neuro.sys »

You nailed it. And yes, my c knowledge is lacking, I will admit. I am really a 3D artist who scripts and I am trying to branch out into coding. And, thanks to your help, I can now read in data from a text file! Yea! Next, I have to make a simple menu to allow users to pick a model from a list. Thanks again for the help.
User avatar
bumpmann
Posts: 3
Joined: Sun Oct 09, 2005 5:03 am

Post by bumpmann »

i made this prog from the cube sample too, but i have some wrong triangles:

Image
(two are wrong but it's not always the sames)

my msh format has no problem (checked with some tech) , nor texture loading and drawing.

here my code:

Code: Select all

#include <pspkernel.h>
#include <malloc.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

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

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

#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? */

struct Vertex
&#123;
	float tu, tv;
	float nx,ny,nz;
	float x,y,z;
&#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; *indices;
unsigned char __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; *tex;
struct Vertex __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; *vertices;

int SetupCallbacks&#40;&#41;;

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	unsigned long nbvert; // vertices count
	unsigned long nbface; // faces count
	char ver&#91;3&#93;; //version &#40;major, minor, release&#41;
	SetupCallbacks&#40;&#41;;

	tex=&#40;unsigned char*&#41;malloc&#40;256*256*4&#41;;
	FILE *fd2 = fopen&#40;"ms0&#58;/PSP/sword.raw", "rb"&#41;;
	fread&#40;&#40;void*&#41;tex, 256*256*4, 1, fd2&#41;;
	fclose&#40;fd2&#41;;

	// load model
	FILE *fd = fopen&#40;"ms0&#58;/PSP/sword.msh","rb"&#41;;
	fseek&#40;fd, 3, SEEK_CUR&#41;; // 'M', 'S', 'H'
	fread&#40;&#40;void*&#41;ver, 3,1,fd&#41;; // major, minor, release
	fread&#40;&#40;void*&#41;&nbvert, 4,1,fd&#41;; // vertices count
	fread&#40;&#40;void*&#41;&nbface, 4,1,fd&#41;; // faces count
	vertices=&#40;struct Vertex*&#41;memalign&#40;16,sizeof&#40;struct Vertex&#41;*nbvert&#41;;
	indices=&#40;unsigned short*&#41;memalign&#40;16,sizeof&#40;unsigned short&#41;*nbface*3&#41;;
	fread&#40;&#40;void*&#41;vertices, sizeof&#40;struct Vertex&#41;,nbvert,fd&#41;;
	fread&#40;&#40;void*&#41;indices, sizeof&#40;unsigned short&#41;,nbface*3,fd&#41;;
	fclose&#40;fd&#41;;

	// 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

	ScePspFVector3 pos = &#123; 50, 20, -50 &#125;;
	ScePspFVector3 rot = &#123; -1.6f,0,0 &#125;;

	for&#40;;;&#41;
	&#123;
		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
		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;;
		sceGumRotateXYZ&#40;&rot&#41;;
		sceGumTranslate&#40;&pos&#41;;

		// setup texture
		sceGuTexMode&#40;GU_PSM_8888,0,0,0&#41;;
		sceGuTexImage&#40;0,256,256,256,tex&#41;;
		sceGuTexFunc&#40;GU_TFX_REPLACE,GU_TCC_RGB&#41;;
		sceGuTexFilter&#40;GU_LINEAR,GU_LINEAR&#41;;

		// draw cube
		sceGumDrawArray&#40;GU_TRIANGLES,GU_INDEX_16BIT|GU_TEXTURE_32BITF|GU_NORMAL_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_3D,nbface*3,indices,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;

... + callbacks
holger
Posts: 204
Joined: Thu Aug 18, 2005 10:57 am

Post by holger »

flush data cache after loading textures and vertices into your arrays, or use uncached pointers to access your arrays.
(can we start some DMA-caching FAQ elaborating somewhere? -- this question becomes quite frequently asked...)
User avatar
bumpmann
Posts: 3
Joined: Sun Oct 09, 2005 5:03 am

Post by bumpmann »

can you explain me how to flush this caches or use uncached pointers? i never used caches. (today it is the first time that i'm using the alignement)
Ratty
Posts: 18
Joined: Sun Sep 18, 2005 12:04 pm

Post by Ratty »

To flush the cache use this

Code: Select all

sceKernelDcacheWritebackAll&#40;&#41;;
Oh and the .3ds format is pretty open (although maybe not offically), i've been using .3ds files for my stuff as it's a relativly simple format to parse.
You can find a lot of the spec here, there maybe more detailed info elsewhere, but anything not documented here is probably little use anyway:

http://www.dcs.ed.ac.uk/home/mxr/gfx/3d/3DS.spec
neuro.sys
Posts: 12
Joined: Mon Oct 03, 2005 7:44 am
Contact:

3ds

Post by neuro.sys »

Yeah, the 3ds format is open, but I was referring to a .max file. 3DS is a pretty old format, and has limits on texture names at 8 chars. The exporter I am working on will be more robust that the 3ds format, but you need max to use it...

Also, someone mentioned earlier about using a png or other file format as texture for a model. Right now I just know how to use a raw file. are there any docs on how to load in another texture type?

Thanks again...

Oh, and one last question, bumpman, what psp screen cap software did you use?
User avatar
bumpmann
Posts: 3
Joined: Sun Oct 09, 2005 5:03 am

Post by bumpmann »

WAAAAAAAW it work =) thx thx thx to Holger and Ratty !!

i don't understand what is the cache but it work =)


neuro> webcam :D
about the other formats, i think we should search for docs about the format and translate the file to a raw... or use libs as pnglib ?

> about the 3ds format: i won't use this format, because i want put others info into my files ^^. and yes i think it is a bit old now.
Post Reply