forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Custom model viewer based off cube sdk sample - help
Goto page 1, 2  Next
 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
neuro.sys



Joined: 03 Oct 2005
Posts: 12

PostPosted: Mon Oct 03, 2005 7:53 am    Post subject: Custom model viewer based off cube sdk sample - help Reply with quote

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!
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
holger



Joined: 18 Aug 2005
Posts: 204

PostPosted: Mon Oct 03, 2005 7:56 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Kro



Joined: 15 Sep 2005
Posts: 10

PostPosted: Mon Oct 03, 2005 8:01 am    Post subject: Reply with quote

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

+----+
|   /|
|  / |
| /  |
|/   |
+----+

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.
Back to top
View user's profile Send private message
neuro.sys



Joined: 03 Oct 2005
Posts: 12

PostPosted: Mon Oct 03, 2005 8:14 am    Post subject: thanks for the quick reply Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Kro



Joined: 15 Sep 2005
Posts: 10

PostPosted: Mon Oct 03, 2005 8:24 am    Post subject: Reply with quote

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. :)
Back to top
View user's profile Send private message
neuro.sys



Joined: 03 Oct 2005
Posts: 12

PostPosted: Mon Oct 03, 2005 10:37 am    Post subject: ok, some progress Reply with quote

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!
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
ashleydb



Joined: 03 Oct 2005
Posts: 26
Location: USA

PostPosted: Mon Oct 03, 2005 11:43 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
neuro.sys



Joined: 03 Oct 2005
Posts: 12

PostPosted: Mon Oct 03, 2005 9:43 pm    Post subject: Reply with quote

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:

