How to create 3D mesh?

Posted By: Andre_Fernando

How to create 3D mesh? - 01/26/11 16:38

Hello guys, I'm developing a project there any time and I did want understand how do I do to create a simple cube with the Directx 9. I know to program in Lite-C, but I don't know nothing of Directx 9. Can somebody teach me step by step how work the process of the creation of 3D mesh?

Thanks.
Posted By: fogman

Re: How to create 3D mesh? - 01/26/11 21:55

I canīt give you any advice, because Iīm just fiddling around, too.
But I can give you a hint: Do a forum search for LPD3DXMESH (without date range).
Posted By: Andre_Fernando

Re: How to create 3D mesh? - 01/27/11 19:16

Right, I'm going to search. But what is "LPD3DXMESH"? Is it an important command of Directx?
Posted By: Pappenheimer

Re: How to create 3D mesh? - 01/27/11 19:22

http://www.opserver.de/ubb7/ubbthreads.p...true#Post342595
Posted By: fogman

Re: How to create 3D mesh? - 01/27/11 21:14

Yes, exactly. You should search your way through msdn also:
http://msdn.microsoft.com/en-us/library/bb174069(v=vs.85).aspx
Posted By: Andre_Fernando

Re: How to create 3D mesh? - 01/29/11 16:36

Thanks Pappenheimer, I'm studying the code of link you passed for me. It's hard, but I'm going to try.

fogman: Understood, thanks for help! Directx is don't easy. grin
Posted By: rojart

Re: How to create 3D mesh? - 01/30/11 21:00

Here is an example that shows you step by step how it works, maybe it helps.

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

#define NVB 9  // Vertices
#define NIB 42 // Indexes
#define NAB 14 // Attributes

LPD3DXMESH pMesh;
D3DVERTEX pvb[NVB];
short pib[NIB];
long  pab[NAB];

ENTITY* ent;

MATERIAL* m2sided ={effect="technique TS{pass p0{CullMode=1;}}";}

function recreate_mesh(ENTITY *entity)
{	
	//printf("Total number of mesh vertices = %d", (long)ent_status(entity, 1));
	
	int num_triangles = NAB;
	int num_vertices  = NVB;
	
	// Vertices

	pvb[0].x = -8;pvb[0].y =  8;pvb[0].z = -8; // 1
	pvb[1].x =  8;pvb[1].y =  8;pvb[1].z = -8; // 2
	pvb[2].x =  8;pvb[2].y =  8;pvb[2].z =  8; // 3
	pvb[3].x = -8;pvb[3].y =  8;pvb[3].z =  8; // 4
	pvb[4].x = -8;pvb[4].y = -8;pvb[4].z = -8; // 5
	pvb[5].x =  8;pvb[5].y = -8;pvb[5].z = -8; // 6
	pvb[6].x =  8;pvb[6].y = -8;pvb[6].z =  8; // 7
	pvb[7].x = -8;pvb[7].y = -8;pvb[7].z =  8; // 8
	pvb[8].x =  0;pvb[8].y = 32;pvb[8].z =  0; // 9
	
	// Triangles / Attributes
	
	pib[ 0] = 6;pib[ 1] = 2;pib[ 2] = 7; pab[ 0] = 0;
	pib[ 3] = 3;pib[ 4] = 7;pib[ 5] = 2; pab[ 1] = 1;
	pib[ 6] = 7;pib[ 7] = 3;pib[ 8] = 4; pab[ 2] = 2;
	pib[ 9] = 0;pib[10] = 4;pib[11] = 3; pab[ 3] = 0;
	pib[12] = 4;pib[13] = 0;pib[14] = 5; pab[ 4] = 1;
	pib[15] = 1;pib[16] = 5;pib[17] = 0; pab[ 5] = 2;
	pib[18] = 5;pib[19] = 1;pib[20] = 6; pab[ 6] = 0;
	pib[21] = 2;pib[22] = 6;pib[23] = 1; pab[ 7] = 1;	
	pib[24] = 7;pib[25] = 4;pib[26] = 6; pab[ 8] = 2;
	pib[27] = 5;pib[28] = 6;pib[29] = 4; pab[ 9] = 0;
	pib[30] = 3;pib[31] = 8;pib[32] = 0; pab[10] = 1;
	pib[33] = 2;pib[34] = 8;pib[35] = 3; pab[11] = 2;
	pib[36] = 1;pib[37] = 8;pib[38] = 2; pab[12] = 0;
	pib[39] = 0;pib[40] = 8;pib[41] = 1; pab[13] = 1;
	
	D3DXCreateMesh(num_triangles, num_vertices, D3DXMESH_MANAGED , pvertexdecl, pd3ddev, &pMesh);

	D3DVERTEX *pnewvb;  pMesh->LockVertexBuffer(0, (void**)&pnewvb);
	short *pnewib;  pMesh->LockIndexBuffer(0, (void**)&pnewib);
	long *pnewab;  pMesh->LockAttributeBuffer(0, &pnewab);
	
	memcpy(pnewvb, pvb, num_vertices*ent_status(entity,22));
	memcpy(pnewib, pib, num_triangles*3*sizeof(short));
	memcpy(pnewab, pab, num_triangles*sizeof(long));
	
	pMesh->UnlockVertexBuffer();
	pMesh->UnlockIndexBuffer();
	pMesh->UnlockAttributeBuffer();
	
	//int entBuffer = ent_buffers(entity,0,0,&pnewvb,&pnewib,&pnewab);
	//printf("Number of triangles of the mesh = %i", entBuffer);
	
	ent_setmesh(entity, pMesh, 0, 0);
	//printf("ent_setmesh(entity, pMesh, 0, 0);");
	
	//int entBuffer = ent_buffers(entity,0,0,&pnewvb,&pnewib,&pnewab);
	//printf("Number of triangles of the mesh = %i", entBuffer);

	//printf("Total number of mesh vertices = %d", (long)ent_status(entity, 1));
}

