121 lines
4.0 KiB
C#
121 lines
4.0 KiB
C#
using System;
|
|
|
|
using OpenMetaverse;
|
|
using OpenMetaverse.Rendering;
|
|
|
|
namespace AvatarPreview
|
|
{
|
|
/// <summary>
|
|
/// Subclass of LindenMesh that adds vertex, index, and texture coordinate
|
|
/// arrays suitable for pushing direct to OpenGL
|
|
/// </summary>
|
|
public class GLMesh : LindenMesh
|
|
{
|
|
/// <summary>
|
|
/// Subclass of LODMesh that adds an index array suitable for pushing
|
|
/// direct to OpenGL
|
|
/// </summary>
|
|
public new class LODMesh : LindenMesh.LODMesh
|
|
{
|
|
public ushort[] Indices;
|
|
|
|
public override void LoadMesh(string filename)
|
|
{
|
|
base.LoadMesh(filename);
|
|
|
|
// Generate the index array
|
|
Indices = new ushort[_numFaces * 3];
|
|
var current = 0;
|
|
for (var i = 0; i < _numFaces; i++)
|
|
{
|
|
Indices[current++] = (ushort)_faces[i].Indices[0];
|
|
Indices[current++] = (ushort)_faces[i].Indices[1];
|
|
Indices[current++] = (ushort)_faces[i].Indices[2];
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public struct GLData
|
|
{
|
|
public float[] Vertices;
|
|
public ushort[] Indices;
|
|
public float[] TexCoords;
|
|
public Vector3 Center;
|
|
}
|
|
|
|
public GLData RenderData;
|
|
|
|
public GLMesh(string name)
|
|
: base(name)
|
|
{
|
|
}
|
|
|
|
public override void LoadMesh(string filename)
|
|
{
|
|
base.LoadMesh(filename);
|
|
|
|
float minY, minZ;
|
|
var minX = minY = minZ = Single.MaxValue;
|
|
float maxY, maxZ;
|
|
var maxX = maxY = maxZ = Single.MinValue;
|
|
|
|
// Generate the vertex array
|
|
RenderData.Vertices = new float[NumVertices * 3];
|
|
int current = 0;
|
|
for (int i = 0; i < NumVertices; i++)
|
|
{
|
|
RenderData.Vertices[current++] = Vertices[i].Coord.X;
|
|
RenderData.Vertices[current++] = Vertices[i].Coord.Y;
|
|
RenderData.Vertices[current++] = Vertices[i].Coord.Z;
|
|
|
|
if (Vertices[i].Coord.X < minX)
|
|
minX = Vertices[i].Coord.X;
|
|
else if (Vertices[i].Coord.X > maxX)
|
|
maxX = Vertices[i].Coord.X;
|
|
|
|
if (Vertices[i].Coord.Y < minY)
|
|
minY = Vertices[i].Coord.Y;
|
|
else if (Vertices[i].Coord.Y > maxY)
|
|
maxY = Vertices[i].Coord.Y;
|
|
|
|
if (Vertices[i].Coord.Z < minZ)
|
|
minZ = Vertices[i].Coord.Z;
|
|
else if (Vertices[i].Coord.Z > maxZ)
|
|
maxZ = Vertices[i].Coord.Z;
|
|
}
|
|
|
|
// Calculate the center-point from the bounding box edges
|
|
RenderData.Center = new Vector3((minX + maxX) / 2, (minY + maxY) / 2, (minZ + maxZ) / 2);
|
|
|
|
// Generate the index array
|
|
RenderData.Indices = new ushort[NumFaces * 3];
|
|
current = 0;
|
|
for (int i = 0; i < NumFaces; i++)
|
|
{
|
|
RenderData.Indices[current++] = (ushort)Faces[i].Indices[0];
|
|
RenderData.Indices[current++] = (ushort)Faces[i].Indices[1];
|
|
RenderData.Indices[current++] = (ushort)Faces[i].Indices[2];
|
|
}
|
|
|
|
// Generate the texcoord array
|
|
RenderData.TexCoords = new float[NumVertices * 2];
|
|
current = 0;
|
|
for (int i = 0; i < NumVertices; i++)
|
|
{
|
|
RenderData.TexCoords[current++] = Vertices[i].TexCoord.X;
|
|
RenderData.TexCoords[current++] = Vertices[i].TexCoord.Y;
|
|
}
|
|
}
|
|
|
|
public override void LoadLODMesh(int level, string filename)
|
|
{
|
|
LODMesh lod = new LODMesh();
|
|
lod.LoadMesh(filename);
|
|
LodMeshes[level] = lod;
|
|
}
|
|
}
|
|
}
|