Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounding Volumes #53

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#Version 2.5 (In Progress)

* All imported APIs now use 'SetLastError = true' to aid in analysing issues (thanks [robinsedlaczek](https://github.com/robinsedlaczek).
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ All documentation is available on [the Wiki](https://github.com/dwmkerr/sharpgl/
Credits, Sponsorship & Thanks
-----------------------------

SharpGL is written and maintained by me.
SharpGL is written and maintained by me. Special thanks go to the following contributors:

* [robinsedlaczek](https://github.com/robinsedlaczek) - Code and documentation updates, tireless patience
while I get through a backlog of work!

### NDepend ###

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public interface IHasObjectSpace
/// </summary>
LinearTransformation Transformation
{
set;
get;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public bool DrawControlGrid
[Description("The Quadric Object Space Transformation"), Category("Evaluator")]
public LinearTransformation Transformation
{
set { hasObjectSpaceHelper.Transformation = value; }
get { return hasObjectSpaceHelper.Transformation; }
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Runtime.InteropServices;

namespace SharpGL.SceneGraph.Primitives
{
/// <summary>
/// Extensions for Array type.
/// </summary>
public static class ArrayExtensions
{
/// <summary>
/// Flattens the specified array.
/// </summary>
/// <typeparam name="T">The array type.</typeparam>
/// <param name="array">The array.</param>
/// <returns>The flattened array.</returns>
public static T[] Flatten<T>(this T[,,] array)
where T : struct
{
int size = Marshal.SizeOf(array[0, 0, 0]);
int totalSize = Buffer.ByteLength(array);
T[] result = new T[totalSize / size];
Buffer.BlockCopy(array, 0, result, 0, totalSize);
return result;
}
}
}
14 changes: 7 additions & 7 deletions source/SharpGL/Core/SharpGL.SceneGraph/Primitives/Axies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ private void CreateDisplayList(OpenGL gl)
gl.DepthFunc(OpenGL.GL_ALWAYS);

// Set a nice fat line width.
gl.LineWidth(1.50f);
gl.LineWidth(2.0f);

// Draw the axies.
gl.Begin(OpenGL.GL_LINES);
gl.Color(1f, 0f, 0f, 1f);
gl.Color(0.75f, 0f, 0f, 0.5f);
gl.Vertex(0, 0, 0);
gl.Vertex(3, 0, 0);
gl.Color(0f, 1f, 0f, 1f);
gl.Vertex(1.5, 0, 0);
gl.Color(0f, 0.75f, 0f, 0.5f);
gl.Vertex(0, 0, 0);
gl.Vertex(0, 3, 0);
gl.Color(0f, 0f, 1f, 1f);
gl.Vertex(0, 1.5, 0);
gl.Color(0f, 0f, 0.75f, 0.5f);
gl.Vertex(0, 0, 0);
gl.Vertex(0, 0, 3);
gl.Vertex(0, 0, 1.5);
gl.End();

// Restore attributes.
Expand Down
5 changes: 4 additions & 1 deletion source/SharpGL/Core/SharpGL.SceneGraph/Primitives/Cube.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ private void CreateCubeGeometry()
face.Indices.Add(new Index(3, 2));
face.Indices.Add(new Index(2, 3));
Faces.Add(face);
}

BoundingVolume.FromVertices(Vertices);
BoundingVolume.Pad(0.1f);
}
}
}
55 changes: 37 additions & 18 deletions source/SharpGL/Core/SharpGL.SceneGraph/Primitives/Polygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,25 @@ public virtual void AddFaceFromVertexData(Vertex[] vertexData)
foreach(Vertex v in vertexData)
{
// Do we have this vertex already?
int at = VertexSearch.Search(vertices, 0, v, 0.01f);
int at = VertexSearch.Search(Vertices, 0, v, 0.01f);

// Add the vertex, and index it.
if (at == -1)
{
newFace.Indices.Add(new Index(vertices.Count));
vertices.Add(v);
newFace.Indices.Add(new Index(Vertices.Count));
Vertices.Add(v);
}
else
{
newFace.Indices.Add(new Index(at));
}
}

// Add the face.
faces.Add(newFace);
boundingVolumeHelper.BoundingVolume.FromVertices(vertices);
boundingVolumeHelper.BoundingVolume.Pad(0.1f);

// Add the face.
faces.Add(newFace);
}

/// <summary>
Expand Down Expand Up @@ -163,7 +166,7 @@ public virtual void Render(OpenGL gl, RenderMode renderMode)
}