function create_mesh()
{
	ent = ent_create(CUBE_MDL, 0, 0);
	ent_setskin(ent, bmap_fill(bmap_createblack(32,32,16),COLOR_RED,100), 1);
	ent_setskin(ent, bmap_fill(bmap_createblack(32,32,16),COLOR_GREEN,100), 2);
	ent_setskin(ent, bmap_fill(bmap_createblack(32,32,16),COLOR_BLUE,100), 3);
	set(ent, LIGHT);
	ent.material = m2sided;
	
	//printf("Total number of model skins = %d", (long)ent_status(ent, 8));
	
	ent_clone(ent);
	recreate_mesh(ent);
	wait_for(recreate_mesh);
	ent_getvertex(ent,NULL,ent_status(ent,0));
	c_setminmax(ent);

	//printf("Total number of model skins = %d", (long)ent_status(ent, 8));
}

function main()
{
	vec_set(sky_color,COLOR_BLACK);
	level_load("");
	vec_set(camera.x,vector(-100,0,10));
	
	create_mesh();
	wait_for(create_mesh);
	
	//printf("Total number of mesh vertices = %d", (long)ent_status(ent, 1));
	
	while(1){
		ent.pan += 3*time_step;
		wait(1);
	}
}

function on_esc_event()
{ 
	pMesh = ent_getmesh(ent,0,0);
	pMesh->Release();
	ent_setmesh(ent,NULL,0,0);wait(1);
	sys_exit("");
}


Posted By: 3run

Re: How to create 3D mesh? - 02/04/11 20:54

Hey guys, I just wanted to ask is that possible to make an action which will:
* create a box model (with POLYGON flag), size of which will equal to the size of BBOX of the model which called this action.
I need that to make good old style and fast collusions. Is there any way to make something like this? Thank you.
Posted By: Quad

Re: How to create 3D mesh? - 02/04/11 22:03

yes actually it's pretty easy check min_x,y,z and max_x,y,z
Posted By: 3run

Re: How to create 3D mesh? - 02/04/11 22:24

So all I need is to create a mesh as rojart showed us, and they make it's size same as models min_x,max_x? laugh
Posted By: Andre_Fernando

Re: How to create 3D mesh? - 02/15/11 18:41

Thank you very much, it helped me a lot!
Posted By: Andre_Fernando

Re: How to create 3D mesh? - 02/18/11 14:53

I learned how the process of creation of the 3D mesh, but I have another question, how do I apply texture the triangles in different faces in uv mapping? I tried to apply but they are all merged. Below is an example of what I need:



I used this code:

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

#define NVB 8  // Vertices
#define NIB 34 // Indexes
#define NAB 12 // Attributes

LPD3DXMESH pMesh;
D3DVERTEX pvb[NVB];
short pib[NIB];
long  pab[NAB];

ENTITY* ent;

MATERIAL* m2sided ={effect="technique TS{pass p0{CullMode=1;}}";}

void export_obj(ENTITY *entity, var scale);

