 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
ttotto
Joined: 26 Nov 2007 Posts: 8
|
Posted: Mon Nov 26, 2007 2:45 am Post subject: .obj loader |
|
|
i have been working on a .obj loader but im having problems:
here's a pic:
www.freewebs.com/ttotto/snap037.bmp
heres the code:
| Code: |
EZ_model* EZ_3d::Load_obj(char *fname)
{
FILE* fp;
fp=fopen(fname,"rb");
if (fp==NULL) return NULL;
int t_verts=0, t_faces=-1;
bool normals=false;// if they are defined on the file or not
bool textures=false;
char buffer[256];
int t_norms=0;
EZ_model* model =(EZ_model*)malloc( sizeof(EZ_model));
model->verts=0;
model->t_verts=0;
model->t_faces=0;
while(!feof(fp))
{
fgets(buffer, sizeof(buffer), fp);
if(buffer[0] == 'v' && buffer[1] == ' ')
{
t_verts++;
}
else if(buffer[0] == 'f' && buffer[1] == ' ')
{
t_faces++;
}
else if(buffer[0] == 'v' && buffer[1] == 'n')
{
normals=true;
t_norms++;
}
else if(buffer[0] == 'v' && buffer[1] == 't')
{
textures=true;
}
}
fseek ( fp , 0 , SEEK_SET );
allVertex tverts[t_verts];
model->t_verts=t_verts;
model->t_faces=t_faces;
model->list = (unsigned int *) malloc(t_faces*3*10);
model->indices=(short*) memalign(16, t_faces * 3 * sizeof(short));
model->verts = (allVertex*) memalign(16, t_faces * 3 * sizeof(allVertex));
short index[t_faces*3];
char tu[4][32];
float tempv[3];
float tempn[3];
int tempf[4];
int tn[3];
int v=0;
int r=0;
int h=0;
int i=0;
while(!feof(fp))
{
fgets(buffer, sizeof(buffer), fp);
if(buffer[0] == 'v' && buffer[1] == ' ')
{
sscanf(buffer,"%*s %s %s %s",tu[0],tu[1],tu[2]);
tempv[0]=atof(tu[0]);
tempv[1]=atof(tu[1]);
tempv[2]=atof(tu[2]);
model->verts[v].x=tempv[0];
model->verts[v].y=tempv[1];
model->verts[v].z=tempv[2];
model->verts[v].color=WHITE(255);
v++;
}
else if(buffer[0] == 'v' && buffer[1] == 'n')
{
sscanf(buffer,"%*s %s %s %s",tu[0],tu[1],tu[2]);
tempn[0]=atof(tu[0]);
tempn[1]=atof(tu[1]);
tempn[2]=atof(tu[2]);
model->verts[h].nx=tempn[0];
model->verts[h].ny=tempn[1];
model->verts[h].nz=tempn[2];
h++;
}
else if(buffer[0] == 'f' && buffer[1] == ' ')
{
if (r<t_faces){
if (normals && !textures){
sscanf(buffer,"%*s %d//%d %d//%d %d//%d",&tempf[0],&tn[0],&tempf[1],&tn[1],&tempf[2],&tn[2]);
i++;
}
else {
sscanf(buffer,"%*s %hd %hd %hd",&index[i*3],&index[i*3+1],&index[i*3+2]);
model->indices[i*3]=index[i*3]-1;
model->indices[i*3+1]=index[i*3+1]-1;
model->indices[i*3+2]=index[i*3+2]-1;
i++;
}
r++;
}
}
}
fclose(fp);
return model;
}
void EZ_model::Draw(){
sceGumDrawArray( GU_TRIANGLES, GU_COLOR_8888|GU_INDEX_16BIT|GU_VERTEX_32BITF|GU_TRANSFORM_3D,
3*t_faces, indices, verts );
}
|
thanks! |
|
| Back to top |
|
 |
Ghoti
Joined: 31 Dec 2005 Posts: 288
|
|
| Back to top |
|
 |
JustChris
Joined: 03 Nov 2005 Posts: 21
|
Posted: Mon Nov 26, 2007 5:20 am Post subject: |
|
|
I have an .obj loader myself and it's almost 100% done, but I could never make a proper file parser the C way :P Vectors, StringStream and FileStream were my tools of choice.
First of all, what kind of shape are you trying to render in the example? Do the number of vertices and faces on the array correspond to the ones on the obj file? Maybe the indices are done out of order, or instead of drawing the set of vertices by index, you are just trying to connect all the unique vertices at once. |
|
| Back to top |
|
 |
ttotto
Joined: 26 Nov 2007 Posts: 8
|
Posted: Mon Nov 26, 2007 5:28 am Post subject: |
|
|
heres my obj file sorry.
| Code: |
# Blender3D v244 OBJ File: <memory>
# www.blender3d.org
mtllib ssdatled.mtl
o Cube_Material
v 2.152884 -2.118817 -2.272749
v 2.158488 -2.118576 2.160933
v -2.275183 -2.128670 2.166537
v -2.280786 -2.128911 -2.267145
v 2.142790 2.314857 -2.272976
v -2.290881 2.304763 -2.267373
v -2.285278 2.305004 2.166308
v 2.148391 2.315098 2.160706
usemtl Material.001
s 1
f 5 1 4
f 5 4 6
f 3 7 4
f 7 6 4
f 2 8 3
f 8 7 3
f 1 5 2
f 5 8 2
f 5 6 7
f 5 7 8
f 1 2 3
f 1 3 4
|
it's just a simple box
all of my verts and indices are loading correctly. Just by looking at it, it should work, but it doesn't. |
|
| Back to top |
|
 |
