Distorting a mesh

Started by Hardcoal, July 03, 2018, 19:33:33

Previous topic - Next topic

Hardcoal

Hi.. im trying to distort a mesh by changing its vertix location..
but each time i do that i get the mesh polygons splitted..

for example a cube, will turn into 6 unattached polygones..
instead of a twisted cube as i desire..

i got threw all the vertex and all the surfaces  (in the cube case its only one surface..)

what am i doing wrong?

first i make a mesh vertex list..

Code: BASIC


Method CreateMeshVertexsList:TList(Mesh)
Local SurfNum, I, CurrentSurf, Vert, VertList:TList, VertHldr:VertHolder, Counter

VertList = CreateList()

SurfNum = xCountSurfaces(Mesh)

For I = 1 To SurfNum

CurrentSurf = xGetSurface(Mesh, I - 1)

Counter = 0

For Vert = 0 To xCountVertices(CurrentSurf) - 1

VertHldr = New VertHolder

VertHldr.SurfaceHandle = CurrentSurf

VertHldr.X = xVertexX(CurrentSurf, Counter)
VertHldr.y = xVertexY(CurrentSurf, Counter)
VertHldr.z = xVertexZ(CurrentSurf, Counter)

ListAddLast(VertList, VertHldr)

Counter = Counter + 1

Next

Next

Return VertList
End Method


and than i randomize its location..

Code: BASIC


Method WobbleMesh(MeshVertList:TList, Wob:Float = 0.15)
Local Vert:VertHolder, Counter

For Vert = EachIn MeshVertList

xVertexCoords(Vert.SurfaceHandle, Counter, Vert.X + Cos(angle + Rnd(-20, 20)) * Wob, Vert.Y + Sin(angle + Rnd(-20, 20)) * Wob, Vert.Z)

Counter = Counter + 1

Next

Angle = Angle + 1

If Angle > 359 Then Angle = 0

End Method



but i get a fragged cube instead of twisted..

cheers thanks
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

STEVIE G

The cube will be unwelded so has 3 vertices at each corner.  If you want to distort the corners you must move all 3 to the same position OR use a welded cube where each face shares vertices with the others.

Hardcoal

I think i get it.. Thanks..
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

col

#3
Don't forget to normalize the vertice normals after any adjustment so that lighting works properly.

For a flat cube face, which is a plane, you do like this:

1. You want to pick any 3 of the 4 corners, think of them as corner A, corner B, corner C.
2. Create 2 vectors using them: A to B and from A to C, you do that by subtracting xyz of C from xyz of A, and also xyz of B from xyz of A.
3. When you have these two vectors you do a 'cross product' using them... this will give you a third vector that points outwards (or inwards, depending on how you form the previous 2 vectors).
4. Normalize this 3rd vector and the result is the face normal. As you want each of the 4 vertices to face the same way you use this value for those 4 vertex normals that make up that face. This is also why there are 3 different vertices at each corner - Each corner has 3 faces. Each vertex has only 1 normal for lighting so for those 3 faces you need 3 vertices at the same position but with a different normal facing different directions.
5. Rinse and repeat for 6 faces of the new shape.

For calculating the cross-product...
a. If you understand math notation then the wikipedia for Cross Product is the best source https://en.wikipedia.org/wiki/Cross_product
or b. If you want the calculation in a more layman language then math-is-fun is a good source https://www.mathsisfun.com/algebra/vectors-cross-product.html

You can use Pythagoras to calculate the length ( or magnitude ) of the vector and divide each component by the length for a normalized vector:
i. Local Length:Float = sqr(n.x * n.x + n.y * n.y + n.z * n.z) where n is the 3rd 'un-normalized' vector result from step 3 above.
ii. Divide each component by the length: n.x :/ length; n.y :/ length; n.z :/ length
iii. The result is a normalized vector for step 4 above.
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Hardcoal

ok thanks to you too col for all your effort..
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

col

#5
You're welcome :)

Just wanted to mention (if others are interested too) that I'm sure you'll be quick to realise that most 3D models will be triangulated already which simplifies the code for doing the above.  A cube face will be made of 2 triangle polygons to form each flat square polygon. In which case each triangle polygon will only have 3 vertices anyway so no need to write code to pick 3 of the 4 vertices for each cube face. You still apply the exact same algorithm, just do it for every polygon and all will work out nice, clean and straight forward at the cost of calculating some vertex normals more than once.
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Hardcoal

Youve Done well man..
More or less i understood what you mean..
Ill deal with it when the time comes
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

Steve Elliott

Blimey, I think I'll stick to 2d  :o
Win11 64Gb 12th Gen Intel i9 12900K 5.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 4.4Ghz Nvidia RTX 2050 8Gb
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.6GHz Nvidia GeForce GTX 1050 2Gb
Linux Mint 8Gb Celeron 2.6Ghz UHD Graphics600
macOS 64Gb M4 Max 16C GPU 40C
Spectrum Next 2Mb

Hardcoal

Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

Hardcoal

Lol Steve.. :)

i wasnt planing to get into it too deeply i i was just wondering why it splitted my cube..
i was certain a cube was made only from 8 vertex points but if each face is made of a different vertex that makes it more complex.. and heavy.
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

Kryzon

Is that Xors? If so, it should have a xUpdateNormals like Blitz3D does, which does something similar to what col mentioned. But in any case it's good to know how to do it manually, the cross product is very useful.

Not only normals but vertex colours and UVs can also cause the verts to be split (basically, any vertex attribute that can have a sharp change).
So I would run a loop once to gather all points that are located at the same places (collecting them in arrays, each for a set of points). This way you can move bunches of points at once, displacing them all together.


Hardcoal

Totally, kryzon.. Simple and clear,, that's exactly what should be done..
Im not messing with this now but when i do thats what I'll do..

I was just wondering why my cube polygons get separated..
I didn't know each side has a separate polygon..
Doesn't that make the cube heavier?
Why did xors3d did that..
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

Kryzon

Because splitting vertices lets you get sharp edges, looks better.
The cube on the left is completely welded (8 vertices), cube on the right has split vertices (4 verts per face X 6 faces = 24 verts).

Hardcoal

interesting.. didnt know that :)
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]