// Set the vertex.
gl.Vertex(vertices[index.Vertex]);
gl.Vertex(Vertices[index.Vertex]);
}

gl.End();
Expand Down Expand Up @@ -191,7 +194,7 @@ public virtual void Render(OpenGL gl, RenderMode renderMode)
if (index.Normal != -1 && index.Vertex != -1)
{
// Get the vertex.
Vertex vertex = vertices[index.Vertex];
Vertex vertex = Vertices[index.Vertex];

// Get the normal vertex.
Vertex normal = normals[index.Normal];
Expand Down Expand Up @@ -279,7 +282,10 @@ public virtual bool CreateFromMap(string filename, int xPoints, int yPoints)
}
}

return true;
boundingVolumeHelper.BoundingVolume.FromVertices(vertices);
boundingVolumeHelper.BoundingVolume.Pad(0.1f);

return true;
}

/// <summary>
Expand Down Expand Up @@ -356,9 +362,9 @@ private Intersection TestIntersection(Ray ray)

// Find the point of intersection upon the plane, as a point 't' along
// the ray.
Vertex point1OnPlane = vertices[face.Indices[0].Vertex];
Vertex point2OnPlane = vertices[face.Indices[1].Vertex];
Vertex point3OnPlane = vertices[face.Indices[2].Vertex];
Vertex point1OnPlane = Vertices[face.Indices[0].Vertex];
Vertex point2OnPlane = Vertices[face.Indices[1].Vertex];
Vertex point3OnPlane = Vertices[face.Indices[2].Vertex];
Vertex midpointOpp1 = (point2OnPlane + point3OnPlane) / 2;
Vertex midpointOpp2 = (point1OnPlane + point3OnPlane) / 2;
Vertex midpointOpp3 = (point1OnPlane + point2OnPlane) / 2;
Expand Down Expand Up @@ -520,7 +526,10 @@ public int Subdivide()

faces = newFaces;

return faces.Count;
boundingVolumeHelper.BoundingVolume.FromVertices(vertices);
boundingVolumeHelper.BoundingVolume.Pad(0.1f);

return faces.Count;
}

/// <summary>
Expand Down Expand Up @@ -644,9 +653,22 @@ public List<Face> Faces
[Description("The vertices that make up the polygon."), Category("Polygon")]
public List<Vertex> Vertices
{
get {return vertices;}
set {vertices = value; }
}
get
{
return vertices;
}

set
{
vertices = value;

// TODO: [RS] Create bv when vertices changed. We need a mechanism that creates the bounding volume
// if one verte is changed, removed or added. Use INotifyPropertyChanged for vertices or
// some other observer pattern?
boundingVolumeHelper.BoundingVolume.FromVertices(vertices);
boundingVolumeHelper.BoundingVolume.Pad(0.1f);
}
}

/// <summary>
/// Gets or sets the U vs.
Expand Down Expand Up @@ -706,9 +728,6 @@ public BoundingVolume BoundingVolume
{
get
{
// todo; only create bv when vertices changed.
boundingVolumeHelper.BoundingVolume.FromVertices(vertices);
boundingVolumeHelper.BoundingVolume.Pad(0.1f);
return boundingVolumeHelper.BoundingVolume;
}
}
Expand Down
Loading