Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Creating Mesh from Cubic Spline

Author
Message
TinTin
20
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 16th Apr 2013 15:18 Edited at: 16th Apr 2013 15:23
Hi Guy's it's been a while, it's good to see some of the old faces are still here.
I'm currently working on an application that requires me to create a mesh from a cubic bezier stored on a file.
Currently I can load parse the file and to get the end and control points of the spline and auto-generate the detail intermediate points depending on quality setting.

What I now want to do, is to create a mesh object for a predefined shape extruded along this spline.

Think down the lines of a complex pipe network with several individual splines all interweaving with each other, with supports and brackets at set distances allong the way.

I'm looking for any ideas or best practices to achieve this.
Initialy these may be saved off as .dbo's but I may require to export as .3ds so that I can do some additional tweaking, so any info on achieving this would also be appreciated.

and yeah, Ive searched the boards and found nothing of any use...

p.s. I'm using Virgin DBPro (i.e. no plugins!!!)
[Transmission Over!...]


Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 16th Apr 2013 22:51
Holy cow, it has been awhile, good to see your still at it

Hmm... one way I guess would be to create a sort of structure piece function. The idea being that the function just creates the desired mesh for a segment of the spline. So when your looping through the spline data you know to create one of theses meshes at the current segment. Now in terms of joining. In the structure function, you supply it with last and next segment data, position/direction or normal. Using this data you can work out half the angle and scale or alter the edges of the structure piece to I'm guess what would be the cross product. So basically everything would meet up between segments.

I don't know if I've explained this well enough so I'll try and make something.

"Get in the Van!" - Van B
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 16th Apr 2013 23:57 Edited at: 16th Apr 2013 23:58
First convert the curve into a series of line segments, so that you get a number of points at roughly equal intervals.

Assuming you have your 2D shape you want to extrude represented as an ordered list of vertices with X/Y coordinates, you need to calculate two perpendicular 3D vectors for each point along the curve, the first representing the X axis for the 2D polygon, and the second the Y axis.

Given these vectors the construction is relatively easy: make a copy of the 2D shape at each point along the curve, using this equation to calculate the new vertex positions:
VertexPos = X*V1 + Y*V2 + PointPos
Where V1 and V2 are the perpendicular vectors, and PointPos is the position of the current point on the curve.

Then it's just a case of connecting up the correct vertices with quads. (imagine extruding each line of the 2d polygon individually into a quad to see where to add the triangles)

There are different ways to generate the two vectors. One is to define a global "Up" vector:

You will also need a "Normal" vector which is the tangent to the curve at the current point, you can get this just by subtracting the positions of two adjacent points on the curve and then normalising.

Take the cross product of the "Normal" vector with the "Up" vector to give the first of the two perpendicular vectors. Then take the cross product of this new vector with the "Normal" vector to get the second vector.

This method will ensure that the 2D polygon is always the right way up in the world, but has the problem that you cannot have any pipes that go exactly in the direction of the "Up" vector.

The only way to solve this is to allow the 2D polygon to rotate freely with the pipe and not maintain a consistent orientation. To do this you would need to store the current 3D rotation using a matrix or quaternion, and then at each join adjust the rotation to lie along the new normal vector. You can get V1 and V2 just by transforming the original X and Y axis unit vectors by the matrix/quaternion.

[b]
TinTin
20
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 17th Apr 2013 01:13
Thanks Sasuke & Diggsey

Yup! thought it might be that easy...lmao

I'm trying to create a load of cables and pipes within the roof space of my corridors dynamically using cubic splines to save memory, so far all I have are the basic splines. Plus I'll be animating a few that are hanging down, i.e. a disconnected steam hose etc. (Puzzle time - follow pipe, deactivate steam, reconnect, activate steam, open door )

I've managed to place a disk aligned at every knot vector along the spline and can figure out how to replace those with 3d point vectors. I'm kind of stuck using these to create a suitable mesh.

I can start easy with 12 points at each end, but will eventually be going from 12 down to 6 points the farther away the knot is, but that's another problem.


Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 17th Apr 2013 06:24 Edited at: 17th Apr 2013 15:43
et voila...



