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:

Hide image

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.

Hide image

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:

  1. P[0].x := u;
  2. P[0].z := v;
  3.  
  4. P[1].x := u+d;
  5. P[1].z := v;
  6.  
  7. P[2].x := u+d;
  8. P[2].z := v+d;
  9.  
  10. P[3].x := u;
  11. P[3].z := v+d;

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

  1. P[0].y := f(P[0].x,P[0].z);
  2. P[1].y := f(P[1].x,P[1].z);
  3. P[2].y := f(P[2].x,P[2].z);
  4. P[3].y := f(P[3].x,P[3].z);

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

  1. with VertexBuffer do begin
  2. Vertices[0] := P[0];
  3. Vertices[1] := P[1];
  4. Vertices[2] := P[2];
  5. Vertices[3] := P[3];
  6. end;

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

  1. // First triangle is (P1,P2,P3)
  2. IndexBuffer[0] := 1;
  3. IndexBuffer[1] := 2;
  4. IndexBuffer[2] := 3;
  5.  
  6. // Second triangle is (P3,P0,P1)
  7. IndexBuffer[3] := 3;
  8. IndexBuffer[4] := 0;
  9. IndexBuffer[5] := 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:

  1. BMP := TBitmap.Create(1,360); // This is actually just a line
  2. for k := 0 to 359 do
  3. 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:

  1. with VertexBuffer do begin
  2. TexCoord0[0] := PointF(0,(P[0].y+35)/45);
  3. TexCoord0[1] := PointF(0,(P[1].y+35)/45);
  4. TexCoord0[2] := PointF(0,(P[2].y+35)/45);
  5. TexCoord0[3] := PointF(0,(P[3].y+35)/45);
  6. end;

Putting it all together

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

  1. function f(x,z : Double) : Double;
  2. var
  3. temp : Double;
  4. begin
  5. temp := x*x+z*z;
  6. if temp < Epsilon then
  7. temp := Epsilon;
  8.  
  9. Result := -2000*Sin(temp/180*Pi)/temp;
  10. end;
  11.  
  12. procedure TForm1.GenerateMesh;
  13. const
  14. MaxX = 30;
  15. MaxZ = 30;
  16. var
  17. u, v : Double;
  18. P : array [0..3] of TPoint3D;
  19. d : Double;
  20. NP, NI : Integer;
  21. BMP : TBitmap;
  22. k : Integer;
  23. begin
  24. Mesh1.Data.Clear;
  25.  
  26. d := 0.5;
  27.  
  28. NP := 0;
  29. NI := 0;
  30.  
  31. Mesh1.Data.VertexBuffer.Length := Round(2*MaxX*2*MaxZ/d/d)*4;
  32. Mesh1.Data.IndexBuffer.Length := Round(2*MaxX*2*MaxZ/d/d)*6;
  33.  
  34. BMP := TBitmap.Create(1,360);
  35. for k := 0 to 359 do
  36. BMP.Pixels[0,k] := CorrectColor(HSLtoRGB(k/360,0.75,0.5));
  37.  
  38. u := -MaxX;
  39. while u < MaxX do begin
  40. v := -MaxZ;
  41. while v < MaxZ do begin
  42. // Set up the points in the XZ plane
  43. P[0].x := u;
  44. P[0].z := v;
  45. P[1].x := u+d;
  46. P[1].z := v;
  47. P[2].x := u+d;
  48. P[2].z := v+d;
  49. P[3].x := u;
  50. P[3].z := v+d;
  51.  
  52. // Calculate the corresponding function values for Y = f(X,Z)
  53. P[0].y := f(Func,P[0].x,P[0].z);
  54. P[1].y := f(Func,P[1].x,P[1].z);
  55. P[2].y := f(Func,P[2].x,P[2].z);
  56. P[3].y := f(Func,P[3].x,P[3].z);
  57.  
  58. with Mesh1.Data do begin
  59. // Set the points
  60. with VertexBuffer do begin
  61. Vertices[NP+0] := P[0];
  62. Vertices[NP+1] := P[1];
  63. Vertices[NP+2] := P[2];
  64. Vertices[NP+3] := P[3];
  65. end;
  66.  
  67. // Map the colors
  68. with VertexBuffer do begin
  69. TexCoord0[NP+0] := PointF(0,(P[0].y+35)/45);
  70. TexCoord0[NP+1] := PointF(0,(P[1].y+35)/45);
  71. TexCoord0[NP+2] := PointF(0,(P[2].y+35)/45);
  72. TexCoord0[NP+3] := PointF(0,(P[3].y+35)/45);
  73. end;
  74.  
  75. // Map the triangles
  76. IndexBuffer[NI+0] := NP+1;
  77. IndexBuffer[NI+1] := NP+2;
  78. IndexBuffer[NI+2] := NP+3;
  79. IndexBuffer[NI+3] := NP+3;
  80. IndexBuffer[NI+4] := NP+0;
  81. IndexBuffer[NI+5] := NP+1;
  82. end;
  83.  
  84. NP := NP+4;
  85. NI := NI+6;
  86.  
  87. v := v+d;
  88. end;
  89. u := u+d;
  90. end;
  91.  
  92. Mesh1.Material.Texture := BMP;
  93. 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:

