Hi.Sample.Wpf/Disp/DemoDrawing.cs

202 lines
5.9 KiB
C#

using Hi.Geom;
using Hi.Disp;
using System.Collections.Generic;
using Hi.Disp.Treat;
using Hi.Coloring;
namespace Sample.Disp
{
/// <summary>
/// Demonstrates various drawing techniques using the HiAPI graphics system.
/// Provides examples of primitive drawing, attribute specification, and rendering options.
/// Includes methods for drawing lines, points, triangles, and other geometric primitives.
/// </summary>
/// <remarks>
/// ### Source Code
/// [!code-csharp[SampleCode](~/../Hi.Sample.Wpf/Disp/DemoDrawing.cs)]
/// </remarks>
public static class DemoDrawing
{
/// <summary>
/// Demonstrates simple line drawing using raw vertex coordinates.
/// Creates a basic line strip from three vertices and displays it using DemoUtil.
/// </summary>
public static void FreeDrawing()
{
#region DocSite.FreeDrawing
double[] vs =
{0,0,0
,1,0,0
,0,0,1};
DemoUtil.RunApplication("EasyDraw"
, new Drawing(vs, Stamp.V, GL.GL_LINE_STRIP));
#endregion
}
/// <summary>
/// Demonstrates various types of line drawing techniques.
/// Shows how to create points, line strips, line strips with normals, and colored line strips.
/// Illustrates different ways to specify vertex attributes like position, color, and normal.
/// </summary>
public static void DrawLines()
{
List<Vec3d> ps;
double[] vs;
int n = 10; //vertex num
////create drawPoints
ps = new List<Vec3d>(n);
for (int i = 0; i < n; i++)
ps.Add(new Vec3d(i, 0, 0));
vs = new double[ps.Count * 3];
for (int i = 0; i < ps.Count; i++)
{
vs[3 * i] = ps[i].x;
vs[3 * i + 1] = ps[i].y;
vs[3 * i + 2] = ps[i].z;
}
//V means Vertex
Drawing drawPoints = new Drawing(vs, Stamp.V, GL.GL_POINTS);
////create drawLineStrip
vs = new double[ps.Count * 3];
for (int i = 0; i < ps.Count; i++)
{
vs[3 * i] = i;
vs[3 * i + 1] = 0;
vs[3 * i + 2] = 1;
}
Drawing drawLineStrip = new Drawing(vs, Stamp.V, GL.GL_LINE_STRIP);
////create drawLineStripWithNormal
//the content of vs is Nx,Ny,Nz,Vx,Vy,Vz, Nx,Ny,Nz,Vx,Vy,Vz, ...
vs = new double[n * 6];
for (int i = 0; i < n; i++)
{
//nx,ny,nz
vs[6 * i] = 1;
vs[6 * i + 1] = 0;
vs[6 * i + 2] = 0;
//x,y,z
vs[6 * i + 3] = i;
vs[6 * i + 4] = 0;
vs[6 * i + 5] = 2;
}
//V means Vertex; N means Normal
Drawing drawLineStripWithNormal =
new Drawing(vs, Stamp.NV, GL.GL_LINE_STRIP);
////create drawColorLines
vs = new double[n * 6];
for (int i = 0; i < n; i++)
{
//the rgb values set casually.
//r,g,b
vs[6 * i] = i % 3 / 2D;
vs[6 * i + 1] = i % 5 / 4D;
vs[6 * i + 2] = i % 7 / 6D;
//x,y,z
vs[6 * i + 3] = i;
vs[6 * i + 4] = 0;
vs[6 * i + 5] = 3;
}
//V means Vertex; C means Color
Drawing drawLineStripWithColor = new Drawing(vs, Stamp.CV, GL.GL_LINE_STRIP);
DemoUtil.RunApplication("DrawLines",
new DispList(drawPoints, drawLineStrip
, drawLineStripWithNormal, drawLineStripWithColor));
}
/// <summary>
/// Demonstrates drawing triangles using OpenGL primitives.
/// Shows how to create colored triangles (CV) and colored triangles with normals (CNV).
/// Illustrates packing of vertex attributes (color, normal, position) into arrays for rendering.
/// </summary>
public static void DrawTrianglesByPrimitives()
{
int n = 10;// triangle num
double[] vs;
////create drawTrisCV
//each triangle has 3 point; each point has r,g,b,x,y,z, totally 6 doubles.
vs = new double[n * 3 * 6];
for (int i = 0, k = 0; i < n; i++)
{
//the rgb values set as casual.
Vec3d rgb = ColorUtil.GetDiscreteRgb(i);
Tri3d tri = new Tri3d(new Vec3d(i, 0, 0), new Vec3d(i + 1, 0, 0), new Vec3d(i + 0.5, 0, 1));
for (int j = 0; j < 3; j++)
{
vs[k++] = rgb.x;
vs[k++] = rgb.y;
vs[k++] = rgb.z;
vs[k++] = tri.ps[j].x;
vs[k++] = tri.ps[j].y;
vs[k++] = tri.ps[j].z;
}
}
//V means Vertex; C means Color
Drawing drawTrisCV = new Drawing(vs, Stamp.CV, GL.GL_TRIANGLES);
////create drawTrisCNV
/////each triangle has 3 point; each point has r,g,b,nx,ny,nz,x,y,z, totally 6 doubles.
vs = new double[n * 3 * 9];
for (int i = 0, k = 0; i < n; i++)
{
//the rgb values set as casual.
Vec3d rgb = ColorUtil.GetDiscreteRgb(i);
Tri3d tri = new Tri3d(new Vec3d(i, 0, 3), new Vec3d(i + 1, 0, 3), new Vec3d(i + 0.5, 0, 4));
Vec3d nn = tri.GetNormal();
for (int j = 0; j < 3; j++)
{
vs[k++] = rgb.x;
vs[k++] = rgb.y;
vs[k++] = rgb.z;
vs[k++] = nn.x;
vs[k++] = nn.y;
vs[k++] = nn.z;
vs[k++] = tri.ps[j].x;
vs[k++] = tri.ps[j].y;
vs[k++] = tri.ps[j].z;
}
}
//V means Vertex; C means Color; N means Normal
Drawing drawTrisCNV = new Drawing(vs, Stamp.CNV, GL.GL_TRIANGLES);
//the color of colorTrisDraw is not influence by outer color setting.
DemoUtil.RunApplication("DrawTrianglesByPrimitives",
new DispList(new RgbTreat(1, 0, 0), drawTrisCV, drawTrisCNV));
}
/// <summary>
/// Demonstrates drawing 3D triangles using the Tri3d helper class.
/// Creates a series of triangles and renders them as both face (filled) and line (wireframe) drawings.
/// Shows how to apply different colors to faces and lines of the same geometry.
/// </summary>
public static void DrawTri3d()
{
int n = 10;
List<Tri3d> tris = new List<Tri3d>(n);
for (int i = 0; i < n; i++)
tris.Add(new Tri3d(new Vec3d(i, 0, 0), new Vec3d(i + 1, 0, 0), new Vec3d(i + 0.5, 0, 1)));
Drawing faceDraw = tris.GetFaceDrawing();
Drawing linesDraw = tris.GetLineDrawing();
DemoUtil.RunApplication("DrawTri3d",
new DispList(new RgbTreat(0, 0, 1), linesDraw
, new RgbTreat(1, 0, 0), faceDraw));
}
static void Main()
{
//FreeDrawing();
//DrawLines();
//DrawTrianglesByPrimitives();
DrawTri3d();
}
}
}