/*
 * 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("Tri 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 logo_start[];

struct Vertex
{
   float u, v;
   unsigned int color;
   float x,y,z;
};

struct Vertex __attribute__((aligned(16))) vertices[12*3] =
{
{0.5,1.0, 0xff7f0000, 2.15068e-007, 0.035955, 0.0 }, //1
{0.0,0.0, 0xff7f0000, 0.634508, 1.30497, -0.634508 }, //2
{1.0,0.0, 0xff7f0000, -0.634508, 1.30497, -0.634508 }, //3
{0.5,1.0, 0xff7f0000, 2.15068e-007, 0.035955, 0.0 }, //1
{1.0,0.0, 0xff7f0000, -0.634508, 1.30497, -0.634508 }, //3
{0.0,0.0, 0xff7f0000, -0.634508, 1.30497, 0.634508 }, //4
{0.5,1.0, 0xff7f0000, 2.15068e-007, 0.035955, 0.0 }, //1
{0.0,0.0, 0xff7f0000, -0.634508, 1.30497, 0.634508 }, //4
{1.0,0.0, 0xff7f0000, 0.634508, 1.30497, 0.634508 }, //5
{0.5,1.0, 0xff7f0000, 2.15068e-007, 0.035955, 0.0 }, //1
{1.0,0.0, 0xff7f0000, 0.634508, 1.30497, 0.634508 }, //5
{0.0,0.0, 0xff7f0000, 0.634508, 1.30497, -0.634508 }, //2
{0.0,0.0, 0xff7f0000, 0.634508, 1.30497, -0.634508 }, //2
{0.0,1.0, 0xff7f0000, 0.0, 1.30497, 0.0 }, //6
{1.0,0.0, 0xff7f0000, -0.634508, 1.30497, -0.634508 }, //3
{1.0,0.0, 0xff7f0000, -0.634508, 1.30497, -0.634508 }, //3
{0.0,1.0, 0xff7f0000, 0.0, 1.30497, 0.0 }, //6
{0.0,0.0, 0xff7f0000, -0.634508, 1.30497, 0.634508 }, //4
{0.0,0.0, 0xff7f0000, -0.634508, 1.30497, 0.634508 }, //4
{0.0,1.0, 0xff7f0000, 0.0, 1.30497, 0.0 }, //6
{1.0,0.0, 0xff7f0000, 0.634508, 1.30497, 0.634508 }, //5
{1.0,0.0, 0xff7f0000, 0.634508, 1.30497, 0.634508 }, //5
{0.0,1.0, 0xff7f0000, 0.0, 1.30497, 0.0 }, //6
{0.0,0.0, 0xff7f0000, 0.634508, 1.30497, -0.634508 }, //2
{1.0,1.0, 0xff7f0000, 0.0, -0.035955, 0.0 }, //7
{1.0,0.0, 0xff7f0000, -0.634508, -1.30497, -0.634508 }, //8
{0.0,0.0, 0xff7f0000, 0.634508, -1.30497, -0.634508 }, //9
{1.0,1.0, 0xff7f0000, 0.0, -0.035955, 0.0 }, //7
{0.0,0.0, 0xff7f0000, 0.634508, -1.30497, -0.634508 }, //9
{0.5,0.5, 0xff7f0000, 0.634508, -1.30497, 0.634508 }, //10
{1.0,1.0, 0xff7f0000, 0.0, -0.035955, 0.0 }, //7
{0.5,0.5, 0xff7f0000, 0.634508, -1.30497, 0.634508 }, //10
{0.5,1.0, 0xff7f0000, -0.634508, -1.30497, 0.634508 }, //11
{1.0,1.0, 0xff7f0000, 0.0, -0.035955, 0.0 }, //7
{0.5,1.0, 0xff7f0000, -0.634508, -1.30497, 0.634508 }, //11
{1.0,0.0, 0xff7f0000, -0.634508, -1.30497, -0.634508 }, //8
{1.0,0.0, 0xff7f0000, -0.634508, -1.30497, -0.634508 }, //8
{0.5,1.0, 0xff7f0000, 0.0, -1.30497, 0.0 }, //12
{0.0,0.0, 0xff7f0000, 0.634508, -1.30497, -0.634508 }, //9
{0.0,0.0, 0xff7f0000, 0.634508, -1.30497, -0.634508 }, //9
{0.5,1.0, 0xff7f0000, 0.0, -1.30497, 0.0 }, //12
{0.5,0.5, 0xff7f0000, 0.634508, -1.30497, 0.634508 }, //10
{0.5,0.5, 0xff7f0000, 0.634508, -1.30497, 0.634508 }, //10
{0.5,1.0, 0xff7f0000, 0.0, -1.30497, 0.0 }, //12
{0.5,1.0, 0xff7f0000, -0.634508, -1.30497, 0.634508 }, //11
{0.5,1.0, 0xff7f0000, -0.634508, -1.30497, 0.634508 }, //11
{0.5,1.0, 0xff7f0000, 0.0, -1.30497, 0.0 }, //12
{1.0,0.0, 0xff7f0000, -0.634508, -1.30497, -0.634508 }, //8
};

int SetupCallbacks();

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

int main(int argc, char* argv[])
{
   SetupCallbacks();

   // 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_TEXTURE_2D);
   sceGuEnable(GU_CLIP_PLANES);
   sceGuFinish();
   sceGuSync(0,0);

   sceDisplayWaitVblankStart();
   sceGuDisplay(GU_TRUE);

   // run sample

   int val = 0;

   for(;;)
   {
      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, -2.5f };
         ScePspFVector3 rot = { val * 0.79f * (M_PI/180.0f), val * 0.98f * (M_PI/180.0f), val * 1.32f * (M_PI/180.0f) };
         sceGumRotateXYZ(&rot);
         sceGumTranslate(&pos);
      }

      // 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,12*3,0,vertices);

      sceGuFinish();
      sceGuSync(0,0);

      sceDisplayWaitVblankStart();
      sceGuSwapBuffers();

      val++;
   }

   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;
}


In case you were wondering what Maxscript looks like, this is the code:
Max has z up, so I swapped y and z.

Code:

for a = 1 to (polyop.getNumFaces $) do
(
   -- PSP Face Format
   -- {u, v, color, x, y, z}
   -- u, v: texture coordinates
   -- color - color (0xAARRGGBB - alpha, red, green, blue)
   -- x, y, z - position

   vList = polyop.getFaceVerts $ a
   
   for vFace = 1 to 3 do
   (
      vTemp = polyop.getVert $ vList[vFace]
      UV = polyOp.getMapVert $ 1 vList[vFace]

      format "{%,%, 0xff7f0000, %, %, % }, //%\n" UV.x UV.y (vTemp.x/100) (vTemp.z/100) (vTemp.y/100) (vList[vFace])
   )
)



So, any links or ideas on how to do file I/O on psp?
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
ashleydb



Joined: 03 Oct 2005
Posts: 26
Location: USA

PostPosted: Tue Oct 04, 2005 3:52 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
neuro.sys



Joined: 03 Oct 2005
Posts: 12

PostPosted: Tue Oct 04, 2005 8:19 am    Post subject: not exactly Reply with quote

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...
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Kro



Joined: 15 Sep 2005
Posts: 10

PostPosted: Tue Oct 04, 2005 8:39 am    Post subject: Reply with quote

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:

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:
sceGumDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices);

to
Code:
sceGumDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,N*3,0,vertices);

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 ;]).
Back to top
View user's profile Send private message
ashleydb



Joined: 03 Oct 2005
Posts: 26
Location: USA

PostPosted: Tue Oct 04, 2005 8:39 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
neuro.sys



Joined: 03 Oct 2005
Posts: 12

PostPosted: Tue Oct 04, 2005 11:12 am    Post subject: Thanks for the feedback Reply with quote

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!
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
neuro.sys



Joined: 03 Oct 2005
Posts: 12

PostPosted: Tue Oct 04, 2005 11:55 am    Post subject: Why max? Reply with quote

Quote:
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...
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Tim



Joined: 12 Jul 2005
Posts: 38

PostPosted: Tue Oct 04, 2005 12:25 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
ashleydb



Joined: 03 Oct 2005
Posts: 26
Location: USA

PostPosted: Tue Oct 04, 2005 1:36 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
holger



Joined: 18 Aug 2005
Posts: 204

PostPosted: Tue Oct 04, 2005 6:23 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
neuro.sys



Joined: 03 Oct 2005
Posts: 12

PostPosted: Wed Oct 05, 2005 11:34 am    Post subject: User ineraction added! Reply with quote

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:

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

#include <pspctrl.h>

PSP_MODULE_INFO("Tri 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 logo_start[];

struct Vertex
{
   float u, v;
   unsigned int color;
   float x,y,z;
};

struct Vertex __attribute__((aligned(16))) vertices[764*3] =
{
// Face: 1
{0.0853705,0.602419, 0xff7f0000, -0.773378, 0.243777, 0.391428 }, //45
{0.104532,0.611974, 0xff7f0000, -0.73747, 0.22865, 0.138037 }, //79
{0.126037,0.667144, 0xff7f0000, -0.697381, 0.333651, 0.147721 }, //24

// Face: 2
{0.126037,0.667144, 0xff7f0000, -0.697381, 0.333651, 0.147721 }, //24
{0.0944046,0.693024, 0xff7f0000, -0.756532, 0.414006, 0.390349 }, //23
{0.0853705,0.602419, 0xff7f0000, -0.773378, 0.243777, 0.391428 }, //45


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


// Face: 764
{0.643987,0.746828, 0xff7f0000, 0.268526, 0.454134, -0.0763672 }, //403
{0.646995,0.708613, 0xff7f0000, 0.274128, 0.383504, -0.0669522 }, //382
{0.710616,0.701221, 0xff7f0000, 0.392752, 0.36966, -0.0664474 }, //381
};

int SetupCallbacks();

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

int main(int argc, char* argv[])
{
   SetupCallbacks();

   // 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_TEXTURE_2D);
   sceGuEnable(GU_CLIP_PLANES);
   sceGuFinish();
   sceGuSync(0,0);

   sceDisplayWaitVblankStart();
   sceGuDisplay(GU_TRUE);

    SceCtrlData pad;

   // run sample

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

    for(;;)
   {
       sceCtrlReadBufferPositive(&pad, 1);
   
       if(pad.Buttons & PSP_CTRL_CROSS)
       {
        xval=0; yval=0; zval=0;               
       }
       
       if(pad.Buttons & PSP_CTRL_DOWN)
       {
          xval = xval + .25;               
       }
       
       if(pad.Buttons & PSP_CTRL_UP)
       {
          xval = xval - .25;               
       }
       
       if(pad.Buttons & PSP_CTRL_LEFT)
       {
          yval = yval + .25;               
       }
       
       if(pad.Buttons & PSP_CTRL_RIGHT)
       {
          yval = yval - .25;               
       }
   

           
      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, -2.5f };
         ScePspFVector3 rot = {xval, yval, zval};
         sceGumRotateXYZ(&rot);
         sceGumTranslate(&pos);
      }

      // 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,764*3,0,vertices);

      sceGuFinish();
      sceGuSync(0,0);

      sceDisplayWaitVblankStart();
      sceGuSwapBuffers();

   
   }

   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;
}


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...
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
cheriff
Regular


Joined: 23 Jun 2004
Posts: 262
Location: Sydney.au

PostPosted: Wed Oct 05, 2005 11:55 am    Post subject: Reply with quote

first, add in your current code something like this:
Code:
FILE *fp = fopen(filename,"w");
int numVerts = whatever;
fwrite(&numVerts,1,sizeof(int),fp);
fwrite(vertices, numVerts, sizeof(struct Vertex), fp);
fclose(fp);

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:
FILE *fp = fopen(filename,"r");
fread(&numVerts,1,sizeof(int),fp);
vertices = memalign(128,numverts * sizeof(struct vertex)); /* check syntax on this */
fread(vertices, numVerts, sizeof(struct Vertex), fp);
fclose(fp);


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!
Back to top
View user's profile Send private message
neuro.sys



