Added Shape.cs and Plane.cs, for use in Examples. Shape is an abstract class that is parent to all shapes, and Plane derives from Shape, implementing a configurable plane (with vertex, normal, index and texcoord arrays).

This commit is contained in:
the_fiddler 2007-09-26 12:00:29 +00:00
parent ff871cff50
commit a64fd74ac1
2 changed files with 134 additions and 0 deletions

View file

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenTK.Math;
namespace Examples.Shapes
{
public class Plane : Shape
{
public Plane(int x_res, int y_res, float x_scale, float y_scale)
{
Vertices = new Vector3[x_res * y_res];
Normals = new Vector3[x_res * y_res];
Indices = new int[6 * x_res * y_res];
Texcoords = new Vector2[x_res * y_res];
int i = 0;
for (int y = -y_res / 2; y < y_res / 2; y++)
{
for (int x = -x_res / 2; x < x_res / 2; x++)
{
Vertices[i].X = x_scale * (float)x / (float)x_res;
Vertices[i].Y = y_scale * (float)y / (float)y_res;
Vertices[i].Z = 0;
Normals[i].X = Normals[i].Y = 0;
Normals[i].Z = 1;
i++;
}
}
i = 0;
for (int y = 0; y < y_res - 1; y++)
{
for (int x = 0; x < x_res - 1; x++)
{
Indices[i++] = (y + 0) * x_res + x;
Indices[i++] = (y + 1) * x_res + x;
Indices[i++] = (y + 0) * x_res + x + 1;
Indices[i++] = (y + 0) * x_res + x + 1;
Indices[i++] = (y + 1) * x_res + x;
Indices[i++] = (y + 1) * x_res + x + 1;
}
}
}
}
}

View file

@ -0,0 +1,87 @@
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using OpenTK.Math;
namespace Examples.Shapes
{
public abstract class Shape
{
private Vector3[] vertices, normals;
private Vector2[] texcoords;
private int[] indices;
unsafe int* index_ptr;
unsafe float* vertex_ptr, normal_ptr, texcoord_ptr;
public Vector3[] Vertices
{
get { return vertices; }
protected set
{
unsafe
{
vertices = value;
//fixed (float* ptr = (float*)vertices[0])
{
vertex_ptr = (float*)vertices[0];
}
}
}
}
public Vector3[] Normals
{
get { return normals; }
protected set
{
unsafe
{
normals = value;
//fixed (float* ptr = (float*)normals[0])
{
normal_ptr = (float*)normals[0];
}
}
}
}
public Vector2[] Texcoords
{
get { return texcoords; }
protected set
{
unsafe
{
texcoords = value;
//fixed (float* ptr = (float*)texcoords[0])
{
texcoord_ptr = (float*)texcoords[0];
}
}
}
}
public int[] Indices
{
get { return indices; }
protected set
{
unsafe
{
indices = value;
fixed (int* ptr = indices)
{
index_ptr = ptr;
}
}
}
}
}
}