Files
Pilz/Pilz.Drawing.Drawing3D.OpenGLRenderer/Rendering/Renderer.cs
2020-09-24 11:21:53 +02:00

485 lines
31 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using global::System.Drawing;
using Bitmap = global::System.Drawing.Bitmap;
using Color = global::System.Drawing.Color;
using Image = global::System.Drawing.Image;
using System.Linq;
using global::System.Threading;
using System.Threading.Tasks;
using global::System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using global::OpenTK;
using global::OpenTK.Graphics.OpenGL;
using global::Pilz.S3DFileParser;
namespace Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN
{
public class Renderer
{
private global::Pilz.S3DFileParser.Object3D obj3d;
private Dictionary<Image, int> dicTextureIDs = new global::System.Collections.Generic.Dictionary<global::System.Drawing.Image, global::System.Int32>();
private global::System.Collections.Generic.Dictionary<global::System.Drawing.Color, global::System.Int32> dicColorIDs = new global::System.Collections.Generic.Dictionary<global::System.Drawing.Color, global::System.Int32>();
private global::System.Drawing.Bitmap emptyTexture = null;
private global::System.Drawing.Bitmap lineTexture = null;
private global::System.Drawing.Bitmap selectedLineTexture = null;
public float ModelScaling { get; set; } = 1.0f;
public bool HasRendered { get; private set; } = false;
public global::System.Collections.Generic.List<global::System.Object> SelectedElements { get; private set; }
private global::System.Collections.Generic.Dictionary<global::Pilz.S3DFileParser.Mesh, global::System.Int32> VertexBuffers { get; set; } = new global::System.Collections.Generic.Dictionary<global::Pilz.S3DFileParser.Mesh, global::System.Int32>();
private global::System.Collections.Generic.Dictionary<global::Pilz.S3DFileParser.Mesh, global::System.Collections.Generic.List<global::System.Int32>> IndicesBuffers { get; set; } = new global::System.Collections.Generic.Dictionary<global::Pilz.S3DFileParser.Mesh, global::System.Collections.Generic.List<global::System.Int32>>();
private global::System.Collections.Generic.Dictionary<global::Pilz.S3DFileParser.Mesh, global::System.Int32> UVBuffers { get; set; } = new global::System.Collections.Generic.Dictionary<global::Pilz.S3DFileParser.Mesh, global::System.Int32>();
private global::System.Collections.Generic.Dictionary<global::Pilz.S3DFileParser.Mesh, global::System.Int32> VertexColorBuffers { get; set; } = new global::System.Collections.Generic.Dictionary<global::Pilz.S3DFileParser.Mesh, global::System.Int32>();
private global::System.Collections.Generic.Dictionary<global::Pilz.S3DFileParser.Mesh, global::System.Int32> NormalBuffers { get; set; } = new global::System.Collections.Generic.Dictionary<global::Pilz.S3DFileParser.Mesh, global::System.Int32>();
public global::Pilz.S3DFileParser.Object3D Model
{
get
{
return this.obj3d;
}
}
public Renderer(global::Pilz.S3DFileParser.Object3D obj3d)
{
this.obj3d = obj3d.ToOneMesh();
// Set Texture used for faces without texture
this.emptyTexture = (global::System.Drawing.Bitmap)this.ColorToTexture(global::System.Drawing.Color.LightGray);
// Set Texture used for lines
this.lineTexture = (global::System.Drawing.Bitmap)this.ColorToTexture(global::System.Drawing.Color.Black);
// Set Texture used for lines of selected faces
this.selectedLineTexture = (global::System.Drawing.Bitmap)this.ColorToTexture(global::System.Drawing.Color.Orange);
}
private global::System.Drawing.Image ColorToTexture(global::System.Drawing.Color color)
{
var tex = new global::System.Drawing.Bitmap(1, 1);
tex.SetPixel(0, 0, color);
return tex;
}
/// <summary>
/// Updates the Data of a Vertex in the buffer.
/// </summary>
/// <param name="m">The Mesh where the Vertex is listed.</param>
/// <param name="v">The Vertex to update.</param>
public void UpdateVertexData(global::Pilz.S3DFileParser.Mesh m, global::Pilz.S3DFileParser.Vertex v)
{
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, this.VertexBuffers[m]);
var argdata = new global::OpenTK.Vector3((global::System.Single)v.X, (global::System.Single)v.Y, (global::System.Single)v.Z);
global::OpenTK.Graphics.OpenGL.GL.BufferSubData(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, (global::System.IntPtr)((m.Vertices.IndexOf(v)) * (global::OpenTK.Vector3.SizeInBytes)), global::OpenTK.Vector3.SizeInBytes, ref argdata);
}
/// <summary>
/// Updates the Data of a Normal in the buffer.
/// </summary>
/// <param name="m">The Mesh where the Vertex is listed.</param>
/// <param name="n">The Normal to update.</param>
public void UpdateNormalData(global::Pilz.S3DFileParser.Mesh m, global::Pilz.S3DFileParser.Normal n)
{
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, this.NormalBuffers[m]);
var argdata = new global::OpenTK.Vector3(n.X, n.Y, n.Z);
global::OpenTK.Graphics.OpenGL.GL.BufferSubData(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, (global::System.IntPtr)((m.Normals.IndexOf(n)) * (global::OpenTK.Vector3.SizeInBytes)), global::OpenTK.Vector3.SizeInBytes, ref argdata);
}
/// <summary>
/// Updates the Data of a Vertex Color in the buffer.
/// </summary>
/// <param name="m">The Mesh where the Vertex is listed.</param>
/// <param name="vc">The Vertex Color to update.</param>
public void UpdateVertexColorData(global::Pilz.S3DFileParser.Mesh m, global::Pilz.S3DFileParser.VertexColor vc)
{
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, this.VertexColorBuffers[m]);
var argdata = new global::OpenTK.Vector4(vc.R, vc.G, vc.B, vc.A);
global::OpenTK.Graphics.OpenGL.GL.BufferSubData(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, (global::System.IntPtr)((m.VertexColors.IndexOf(vc)) * (global::OpenTK.Vector4.SizeInBytes)), global::OpenTK.Vector4.SizeInBytes, ref argdata);
}
/// <summary>
/// Updates the Data of a UV in the buffer.
/// </summary>
/// <param name="m">The Mesh where the Vertex is listed.</param>
/// <param name="uv">The UV to update.</param>
public void UpdateUVData(global::Pilz.S3DFileParser.Mesh m, global::Pilz.S3DFileParser.UV uv)
{
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, this.UVBuffers[m]);
var argdata = new global::OpenTK.Vector2(uv.U, uv.V);
global::OpenTK.Graphics.OpenGL.GL.BufferSubData(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, (global::System.IntPtr)((m.UVs.IndexOf(uv)) * (global::OpenTK.Vector2.SizeInBytes)), global::OpenTK.Vector2.SizeInBytes, ref argdata);
}
/// <summary>
/// Updates the indicies of a face in the buffer.
/// </summary>
/// <param name="m">The Mesh where the Vertex is listed.</param>
/// <param name="f">The Face to update.</param>
public void UpdateFaceIndicies(global::Pilz.S3DFileParser.Mesh m, global::Pilz.S3DFileParser.Face f)
{
global::System.Int32 faceIndex = m.Faces.IndexOf(f);
global::System.Byte uintlen = (global::System.Byte)global::Microsoft.VisualBasic.Strings.Len(new uint());
var indicies = new global::OpenTK.Vector3((global::System.Single)m.Vertices.IndexOf(f.Points[0].Vertex), (global::System.Single)m.Vertices.IndexOf(f.Points[1].Vertex), (global::System.Single)m.Vertices.IndexOf(f.Points[2].Vertex));
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, this.IndicesBuffers[m][faceIndex]);
global::OpenTK.Graphics.OpenGL.GL.BufferSubData(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, (global::System.IntPtr)((global::System.Int32)(uintlen) * (faceIndex)), (global::System.Int32)uintlen, ref indicies);
}
/// <summary>
/// Replace an Image with a new one.
/// </summary>
/// <param name="oldImage"></param>
/// <param name="newImage"></param>
public void UpdateTexture(global::System.Drawing.Image oldImage, global::System.Drawing.Image newImage)
{
if (this.dicTextureIDs.ContainsKey(oldImage))
{
global::System.Int32 id = this.dicTextureIDs[oldImage];
this.dicTextureIDs.Remove(oldImage);
this.dicTextureIDs.Add(newImage, id);
global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.ContentPipe.LoadTexture((global::System.Drawing.Bitmap)newImage, id);
}
}
/// <summary>
/// Updates an Image.
/// </summary>
/// <param name="image"></param>
public void UpdateTexture(global::System.Drawing.Image image)
{
if (this.dicTextureIDs.ContainsKey(image))
{
global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.ContentPipe.LoadTexture((this.dicTextureIDs[image]).ToString());
}
}
/// <summary>
/// Creates the Buffers and store the requied Data.
/// </summary>
public void RenderModel()
{
this.ReleaseBuffers();
foreach (global::Pilz.S3DFileParser.Mesh mesh in this.obj3d.Meshes)
{
var nibo = new global::System.Collections.Generic.List<global::System.Int32>();
global::System.Boolean enablecols = ((mesh.VertexColors.Count) > (0));
global::System.Boolean enablenorms = (((!(enablecols))) && ((mesh.Normals.Count) > (0)));
var verts = new global::System.Collections.Generic.List<global::OpenTK.Vector3>();
var uvs = new global::System.Collections.Generic.List<global::OpenTK.Vector2>();
var cols = new global::System.Collections.Generic.List<global::OpenTK.Vector4>();
var norms = new global::System.Collections.Generic.List<global::OpenTK.Vector3>();
global::System.UInt64 curvi = 0UL;
this.IndicesBuffers.Add(mesh, nibo);
for (global::System.Int32 i = 0, loopTo = (mesh.Faces.Count) - (1); i <= loopTo; i++)
{
{
var currentFace = mesh.Faces[i];
var indices = new List<UInt32>();
foreach (S3DFileParser.Point p in currentFace.Points)
{
indices.Add((global::System.UInt32)curvi);
curvi = (global::System.UInt64)(curvi + 1m);
if (p.Vertex is object)
{
verts.Add(new Vector3((Single)p.Vertex.X, (Single)p.Vertex.Y, (Single)p.Vertex.Z));
}
else
{
verts.Add(new Vector3(0f, 0f, 0f));
}
if (p.UV is object)
{
uvs.Add(new Vector2(p.UV.U, p.UV.V));
}
else
{
uvs.Add(new Vector2(0f, 0f));
}
if (((enablecols) && p.VertexColor is object))
{
cols.Add(new Vector4(p.VertexColor.R, p.VertexColor.G, p.VertexColor.B, p.VertexColor.A));
}
else
{
cols.Add(new Vector4(1f, 1f, 1f, 1f));
}
if (((enablenorms) && p.Normal is object))
{
norms.Add(new global::OpenTK.Vector3(p.Normal.X, p.Normal.Y, p.Normal.Z));
}
else
{
norms.Add(new global::OpenTK.Vector3(1f, 1f, 1f));
}
}
nibo.Add(GL.GenBuffer());
GL.BindBuffer(BufferTarget.ElementArrayBuffer, nibo[i]);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)((Strings.Len(new uint())) * (indices.Count)), indices.ToArray(), global::OpenTK.Graphics.OpenGL.BufferUsageHint.StaticDraw);
if (currentFace.Material?.Image is object)
{
if (!(this.dicTextureIDs.ContainsKey(currentFace.Material.Image)))
{
this.dicTextureIDs.Add(currentFace.Material.Image, global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.ContentPipe.LoadTexture((global::System.Drawing.Bitmap)currentFace.Material.Image));
}
}
else if (currentFace.Material?.Color is object)
{
if (!(this.dicColorIDs.ContainsKey((global::System.Drawing.Color)currentFace.Material.Color)))
{
this.dicColorIDs.Add((global::System.Drawing.Color)currentFace.Material.Color, global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.ContentPipe.LoadTexture((global::System.Drawing.Bitmap)this.ColorToTexture((global::System.Drawing.Color)currentFace.Material.Color)));
}
}
else if (!(this.dicTextureIDs.ContainsKey(this.emptyTexture)))
{
this.dicTextureIDs.Add(this.emptyTexture, global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.ContentPipe.LoadTexture(this.emptyTexture));
}
}
}
global::System.Int32 nvbo = global::OpenTK.Graphics.OpenGL.GL.GenBuffer();
this.VertexBuffers.Add(mesh, nvbo);
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, nvbo);
global::OpenTK.Graphics.OpenGL.GL.BufferData(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, (global::System.IntPtr)((global::OpenTK.Vector3.SizeInBytes) * (verts.Count)), verts.ToArray(), global::OpenTK.Graphics.OpenGL.BufferUsageHint.StaticDraw);
global::System.Int32 ntbo = global::OpenTK.Graphics.OpenGL.GL.GenBuffer();
this.UVBuffers.Add(mesh, ntbo);
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, ntbo);
global::OpenTK.Graphics.OpenGL.GL.BufferData(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, (global::System.IntPtr)((global::OpenTK.Vector2.SizeInBytes) * (uvs.Count)), uvs.ToArray(), global::OpenTK.Graphics.OpenGL.BufferUsageHint.StaticDraw);
if (enablecols)
{
global::System.Int32 ncbo = global::OpenTK.Graphics.OpenGL.GL.GenBuffer();
this.VertexColorBuffers.Add(mesh, ncbo);
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, ncbo);
global::OpenTK.Graphics.OpenGL.GL.BufferData(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, (global::System.IntPtr)((global::OpenTK.Vector4.SizeInBytes) * (cols.Count)), cols.ToArray(), global::OpenTK.Graphics.OpenGL.BufferUsageHint.StaticDraw);
}
if (enablenorms)
{
global::System.Int32 nnbo = global::OpenTK.Graphics.OpenGL.GL.GenBuffer();
this.NormalBuffers.Add(mesh, nnbo);
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, nnbo);
global::OpenTK.Graphics.OpenGL.GL.BufferData(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, (global::System.IntPtr)((global::OpenTK.Vector3.SizeInBytes) * (norms.Count)), norms.ToArray(), global::OpenTK.Graphics.OpenGL.BufferUsageHint.StaticDraw);
}
}
if (!(this.dicTextureIDs.ContainsKey(this.lineTexture)))
{
this.dicTextureIDs.Add(this.lineTexture, global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.ContentPipe.LoadTexture(this.lineTexture));
}
this.HasRendered = true;
}
public void DrawModel(global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.RenderMode mode)
{
this.DrawModel(mode, global::OpenTK.Vector3.Zero, global::OpenTK.Quaternion.Identity, new global::OpenTK.Vector3(this.ModelScaling, this.ModelScaling, this.ModelScaling));
}
public void DrawModel(global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.RenderMode mode, global::OpenTK.Vector3 pos, global::OpenTK.Quaternion rot)
{
this.DrawModel(mode, pos, rot, new global::OpenTK.Vector3(this.ModelScaling, this.ModelScaling, this.ModelScaling));
}
public void DrawModel(global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.RenderMode mode, global::OpenTK.Vector3 pos, global::OpenTK.Quaternion rot, global::OpenTK.Vector3 scale)
{
if ((mode == global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.RenderMode.None))
return;
if (!(this.HasRendered))
return;
global::OpenTK.Graphics.OpenGL.GL.PushMatrix();
global::OpenTK.Graphics.OpenGL.GL.Translate(pos.X, pos.Y, pos.Z);
global::OpenTK.Graphics.OpenGL.GL.Rotate(rot.X, 1f, 0f, 0f);
global::OpenTK.Graphics.OpenGL.GL.Rotate(rot.Y, 0f, 1f, 0f);
global::OpenTK.Graphics.OpenGL.GL.Rotate(rot.Z, 0f, 0f, 1f);
global::OpenTK.Graphics.OpenGL.GL.Scale(scale); // GL.Scale(scale.X, scale.Y, scale.Z)
global::OpenTK.Graphics.OpenGL.GL.EnableClientState(global::OpenTK.Graphics.OpenGL.ArrayCap.VertexArray);
global::OpenTK.Graphics.OpenGL.GL.EnableClientState(global::OpenTK.Graphics.OpenGL.ArrayCap.TextureCoordArray);
foreach (global::Pilz.S3DFileParser.Mesh mesh in this.obj3d.Meshes)
{
if (this.VertexColorBuffers.ContainsKey(mesh))
{
global::OpenTK.Graphics.OpenGL.GL.EnableClientState(global::OpenTK.Graphics.OpenGL.ArrayCap.ColorArray);
}
else if (this.NormalBuffers.ContainsKey(mesh))
{
global::OpenTK.Graphics.OpenGL.GL.EnableClientState(global::OpenTK.Graphics.OpenGL.ArrayCap.NormalArray);
}
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, this.VertexBuffers[mesh]);
global::OpenTK.Graphics.OpenGL.GL.VertexPointer(3, global::OpenTK.Graphics.OpenGL.VertexPointerType.Float, 0, global::System.IntPtr.Zero);
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, this.UVBuffers[mesh]);
global::OpenTK.Graphics.OpenGL.GL.TexCoordPointer(2, global::OpenTK.Graphics.OpenGL.TexCoordPointerType.Float, 0, global::System.IntPtr.Zero);
if (this.VertexColorBuffers.ContainsKey(mesh))
{
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, this.VertexColorBuffers[mesh]);
global::OpenTK.Graphics.OpenGL.GL.ColorPointer(4, global::OpenTK.Graphics.OpenGL.ColorPointerType.Float, 0, global::System.IntPtr.Zero);
}
else if (this.NormalBuffers.ContainsKey(mesh))
{
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, this.NormalBuffers[mesh]);
global::OpenTK.Graphics.OpenGL.GL.NormalPointer(global::OpenTK.Graphics.OpenGL.NormalPointerType.Float, 0, global::System.IntPtr.Zero);
}
for (global::System.Int32 i = 0, loopTo = (mesh.Faces.Count) - (1); i <= loopTo; i++)
{
var l = mesh.Faces[i];
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ElementArrayBuffer, this.IndicesBuffers[mesh][i]);
global::System.Boolean isEmptyTexture = l.Material?.Image is null;
global::System.Boolean isEmptyColor = l.Material?.Color is null;
void setMaterialTextureOrColor()
{
global::System.Int32 texID;
if (!(isEmptyTexture))
{
texID = this.dicTextureIDs[l.Material.Image];
}
else if (!(isEmptyColor))
{
texID = this.dicColorIDs[(global::System.Drawing.Color)l.Material.Color];
}
else
{
texID = this.dicTextureIDs[this.emptyTexture];
}
global::OpenTK.Graphics.OpenGL.GL.BindTexture(global::OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, texID);
};
if ((((mode & global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.RenderMode.Fill)) == global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.RenderMode.Fill))
{
setMaterialTextureOrColor();
if (!(isEmptyTexture))
{
global::OpenTK.Graphics.OpenGL.GL.TexParameter(global::OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, global::OpenTK.Graphics.OpenGL.TextureParameterName.TextureWrapT, l.Material.Wrap.X);
global::OpenTK.Graphics.OpenGL.GL.TexParameter(global::OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, global::OpenTK.Graphics.OpenGL.TextureParameterName.TextureWrapS, l.Material.Wrap.Y);
}
global::OpenTK.Graphics.OpenGL.GL.PolygonMode(global::OpenTK.Graphics.OpenGL.MaterialFace.FrontAndBack, global::OpenTK.Graphics.OpenGL.PolygonMode.Fill);
global::OpenTK.Graphics.OpenGL.GL.DrawElements(global::OpenTK.Graphics.OpenGL.PrimitiveType.Triangles, l.Points.Count, global::OpenTK.Graphics.OpenGL.DrawElementsType.UnsignedInt, global::System.IntPtr.Zero);
}
if ((((mode & global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.RenderMode.Outline)) == global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.RenderMode.Outline))
{
if ((((mode & global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.RenderMode.Fill)) == global::Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN.RenderMode.Fill))
{
global::OpenTK.Graphics.OpenGL.GL.BindTexture(global::OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, this.dicTextureIDs[this.lineTexture]);
}
else
{
setMaterialTextureOrColor();
if (!(isEmptyTexture))
{
global::OpenTK.Graphics.OpenGL.GL.TexParameter(global::OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, global::OpenTK.Graphics.OpenGL.TextureParameterName.TextureWrapT, l.Material.Wrap.X);
global::OpenTK.Graphics.OpenGL.GL.TexParameter(global::OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, global::OpenTK.Graphics.OpenGL.TextureParameterName.TextureWrapS, l.Material.Wrap.Y);
}
}
global::OpenTK.Graphics.OpenGL.GL.PolygonMode(global::OpenTK.Graphics.OpenGL.MaterialFace.FrontAndBack, global::OpenTK.Graphics.OpenGL.PolygonMode.Line);
global::OpenTK.Graphics.OpenGL.GL.DrawElements(global::OpenTK.Graphics.OpenGL.PrimitiveType.Triangles, l.Points.Count, global::OpenTK.Graphics.OpenGL.DrawElementsType.UnsignedInt, global::System.IntPtr.Zero);
}
}
global::OpenTK.Graphics.OpenGL.GL.PolygonMode(global::OpenTK.Graphics.OpenGL.MaterialFace.FrontAndBack, global::OpenTK.Graphics.OpenGL.PolygonMode.Fill); // Reset for RenderEngineOld
if (this.VertexColorBuffers.ContainsKey(mesh))
{
global::OpenTK.Graphics.OpenGL.GL.DisableClientState(global::OpenTK.Graphics.OpenGL.ArrayCap.ColorArray);
}
else if (this.NormalBuffers.ContainsKey(mesh))
{
global::OpenTK.Graphics.OpenGL.GL.DisableClientState(global::OpenTK.Graphics.OpenGL.ArrayCap.NormalArray);
}
}
global::OpenTK.Graphics.OpenGL.GL.DisableClientState(global::OpenTK.Graphics.OpenGL.ArrayCap.VertexArray);
global::OpenTK.Graphics.OpenGL.GL.DisableClientState(global::OpenTK.Graphics.OpenGL.ArrayCap.TextureCoordArray);
global::OpenTK.Graphics.OpenGL.GL.PopMatrix();
}
public void DrawFacePicking()
{
this.DrawFacePicking(global::OpenTK.Vector3.Zero, global::OpenTK.Quaternion.Identity, new global::OpenTK.Vector3(this.ModelScaling, this.ModelScaling, this.ModelScaling));
}
public void DrawFacePicking(global::OpenTK.Vector3 pos, global::OpenTK.Quaternion rot)
{
this.DrawFacePicking(pos, rot, new global::OpenTK.Vector3(this.ModelScaling, this.ModelScaling, this.ModelScaling));
}
public void DrawFacePicking(global::OpenTK.Vector3 pos, global::OpenTK.Quaternion rot, global::OpenTK.Vector3 scale)
{
if (!(this.HasRendered))
return;
global::OpenTK.Graphics.OpenGL.GL.PushMatrix();
global::OpenTK.Graphics.OpenGL.GL.Translate(pos.X, pos.Y, pos.Z);
global::OpenTK.Graphics.OpenGL.GL.Rotate(rot.X, 1f, 0f, 0f);
global::OpenTK.Graphics.OpenGL.GL.Rotate(rot.Y, 0f, 1f, 0f);
global::OpenTK.Graphics.OpenGL.GL.Rotate(rot.Z, 0f, 0f, 1f);
global::OpenTK.Graphics.OpenGL.GL.Scale(scale);
global::OpenTK.Graphics.OpenGL.GL.EnableClientState(global::OpenTK.Graphics.OpenGL.ArrayCap.VertexArray);
for (global::System.Int32 iMesh = 0, loopTo = (this.obj3d.Meshes.Count) - (1); iMesh <= loopTo; iMesh++)
{
var mesh = this.obj3d.Meshes[iMesh];
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, this.VertexBuffers[mesh]);
global::OpenTK.Graphics.OpenGL.GL.VertexPointer(3, global::OpenTK.Graphics.OpenGL.VertexPointerType.Float, 0, global::System.IntPtr.Zero);
for (global::System.Int32 iFace = 0, loopTo1 = (mesh.Faces.Count) - (1); iFace <= loopTo1; iFace++)
{
var l = mesh.Faces[iFace];
global::OpenTK.Graphics.OpenGL.GL.BindBuffer(global::OpenTK.Graphics.OpenGL.BufferTarget.ElementArrayBuffer, this.IndicesBuffers[mesh][iFace]);
global::System.Int32 colorCode = (((0x20000000) + ((((iMesh) << (16))))) + (iFace));
global::OpenTK.Graphics.OpenGL.GL.Color4(global::System.Drawing.Color.FromArgb(colorCode)); // Color: "2f ff xx xx" -> where 'f' = mesh index and where 'x' is face index
global::OpenTK.Graphics.OpenGL.GL.PolygonMode(global::OpenTK.Graphics.OpenGL.MaterialFace.FrontAndBack, global::OpenTK.Graphics.OpenGL.PolygonMode.Fill);
global::OpenTK.Graphics.OpenGL.GL.DrawElements(global::OpenTK.Graphics.OpenGL.PrimitiveType.Triangles, l.Points.Count, global::OpenTK.Graphics.OpenGL.DrawElementsType.UnsignedInt, global::System.IntPtr.Zero);
}
global::OpenTK.Graphics.OpenGL.GL.PolygonMode(global::OpenTK.Graphics.OpenGL.MaterialFace.FrontAndBack, global::OpenTK.Graphics.OpenGL.PolygonMode.Fill);
}
global::OpenTK.Graphics.OpenGL.GL.DisableClientState(global::OpenTK.Graphics.OpenGL.ArrayCap.VertexArray);
global::OpenTK.Graphics.OpenGL.GL.PopMatrix();
}
public void ReleaseBuffers()
{
if (!(this.HasRendered))
return;
foreach (global::System.Collections.Generic.KeyValuePair<global::Pilz.S3DFileParser.Mesh, global::System.Int32> kvp in this.VertexBuffers)
global::OpenTK.Graphics.OpenGL.GL.DeleteBuffer(kvp.Value);
this.VertexBuffers.Clear();
foreach (global::System.Collections.Generic.KeyValuePair<global::Pilz.S3DFileParser.Mesh, global::System.Int32> kvp in this.UVBuffers)
global::OpenTK.Graphics.OpenGL.GL.DeleteBuffer(kvp.Value);
this.UVBuffers.Clear();
foreach (global::System.Collections.Generic.KeyValuePair<global::Pilz.S3DFileParser.Mesh, global::System.Int32> kvp in this.VertexColorBuffers)
global::OpenTK.Graphics.OpenGL.GL.DeleteBuffer(kvp.Value);
this.VertexColorBuffers.Clear();
foreach (global::System.Collections.Generic.KeyValuePair<global::Pilz.S3DFileParser.Mesh, global::System.Int32> kvp in this.NormalBuffers)
global::OpenTK.Graphics.OpenGL.GL.DeleteBuffer(kvp.Value);
this.NormalBuffers.Clear();
foreach (global::System.Collections.Generic.KeyValuePair<global::Pilz.S3DFileParser.Mesh, global::System.Collections.Generic.List<global::System.Int32>> kvp in this.IndicesBuffers)
{
foreach (global::System.Int32 i in kvp.Value)
global::OpenTK.Graphics.OpenGL.GL.DeleteBuffer(i);
kvp.Value.Clear();
}
this.IndicesBuffers.Clear();
foreach (global::System.Collections.Generic.KeyValuePair<global::System.Drawing.Image, global::System.Int32> kvp in this.dicTextureIDs)
global::OpenTK.Graphics.OpenGL.GL.DeleteBuffer(kvp.Value);
this.dicTextureIDs.Clear();
foreach (global::System.Collections.Generic.KeyValuePair<global::System.Drawing.Color, global::System.Int32> kvp in this.dicColorIDs)
global::OpenTK.Graphics.OpenGL.GL.DeleteBuffer(kvp.Value);
this.dicColorIDs.Clear();
this.HasRendered = false;
}
~Renderer()
{
// ReleaseBuffers()
}
}
}