Hide imageHide imageHide imageHide image

Contact

Please feel free to email me with feedback to aohlsson at embarcadero dot com

 
http://edn.embarcadero.com/article/42007

Visualizing mathematical functions by generating custom meshes using FireMonkey(很美)的更多相关文章

  1. Visualizing wave interference using FireMonkey(很美)

      Visualizing wave interference using FireMonkey By: Anders Ohlsson Abstract: This article discusses ...

  2. Part 14 Mathematical functions in sql server

    Part 29 Mathematical functions in sql server

  3. NVIDIA CG语言 函数之所有数学类函数(Mathematical Functions)

    数学类函数(Mathematical Functions) abs(x) 返回标量和向量x的绝对值 如果x是向量,则返回每一个成员的绝对值 acos(x) 返回标量和向量x的反余弦 x的范围是[-1, ...

  4. Custom Grid Columns - FireMonkey Guide

    原文 http://monkeystyler.com/guide/Custom-Grid-Columns ack to FireMonkey Topics As we saw in TGrid a F ...

  5. [翻译] Using Custom Functions in a Report 在报表中使用自己义函数

    Using Custom Functions in a Report  在报表中使用自己义函数   FastReport has a large number of built-in standard ...

  6. [HIve - LanguageManual] Hive Operators and User-Defined Functions (UDFs)

    Hive Operators and User-Defined Functions (UDFs) Hive Operators and User-Defined Functions (UDFs) Bu ...

  7. [Hive - Tutorial] Built In Operators and Functions 内置操作符与内置函数

    Built-in Operators Relational Operators The following operators compare the passed operands and gene ...

  8. Generating Complex Procedural Terrains Using GPU

    前言:感慨于居然不用tesselation也可以产生这么复杂的地形,当然致命的那个关于不能有洞的缺陷还是没有办法,但是这个赶脚生成的已经足够好了,再加上其它模型估 计效果还是比较震撼的.总之好文共分享 ...

  9. [中英双语] 数学缩写列表 (List of mathematical abbreviations)

    List of mathematical abbreviations From Wikipedia, the free encyclopedia 数学缩写列表 维基百科,自由的百科全书 This ar ...

随机推荐

  1. 从A页面带参数跳转到B页面;进行解析,并显示数据,进行编辑

    A页面跳转时候的地址: parent.layer.open({ type: 2, title:'新建草稿', shadeClose: true, shade: 0.8, scrollbar: fals ...

  2. HDU 1020 Encoding 模拟

    Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. winform播放音乐

    string sound = Application.StartupPath +@"\song\123.wav"; //Application.StartupPath:程序exe所 ...

  4. linux学习笔记18---目录结构

    对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统至关重要,下面 ...

  5. lnmp集成开发环境安装pdo_dblib扩展

    php连接mssql,获取的结果中文乱码,pdo_dblib扩展使用的是apt-get install php5-sybase方法安装的,尝试了修改freetds.conf php.ini 文件编码 ...

  6. hdu6076 Security Check 分类dp 思维

    /** 题目:hdu6076 Security Check 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6076 题意:有两个队列在排队,每一次警察可以检 ...

  7. Flow construction SGU - 176 有源汇有上下界最小流 二分法和回流法

    /** 题目:Flow construction SGU - 176 链接:https://vjudge.net/problem/SGU-176 题意: 有源汇有上下界的最小流. 给定n个点,m个管道 ...

  8. 配置Docker中国区官方镜像http://get.daocloud.io/ 很好的一个源http://get.daocloud.io/#install-docker

    https://www.daocloud.io/mirror#accelerator-doc 配置Docker中国区官方镜像http://get.daocloud.io/ 很好的一个源http://g ...

  9. C++ STL标准模板库(stack)

    //stack的使用 #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<stack> using name ...

  10. 几个比较经典的算法问题的java实现

    1.八皇后问题 public class EightQueen { private static final int ROW = 16; private static final int COL = ...