Joined: 03 Oct 2005
Posts: 12

PostPosted: Wed Oct 05, 2005 3:17 pm    Post subject: thanks cheriff Reply with quote

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:
FILE *file = fopen("test.txt", "r" );


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...
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
cheriff
Regular


Joined: 23 Jun 2004
Posts: 262
Location: Sydney.au

PostPosted: Wed Oct 05, 2005 3:34 pm    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message
neuro.sys



Joined: 03 Oct 2005
Posts: 12

PostPosted: Wed Oct 05, 2005 3:48 pm    Post subject: okay that was right Reply with quote

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?
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
cheriff
Regular


Joined: 23 Jun 2004
Posts: 262
Location: Sydney.au

PostPosted: Wed Oct 05, 2005 4:13 pm    Post subject: Reply with quote

you dont have that line outside a function (at topmost level) do you? If you need file to be global:
Code:
FILE *file;
int main(){
     file = fpen("data.txt","r");
}


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!
Back to top
View user's profile Send private message
neuro.sys



Joined: 03 Oct 2005
Posts: 12

PostPosted: Wed Oct 05, 2005 4:39 pm    Post subject: yup Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
bumpmann



Joined: 09 Oct 2005
Posts: 3

PostPosted: Sun Oct 09, 2005 5:15 am    Post subject: Reply with quote

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


