How to create mesh faces with a list of vertices ?

Everything related to 3D programming
marc_256
Addict
Addict
Posts: 842
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

How to create mesh faces with a list of vertices ?

Post by marc_256 »

Hi,

Yes, need some help (again) ...

From inside my CAD program, I like to export .mesh objects.
see example image below.

This is a fully math/algorithm created 3D object using LERP/CERP, that I like to integrate in my CAD program.
It's a rotated wedge with a programmable radius inside, radius outside, height and angle.
All vertices X,Y,Z positions are stored in an array()

1) calculated/created the bottom vertices (red)
2) calculated/created the inside vertices (yellow)
3) calculated/created the outside vertices (light blue)
4) calculated/created the top vertices (green)
5) calculated/created the side vertices (orange)

So, I have a lot of double vertices in my list/array.

Q1) Is there a simple way to create the needed triangles/faces to create a mesh ?
Q2) How can I best remove the doubles vertices in the list ?

I did some tests for the creation of the edges ... (see image)
but the next step to create the faces is a step to far ...
(or I'm to tired now after a day and night of work)



Greetings,
thanks,
marc


Image
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
minimy
Enthusiast
Enthusiast
Posts: 619
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: How to create mesh faces with a list of vertices ?

Post by minimy »

Hello marc_256, in my opinion the easiest way is to create the vertices 4 by 4 to be able to create two one face with 4 vertices or 2 triangles. Another way can be to store the vertices in an array and then create the faces with MeshFace(v1,v2,v3) or MeshFace(v1,v2,v3,v4)
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 386
Joined: Thu Jul 09, 2015 9:07 am

Re: How to create mesh faces with a list of vertices ?

Post by pf shadoko »

I didn't really understand what you wanted
here's an Example to make this mesh using CreateDataMesh()
otherwise you just need meshvertex() and meshface()

did you see my last code?
https://www.purebasic.fr/english/viewto ... 81#p620181

Code: Select all

InitEngine3D():InitSprite():InitKeyboard():InitMouse()
ExamineDesktops()
OpenWindow(0, 0,0, DesktopWidth(0)*0.8,DesktopHeight(0)*0.8, "[Esc] quit",#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0)

CreateCamera(0, 0, 0, 100, 100):MoveCamera(0,0,1,-1):CameraLookAt(0,0,0,0)
CreateLight(0,$ffffff, -100, 100, 100)
AmbientColor($111111*8)
CameraBackColor(0,$444488)

CreateMaterial(0,0,$ffff)
MaterialShadingMode(0,#PB_Material_Wireframe):MaterialCullingMode(0,#PB_Material_NoCulling)

Procedure ameshvertex(*v.meshvertex, x.f,y.f,z.f)
    *v\x=x
    *v\y=y
    *v\z=z
EndProcedure

#ri=0.50 ; internal radius
#re=0.58 ; external radius
#ag=45: #da=1.25  ; angle, delata angle 
#h=0.05  ; height

Define.f a,xe,ye,xi,yi,h,  ni.l=#ag/#da

Dim v.MeshVertex(ni+1,7)
For i=0 To ni+1
  
  If i<ni+1
  a=-Radian(i/ni*#ag)
  xe=Cos(a)*#ri:ye=Sin(a)*#ri
  xi=Cos(a)*#re:yi=Sin(a)*#re
  h=i/ni*#h
Else
  h=0
  EndIf
  
  j=0
  aMeshVertex(v(i,j),xi,0,yi):j+1
  aMeshVertex(v(i,j),xe,0,ye):j+1
  aMeshVertex(v(i,j),xe,0,ye):j+1
  aMeshVertex(v(i,j),xe,h,ye):j+1
  aMeshVertex(v(i,j),xe,h,ye):j+1
  aMeshVertex(v(i,j),xi,h,yi):j+1
  aMeshVertex(v(i,j),xi,h,yi):j+1
  aMeshVertex(v(i,j),xi,0,yi):j+1

 Next
 CreateDataMesh(0,v(),8)
CreateEntity(0,MeshID(0),MaterialID(0))

Repeat
	While WindowEvent():Wend
	ExamineKeyboard()
	ExamineMouse()
	If KeyboardPushed(#PB_Key_Space)=0:RotateEntity(0,0,0.5,0, #PB_Relative):EndIf
	
	RenderWorld()
	FlipBuffers()    
Until KeyboardReleased(#PB_Key_Escape) Or MouseButton(3)
marc_256
Addict
Addict
Posts: 842
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: How to create mesh faces with a list of vertices ?

Post by marc_256 »

@ minimy,

Sorry, I didn't express myself very well here, my question was bad. :oops:
I know how to create vertices, edges and faces.
but ...
I workt all day long yesterday to try something that is impossible to do. :twisted:

What I mean is, you can't create a mesh with only an array of vertices.
You need to know the form of the mesh also and the 3 vertices who are connected forming a face.
So, I need to create the faces directly, the moment I calculate/create the form/model of the mesh.



Hi pf shadoko,

Thanks again for all your time you spend on my questions,
What I was asking is impossible to do. :oops:

I have made a list/array of vertices with meshvertex()
But, you need to know the 3 vertices for the face so I can create the face with meshface().

For example, with a cube, you need to find the 4 vertices who lies on the same plane in 3D.
But with a non known form you are not able to find the form of the mesh with only an array of vertices ...
Specially when you use triangles, every vertex can be connected to every other vertex in the 3D world.

I hope I explained myself well here ...



So, I'm trying to correct my algorithm to create faces at the same time I calculate the X,Y,Z of the vertices.
So far, it works well, and I have made/constructed the mesh for ogre, but now I'm struggling with the NORMALS of the vertices.
I used NormalizeMesh() but is not working very well ...
Sometimes the faces are transparent in the view of the camera.
I will show some images soon ...


Thanks (again),
marc

PS: computer 3D word is very difficult to understand, that's why there are only a handful of people working on that.

My 2D CAD program is difficult ...
My 3D CAD program is very difficult ...
But understanding the 3D OGRE world, is extremely difficult ...
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: How to create mesh faces with a list of vertices ?

Post by Caronte3D »

I think you need the vertex matrix and the faces matrix. For vertices you only need the x, y, z points, but for faces you need the order of the vertices that make up each face.
In general 3D programs, a common practice is to arrange the vertices of faces clockwise or counterclockwise to set their normal orientation outside or inside the face.
marc_256
Addict
Addict
Posts: 842
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: How to create mesh faces with a list of vertices ?

Post by marc_256 »

Hello Caronte3D,

Thanks for your tip.
I gone test this in my program.
CW / CCW I didn't know that.
I tried to use/calculate NORMALS.

Marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
SMaag
Enthusiast
Enthusiast
Posts: 325
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: How to create mesh faces with a list of vertices ?

Post by SMaag »

The minimum information you need are the 3 points and the normal vector of the triangle face.
That's the minimum information. See .STL File Format.

There are 2 normals vectors possible: 180° chagend. (both are pointing 90° form the surface)
For a closed surfece mesh you need the normal vector, pointing outside of the solid.

Calculating the normal is a vector cross product.

Because of all this stuff I started the SSE optimized Modules VECTORf and VECTORd, for single an double float
calculations.

There are a lot of professional videos on Youtube for the 3D Grafic basics.
A lot of links you can find in the documentaiton header of my PbFw_Module_VECTORf.pb
but this videos are mostly in german.
SMaag
Enthusiast
Enthusiast
Posts: 325
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: How to create mesh faces with a list of vertices ?

Post by SMaag »

The way how to decide what is inside or outside direction, I saw in one of the videos.

As I remember, but I'm not sure!
(don't ask me how to do in code at the moment! I don't know until now!)

-virtually rotatet your face normal to be on a axis x,y or z.
-take a point far away on the axis where you are sure it is outside.
Then compare the direction of your point far away with the normal.
(if same direction: you are outside, if it's oppsite direction you are behinde the face.
Behinde the face can be inside the solid or behind the solid.
Direction means + or - of the virtal axis you rotated to.

This you have to do from more sides, then you are sure where you are!

Such stuff we sould put in an open source library!

Maybe that could be part of OpenGL Library! But I'm not firm with openGL and a short search brought up nothing!
marc_256
Addict
Addict
Posts: 842
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: How to create mesh faces with a list of vertices ?

Post by marc_256 »

@Caronte3D,

I did some stuff manually by try and error...
And you are right, it works very wel. (see images below)
this is the answer to my problem.
NormalizeMesh() is not working at all in PB.
Thanks,


@sMaag
I think you are correct,
You need 3 VERTICES to make a FACE, and the link between them.
Now this VERTICES/FACES problem is solved, up to the next part of my problems.
The NORMALS, ...

I'm tying to integrate this in my GamingEngine ...
But first like you sad, I need to look a lot to YouTube I think.
To well understand the NORMAL calculations.

Thanks,
marc


Image

Image

Image

Image
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: How to create mesh faces with a list of vertices ?

Post by Caronte3D »

marc_256 wrote: Tue May 14, 2024 1:55 pm @Caronte3D,

I did some stuff manually by try and error...
And you are right, it works very wel. (see images below)
this is the answer to my problem.
NormalizeMesh() is not working at all in PB.
Thanks,
Also...
If you use the method I suggested (vertex order) to correctly paint the face, you only need a boolean flag to indicate whether the face has the normal position pointing outward or inward. :wink:

That is the easyway, but you can do it more complex with a vector for the normal of each face, so you aren't limited to perpendicular normals.
User avatar
pf shadoko
Enthusiast
Enthusiast
Posts: 386
Joined: Thu Jul 09, 2015 9:07 am

Re: How to create mesh faces with a list of vertices ?

Post by pf shadoko »

Faces have a top and a bottom
the top is determined by the winding direction of the vertexes
clockwise, it's the top

by default, only the top of faces is displayed (you can change this with MaterialCullingMode())

v1 -------- v2
|......../
|...../
|../
v3

with meshface (v1,v2,v3) the face will be visible. if you turn over the face or the camera, it will be invisible
with meshface (v1,v3,v2) the face will be invisible. if you turn over the face or the camera, it will be visible.


normals:
vectors perpendicular to a surface
they are used to manage the lighting of the face
to calculate them, use normalisemesh()
but be careful: for normalisemesh to work, you must have defined the normal vector (with any value), otherwise the vertex will be considered as not having a normal.

MeshVertex (x,y,z, u,v, color, 1,0,0) here 1,0,0 represents the normal vector, you can keep this value for all vertices, normalisemesh will recalculate them

if your object has a protruding edge, you need to double the vertexes so that each side has its own normal
(the mesh created by createcube doesn't contain 8 vertices, but 24!)
Post Reply