PDA

View Full Version : Nurbs Surface of a dynamically list of vertexes



Kleinstein
09-10-2017, 09:26 AM
hello guys,

I am pretty new in Open GL and need basics help. I use SharpGL (OpenGL wrapped for c#).
I am trying to write an example program to create a NurbsSurface with a
List<Vertex> .

Unfortunately my program don't draw anything. Before it worked with static points. Thus could you say me if I am even on the right way because I read a lot of vertex buffer objects but it seemed a way too difficult for beginner.

Here's my code:


public partial class MainWindow : Window
{
OpenGL gl = new OpenGL();
public List<Vertex> Vertices;
public float[] sKnots = new float[] { 0, 0, 0, 0, 1, 1, 1, 1 };
public float[] tKnots = new float[] { 0, 0, 0, 0, 1, 1, 1, 1 };
GLUnurbsObj theNurb = new GLUnurbsObj();
public NurbsDisplayMode displayMode = NurbsDisplayMode.OutlinePolygon;
double zoom = 0;
bool isRunning;
int column = 40;
int row = 48;
bool DrawControlPoints = true;
bool DrawControlGrid = true;
public MainWindow()
{
try
{
InitializeComponent();
}

catch (Exception e)
{
Console.WriteLine(Convert.ToString(e));
}
}
private int create_zCoord(int i, int j)
{
Random R = new Random();
var a = new int();
a = R.Next(i, j);
return a;
}
private void bStart_Click(object sender, RoutedEventArgs e)
{
new Task(transfer).Start();
}
private void bStop_Click(object sender, RoutedEventArgs e)
{
isRunning = false;
}
public void transfer()
{
isRunning = true;
try
{
do
{
Vertices = new List<Vertex>();
for(int x=0;x<column;x++)
{
for(int y =0;y<row;y++)
{
Vertices.Add(new Vertex(x, y, create_zCoord(0, 10)));
}
}
} while (isRunning);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public float[] ToFloatArray()
{
float[] floats = new float[column * row * 3];
int index = 0;
foreach (Vertex pt in Vertices)
{
floats[index] = pt.X;
floats[index] = pt.Y;
floats[index] = pt.Z;
index++;
}
return floats;
}
public enum NurbsDisplayMode : uint
{
OutlinePatch = OpenGL.GLU_OUTLINE_PATCH,
OutlinePolygon = OpenGL.GLU_OUTLINE_POLYGON,
Fill = OpenGL.GLU_FILL,
}
public void openGLControl_OpenGLDraw(object sender, OpenGLEventArgs args)
{
try
{
if (isRunning)
{
int degree = 2;
// Get the OpenGL object.
OpenGL gl = openGLControl.OpenGL;
// Clear the color and depth buffer.
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
// Load the identity matrix.
gl.LoadIdentity();
gl.ClearColor(1.0f, 1.0f, 1.0f, 1.0f);
theNurb = gl.NewNurbsRenderer();
gl.NurbsProperty(theNurb, (int)OpenGL.GLU_DISPLAY_MODE, (float)displayMode);
// Begin drawing a NURBS surface.
gl.BeginSurface(theNurb);
gl.Color(1, 5, 1);
//gl.BeginTrim(theNurb);
// Draw the surface.
gl.NurbsSurface(theNurb, // The internal nurbs object.
sKnots.Length, // Number of s-knots.
sKnots, // The s-knots themselves.
tKnots.Length, // The number of t-knots.
tKnots, // The t-knots themselves.
4 * 3, // The size of a row of control points. -- Stride is "how far away is it to the next vertex?". It is measured in bytes.Each float is 4 bytes.Vertex 0 is at address 0 and Vertex 1 is at address 3*4 = 12 bytes
3, // The size of a control points.
ToFloatArray(), // The control points.
degree, // The order, i.e degree + 1.
degree, // The order, i.e degree + 1.
OpenGL.GL_MAP2_VERTEX_3); // Type of data to generate.
// End the surface.
gl.EndSurface(theNurb);
}
}
catch (Exception e)
{
Console.WriteLine(Convert.ToString(e));
}

}
/// <summary>
/// Handles the OpenGLInitialized event of the openGLControl1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="args">The <see cref="SharpGL.SceneGraph.OpenGLEventArgs"/> instance containing the event data.</param>
private void openGLControl_OpenGLInitialized(object sender, OpenGLEventArgs args)
{
OpenGL gl = openGLControl.OpenGL;

// TODO: Initialise OpenGL here.

// Set the clear color.
gl.ClearColor(0, 0, 0, 0);
gl.Enable(OpenGL.GL_DEPTH_TEST);
gl.Enable(OpenGL.GL_CULL_FACE); // do not calculate inside of poly's----Definition der Auszublendenen Flächenseite
gl.FrontFace(OpenGL.GL_CCW); // counter clock-wise polygons are out --- Flächenvorderseitendefinition
gl.Enable(OpenGL.GL_AUTO_NORMAL);
gl.Enable(OpenGL.GL_MAP2_VERTEX_3);
gl.ShadeModel(OpenGL.GL_FLAT); // Set the shading model to GL_FLAT
gl.Enable(OpenGL.GL_LINE_SMOOTH);
gl.Hint(OpenGL.GL_LINE_SMOOTH_HINT, OpenGL.GL_NICEST); // Set Line Antialiasing

}

/// <summary>
/// Handles the Resized event of the openGLControl1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="args">The <see cref="SharpGL.SceneGraph.OpenGLEventArgs"/> instance containing the event data.</param>
private void openGLControl_Resized(object sender, OpenGLEventArgs args)
{
// TODO: Set the projection matrix here.
// Get the OpenGL object.
OpenGL gl = openGLControl.OpenGL;
int w = gl.RenderContextProvider.Width;
int h = gl.RenderContextProvider.Height;
zoom = 200;
// Set the projection matrix.
gl.MatrixMode(OpenGL.GL_PROJECTION);
// Load the identity.
gl.LoadIdentity();
gl.Ortho(gl.RenderContextProvider.Width / zoom, -gl.RenderContextProvider.Width / zoom, -gl.RenderContextProvider.Height / zoom,
gl.RenderContextProvider.Height / zoom, -200, 200);
gl.Rotate(-65.0f, 1.0f, 0);
gl.Rotate(-55, 0, 35);
gl.Viewport(0, 0, w, h);
//if (w <= h)
// gl.Ortho(-5.0, 5.0, -5.0 * (float)h / (float)w,
// 5.0 * (float)h / (float)w, -5.0, 5.0);
//else
// gl.Ortho(-5.0 * (float)w / (float)h,
// 5.0 * (float)w / (float)h, -5.0, 5.0, -5.0, 5.0);

gl.MatrixMode(OpenGL.GL_MODELVIEW);
}
}

Xaml:


<Window x:Class="Visualization.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SharpGL WPF Application" Height="Auto" Width="Auto"
xmlns:sharpGL="clr-namespace:SharpGL.WPF;assembly=SharpGL.WPF">



<Grid HorizontalAlignment="Left" Height="475" Margin="5,24,0,0" Grid.RowSpan="2" VerticalAlignment="Top" Width="742">
<Button x:Name="bStart" Content="Start" HorizontalAlignment="Right" Margin="0,0,20,20" VerticalAlignment="Bottom" Width="75" Click="bStart_Click"/>
<Button x:Name="bStop" Content="Stop" HorizontalAlignment="Right" Margin="0,0,20,54" VerticalAlignment="Bottom" Width="75" Click="bStop_Click"/>
<!-- The OpenGL control provides a OpenGL drawing surface. -->
<sharpGL:OpenGLControl
Name="openGLControl" OpenGLDraw="openGLControl_OpenGLDraw"
OpenGLInitialized="openGLControl_OpenGLInitialized" Resized="openGLControl_Resized"
DrawFPS="True" FrameRate="1000" Margin="0,0,20,93">
</sharpGL:OpenGLControl>
</Grid>


</Window>


Thank you for every help or note.