tommydanger
Joined: 26 Nov 2007 Posts: 2
|
Posted: Mon Nov 26, 2007 5:54 am Post subject: |
|
|
| I made one myself and I had strange glitches too but they were gone after I put a sceKernelDcacheWritebackAll() in front of my rendering code ;) |
|
| Back to top |
|
 |
Ghoti
Joined: 31 Dec 2005 Posts: 288
|
Posted: Mon Nov 26, 2007 6:05 am Post subject: |
|
|
few things:
1.) why start the faces with -1?
2.) It looks like that you save the vertices the same order that you read them in model->verts and try to render those, you do not seem to use the faces. You should save the vertexnumbers belong to the faces and those in order so the first three in verts should be the vertex 5, vertex 1 and vertex4 followed by vertex 5, vertex4 and vertex6.
3.) How is your graphics environment set up ?
greet ghoti
PS mine .obj works but has no strange glitches and does not need the sceKernelDcacheWritebackAll. _________________ My PSP games:
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php |
|
| Back to top |
|
 |
ttotto
Joined: 26 Nov 2007 Posts: 8
|
Posted: Mon Nov 26, 2007 6:05 am Post subject: |
|
|
| i tried adding sceKernelDcacheWritebackAll(); to my draw code and it didn't help. Thanks anyway. Is it posible to see one of your guys code so i can learn what im doing wrong. Thanks for all the help! |
|
| Back to top |
|
 |
tommydanger
Joined: 26 Nov 2007 Posts: 2
|
Posted: Mon Nov 26, 2007 6:24 am Post subject: |
|
|
hmm if you have double checked your imported obj values then the error lies in your rendering code.
when i first executed my obj loader on the psp the output was like this:
simple box
but after I added sceKernelDcacheWritebackAll all was working as expected
well I don't know if it really was sceKernelDcacheWritebackAll but something very simple and my loading code was fine
well it's been 1h year and my code says sceKernelDcacheWritebackAll, I could be reading from an outdated version :/
you clearing the depth buffer? |
|
| Back to top |
|
 |
ttotto
Joined: 26 Nov 2007 Posts: 8
|
Posted: Mon Nov 26, 2007 9:06 am Post subject: Re |
|
|
tommydanger:
Yes i am clearing the depthbuffer.
is it posible you could upload or post your code, It would be a major help.
Ghoti:
1: i use -1 because when i was counting the number of faces, I would always get one more than their really is. so i set it to -1 and its worked fine.
2:i dont need to because i use indices.
3. here is my gu set up:
| Code: |
void InitGU( void ){
fbp0 = 0;
// Init GU
sceGuInit();
sceGuStart( GU_DIRECT, list );
// Set Buffers
sceGuDrawBuffer( GU_PSM_8888, fbp0, 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( 65535, 0);
// Set Render States
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_CLIP_PLANES );
sceGuEnable(GU_TEXTURE_2D);
sceGuTexMode( GU_PSM_8888, 3, 0, GU_TRUE);
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA); // Modulate the color of the image
sceGuTexFilter(GU_NEAREST, GU_NEAREST); // Linear filtering
sceGuTexScale( 1.0f, 1.0f ); // No scaling
sceGuTexOffset( 0.0f, 0.0f );
sceGuAmbientColor(0xffffffff);
sceGuEnable(GU_BLEND);
sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
gumInit();
sceGuFinish();
sceGuSync(0,0);
sceDisplayWaitVblankStart();
sceGuDisplay(GU_TRUE);
// finish
sceGuStart(GU_CALL,list[listNum&1]);
};
|
|
|
| Back to top |
|
 |
ttotto
Joined: 26 Nov 2007 Posts: 8
|
Posted: Mon Nov 26, 2007 10:24 am Post subject: |
|
|
| b |
|
| Back to top |
|
 |
ttotto
Joined: 26 Nov 2007 Posts: 8
|
Posted: Mon Nov 26, 2007 11:28 am Post subject: u |
|
|
Solved:
It was my vertex struct. |
|
| Back to top |
|
 |
Ghoti
Joined: 31 Dec 2005 Posts: 288
|
Posted: Mon Nov 26, 2007 8:12 pm Post subject: Re: Re |
|
|
| ttotto wrote: |
Ghoti:
1: i use -1 because when i was counting the number of faces, I would always get one more than their really is. so i set it to -1 and its worked fine.
|
This will probebly be due to that the last line in the file will be read twice.
I had this problem also. I first solved it by including another line with just crazy text like: This line does nothing. because it does not include one of the start letters you search for. I am busy with a object loader tutorial at the moment and there I am trying to solve it :)
nice to hear it works ! _________________ My PSP games:
Boxy II: http://www.ghoti.nl/boxyii.php
Elementals: http://www.ghoti.nl/Elementals.php |
|
| Back to top |
|
 |
flynn_nrg
Joined: 29 Sep 2007 Posts: 4 Location: Madrid, Spain
|
Posted: Tue Nov 27, 2007 6:03 am Post subject: |
|
|
Since this is a .obj converter thread I might as well put a link to my work-in-progress one. It's BSD licensed and free to use by anybody. It's not 100% done yet as it still doesn't grasp the concept of groups, but works fine with every .obj I've thrown at it.
It's designed to work with Softimage|3D's wavefront exporter output (long story) and thus assumes objects are textured. I didn't bother implementing the negative vertex referencing but, as I said, the code might be useful to others. The generated .h file can be trivially included into the cube.c example.
The code with some samples: obj2psp-0.1.tar.gz |
|
| Back to top |
|
 |
ttotto
Joined: 26 Nov 2007 Posts: 8
|
Posted: Tue Nov 27, 2007 10:00 am Post subject: |
|
|
| thx for your contribution this will help many of those in need. Including ME! |
|
| Back to top |
|
 |
|
|
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
|