1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
using System;
using System.Windows;
using System.Windows.Controls;
//references
using SharpGL;
using SharpGL.Enumerations;
using SharpGL.SceneGraph;//vertex
using SharpGL.SceneGraph.Core;//rendermode
using SharpGL.SceneGraph.Primitives;// axies,cube,grid,polygon
using SharpGL.SceneGraph.Quadrics;// sphere
namespace WpfCourbe3D
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Axies axies = new Axies();
private Vertex[] vertices = null;
private uint NumberOfVertices = 300;
private Sphere solid = new Sphere();
private double radius = 0.05;
private int anim = -1;
private bool flag = true;
public MainWindow()
{
InitializeComponent();
}
private void openGLControl1_OpenGLInitialized(object sender, OpenGLEventArgs args)
{
// Get the OpenGL object.
OpenGL gl = openGLControl1.OpenGL;
// vertices.
vertices = GeometryGenerator.GenerateGeometry(NumberOfVertices);
// Thickness Point.
args.OpenGL.PointSize(3.0f);
// Create Sphere
CreateSphere(gl, solid, radius);
}
private void openGLControl1_OpenGLDraw(object sender, OpenGLEventArgs args)
{
// Get the OpenGL instance that's been passed to us.
OpenGL gl = args.OpenGL;
// Clear the color and depth buffers.
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
// Reset the modelview matrix.
gl.LoadIdentity();
gl.LookAt(3.0, 3.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// Move the geometry into a fairly central position.
gl.Translate(0.0f, 0.0f, 0.0f);
gl.Rotate(0.0f, 30.0f, 0.0f);
//Render axies
axies.Render(gl, RenderMode.Design);
gl.Scale(2.5f, 1.0f, 2.5f);
RenderVertices(args.OpenGL);
if (flag)
anim++;
else
anim--;
if (anim > vertices.Length - 1)
{
anim = vertices.Length - 1;
flag = false;
}
if (anim < 0)
{
anim = 0;
flag = true;
};
gl.Translate(vertices[anim].X, vertices[anim].Y, vertices[anim].Z);
gl.Scale(0.5f, 0.5f, 0.5f);
RenderSphere(gl, solid);
// Flush OpenGL.
gl.Flush();
}
//render vertices
private void RenderVertices(OpenGL gl)
{
gl.Color(1.0f, 0.0f, 0.0f);
gl.Begin(OpenGL.GL_POINTS);
for (uint i = 0; i < vertices.Length; i++)
{
gl.Vertex(vertices[i]);
}
gl.End();
gl.Color(0.0f, 1f, 0.5f);
gl.Begin(OpenGL.GL_LINE_STRIP);
for (uint i = 0; i < vertices.Length; i++)
{
gl.Vertex(vertices[i]);
}
gl.End();
}
//render sphere
private void RenderSphere(OpenGL gl, Sphere solid)
{
gl.Color(0.0f, 0.0f, 1.0f);
solid.Render(gl, RenderMode.Design);
}
//initialisation sphere
private void CreateSphere(OpenGL gl, Sphere sph, double rad)
{
sph.CreateInContext(gl);
sph.Stacks = 20;
sph.Slices = 20;
sph.QuadricDrawStyle = DrawStyle.Fill;
sph.Radius = rad;
}
}
} |
Partager