Not completely plugin free - creating a fresh object with a set number of vertices and indices is a real pain in native dbp. IanM's plugins are practically part of dbp anyway

The first preview you see is just a simple mock-up of the curve using 3D boxes, after pressing enter it will generate a proper mesh using the 2D template (by default a semicircle).

Not bad for 280 lines of code

[b]
TinTin
20
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 17th Apr 2013 14:10
Thanks Diggsey.

That looks like what I'm looking for, I'll give it a go later tonight...

So it looks like you use make object to create an object that supports a predfined size? you then lock the vertex data, load the point data then set the indexs to reference the appropriate points before unlocking the vertex data...

I guess somehow, the new object creates all the polygon surfaces by default.


Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 17th Apr 2013 15:50
Just fixed a minor bug with the previous code (needed to normalize v1 before using it)

The surfaces of the new object are defined by the indices. Each 3 indices defines a triangle. The numbers given to these indices identify which vertex to use for each point on the triangle.

If I want to make a quad between vertices 0, 1, 2, 3 like so:


I could set the indices like this:
0, 1, 2
2, 1, 3

ie. two triangles making a quad.

[b]
TinTin
20
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 17th Apr 2013 20:16
Ok Diggsey

As a puritan, (using only vanilla flavoured DBP) your sample code (remarkable as it seems) only produced two errors, (1) for Create Object & (2) for the Move Camera Right command, both of which use MatrixUtils. I've changed the Move Camera Right to Turn Camera Right. I'm now attempting to convert the create object into a create memblock function, hopefully this will all work out fine..

Thanks for the help


Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
paul5147
20
Years of Service
User Offline
Joined: 11th Jan 2006
Location: Hot & Sunny
Posted: 17th Apr 2013 20:25 Edited at: 17th Apr 2013 20:28
Have a quick look through 2 of my old posts and im sure you will find some code that will do exactly what you after
http://forum.thegamecreators.com/?m=forum_view&t=158864&b=8
http://forum.thegamecreators.com/?m=forum_view&t=200791&b=8
If you want a stripped down version of the code that just makes the tracks then let me know.
The earlier version from about 2006 didn't use any plugins for the object creation from what i remember, though it may have used matrix1 for other things,it has been a while since i started this thing.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 17th Apr 2013 23:44 Edited at: 17th Apr 2013 23:46
You can't do it the same way with memblocks as you can't make indices using memblocks.

To make things easy I've written a replacement "make object new" function you can simply substitute into my original code:


As you can see it's quite an inefficient way of doing it, but that's why plugins exist

[b]
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 18th Apr 2013 00:56
All of the above info is good stuff. I'm only throwing this bit into the mix as an alternative for the hard of thinking.

When I created my tracks, I was only creating a plane that meandered along the spline. I used a method that was described to me by Ravey.

For each point in turn on the spline, place a cube, and point it at the next point on the spline.

Move the cube left x units, record the point
Move the cube right x units, record the point

Use the recorded points to create your polys, as described by others above.

You could apply the same logic by placing a 2D profile instead of the cube, and recording the vertices at each point instead of moving left/right.

It's not nearly as efficient or slick as the above, but it's easy to understand, and you can watch it in action too

TinTin
20
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 18th Apr 2013 20:06
Thanks Guys, I'm slowly getting to the bottom of this.

The cubic splines are all working
I've figured out how to generate a profile along these using memblocks, (thanks Diggsey )

There are still a few issues with this method that still need ironing out, the lack of decent memblock documentation isn't helping.... grrrrr Lee

I've got my spline generating a few hosepipes hanging from the roof, nothing too fancy at the moment.

Now to animate them as they move by the escaping vapour, I have to move some of the control points, which means I have to recreate the object from memblock every frame?

do the memblocks support limbs, which would allow me to animate/deform the created object instead directly?

Once done, I'll pull together a small demo of how it all works and post it into the 'code snippets'


Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 18th Apr 2013 20:35 Edited at: 18th Apr 2013 20:35
If you can figure our how to create a bone in your object, you could animate that via the limb commands and get a nice mesh deformation too.

