update geometry
Forget a bit about this thread till today, so I decided to try it and again and now have the export of geometry of all the entities in a level to 1 obj file working. laugh

I also added the entities scales xyz and pos xyz.

Note that while importing the obj in e.g. med or possible other programs, everything needs to be rotated by 90 or 180 or 270 degrees on xy rotation. But relative to each other, the objects should be positioned and rotated correctly.

Code:

Code:
#include <acknex.h>
#include <default.c>
#include <d3d9.h>
#include <stdio.h>

void export_all_obj(ENTITY *entity, var scale)
{
	STRING* name = str_create("#256");
	STRING* objname = str_create("#256");
	STRING* mtlname = str_create("#256");
	
	int i;
	var beginVertex = 0; //for entities past the first; vertex index is not reset between entities in same obj file 
	
	//get names for obj file
	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");
	
	
//	//MATERIAL + SKINS (of given *entity) 
//	int numskins = ent_skins(entity);	
//	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);
	
	
	//MODEL (geometry of all entities) 
	FILE *file = fopen(objname->chars, "w");
	for(you = ent_next(NULL); you; you = ent_next(you)) 
	{	
		//get names
		str_for_entfile(name, entity);
		str_trunc(name, 4);
		str_lwr(name);
		str_cpy(mtlname, name);
		str_cat(mtlname, ".mtl");
		
		LPD3DXMESH mesh = (LPD3DXMESH)ent_getmesh(you, 0, 0);
		int numvertices = mesh->GetNumVertices();
		int numfaces = mesh->GetNumFaces();
		
		//add object info 
		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 + you.x) * you.scale_x * scale), (double)((vb[i].y + you.z) * you.scale_z * scale), (double)((vb[i].z + you.y) * you.scale_y * 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 + beginVertex;
			b = ib[i*3+1] + 1 + beginVertex;
			c = ib[i*3+0] + 1 + beginVertex;
			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();
		
		beginVertex += numvertices; //for next entities faces
	}
	
	fclose(file);
	str_remove(name);
	str_remove(objname);
	str_remove(mtlname);
}



Call it through "export_all_obj(entitypointer, scale);" without the quotes and for entitypointer use the pointer to the entity you want for the name of the file. So e.g. if write "player" there (without the quotes) and the player entity's modelfile name is called player.mdl, the obj file is named player.obj.
And for "scale" type in some number depending on how small or big you want the obj file in other programs. If you dont know what to choose here, just type in 1.

Note: the exported mesh appears to be not always closed. So far it seems to be non-closed for higher poly models but I need to check that to be sure.

Note2: this is only geometry, not yet skins and materials.

Last edited by Reconnoiter; 04/22/16 13:17.