Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Trading Journey
by howardR. 04/24/24 20:04
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, howardR, 1 invisible), 777 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
obj export #185155
02/23/08 00:51
02/23/08 00:51
Joined: May 2002
Posts: 7,441
ventilator Offline OP
Senior Expert
ventilator  Offline OP
Senior Expert

Joined: May 2002
Posts: 7,441
Code:
#include <d3d9.h>
#include <stdio.h>

void export_obj(ENTITY *entity, var scale)
{
STRING* name = str_create("#256");
STRING* objname = str_create("#256");
STRING* mtlname = str_create("#256");
str_for_entfile(name, entity);
str_trunc(name, 4);
str_lwr(name);
str_cpy(objname, name);
str_cpy(mtlname, name);
str_cat(objname, ".obj");
str_cat(mtlname, ".mtl");

int numskins = ent_skins(entity);
int i;

FILE *file = fopen(mtlname->chars, "w");

for(i = 0; i < numskins; i++)
{
fprintf(file, "newmtl material%d\n", i);
BMAP *b = bmap_for_entity(entity, i + 1);
if(b)
{
char s[80];
sprintf(s, "%s%d.bmp", name->chars, i);
bmap_save(b, s);
fprintf(file, "map_Kd %s\n", s);
}
fprintf(file, "\n");
}

fclose(file);



LPD3DXMESH mesh = (LPD3DXMESH)ent_getmesh(entity, 0, 0);
int numvertices = mesh->GetNumVertices();
int numfaces = mesh->GetNumFaces();

FILE *file = fopen(objname->chars, "w");

fprintf(file, "mtllib %s\n", mtlname->chars);
fprintf(file, "o %s\n", name->chars);

D3DVERTEX *vb; mesh->LockVertexBuffer(0, (void**)&vb);
WORD *ib; mesh->LockIndexBuffer(0, (void**)&ib);
DWORD *ab; mesh->LockAttributeBuffer(0, &ab);

for(i = 0; i < numvertices; i++)
fprintf(file, "v %f %f %f\n",
-(double)(vb[i].x * scale), (double)(vb[i].y * scale), (double)(vb[i].z * scale));

for(i = 0; i < numvertices; i++)
fprintf(file, "vt %f %f\n", (double)vb[i].u1, 1 - (double)vb[i].v1);

for(i = 0; i < numvertices; i++)
fprintf(file, "vn %f %f %f\n", -(double)vb[i].nx, (double)vb[i].ny, (double)vb[i].nz);

fprintf(file, "s on\n");

int material, a, b, c;
int previousmaterial = -999;
for(i = 0; i < numfaces; i++)
{
material = ab[i];
if(material != previousmaterial)
fprintf(file, "usemtl material%d\n", material);
previousmaterial = material;

a = ib[i*3+2] + 1;
b = ib[i*3+1] + 1;
c = ib[i*3+0] + 1;
fprintf(file, "f %d/%d/%d %d/%d/%d %d/%d/%d\n", a, a, a, b, b, b, c, c, c);
}

mesh->UnlockVertexBuffer();
mesh->UnlockIndexBuffer();
mesh->UnlockAttributeBuffer();

fclose(file);

str_remove(name);
str_remove(objname);
str_remove(mtlname);
}



a simple obj exporter. it writes out the mesh buffers directly so there will be hard edges at uv-borders if your modeling application recalculates the normals. the exporter could solve this by building an optimized mesh (and by supporting smoothing groups) but it would be a lot more work. in your modeling application you just would have to weld the split vertices at those borders anyway.

Re: obj export [Re: ventilator] #185156
02/23/08 20:11
02/23/08 20:11

A
Anonymous
Unregistered
Anonymous
Unregistered
A



wow! Thanx a lot!
This is very useful.

Re: obj export [Re: ] #185157
03/25/08 13:03
03/25/08 13:03
Joined: Mar 2006
Posts: 1,993
Karlsruhe
PadMalcom Offline
Serious User
PadMalcom  Offline
Serious User

Joined: Mar 2006
Posts: 1,993
Karlsruhe
Yes it is, incredible skill!!! What aboup appended models, meaning when I create a player where the gamer can choose the players head, feet, torso, etc.

If you have a solution for that, you're my current Lite-C god

Re: obj export [Re: PadMalcom] #185158
03/25/08 22:53
03/25/08 22:53
Joined: May 2002
Posts: 7,441
ventilator Offline OP
Senior Expert
ventilator  Offline OP
Senior Expert

Joined: May 2002
Posts: 7,441
this wouldn't be very hard but somebody else has to do it.

Re: obj export [Re: ventilator] #200085
04/02/08 13:54
04/02/08 13:54
Joined: Jun 2004
Posts: 655
to your left
BoH_Havoc Offline
User
BoH_Havoc  Offline
User

Joined: Jun 2004
Posts: 655
to your left
Wow, very nice contribution!
Thanks for this \:\)


Shade-C EVO Lite-C Shader Framework

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1