|
|

Watch, Follow, &
Connect with Us

Developer Tools Community.

# Visualizing mathematical functions by generating custom meshes using FireMonkey

Abstract: This article discusses how you can generate your own 3-dimensional mesh for visualizing mathematical functions using Delphi XE2 and FireMonkey.

# Prerequisites!

This article assumes that you are familiar with the basics of 3D graphics, including meshes and textures.

# The goal!

The goal is to graph a function like sin(x*x+z*z)/(x*x+z*z) in three dimensions using brilliant colors, as the image below shows:

# Generating the mesh

The easiest way to generate a mesh is to use the Data.Points and Data.TriangleIndices of the TMesh object. However, these two properties are strings, and they get parsed in order to generate the mesh at runtime (and design time if populated at design time). This parsing is pretty time consuming, in fact, in this particular case about 65 times as slow as using the internal buffers. Therefore we will instead be using the non-published properties Data.VertexBuffer and Data.IndexBuffer.

In our example we will iterate along the X-axis from -30 to +30, and the same for the Z-axis. The function we're graphing gives us the value for Y for each point.

# Step 1: Generating the wire frame

The image below shows a sparse wire frame representing the surface f = exp(sin x + cos z). Shown in red is one of the squares. Each square gets split into two triangles in order to generate the mesh. The mesh is simply built up from all of the triangles that we get when we iterate over the XZ plane.

We name the corners of the square P0, P1, P2 and P3:

Hide image The two triangles now become (P1,P2,P3) and (P3,P0,P1).

Given that u is somewhere on the X-axis, v is somewhere on the Z-axis, and that d is our delta step, the code to set up these four points in the XZ-plane becomes:

```P.x := u;
P.z := v;

P.x := u+d;
P.z := v;

P.x := u+d;
P.z := v+d;

P.x := u;
P.z := v+d;
```

Now we calculate the corresponding function values for the Y component of each point. f is our function f(x,z).

```P.y := f(P.x,P.z);
P.y := f(P.x,P.z);
P.y := f(P.x,P.z);
P.y := f(P.x,P.z);
```

The points are now fully defined in all three dimensions. Next, we plug them into the mesh.

````with` VertexBuffer `do` `begin`
Vertices := P;
Vertices := P;
Vertices := P;
Vertices := P;
`end`;
```

That part was easy. Now we need to tell the mesh which points make up which triangles. We do that like so:

````// First triangle is (P1,P2,P3)`
IndexBuffer := 1;
IndexBuffer := 2;
IndexBuffer := 3;

`// Second triangle is (P3,P0,P1)`
IndexBuffer := 3;
IndexBuffer := 0;
IndexBuffer := 1;
```

# Step 2: Generating the texture

In order to give the mesh some color, we create a texture bitmap that looks like this: This is simply a HSL color map where the hue goes from 0 to 359 degrees. The saturation and value are fixed.

The code to generate this texture looks like this:

```BMP := TBitmap.Create(1,360); `// This is actually just a line`
`for` k := 0 `to` 359 `do`
BMP.Pixels[0,k] := HSLtoRGB(k/360,0.75,0.5);
```

# Step 3: Mapping the texture onto the wire frame

Finally, we need to map the texture onto the mesh. This is done using the TexCoord0 array. Each item in the TexCoord0 array is a point in a square (0,0)-(1,1) coordinate system. Since we're mapping to a texture that is just a line, our x-coordinate is always 0. The y-coordinate is mapped into (0,1), and the code becomes:

````with` VertexBuffer `do` `begin`
TexCoord0 := PointF(0,(P.y+35)/45);
TexCoord0 := PointF(0,(P.y+35)/45);
TexCoord0 := PointF(0,(P.y+35)/45);
TexCoord0 := PointF(0,(P.y+35)/45);
`end`;
```

# Putting it all together

The full code to generate the entire mesh is listed below:

````function` f(x,z : Double) : Double;
`var`
temp : Double;
`begin`
temp := x*x+z*z;
`if` temp < Epsilon `then`
temp := Epsilon;

Result := -2000*Sin(temp/180*Pi)/temp;
`end`;

`procedure` TForm1.GenerateMesh;
`const`
MaxX = 30;
MaxZ = 30;
`var`
u, v : Double;
P : `array` [0..3] `of` TPoint3D;
d : Double;
NP, NI : Integer;
BMP : TBitmap;
k : Integer;
`begin`
Mesh1.Data.Clear;

d := 0.5;

NP := 0;
NI := 0;

Mesh1.Data.VertexBuffer.Length := Round(2*MaxX*2*MaxZ/d/d)*4;
Mesh1.Data.IndexBuffer.Length := Round(2*MaxX*2*MaxZ/d/d)*6;

BMP := TBitmap.Create(1,360);
`for` k := 0 `to` 359 `do`
BMP.Pixels[0,k] := CorrectColor(HSLtoRGB(k/360,0.75,0.5));

u := -MaxX;
`while` u < MaxX `do` `begin`
v := -MaxZ;
`while` v < MaxZ `do` `begin`
`// Set up the points in the XZ plane`
P.x := u;
P.z := v;
P.x := u+d;
P.z := v;
P.x := u+d;
P.z := v+d;
P.x := u;
P.z := v+d;

`// Calculate the corresponding function values for Y = f(X,Z)`
P.y := f(Func,P.x,P.z);
P.y := f(Func,P.x,P.z);
P.y := f(Func,P.x,P.z);
P.y := f(Func,P.x,P.z);

`with` Mesh1.Data `do` `begin`
`// Set the points`
`with` VertexBuffer `do` `begin`
Vertices[NP+0] := P;
Vertices[NP+1] := P;
Vertices[NP+2] := P;
Vertices[NP+3] := P;
`end`;

`// Map the colors`
`with` VertexBuffer `do` `begin`
TexCoord0[NP+0] := PointF(0,(P.y+35)/45);
TexCoord0[NP+1] := PointF(0,(P.y+35)/45);
TexCoord0[NP+2] := PointF(0,(P.y+35)/45);
TexCoord0[NP+3] := PointF(0,(P.y+35)/45);
`end`;

`// Map the triangles`
IndexBuffer[NI+0] := NP+1;
IndexBuffer[NI+1] := NP+2;
IndexBuffer[NI+2] := NP+3;
IndexBuffer[NI+3] := NP+3;
IndexBuffer[NI+4] := NP+0;
IndexBuffer[NI+5] := NP+1;
`end`;

NP := NP+4;
NI := NI+6;

v := v+d;
`end`;
u := u+d;
`end`;

Mesh1.Material.Texture := BMP;
`end`;
```

# Demo application

You can find my demo application that graphs 5 different mathematical functions in CodeCentral. Here are a few screen shots from the application:

# Contact

Please feel free to email me with feedback to aohlsson at embarcadero dot com. Server Response from: ETNASC04