function recreate_mesh(ENTITY *entity)
{	
	//printf("Total number of mesh vertices = %d", (long)ent_status(entity, 1));
	
	int num_triangles = NAB;
	int num_vertices  = NVB;
	
	// Vertices

	pvb[0].x = -8;pvb[0].y =  8;pvb[0].z = -8;	pvb[0].u1 = 0;pvb[0].v1 = 1;
	pvb[1].x =  8;pvb[1].y =  8;pvb[1].z = -8;	pvb[1].u1 = 1;pvb[1].v1 = 1;
	pvb[2].x =  8;pvb[2].y =  8;pvb[2].z =  8;	pvb[2].u1 = 0;pvb[2].v1 = 1;
	pvb[3].x = -8;pvb[3].y =  8;pvb[3].z =  8;	pvb[3].u1 = 1;pvb[3].v1 = 1;
	pvb[4].x = -8;pvb[4].y = -8;pvb[4].z = -8;	pvb[4].u1 = 0;pvb[4].v1 = 0;
	pvb[5].x =  8;pvb[5].y = -8;pvb[5].z = -8;	pvb[5].u1 = 1;pvb[5].v1 = 0;
	pvb[6].x =  8;pvb[6].y = -8;pvb[6].z =  8;	pvb[6].u1 = 0;pvb[6].v1 = 0;
	pvb[7].x = -8;pvb[7].y = -8;pvb[7].z =  8;	pvb[7].u1 = 1;pvb[7].v1 = 0;
	
	// Triangles / Attributes
	
	pib[ 0] = 6;pib[ 1] = 2;pib[ 2] = 7; pab[ 0] = 0;
	pib[ 3] = 3;pib[ 4] = 7;pib[ 5] = 2; pab[ 1] = 0;
	pib[ 6] = 7;pib[ 7] = 3;pib[ 8] = 4; pab[ 2] = 0;
	pib[ 9] = 0;pib[10] = 4;pib[11] = 3; pab[ 3] = 0;
	pib[12] = 4;pib[13] = 0;pib[14] = 5; pab[ 4] = 0;
	pib[15] = 1;pib[16] = 5;pib[17] = 0; pab[ 5] = 0;
	pib[18] = 5;pib[19] = 1;pib[20] = 6; pab[ 6] = 0;
	pib[21] = 2;pib[22] = 6;pib[23] = 1; pab[ 7] = 0;
	pib[24] = 7;pib[25] = 4;pib[26] = 6; pab[ 8] = 0;
	pib[27] = 5;pib[28] = 6;pib[29] = 4; pab[ 9] = 0;
	pib[30] = 3;pib[31] = 0;pib[32] = 2; pab[10] = 0;
	pib[33] = 1;pib[34] = 2;pib[35] = 0; pab[11] = 0;
	
	D3DXCreateMesh(num_triangles, num_vertices, D3DXMESH_MANAGED , pvertexdecl, pd3ddev, &pMesh);

	D3DVERTEX *pnewvb;  pMesh->LockVertexBuffer(1, (void**)&pnewvb);
	short *pnewib;  pMesh->LockIndexBuffer(0, (void**)&pnewib);
	long *pnewab;  pMesh->LockAttributeBuffer(0, &pnewab);
	
	memcpy(pnewvb, pvb, num_vertices*ent_status(entity,22));
	memcpy(pnewib, pib, num_triangles*3*sizeof(short));
	memcpy(pnewab, pab, num_triangles*sizeof(long));
	
	
	pMesh->UnlockVertexBuffer();
	pMesh->UnlockIndexBuffer();
	pMesh->UnlockAttributeBuffer();
	
	//int entBuffer = ent_buffers(entity,0,0,&pnewvb,&pnewib,&pnewab);
	//printf("Number of triangles of the mesh = %i", entBuffer);
	
	ent_setmesh(entity, pMesh, 0, 0);
//	export_obj(entity,2);
	//printf("ent_setmesh(entity, pMesh, 0, 0);");
	
	//int entBuffer = ent_buffers(entity,0,0,&pnewvb,&pnewib,&pnewab);
	//printf("Number of triangles of the mesh = %i", entBuffer);

	//printf("Total number of mesh vertices = %d", (long)ent_status(entity, 1));
}

function create_mesh()
{
	ent = ent_create(CUBE_MDL, 0, 0);
	ent_setskin(ent, bmap_create("texture.bmp"), 1);
	set(ent, LIGHT);
	ent.material = m2sided;
	
	//printf("Total number of model skins = %d", (long)ent_status(ent, 8));
	
	ent_clone(ent);
	recreate_mesh(ent);
	wait(2);
//	wait_for(recreate_mesh);
	ent_getvertex(ent,NULL,ent_status(ent,0));
	c_setminmax(ent);
	//printf("Total number of model skins = %d", (long)ent_status(ent, 8));
}