(You could create a simple 2 cube object connected with a bone, convert it to a memblock and check out it's structure to see if you can add a bone manually...)

I live for video games! (And beers, and football, and cars!)
See what I live for here: [url]http:\\www.TeamDefiant.co.uk[/url]
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 19th Apr 2013 01:26
You could make a vertex shader which deforms an object according to a bezier curve, then you just have to make a straight cylindrical object with lots of segments and let the shader do the rest - you can animate it just by adjusting the parameters to the bezier curve.

If you make the cylinder 1 unit long and lie along the Z axis, then it's basically the exact same idea as before but in HLSL rather than DBP - the Z position of the vertex becomes the parameter used to calculate a point along the curve, the X and Y positions are the coordinates of the 2D shape.

In fact it's even easier this way because you don't have to generate the surfaces. Plus HLSL has all the vector math stuff built in.

[b]
TinTin
20
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 20th Apr 2013 17:36
Nothing better than doing it the easy way

I never considered HLSL Diggsey, Don't know if I want to go down that route yet

I'm trying to create all this stuff randomly without having to store loads of objects even the splines are kind of random.

Mobiius, I thought of using limbs, but converting a boned object into a memblock to figure out if I can add a bone manually sounds like a challenge. Oh how I wish the docs were better!



Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 20th Apr 2013 20:40 Edited at: 20th Apr 2013 20:42
The code would be quite simple, something like this:


Then you just call "adjustVertex()" with the original vertex position and it will give you the new position for that vertex.

Plus it will be very fast (this is pretty much how the fastbone.fx shader works that speeds up animated objects although it uses vertex weights rather than a bezier curve)

[b]
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 21st Apr 2013 23:15
Quote: "I've got my spline generating a few hosepipes hanging from the roof, nothing too fancy at the moment.
Now to animate them as they move by the escaping vapour."


Hey TinTin, read this part and wondered if you've considered another method - Springs

Springs Physics is perfect for stuff like this plus there's mass amounts of docs out there to study for stuff like this. I use them in my particles system - (particles don't actually have an appearance, most people believe there sole purpose is just effects, the a particle itself is just a point in space, what we do with that point defines it. Add a visual plane or mesh to create effect, use them as anchors or control points and may more things).

What you could do is convert your control points into particles and add springs between them. The spring centre will be where you position any mesh attached to them and all the data used to create the springs can be easily adjusted to scale and align any mesh correctly to adjacent springs. What's really great about springs is animating them. For example that escaping vapour, add a tiny random pulsing force to the bottom control point of the hose and the thing will look like the vapour is pushing it around.

I remember and article called 'Rope Physics' for games, this would be a place to start if your interested in the idea

"Get in the Van!" - Van B
TinTin
20
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 24th Apr 2013 22:15
Yeah Thanks guys,

@Diggsey... tut tut you do know this is a DBPro board? Lucky I understand C

@Sasuke.. digging through heaps of that stuff right now, keep forgetting springs don't always have to be coiled.

Now that I'm thinking about it, I could try and combine bits from all these methods to get the perfect solution. Something to keep me busy till Xmas. lol


Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 24th Apr 2013 23:11
Quote: "@Diggsey... tut tut you do know this is a DBPro board? Lucky I understand C"


I'm guessing HLSL is very similar to C cause I thought it was HLSL

Quote: "Something to keep me busy till Xmas"


Really?! You must be doing a monolithic amount of things or are incredible slow. Here's to hoping it's the former

"Get in the Van!" - Van B
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 25th Apr 2013 01:48
Quote: "@Diggsey... tut tut you do know this is a DBPro board? Lucky I understand C"


Ah yeah, sasuke was right that's HLSL not C (Hence the relevance to doing it in a vertex shader!)

[b]
TinTin
20
Years of Service
User Offline
Joined: 16th May 2006
Location: BORG Drone Ship - Being Assimilated near Roda Beta (28)
Posted: 26th Apr 2013 17:22
Yup, That code would sit happily in a C++ Appy...

@Diggsey, nope it's the latter, I've got loads of other stuff on the go... honest


Cyberspace was becoming overcrowded and slummy so I decided to move. These nice chaps gave me a lift.

Login to post a reply

Server time is: 2026-07-08 03:49:22
Your offset time is: 2026-07-08 03:49:22