Fixed memory corruption issue with (Cube's vertices were being moved by the GC). I think.

This commit is contained in:
the_fiddler 2007-09-30 12:34:20 +00:00
parent d526f5a3a4
commit 6341ced26c
2 changed files with 68 additions and 82 deletions

View file

@ -10,61 +10,65 @@ using System.Text;
using System.Drawing; using System.Drawing;
using OpenTK.Math; using OpenTK.Math;
using System.Runtime.InteropServices;
namespace Examples.Shapes namespace Examples.Shapes
{ {
public static class Cube public class Cube : Shape
{ {
public static readonly Vector3[] Vertices = new Vector3[8] public Cube()
{ {
new Vector3(-1.0f, -1.0f, 1.0f), Vertices = new Vector3[]
new Vector3( 1.0f, -1.0f, 1.0f), {
new Vector3( 1.0f, 1.0f, 1.0f), new Vector3(-1.0f, -1.0f, 1.0f),
new Vector3(-1.0f, 1.0f, 1.0f), new Vector3( 1.0f, -1.0f, 1.0f),
new Vector3(-1.0f, -1.0f, -1.0f), new Vector3( 1.0f, 1.0f, 1.0f),
new Vector3( 1.0f, -1.0f, -1.0f), new Vector3(-1.0f, 1.0f, 1.0f),
new Vector3( 1.0f, 1.0f, -1.0f), new Vector3(-1.0f, -1.0f, -1.0f),
new Vector3(-1.0f, 1.0f, -1.0f), new Vector3( 1.0f, -1.0f, -1.0f),
}; new Vector3( 1.0f, 1.0f, -1.0f),
new Vector3(-1.0f, 1.0f, -1.0f)
};
public static readonly ushort[] Indices = Indices = new int[]
{ {
// front face // front face
0, 1, 2, 2, 3, 0, 0, 1, 2, 2, 3, 0,
// top face // top face
3, 2, 6, 6, 7, 3, 3, 2, 6, 6, 7, 3,
// back face // back face
7, 6, 5, 5, 4, 7, 7, 6, 5, 5, 4, 7,
// left face // left face
4, 0, 3, 3, 7, 4, 4, 0, 3, 3, 7, 4,
// bottom face // bottom face
0, 1, 5, 5, 4, 0, 0, 1, 5, 5, 4, 0,
// right face // right face
1, 5, 6, 6, 2, 1, 1, 5, 6, 6, 2, 1,
}; };
public static readonly Vector3[] Normals = Normals = new Vector3[]
{ {
new Vector3(-1.0f, -1.0f, 1.0f), new Vector3(-1.0f, -1.0f, 1.0f),
new Vector3( 1.0f, -1.0f, 1.0f), new Vector3( 1.0f, -1.0f, 1.0f),
new Vector3( 1.0f, 1.0f, 1.0f), new Vector3( 1.0f, 1.0f, 1.0f),
new Vector3(-1.0f, 1.0f, 1.0f), new Vector3(-1.0f, 1.0f, 1.0f),
new Vector3(-1.0f, -1.0f, -1.0f), new Vector3(-1.0f, -1.0f, -1.0f),
new Vector3( 1.0f, -1.0f, -1.0f), new Vector3( 1.0f, -1.0f, -1.0f),
new Vector3( 1.0f, 1.0f, -1.0f), new Vector3( 1.0f, 1.0f, -1.0f),
new Vector3(-1.0f, 1.0f, -1.0f), new Vector3(-1.0f, 1.0f, -1.0f),
}; };
public static readonly int[] Colors = Colors = new int[]
{ {
Color.Firebrick.ToArgb(), Color.Firebrick.ToArgb(),
Color.Honeydew.ToArgb(), Color.Honeydew.ToArgb(),
Color.Moccasin.ToArgb(), Color.Moccasin.ToArgb(),
Color.Yellow.ToArgb(), Color.Yellow.ToArgb(),
Color.Crimson.ToArgb(), Color.Crimson.ToArgb(),
Color.DarkGoldenrod.ToArgb(), Color.DarkGoldenrod.ToArgb(),
Color.ForestGreen.ToArgb(), Color.ForestGreen.ToArgb(),
Color.Sienna.ToArgb(), Color.Sienna.ToArgb(),
}; };
}
} }
} }

View file

@ -9,6 +9,8 @@ using System.Collections.Generic;
using System.Text; using System.Text;
using OpenTK.Math; using OpenTK.Math;
using System.Runtime.InteropServices;
using System.Drawing;
namespace Examples.Shapes namespace Examples.Shapes
{ {
@ -17,22 +19,14 @@ namespace Examples.Shapes
private Vector3[] vertices, normals; private Vector3[] vertices, normals;
private Vector2[] texcoords; private Vector2[] texcoords;
private int[] indices; private int[] indices;
unsafe int* index_ptr; private int[] colors;
unsafe float* vertex_ptr, normal_ptr, texcoord_ptr;
public Vector3[] Vertices public Vector3[] Vertices
{ {
get { return vertices; } get { return vertices; }
protected set protected set
{ {
unsafe vertices = value;
{
vertices = value;
//fixed (float* ptr = (float*)vertices[0])
{
vertex_ptr = (float*)vertices[0];
}
}
} }
} }
@ -41,14 +35,7 @@ namespace Examples.Shapes
get { return normals; } get { return normals; }
protected set protected set
{ {
unsafe normals = value;
{
normals = value;
//fixed (float* ptr = (float*)normals[0])
{
normal_ptr = (float*)normals[0];
}
}
} }
} }
@ -57,14 +44,7 @@ namespace Examples.Shapes
get { return texcoords; } get { return texcoords; }
protected set protected set
{ {
unsafe texcoords = value;
{
texcoords = value;
//fixed (float* ptr = (float*)texcoords[0])
{
texcoord_ptr = (float*)texcoords[0];
}
}
} }
} }
@ -73,14 +53,16 @@ namespace Examples.Shapes
get { return indices; } get { return indices; }
protected set protected set
{ {
unsafe indices = value;
{ }
indices = value; }
fixed (int* ptr = indices)
{ public int[] Colors
index_ptr = ptr; {
} get { return colors; }
} protected set
{
colors = value;
} }
} }
} }