function main()
{
	vec_set(sky_color,COLOR_BLACK);
	level_load("");
	vec_set(camera.x,vector(-50,0,0));
	
	create_mesh();
	wait_for(create_mesh);
	//printf("Total number of mesh vertices = %d", (long)ent_status(ent, 1));
	
	while(1){
		ent.pan +=2*mickey.x*time_step;
		ent.tilt +=2*mickey.y*time_step;
		wait(1);
	}
}

function on_esc_event()
{ 
	pMesh = ent_getmesh(ent,0,0);
	pMesh->Release();
	ent_setmesh(ent,NULL,0,0);wait(1);
	sys_exit("");
}



I used this texture:



Someone can tell me?
Thanks
Posted By: FBL

Re: How to create 3D mesh? - 02/18/11 15:40

pvb[x].u1 = 0;pvb[x].v1 = 1

Here you have to set the UV mapping. 0/0 is the top left pixel of the skin, 1/1 the bottom right pixel of the skin. With the current setup you always use the full skin.

So you might want to choose one side of the cube to be at 0.1/0.1..0.4/0.4, the next side 0.6/0.1..0.9/0.4 the third side 0.1/0.6..0.4/0.9

You may need to use more vertices to get a proper UV map.
Posted By: Andre_Fernando

Re: How to create 3D mesh? - 02/18/11 22:09

I don't understand, how do I associate more vertices in the mesh in UV?
I wanted each individual face of the cube were to UV.
Posted By: FBL

Re: How to create 3D mesh? - 02/19/11 01:19

You need more vertices in total. Each vertex has UV coordinates. So if a vertex is shared by two faces, but each faces needs different UVs for this vertex, you need to split this vertex into two vertices with the same position (xyz), but different u1/v1.
This way each face can have its own coordinates.
Posted By: rojart

Re: How to create 3D mesh? - 02/19/11 01:38

@Andre_Fernando Firstly, you should understand how the uv mapping works, like here.



What you show us, is just the maximum uv coordinate from 0 to 1, therefore they is merged.
Posted By: Andre_Fernando

Re: How to create 3D mesh? - 04/19/11 21:12

Sorry about the delay in responding, because I was studying the matter. Ok, now I know the process of creating a 3D mesh and how work the UV Mapping in the mesh. Thank you!

What I am now in doubt is the command "mouse_pos3d". I did many attempts but could not. I searched the forum but did not find anything like what I needed. Below a picture of animation frames of what needed:



I wanted to create a mesh from a region of a scene, where the 3D positions are relative to mouse, and an axle is fixed in the grid. The code below is almost what I want:


Code:
function shoot_with_mouse() 
{  
  VECTOR to;
  vec_set(to,mouse_dir3d);
  vec_scale(to,1000); // set a range
  vec_add(to,mouse_pos3d);
  c_trace(mouse_pos3d,to,ACTIVATE_SHOOT);
}



Can anyone help me?

Thanks
Posted By: Pappenheimer

Re: How to create 3D mesh? - 04/20/11 20:43

Code:
function shoot_with_mouse() 
{  
  VECTOR to;
  vec_set(to,mouse_dir3d);
  vec_scale(to,1000); // set a range
  vec_add(to,mouse_pos3d);
  c_trace(mouse_pos3d,to,ACTIVATE_SHOOT);
  vec_set(to,mouse_dir3d);
  vec_scale(to,result); // set the range to the result of the trace
  vec_add(to,mouse_pos3d);//'to' contains now the position on the grid
}


Posted By: Andre_Fernando

Re: How to create 3D mesh? - 05/10/11 13:43

Hello. When I create the 3D mesh in the scene with DirectX, the mesh is not diffuse and not speculate too. I tryed change anything in the D3DXCreateMesh but not work. I can change the Ambient and Emissive, but Diffuse and Specular I can not change. Why is it happens? Is There any line of code that's missing?

Thanks!
Posted By: rojart

Re: How to create 3D mesh? - 05/12/11 11:48

Have you tried to use Surface normals?

Face and Vertex Normal Vectors (Direct3D 9)

Simply add D3DXComputeNormals(pMesh,0); but prior ent_setmesh(entity, pMesh, 0, 0); was executed.
Posted By: Andre_Fernando

Re: How to create 3D mesh? - 05/12/11 17:06

Thanks man! worked perfectly. I didn't know needed add D3DXComputeNormals. like I said, I don't know almost nothing of directx 9.
© 2024 lite-C Forums