(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:
#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("3D Object Test", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);

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

struct Vertex
{
   float tu, tv;
   float nx,ny,nz;
   float x,y,z;
};

static unsigned int __attribute__((aligned(16))) list[262144];
unsigned short __attribute__((aligned(16))) *indices;
unsigned char __attribute__((aligned(16))) *tex;
struct Vertex __attribute__((aligned(16))) *vertices;

int SetupCallbacks();

int main(int argc, char* argv[])
{
   unsigned long nbvert; // vertices count
   unsigned long nbface; // faces count
   char ver[3]; //version (major, minor, release)
   SetupCallbacks();

   tex=(unsigned char*)malloc(256*256*4);
   FILE *fd2 = fopen("ms0:/PSP/sword.raw", "rb");
   fread((void*)tex, 256*256*4, 1, fd2);
   fclose(fd2);

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

   // 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_CCW);
   sceGuShadeModel(GU_SMOOTH);
   sceGuEnable(GU_CULL_FACE);
   sceGuEnable(GU_TEXTURE_2D);
   sceGuEnable(GU_CLIP_PLANES);
   sceGuFinish();
   sceGuSync(0,0);
   sceDisplayWaitVblankStart();
   sceGuDisplay(GU_TRUE);

   // run sample

   ScePspFVector3 pos = { 50, 20, -50 };
   ScePspFVector3 rot = { -1.6f,0,0 };

   for(;;)
   {
      sceGuStart(GU_DIRECT,list);

      // clear screen
      sceGuClearColor(0xff000000);
      sceGuClearDepth(0);
      sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);

      // setup matrices
      sceGumMatrixMode(GU_PROJECTION);
      sceGumLoadIdentity();
      sceGumPerspective(75.0f,16.0f/9.0f,0.5f,1000.0f);
      sceGumMatrixMode(GU_VIEW);
      sceGumLoadIdentity();
      sceGumMatrixMode(GU_MODEL);
      sceGumLoadIdentity();
      sceGumRotateXYZ(&rot);
      sceGumTranslate(&pos);

      // setup texture
      sceGuTexMode(GU_PSM_8888,0,0,0);
      sceGuTexImage(0,256,256,256,tex);
      sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGB);
      sceGuTexFilter(GU_LINEAR,GU_LINEAR);

      // draw cube
      sceGumDrawArray(GU_TRIANGLES,GU_INDEX_16BIT|GU_TEXTURE_32BITF|GU_NORMAL_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_3D,nbface*3,indices,vertices);

      sceGuFinish();

      sceGuSync(0,0);
      sceDisplayWaitVblankStart();
      sceGuSwapBuffers();
   }

   sceGuTerm();

   sceKernelExitGame();
   return 0;
}

... + callbacks
Back to top
View user's profile Send private message
holger



Joined: 18 Aug 2005
Posts: 204

PostPosted: Sun Oct 09, 2005 5:26 am    Post subject: Reply with quote

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...)
Back to top
View user's profile Send private message
bumpmann



Joined: 09 Oct 2005
Posts: 3

PostPosted: Sun Oct 09, 2005 6:40 am    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message
Ratty



Joined: 18 Sep 2005
Posts: 18

PostPosted: Sun Oct 09, 2005 8:12 am    Post subject: Reply with quote

To flush the cache use this
Code:
sceKernelDcacheWritebackAll();

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
Back to top
View user's profile Send private message
neuro.sys



Joined: 03 Oct 2005
Posts: 12

PostPosted: Sun Oct 09, 2005 1:36 pm    Post subject: 3ds Reply with quote

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?
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
bumpmann



Joined: 09 Oct 2005
Posts: 3

PostPosted: Sun Oct 09, 2005 6:28 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group