revert to VB and update to new project file format
This commit is contained in:
@@ -1,92 +0,0 @@
|
||||
using global::System.Drawing;
|
||||
using global::OpenTK.Graphics.OpenGL;
|
||||
|
||||
namespace Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN
|
||||
{
|
||||
public class BoundingBox
|
||||
{
|
||||
public static void DrawSolid(System.Numerics.Vector3 scale, System.Numerics.Quaternion rot, System.Numerics.Vector3 pos, Color color, System.Numerics.Vector3 upper, System.Numerics.Vector3 lower)
|
||||
{
|
||||
GL.Disable(EnableCap.Blend);
|
||||
GL.Disable(EnableCap.Texture2D);
|
||||
GL.Disable(EnableCap.AlphaTest);
|
||||
GL.PushMatrix();
|
||||
GL.Translate(pos.X, pos.Y, pos.Z);
|
||||
GL.Rotate(rot.X, 1f, 0f, 0f);
|
||||
GL.Rotate(rot.Y, 0f, 1f, 0f);
|
||||
GL.Rotate(rot.Z, 0f, 0f, 1f);
|
||||
GL.Scale(scale.X, scale.Y, scale.Z);
|
||||
GL.Begin(PrimitiveType.Quads);
|
||||
GL.Color4(color);
|
||||
GL.Vertex3(upper.X, upper.Y, lower.Z); // Top-right of top face
|
||||
GL.Vertex3(lower.X, upper.Y, lower.Z); // Top-left of top face
|
||||
GL.Vertex3(lower.X, upper.Y, upper.Z); // Bottom-left of top face
|
||||
GL.Vertex3(upper.X, upper.Y, upper.Z); // Bottom-right of top face
|
||||
GL.Vertex3(upper.X, lower.Y, lower.Z); // Top-right of bottom face
|
||||
GL.Vertex3(lower.X, lower.Y, lower.Z); // Top-left of bottom face
|
||||
GL.Vertex3(lower.X, lower.Y, upper.Z); // Bottom-left of bottom face
|
||||
GL.Vertex3(upper.X, lower.Y, upper.Z); // Bottom-right of bottom face
|
||||
GL.Vertex3(upper.X, upper.Y, upper.Z); // Top-Right of front face
|
||||
GL.Vertex3(lower.X, upper.Y, upper.Z); // Top-left of front face
|
||||
GL.Vertex3(lower.X, lower.Y, upper.Z); // Bottom-left of front face
|
||||
GL.Vertex3(upper.X, lower.Y, upper.Z); // Bottom-right of front face
|
||||
GL.Vertex3(upper.X, lower.Y, lower.Z); // Bottom-Left of back face
|
||||
GL.Vertex3(lower.X, lower.Y, lower.Z); // Bottom-Right of back face
|
||||
GL.Vertex3(lower.X, upper.Y, lower.Z); // Top-Right of back face
|
||||
GL.Vertex3(upper.X, upper.Y, lower.Z); // Top-Left of back face
|
||||
GL.Vertex3(lower.X, upper.Y, upper.Z); // Top-Right of left face
|
||||
GL.Vertex3(lower.X, upper.Y, lower.Z); // Top-Left of left face
|
||||
GL.Vertex3(lower.X, lower.Y, lower.Z); // Bottom-Left of left face
|
||||
GL.Vertex3(lower.X, lower.Y, upper.Z); // Bottom-Right of left face
|
||||
GL.Vertex3(upper.X, upper.Y, upper.Z); // Top-Right of left face
|
||||
GL.Vertex3(upper.X, upper.Y, lower.Z); // Top-Left of left face
|
||||
GL.Vertex3(upper.X, lower.Y, lower.Z); // Bottom-Left of left face
|
||||
GL.Vertex3(upper.X, lower.Y, upper.Z); // Bottom-Right of left face
|
||||
GL.Color4(Color.White);
|
||||
GL.End();
|
||||
GL.PopMatrix();
|
||||
GL.Enable(EnableCap.Blend);
|
||||
GL.Enable(EnableCap.Texture2D);
|
||||
GL.Enable(EnableCap.AlphaTest);
|
||||
}
|
||||
|
||||
public static void Draw(System.Numerics.Vector3 scale, System.Numerics.Quaternion rot, System.Numerics.Vector3 pos, Color color, System.Numerics.Vector3 upper, System.Numerics.Vector3 lower)
|
||||
{
|
||||
GL.Disable(EnableCap.Blend);
|
||||
GL.Disable(EnableCap.Texture2D);
|
||||
GL.Disable(EnableCap.AlphaTest);
|
||||
GL.PushMatrix();
|
||||
GL.Translate(pos.X, pos.Y, pos.Z);
|
||||
GL.Rotate(rot.X, 1f, 0f, 0f);
|
||||
GL.Rotate(rot.Y, 0f, 1f, 0f);
|
||||
GL.Rotate(rot.Z, 0f, 0f, 1f);
|
||||
GL.Scale(scale.X, scale.Y, scale.Z);
|
||||
GL.Begin(PrimitiveType.LineLoop);
|
||||
GL.Color4(color);
|
||||
GL.Vertex3(upper.X, upper.Y, lower.Z); // 1
|
||||
GL.Vertex3(lower.X, upper.Y, lower.Z); // 2
|
||||
GL.Vertex3(lower.X, upper.Y, upper.Z); // 3
|
||||
GL.Vertex3(upper.X, upper.Y, lower.Z); // 1
|
||||
GL.Vertex3(upper.X, upper.Y, upper.Z); // 4
|
||||
GL.Vertex3(lower.X, upper.Y, upper.Z); // 3
|
||||
GL.Vertex3(lower.X, lower.Y, upper.Z); // 7
|
||||
GL.Vertex3(lower.X, lower.Y, lower.Z); // 6
|
||||
GL.Vertex3(upper.X, lower.Y, lower.Z); // 5
|
||||
GL.Vertex3(lower.X, lower.Y, upper.Z); // 7
|
||||
GL.Vertex3(upper.X, lower.Y, upper.Z); // 8
|
||||
GL.Vertex3(upper.X, lower.Y, lower.Z); // 5
|
||||
GL.Vertex3(lower.X, upper.Y, lower.Z); // 2
|
||||
GL.Vertex3(lower.X, lower.Y, lower.Z); // 6
|
||||
GL.Vertex3(lower.X, upper.Y, upper.Z); // 3
|
||||
GL.Vertex3(upper.X, lower.Y, upper.Z); // 8
|
||||
GL.Vertex3(upper.X, upper.Y, upper.Z); // 4
|
||||
GL.Vertex3(upper.X, lower.Y, lower.Z); // 5
|
||||
GL.Color4(Color.White);
|
||||
GL.End();
|
||||
GL.PopMatrix();
|
||||
GL.Enable(EnableCap.Blend);
|
||||
GL.Enable(EnableCap.Texture2D);
|
||||
GL.Enable(EnableCap.AlphaTest);
|
||||
}
|
||||
}
|
||||
}
|
||||
105
Pilz.Drawing.Drawing3D.OpenGLRenderer/Rendering/BoundingBox.vb
Normal file
105
Pilz.Drawing.Drawing3D.OpenGLRenderer/Rendering/BoundingBox.vb
Normal file
@@ -0,0 +1,105 @@
|
||||
Imports System.Drawing
|
||||
Imports OpenTK
|
||||
Imports OpenTK.Graphics.OpenGL
|
||||
|
||||
Namespace RenderingN
|
||||
|
||||
Public Class BoundingBox
|
||||
|
||||
Public Shared Sub DrawSolid(scale As System.Numerics.Vector3, rot As System.Numerics.Quaternion, pos As System.Numerics.Vector3, color As Color, upper As System.Numerics.Vector3, lower As System.Numerics.Vector3)
|
||||
GL.Disable(EnableCap.Blend)
|
||||
GL.Disable(EnableCap.Texture2D)
|
||||
GL.Disable(EnableCap.AlphaTest)
|
||||
GL.PushMatrix()
|
||||
GL.Translate(pos.X, pos.Y, pos.Z)
|
||||
GL.Rotate(rot.X, 1, 0, 0)
|
||||
GL.Rotate(rot.Y, 0, 1, 0)
|
||||
GL.Rotate(rot.Z, 0, 0, 1)
|
||||
GL.Scale(scale.X, scale.Y, scale.Z)
|
||||
|
||||
GL.Begin(PrimitiveType.Quads)
|
||||
GL.Color4(color)
|
||||
|
||||
GL.Vertex3(upper.X, upper.Y, lower.Z) ' Top-right of top face
|
||||
GL.Vertex3(lower.X, upper.Y, lower.Z) ' Top-left of top face
|
||||
GL.Vertex3(lower.X, upper.Y, upper.Z) ' Bottom-left of top face
|
||||
GL.Vertex3(upper.X, upper.Y, upper.Z) ' Bottom-right of top face
|
||||
|
||||
GL.Vertex3(upper.X, lower.Y, lower.Z) ' Top-right of bottom face
|
||||
GL.Vertex3(lower.X, lower.Y, lower.Z) ' Top-left of bottom face
|
||||
GL.Vertex3(lower.X, lower.Y, upper.Z) ' Bottom-left of bottom face
|
||||
GL.Vertex3(upper.X, lower.Y, upper.Z) ' Bottom-right of bottom face
|
||||
|
||||
GL.Vertex3(upper.X, upper.Y, upper.Z) ' Top-Right of front face
|
||||
GL.Vertex3(lower.X, upper.Y, upper.Z) ' Top-left of front face
|
||||
GL.Vertex3(lower.X, lower.Y, upper.Z) ' Bottom-left of front face
|
||||
GL.Vertex3(upper.X, lower.Y, upper.Z) ' Bottom-right of front face
|
||||
|
||||
GL.Vertex3(upper.X, lower.Y, lower.Z) ' Bottom-Left of back face
|
||||
GL.Vertex3(lower.X, lower.Y, lower.Z) ' Bottom-Right of back face
|
||||
GL.Vertex3(lower.X, upper.Y, lower.Z) ' Top-Right of back face
|
||||
GL.Vertex3(upper.X, upper.Y, lower.Z) ' Top-Left of back face
|
||||
|
||||
GL.Vertex3(lower.X, upper.Y, upper.Z) ' Top-Right of left face
|
||||
GL.Vertex3(lower.X, upper.Y, lower.Z) ' Top-Left of left face
|
||||
GL.Vertex3(lower.X, lower.Y, lower.Z) ' Bottom-Left of left face
|
||||
GL.Vertex3(lower.X, lower.Y, upper.Z) ' Bottom-Right of left face
|
||||
|
||||
GL.Vertex3(upper.X, upper.Y, upper.Z) ' Top-Right of left face
|
||||
GL.Vertex3(upper.X, upper.Y, lower.Z) ' Top-Left of left face
|
||||
GL.Vertex3(upper.X, lower.Y, lower.Z) ' Bottom-Left of left face
|
||||
GL.Vertex3(upper.X, lower.Y, upper.Z) ' Bottom-Right of left face
|
||||
|
||||
GL.Color4(Color.White)
|
||||
GL.End()
|
||||
GL.PopMatrix()
|
||||
GL.Enable(EnableCap.Blend)
|
||||
GL.Enable(EnableCap.Texture2D)
|
||||
GL.Enable(EnableCap.AlphaTest)
|
||||
End Sub
|
||||
|
||||
Public Shared Sub Draw(scale As Numerics.Vector3, rot As Numerics.Quaternion, pos As Numerics.Vector3, color As Color, upper As Numerics.Vector3, lower As Numerics.Vector3)
|
||||
GL.Disable(EnableCap.Blend)
|
||||
GL.Disable(EnableCap.Texture2D)
|
||||
GL.Disable(EnableCap.AlphaTest)
|
||||
GL.PushMatrix()
|
||||
GL.Translate(pos.X, pos.Y, pos.Z)
|
||||
GL.Rotate(rot.X, 1, 0, 0)
|
||||
GL.Rotate(rot.Y, 0, 1, 0)
|
||||
GL.Rotate(rot.Z, 0, 0, 1)
|
||||
GL.Scale(scale.X, scale.Y, scale.Z)
|
||||
|
||||
GL.Begin(PrimitiveType.LineLoop)
|
||||
GL.Color4(color)
|
||||
|
||||
GL.Vertex3(upper.X, upper.Y, lower.Z) ' 1
|
||||
GL.Vertex3(lower.X, upper.Y, lower.Z) ' 2
|
||||
GL.Vertex3(lower.X, upper.Y, upper.Z) ' 3
|
||||
GL.Vertex3(upper.X, upper.Y, lower.Z) ' 1
|
||||
GL.Vertex3(upper.X, upper.Y, upper.Z) ' 4
|
||||
GL.Vertex3(lower.X, upper.Y, upper.Z) ' 3
|
||||
|
||||
GL.Vertex3(lower.X, lower.Y, upper.Z) ' 7
|
||||
GL.Vertex3(lower.X, lower.Y, lower.Z) ' 6
|
||||
GL.Vertex3(upper.X, lower.Y, lower.Z) ' 5
|
||||
GL.Vertex3(lower.X, lower.Y, upper.Z) ' 7
|
||||
GL.Vertex3(upper.X, lower.Y, upper.Z) ' 8
|
||||
GL.Vertex3(upper.X, lower.Y, lower.Z) ' 5
|
||||
|
||||
GL.Vertex3(lower.X, upper.Y, lower.Z) ' 2
|
||||
GL.Vertex3(lower.X, lower.Y, lower.Z) ' 6
|
||||
GL.Vertex3(lower.X, upper.Y, upper.Z) ' 3
|
||||
GL.Vertex3(upper.X, lower.Y, upper.Z) ' 8
|
||||
GL.Vertex3(upper.X, upper.Y, upper.Z) ' 4
|
||||
GL.Vertex3(upper.X, lower.Y, lower.Z) ' 5
|
||||
|
||||
GL.Color4(Color.White)
|
||||
GL.End()
|
||||
GL.PopMatrix()
|
||||
GL.Enable(EnableCap.Blend)
|
||||
GL.Enable(EnableCap.Texture2D)
|
||||
GL.Enable(EnableCap.AlphaTest)
|
||||
End Sub
|
||||
End Class
|
||||
|
||||
End Namespace
|
||||
@@ -1,34 +0,0 @@
|
||||
using global::System;
|
||||
using global::System.Drawing;
|
||||
using Bitmap = System.Drawing.Bitmap;
|
||||
using global::System.Drawing.Imaging;
|
||||
using global::OpenTK.Graphics.OpenGL;
|
||||
|
||||
namespace Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN
|
||||
{
|
||||
public class ContentPipe
|
||||
{
|
||||
public static int LoadTexture(string filepath)
|
||||
{
|
||||
var bitmap = new Bitmap(filepath);
|
||||
return LoadTexture(bitmap);
|
||||
}
|
||||
|
||||
public static int LoadTexture(Bitmap bitmap)
|
||||
{
|
||||
int id = GL.GenTexture();
|
||||
LoadTexture(bitmap, id);
|
||||
return id;
|
||||
}
|
||||
|
||||
public static void LoadTexture(Bitmap bitmap, int id)
|
||||
{
|
||||
var bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
|
||||
GL.BindTexture(TextureTarget.Texture2D, id);
|
||||
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bitmap.Width, bitmap.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmpData.Scan0);
|
||||
bitmap.UnlockBits(bmpData);
|
||||
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)Math.Truncate((decimal)TextureMinFilter.Linear));
|
||||
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)Math.Truncate((decimal)TextureMagFilter.Linear));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
Imports System
|
||||
Imports OpenTK
|
||||
Imports OpenTK.Graphics.OpenGL
|
||||
Imports System.Drawing
|
||||
Imports System.Drawing.Imaging
|
||||
Imports Bitmap = System.Drawing.Bitmap
|
||||
|
||||
Namespace RenderingN
|
||||
|
||||
Public Class ContentPipe
|
||||
|
||||
Public Shared Function LoadTexture(filepath As String) As Integer
|
||||
Dim bitmap As New Bitmap(filepath)
|
||||
Return LoadTexture(bitmap)
|
||||
End Function
|
||||
|
||||
Public Shared Function LoadTexture(bitmap As Bitmap) As Integer
|
||||
Dim id As Integer = GL.GenTexture()
|
||||
LoadTexture(bitmap, id)
|
||||
Return id
|
||||
End Function
|
||||
|
||||
Public Shared Sub LoadTexture(bitmap As Bitmap, id As Integer)
|
||||
Dim bmpData As BitmapData = bitmap.LockBits(New Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, Imaging.PixelFormat.Format32bppArgb)
|
||||
GL.BindTexture(TextureTarget.Texture2D, id)
|
||||
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bitmap.Width, bitmap.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bmpData.Scan0)
|
||||
bitmap.UnlockBits(bmpData)
|
||||
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, CInt(Math.Truncate(TextureMinFilter.Linear)))
|
||||
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, CInt(Math.Truncate(TextureMagFilter.Linear)))
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
|
||||
End Namespace
|
||||
@@ -1,11 +0,0 @@
|
||||
|
||||
namespace Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN
|
||||
{
|
||||
public enum RenderMode : byte
|
||||
{
|
||||
None = 0x0,
|
||||
Fill = 0x1,
|
||||
Outline = 0x2,
|
||||
FillOutline = Fill | Outline
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
Namespace RenderingN
|
||||
|
||||
Public Enum RenderMode As Byte
|
||||
None = &H0
|
||||
Fill = &H1
|
||||
Outline = &H2
|
||||
FillOutline = Fill Or Outline
|
||||
End Enum
|
||||
|
||||
End Namespace
|
||||
@@ -1,492 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using global::System.Drawing;
|
||||
using Bitmap = System.Drawing.Bitmap;
|
||||
using Color = System.Drawing.Color;
|
||||
using Image = System.Drawing.Image;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security;
|
||||
using System.Text;
|
||||
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 OpenTK;
|
||||
using OpenTK.Graphics.OpenGL;
|
||||
using Pilz.S3DFileParser;
|
||||
using Pilz.Drawing.Drawing3D.OpenGLFactory.RenderingN;
|
||||
|
||||
namespace RenderingN
|
||||
{
|
||||
public partial class Renderer
|
||||
{
|
||||
private Object3D obj3d;
|
||||
private Dictionary<Image, int> dicTextureIDs = new Dictionary<Image, int>();
|
||||
private Dictionary<Color, int> dicColorIDs = new Dictionary<Color, int>();
|
||||
private Bitmap emptyTexture = null;
|
||||
private Bitmap lineTexture = null;
|
||||
private Bitmap selectedLineTexture = null;
|
||||
|
||||
public float ModelScaling { get; set; } = 1.0f;
|
||||
public bool HasRendered { get; private set; } = false;
|
||||
public List<global::System.Object> SelectedElements { get; private set; }
|
||||
private Dictionary<Mesh, int> VertexBuffers { get; set; } = new Dictionary<Mesh, int>();
|
||||
private Dictionary<Mesh, List<int>> IndicesBuffers { get; set; } = new Dictionary<Mesh, List<int>>();
|
||||
private Dictionary<Mesh, int> UVBuffers { get; set; } = new Dictionary<Mesh, int>();
|
||||
private Dictionary<Mesh, int> VertexColorBuffers { get; set; } = new Dictionary<Mesh, int>();
|
||||
private Dictionary<Mesh, int> NormalBuffers { get; set; } = new Dictionary<Mesh, int>();
|
||||
|
||||
public Object3D Model
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.obj3d;
|
||||
}
|
||||
}
|
||||
|
||||
public Renderer(Object3D obj3d)
|
||||
{
|
||||
this.obj3d = obj3d.ToOneMesh();
|
||||
|
||||
// Set Texture used for faces without texture
|
||||
this.emptyTexture = (Bitmap)this.ColorToTexture(Color.LightGray);
|
||||
|
||||
// Set Texture used for lines
|
||||
this.lineTexture = (Bitmap)this.ColorToTexture(Color.Black);
|
||||
|
||||
// Set Texture used for lines of selected faces
|
||||
this.selectedLineTexture = (Bitmap)this.ColorToTexture(Color.Orange);
|
||||
}
|
||||
|
||||
private Image ColorToTexture(Color color)
|
||||
{
|
||||
var tex = new 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(Mesh m, Vertex v)
|
||||
{
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, this.VertexBuffers[m]);
|
||||
var vector = new Vector3((float)v.X, (float)v.Y, (float)v.Z);
|
||||
GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)((m.Vertices.IndexOf(v)) * (Vector3.SizeInBytes)), Vector3.SizeInBytes, ref vector);
|
||||
}
|
||||
|
||||
/// <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(Mesh m, Normal n)
|
||||
{
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, this.NormalBuffers[m]);
|
||||
var vector = new Vector3((float)n.X, (float)n.Y, (float)n.Z);
|
||||
GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)((m.Normals.IndexOf(n)) * (Vector3.SizeInBytes)), Vector3.SizeInBytes, ref vector);
|
||||
}
|
||||
|
||||
/// <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(Mesh m, VertexColor vc)
|
||||
{
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, this.VertexColorBuffers[m]);
|
||||
var vector = new Vector4(vc.R, vc.G, vc.B, vc.A);
|
||||
GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)((m.VertexColors.IndexOf(vc)) * (Vector4.SizeInBytes)), Vector4.SizeInBytes, ref vector);
|
||||
}
|
||||
|
||||
/// <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(Mesh m, UV uv)
|
||||
{
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, this.UVBuffers[m]);
|
||||
var vector = new Vector2(uv.U, uv.V);
|
||||
GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)((m.UVs.IndexOf(uv)) * (Vector2.SizeInBytes)), Vector2.SizeInBytes, ref vector);
|
||||
}
|
||||
|
||||
/// <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(Mesh m, Face f)
|
||||
{
|
||||
int faceIndex = m.Faces.IndexOf(f);
|
||||
byte uintlen = (byte)Strings.Len(new uint());
|
||||
var indicies = new Vector3(m.Vertices.IndexOf(f.Points[0].Vertex), m.Vertices.IndexOf(f.Points[1].Vertex), m.Vertices.IndexOf(f.Points[2].Vertex));
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, this.IndicesBuffers[m][faceIndex]);
|
||||
GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)(uintlen * faceIndex), uintlen, ref indicies);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replace an Image with a new one.
|
||||
/// </summary>
|
||||
/// <param name="oldImage"></param>
|
||||
/// <param name="newImage"></param>
|
||||
public void UpdateTexture(Image oldImage, Image newImage)
|
||||
{
|
||||
if (this.dicTextureIDs.ContainsKey(oldImage))
|
||||
{
|
||||
int id = this.dicTextureIDs[oldImage];
|
||||
this.dicTextureIDs.Remove(oldImage);
|
||||
this.dicTextureIDs.Add(newImage, id);
|
||||
ContentPipe.LoadTexture((Bitmap)newImage, id);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates an Image.
|
||||
/// </summary>
|
||||
/// <param name="image"></param>
|
||||
public void UpdateTexture(Image image)
|
||||
{
|
||||
if (this.dicTextureIDs.ContainsKey(image))
|
||||
{
|
||||
ContentPipe.LoadTexture(this.dicTextureIDs[image].ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the Buffers and store the requied Data.
|
||||
/// </summary>
|
||||
public void RenderModel()
|
||||
{
|
||||
this.ReleaseBuffers();
|
||||
foreach (Mesh mesh in this.obj3d.Meshes)
|
||||
{
|
||||
var nibo = new List<int>();
|
||||
global::System.Boolean enablecols = ((mesh.VertexColors.Count) > (0));
|
||||
global::System.Boolean enablenorms = (((!(enablecols))) && ((mesh.Normals.Count) > (0)));
|
||||
var verts = new List<Vector3>();
|
||||
var uvs = new List<Vector2>();
|
||||
var cols = new List<Vector4>();
|
||||
var norms = new List<Vector3>();
|
||||
global::System.UInt64 curvi = 0UL;
|
||||
this.IndicesBuffers.Add(mesh, nibo);
|
||||
for (int i = 0, loopTo = (mesh.Faces.Count) - (1); i <= loopTo; i++)
|
||||
{
|
||||
{
|
||||
var withBlock = mesh.Faces[i];
|
||||
var indices = new List<global::System.UInt32>();
|
||||
foreach (Pilz.S3DFileParser.Point p in withBlock.Points)
|
||||
{
|
||||
indices.Add((global::System.UInt32)curvi);
|
||||
curvi = (global::System.UInt64)(curvi + 1m);
|
||||
if (p.Vertex is object)
|
||||
{
|
||||
verts.Add(new Vector3((float)p.Vertex.X, (float)p.Vertex.Y, (float)p.Vertex.Z));
|
||||
}
|
||||
else
|
||||
{
|
||||
verts.Add(new Vector3(0, 0, 0));
|
||||
}
|
||||
|
||||
if (p.UV is object)
|
||||
{
|
||||
uvs.Add(new Vector2(p.UV.U, p.UV.V));
|
||||
}
|
||||
else
|
||||
{
|
||||
uvs.Add(new Vector2(0, 0));
|
||||
}
|
||||
|
||||
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(1, 1, 1, 1));
|
||||
}
|
||||
|
||||
if (((enablenorms) && p.Normal is object))
|
||||
{
|
||||
norms.Add(new Vector3(p.Normal.X, p.Normal.Y, p.Normal.Z));
|
||||
}
|
||||
else
|
||||
{
|
||||
norms.Add(new Vector3(1, 1, 1));
|
||||
}
|
||||
}
|
||||
|
||||
nibo.Add(GL.GenBuffer());
|
||||
GL.BindBuffer(BufferTarget.ElementArrayBuffer, nibo[i]);
|
||||
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)((Strings.Len(new uint())) * (indices.Count)), indices.ToArray(), BufferUsageHint.StaticDraw);
|
||||
if (withBlock.Material?.Image is object)
|
||||
{
|
||||
if (!(this.dicTextureIDs.ContainsKey(withBlock.Material.Image)))
|
||||
{
|
||||
this.dicTextureIDs.Add(withBlock.Material.Image, ContentPipe.LoadTexture((Bitmap)withBlock.Material.Image));
|
||||
}
|
||||
}
|
||||
else if (withBlock.Material?.Color is object)
|
||||
{
|
||||
if (!(this.dicColorIDs.ContainsKey((Color)withBlock.Material.Color)))
|
||||
{
|
||||
this.dicColorIDs.Add((Color)withBlock.Material.Color, ContentPipe.LoadTexture((Bitmap)ColorToTexture((Color)withBlock.Material.Color)));
|
||||
}
|
||||
}
|
||||
else if (!(this.dicTextureIDs.ContainsKey(this.emptyTexture)))
|
||||
{
|
||||
this.dicTextureIDs.Add(this.emptyTexture, ContentPipe.LoadTexture(this.emptyTexture));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int nvbo = GL.GenBuffer();
|
||||
this.VertexBuffers.Add(mesh, nvbo);
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, nvbo);
|
||||
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)((Vector3.SizeInBytes) * (verts.Count)), verts.ToArray(), BufferUsageHint.StaticDraw);
|
||||
int ntbo = GL.GenBuffer();
|
||||
this.UVBuffers.Add(mesh, ntbo);
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, ntbo);
|
||||
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)((Vector2.SizeInBytes) * (uvs.Count)), uvs.ToArray(), BufferUsageHint.StaticDraw);
|
||||
if (enablecols)
|
||||
{
|
||||
int ncbo = GL.GenBuffer();
|
||||
this.VertexColorBuffers.Add(mesh, ncbo);
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, ncbo);
|
||||
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)((Vector4.SizeInBytes) * (cols.Count)), cols.ToArray(), BufferUsageHint.StaticDraw);
|
||||
}
|
||||
|
||||
if (enablenorms)
|
||||
{
|
||||
int nnbo = GL.GenBuffer();
|
||||
this.NormalBuffers.Add(mesh, nnbo);
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, nnbo);
|
||||
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)((Vector3.SizeInBytes) * (norms.Count)), norms.ToArray(), BufferUsageHint.StaticDraw);
|
||||
}
|
||||
}
|
||||
|
||||
if (!(this.dicTextureIDs.ContainsKey(this.lineTexture)))
|
||||
{
|
||||
this.dicTextureIDs.Add(this.lineTexture, ContentPipe.LoadTexture(this.lineTexture));
|
||||
}
|
||||
|
||||
this.HasRendered = true;
|
||||
}
|
||||
|
||||
public void DrawModel(RenderMode mode)
|
||||
{
|
||||
DrawModel(mode, Vector3.Zero, Quaternion.Identity, new Vector3(this.ModelScaling, this.ModelScaling, this.ModelScaling));
|
||||
}
|
||||
|
||||
public void DrawModel(RenderMode mode, Vector3 pos, Quaternion rot)
|
||||
{
|
||||
DrawModel(mode, pos, rot, new Vector3(this.ModelScaling, this.ModelScaling, this.ModelScaling));
|
||||
}
|
||||
|
||||
public void DrawModel(RenderMode mode, Vector3 pos, Quaternion rot, Vector3 scale)
|
||||
{
|
||||
if (((mode) == (RenderMode.None)))
|
||||
return;
|
||||
if (!(this.HasRendered))
|
||||
return;
|
||||
GL.PushMatrix();
|
||||
GL.Translate(pos.X, pos.Y, pos.Z);
|
||||
GL.Rotate(rot.X, 1, 0, 0);
|
||||
GL.Rotate(rot.Y, 0, 1, 0);
|
||||
GL.Rotate(rot.Z, 0, 0, 1);
|
||||
GL.Scale(scale); // GL.Scale(scale.X, scale.Y, scale.Z)
|
||||
GL.EnableClientState(ArrayCap.VertexArray);
|
||||
GL.EnableClientState(ArrayCap.TextureCoordArray);
|
||||
foreach (Mesh mesh in this.obj3d.Meshes)
|
||||
{
|
||||
if (this.VertexColorBuffers.ContainsKey(mesh))
|
||||
{
|
||||
GL.EnableClientState(ArrayCap.ColorArray);
|
||||
}
|
||||
else if (this.NormalBuffers.ContainsKey(mesh))
|
||||
{
|
||||
GL.EnableClientState(ArrayCap.NormalArray);
|
||||
}
|
||||
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, this.VertexBuffers[mesh]);
|
||||
GL.VertexPointer(3, VertexPointerType.Float, 0, IntPtr.Zero);
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, this.UVBuffers[mesh]);
|
||||
GL.TexCoordPointer(2, TexCoordPointerType.Float, 0, IntPtr.Zero);
|
||||
if (this.VertexColorBuffers.ContainsKey(mesh))
|
||||
{
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, this.VertexColorBuffers[mesh]);
|
||||
GL.ColorPointer(4, ColorPointerType.Float, 0, IntPtr.Zero);
|
||||
}
|
||||
else if (this.NormalBuffers.ContainsKey(mesh))
|
||||
{
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, this.NormalBuffers[mesh]);
|
||||
GL.NormalPointer(NormalPointerType.Float, 0, IntPtr.Zero);
|
||||
}
|
||||
|
||||
for (int i = 0, loopTo = (mesh.Faces.Count) - (1); i <= loopTo; i++)
|
||||
{
|
||||
Face l = mesh.Faces[i];
|
||||
GL.BindBuffer(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()
|
||||
{
|
||||
int texID;
|
||||
if (!(isEmptyTexture))
|
||||
{
|
||||
texID = this.dicTextureIDs[l.Material.Image];
|
||||
}
|
||||
else if (!(isEmptyColor))
|
||||
{
|
||||
texID = this.dicColorIDs[(Color)l.Material.Color];
|
||||
}
|
||||
else
|
||||
{
|
||||
texID = this.dicTextureIDs[this.emptyTexture];
|
||||
}
|
||||
|
||||
GL.BindTexture(TextureTarget.Texture2D, texID);
|
||||
};
|
||||
if ((((((mode) & (RenderMode.Fill)))) == (RenderMode.Fill)))
|
||||
{
|
||||
setMaterialTextureOrColor();
|
||||
if (!(isEmptyTexture))
|
||||
{
|
||||
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, l.Material.Wrap.X);
|
||||
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, l.Material.Wrap.Y);
|
||||
}
|
||||
|
||||
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
|
||||
GL.DrawElements(PrimitiveType.Triangles, l.Points.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
|
||||
}
|
||||
|
||||
if ((((((mode) & (RenderMode.Outline)))) == (RenderMode.Outline)))
|
||||
{
|
||||
if ((((((mode) & (RenderMode.Fill)))) == (RenderMode.Fill)))
|
||||
{
|
||||
GL.BindTexture(TextureTarget.Texture2D, this.dicTextureIDs[this.lineTexture]);
|
||||
}
|
||||
else
|
||||
{
|
||||
setMaterialTextureOrColor();
|
||||
if (!(isEmptyTexture))
|
||||
{
|
||||
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, l.Material.Wrap.X);
|
||||
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, l.Material.Wrap.Y);
|
||||
}
|
||||
}
|
||||
|
||||
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
|
||||
GL.DrawElements(PrimitiveType.Triangles, l.Points.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
|
||||
}
|
||||
}
|
||||
|
||||
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); // Reset for RenderEngineOld
|
||||
if (this.VertexColorBuffers.ContainsKey(mesh))
|
||||
{
|
||||
GL.DisableClientState(ArrayCap.ColorArray);
|
||||
}
|
||||
else if (this.NormalBuffers.ContainsKey(mesh))
|
||||
{
|
||||
GL.DisableClientState(ArrayCap.NormalArray);
|
||||
}
|
||||
}
|
||||
|
||||
GL.DisableClientState(ArrayCap.VertexArray);
|
||||
GL.DisableClientState(ArrayCap.TextureCoordArray);
|
||||
GL.PopMatrix();
|
||||
}
|
||||
|
||||
public void DrawFacePicking()
|
||||
{
|
||||
DrawFacePicking(Vector3.Zero, Quaternion.Identity, new Vector3(this.ModelScaling, this.ModelScaling, this.ModelScaling));
|
||||
}
|
||||
|
||||
public void DrawFacePicking(Vector3 pos, Quaternion rot)
|
||||
{
|
||||
DrawFacePicking(pos, rot, new Vector3(this.ModelScaling, this.ModelScaling, this.ModelScaling));
|
||||
}
|
||||
|
||||
public void DrawFacePicking(Vector3 pos, Quaternion rot, Vector3 scale)
|
||||
{
|
||||
if (!(this.HasRendered))
|
||||
return;
|
||||
GL.PushMatrix();
|
||||
GL.Translate(pos.X, pos.Y, pos.Z);
|
||||
GL.Rotate(rot.X, 1, 0, 0);
|
||||
GL.Rotate(rot.Y, 0, 1, 0);
|
||||
GL.Rotate(rot.Z, 0, 0, 1);
|
||||
GL.Scale(scale);
|
||||
GL.EnableClientState(ArrayCap.VertexArray);
|
||||
for (int iMesh = 0, loopTo = (this.obj3d.Meshes.Count) - (1); iMesh <= loopTo; iMesh++)
|
||||
{
|
||||
Mesh mesh = this.obj3d.Meshes[iMesh];
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, this.VertexBuffers[mesh]);
|
||||
GL.VertexPointer(3, VertexPointerType.Float, 0, IntPtr.Zero);
|
||||
for (int iFace = 0, loopTo1 = (mesh.Faces.Count) - (1); iFace <= loopTo1; iFace++)
|
||||
{
|
||||
Face l = mesh.Faces[iFace];
|
||||
GL.BindBuffer(BufferTarget.ElementArrayBuffer, this.IndicesBuffers[mesh][iFace]);
|
||||
int colorCode = (((0x20000000) + ((((iMesh) << (16))))) + (iFace));
|
||||
GL.Color4(Color.FromArgb(colorCode)); // Color: "2f ff xx xx" -> where 'f' = mesh index and where 'x' is face index
|
||||
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
|
||||
GL.DrawElements(PrimitiveType.Triangles, l.Points.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
|
||||
}
|
||||
|
||||
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
|
||||
}
|
||||
|
||||
GL.DisableClientState(ArrayCap.VertexArray);
|
||||
GL.PopMatrix();
|
||||
}
|
||||
|
||||
public void ReleaseBuffers()
|
||||
{
|
||||
if (!(this.HasRendered))
|
||||
return;
|
||||
foreach (KeyValuePair<Mesh, int> kvp in this.VertexBuffers)
|
||||
GL.DeleteBuffer(kvp.Value);
|
||||
this.VertexBuffers.Clear();
|
||||
foreach (KeyValuePair<Mesh, int> kvp in this.UVBuffers)
|
||||
GL.DeleteBuffer(kvp.Value);
|
||||
this.UVBuffers.Clear();
|
||||
foreach (KeyValuePair<Mesh, int> kvp in this.VertexColorBuffers)
|
||||
GL.DeleteBuffer(kvp.Value);
|
||||
this.VertexColorBuffers.Clear();
|
||||
foreach (KeyValuePair<Mesh, int> kvp in this.NormalBuffers)
|
||||
GL.DeleteBuffer(kvp.Value);
|
||||
this.NormalBuffers.Clear();
|
||||
foreach (KeyValuePair<Mesh, List<int>> kvp in this.IndicesBuffers)
|
||||
{
|
||||
foreach (int i in kvp.Value)
|
||||
GL.DeleteBuffer(i);
|
||||
kvp.Value.Clear();
|
||||
}
|
||||
|
||||
this.IndicesBuffers.Clear();
|
||||
foreach (KeyValuePair<Image, int> kvp in this.dicTextureIDs)
|
||||
GL.DeleteBuffer(kvp.Value);
|
||||
this.dicTextureIDs.Clear();
|
||||
foreach (KeyValuePair<Color, int> kvp in this.dicColorIDs)
|
||||
GL.DeleteBuffer(kvp.Value);
|
||||
this.dicColorIDs.Clear();
|
||||
this.HasRendered = false;
|
||||
}
|
||||
|
||||
~Renderer()
|
||||
{
|
||||
// ReleaseBuffers()
|
||||
}
|
||||
}
|
||||
}
|
||||
471
Pilz.Drawing.Drawing3D.OpenGLRenderer/Rendering/Renderer.vb
Normal file
471
Pilz.Drawing.Drawing3D.OpenGLRenderer/Rendering/Renderer.vb
Normal file
@@ -0,0 +1,471 @@
|
||||
Imports System.Drawing
|
||||
Imports System.Threading
|
||||
Imports System.Windows.Forms
|
||||
Imports OpenTK
|
||||
Imports OpenTK.Graphics.OpenGL
|
||||
Imports Pilz.S3DFileParser
|
||||
Imports Bitmap = System.Drawing.Bitmap
|
||||
Imports Color = System.Drawing.Color
|
||||
Imports Image = System.Drawing.Image
|
||||
|
||||
Namespace RenderingN
|
||||
|
||||
Public Class Renderer
|
||||
|
||||
Private obj3d As Object3D
|
||||
Private dicTextureIDs As New Dictionary(Of Image, Integer)
|
||||
Private dicColorIDs As New Dictionary(Of Color, Integer)
|
||||
Private emptyTexture As Bitmap = Nothing
|
||||
Private lineTexture As Bitmap = Nothing
|
||||
Private selectedLineTexture As Bitmap = Nothing
|
||||
Public Property ModelScaling As Single = 1.0F
|
||||
Public ReadOnly Property HasRendered As Boolean = False
|
||||
Public ReadOnly Property SelectedElements As List(Of Object)
|
||||
|
||||
Private ReadOnly Property VertexBuffers As New Dictionary(Of Mesh, Integer)
|
||||
Private ReadOnly Property IndicesBuffers As New Dictionary(Of Mesh, List(Of Integer))
|
||||
Private ReadOnly Property UVBuffers As New Dictionary(Of Mesh, Integer)
|
||||
Private ReadOnly Property VertexColorBuffers As New Dictionary(Of Mesh, Integer)
|
||||
Private ReadOnly Property NormalBuffers As New Dictionary(Of Mesh, Integer)
|
||||
|
||||
Public ReadOnly Property Model As Object3D
|
||||
Get
|
||||
Return obj3d
|
||||
End Get
|
||||
End Property
|
||||
|
||||
Public Sub New(obj3d As Object3D)
|
||||
Me.obj3d = obj3d.ToOneMesh
|
||||
|
||||
'Set Texture used for faces without texture
|
||||
emptyTexture = ColorToTexture(Color.LightGray)
|
||||
|
||||
'Set Texture used for lines
|
||||
lineTexture = ColorToTexture(Color.Black)
|
||||
|
||||
'Set Texture used for lines of selected faces
|
||||
selectedLineTexture = ColorToTexture(Color.Orange)
|
||||
End Sub
|
||||
|
||||
Private Function ColorToTexture(color As Color) As Image
|
||||
Dim tex As New Bitmap(1, 1)
|
||||
tex.SetPixel(0, 0, color)
|
||||
Return tex
|
||||
End Function
|
||||
|
||||
''' <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 Sub UpdateVertexData(m As Mesh, v As Vertex)
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBuffers(m))
|
||||
GL.BufferSubData(BufferTarget.ArrayBuffer, CType(m.Vertices.IndexOf(v) * Vector3.SizeInBytes, IntPtr), Vector3.SizeInBytes, New Vector3(v.X, v.Y, v.Z))
|
||||
End Sub
|
||||
|
||||
''' <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 Sub UpdateNormalData(m As Mesh, n As Normal)
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, NormalBuffers(m))
|
||||
GL.BufferSubData(BufferTarget.ArrayBuffer, CType(m.Normals.IndexOf(n) * Vector3.SizeInBytes, IntPtr), Vector3.SizeInBytes, New Vector3(n.X, n.Y, n.Z))
|
||||
End Sub
|
||||
|
||||
''' <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 Sub UpdateVertexColorData(m As Mesh, vc As VertexColor)
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexColorBuffers(m))
|
||||
GL.BufferSubData(BufferTarget.ArrayBuffer, CType(m.VertexColors.IndexOf(vc) * Vector4.SizeInBytes, IntPtr), Vector4.SizeInBytes, New Vector4(vc.R, vc.G, vc.B, vc.A))
|
||||
End Sub
|
||||
|
||||
''' <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 Sub UpdateUVData(m As Mesh, uv As UV)
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, UVBuffers(m))
|
||||
GL.BufferSubData(BufferTarget.ArrayBuffer, CType(m.UVs.IndexOf(uv) * Vector2.SizeInBytes, IntPtr), Vector2.SizeInBytes, New Vector2(uv.U, uv.V))
|
||||
End Sub
|
||||
|
||||
''' <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 Sub UpdateFaceIndicies(m As Mesh, f As Face)
|
||||
Dim faceIndex As Integer = m.Faces.IndexOf(f)
|
||||
Dim uintlen As Byte = Len(New UInteger)
|
||||
Dim indicies As New Vector3(m.Vertices.IndexOf(f.Points(0).Vertex),
|
||||
m.Vertices.IndexOf(f.Points(1).Vertex),
|
||||
m.Vertices.IndexOf(f.Points(2).Vertex))
|
||||
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, IndicesBuffers(m)(faceIndex))
|
||||
GL.BufferSubData(BufferTarget.ArrayBuffer, CType(uintlen * faceIndex, IntPtr), uintlen, indicies)
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Replace an Image with a new one.
|
||||
''' </summary>
|
||||
''' <param name="oldImage"></param>
|
||||
''' <param name="newImage"></param>
|
||||
Public Sub UpdateTexture(oldImage As Image, newImage As Image)
|
||||
If dicTextureIDs.ContainsKey(oldImage) Then
|
||||
Dim id As Integer = dicTextureIDs(oldImage)
|
||||
dicTextureIDs.Remove(oldImage)
|
||||
dicTextureIDs.Add(newImage, id)
|
||||
ContentPipe.LoadTexture(newImage, id)
|
||||
End If
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Updates an Image.
|
||||
''' </summary>
|
||||
''' <param name="image"></param>
|
||||
Public Sub UpdateTexture(image As Image)
|
||||
If dicTextureIDs.ContainsKey(image) Then
|
||||
ContentPipe.LoadTexture(dicTextureIDs(image))
|
||||
End If
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Creates the Buffers and store the requied Data.
|
||||
''' </summary>
|
||||
Public Sub RenderModel()
|
||||
ReleaseBuffers()
|
||||
|
||||
For Each mesh As Mesh In obj3d.Meshes
|
||||
|
||||
Dim nibo As New List(Of Integer)
|
||||
Dim enablecols As Boolean = mesh.VertexColors.Count > 0
|
||||
Dim enablenorms As Boolean = (Not enablecols) AndAlso mesh.Normals.Count > 0
|
||||
Dim verts As New List(Of Vector3)
|
||||
Dim uvs As New List(Of Vector2)
|
||||
Dim cols As New List(Of Vector4)
|
||||
Dim norms As New List(Of Vector3)
|
||||
Dim curvi As ULong = 0
|
||||
IndicesBuffers.Add(mesh, nibo)
|
||||
|
||||
For i As Integer = 0 To mesh.Faces.Count - 1
|
||||
With mesh.Faces(i)
|
||||
Dim indices As New List(Of UInteger)
|
||||
For Each p As S3DFileParser.Point In .Points
|
||||
indices.Add(curvi)
|
||||
curvi += 1
|
||||
|
||||
If p.Vertex IsNot Nothing Then
|
||||
verts.Add(New Vector3(p.Vertex.X, p.Vertex.Y, p.Vertex.Z))
|
||||
Else
|
||||
verts.Add(New Vector3(0, 0, 0))
|
||||
End If
|
||||
|
||||
If p.UV IsNot Nothing Then
|
||||
uvs.Add(New Vector2(p.UV.U, p.UV.V))
|
||||
Else
|
||||
uvs.Add(New Vector2(0, 0))
|
||||
End If
|
||||
|
||||
If enablecols AndAlso p.VertexColor IsNot Nothing Then
|
||||
cols.Add(New Vector4(p.VertexColor.R, p.VertexColor.G, p.VertexColor.B, p.VertexColor.A))
|
||||
Else
|
||||
cols.Add(New Vector4(1, 1, 1, 1))
|
||||
End If
|
||||
|
||||
If enablenorms AndAlso p.Normal IsNot Nothing Then
|
||||
norms.Add(New Vector3(p.Normal.X, p.Normal.Y, p.Normal.Z))
|
||||
Else
|
||||
norms.Add(New Vector3(1, 1, 1))
|
||||
End If
|
||||
Next
|
||||
|
||||
nibo.Add(GL.GenBuffer)
|
||||
GL.BindBuffer(BufferTarget.ElementArrayBuffer, nibo(i))
|
||||
GL.BufferData(
|
||||
BufferTarget.ElementArrayBuffer,
|
||||
CType(Len(New UInteger) * indices.Count, IntPtr),
|
||||
indices.ToArray,
|
||||
BufferUsageHint.StaticDraw)
|
||||
|
||||
If .Material?.Image IsNot Nothing Then
|
||||
If Not dicTextureIDs.ContainsKey(.Material.Image) Then
|
||||
dicTextureIDs.Add(.Material.Image, ContentPipe.LoadTexture(.Material.Image))
|
||||
End If
|
||||
ElseIf .Material?.Color IsNot Nothing Then
|
||||
If Not dicColorIDs.ContainsKey(.Material.Color) Then
|
||||
dicColorIDs.Add(.Material.Color, ContentPipe.LoadTexture(ColorToTexture(.Material.Color)))
|
||||
End If
|
||||
Else
|
||||
If Not dicTextureIDs.ContainsKey(emptyTexture) Then
|
||||
dicTextureIDs.Add(emptyTexture, ContentPipe.LoadTexture(emptyTexture))
|
||||
End If
|
||||
End If
|
||||
End With
|
||||
Next
|
||||
|
||||
Dim nvbo As Integer = GL.GenBuffer
|
||||
VertexBuffers.Add(mesh, nvbo)
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, nvbo)
|
||||
GL.BufferData(
|
||||
BufferTarget.ArrayBuffer,
|
||||
CType(Vector3.SizeInBytes * verts.Count, IntPtr),
|
||||
verts.ToArray,
|
||||
BufferUsageHint.StaticDraw
|
||||
)
|
||||
|
||||
Dim ntbo As Integer = GL.GenBuffer
|
||||
UVBuffers.Add(mesh, ntbo)
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, ntbo)
|
||||
GL.BufferData(
|
||||
BufferTarget.ArrayBuffer,
|
||||
CType(Vector2.SizeInBytes * uvs.Count, IntPtr),
|
||||
uvs.ToArray,
|
||||
BufferUsageHint.StaticDraw
|
||||
)
|
||||
|
||||
If enablecols Then
|
||||
Dim ncbo As Integer = GL.GenBuffer
|
||||
VertexColorBuffers.Add(mesh, ncbo)
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, ncbo)
|
||||
GL.BufferData(
|
||||
BufferTarget.ArrayBuffer,
|
||||
CType(Vector4.SizeInBytes * cols.Count, IntPtr),
|
||||
cols.ToArray,
|
||||
BufferUsageHint.StaticDraw
|
||||
)
|
||||
End If
|
||||
|
||||
If enablenorms Then
|
||||
Dim nnbo As Integer = GL.GenBuffer
|
||||
NormalBuffers.Add(mesh, nnbo)
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, nnbo)
|
||||
GL.BufferData(
|
||||
BufferTarget.ArrayBuffer,
|
||||
CType(Vector3.SizeInBytes * norms.Count, IntPtr),
|
||||
norms.ToArray,
|
||||
BufferUsageHint.StaticDraw
|
||||
)
|
||||
End If
|
||||
|
||||
Next
|
||||
|
||||
If Not dicTextureIDs.ContainsKey(lineTexture) Then
|
||||
dicTextureIDs.Add(lineTexture, ContentPipe.LoadTexture(lineTexture))
|
||||
End If
|
||||
|
||||
_HasRendered = True
|
||||
End Sub
|
||||
|
||||
Public Sub DrawModel(mode As RenderMode)
|
||||
DrawModel(mode, Vector3.Zero, Quaternion.Identity, New Vector3(ModelScaling, ModelScaling, ModelScaling))
|
||||
End Sub
|
||||
|
||||
Public Sub DrawModel(mode As RenderMode, pos As Vector3, rot As Quaternion)
|
||||
DrawModel(mode, pos, rot, New Vector3(ModelScaling, ModelScaling, ModelScaling))
|
||||
End Sub
|
||||
|
||||
Public Sub DrawModel(mode As RenderMode, pos As Vector3, rot As Quaternion, scale As Vector3)
|
||||
If mode = RenderMode.None Then Return
|
||||
If Not _HasRendered Then Return
|
||||
|
||||
GL.PushMatrix()
|
||||
GL.Translate(pos.X, pos.Y, pos.Z)
|
||||
GL.Rotate(rot.X, 1, 0, 0)
|
||||
GL.Rotate(rot.Y, 0, 1, 0)
|
||||
GL.Rotate(rot.Z, 0, 0, 1)
|
||||
GL.Scale(scale) 'GL.Scale(scale.X, scale.Y, scale.Z)
|
||||
GL.EnableClientState(ArrayCap.VertexArray)
|
||||
GL.EnableClientState(ArrayCap.TextureCoordArray)
|
||||
|
||||
For Each mesh As Mesh In obj3d.Meshes
|
||||
|
||||
If VertexColorBuffers.ContainsKey(mesh) Then
|
||||
GL.EnableClientState(ArrayCap.ColorArray)
|
||||
ElseIf NormalBuffers.ContainsKey(mesh) Then
|
||||
GL.EnableClientState(ArrayCap.NormalArray)
|
||||
End If
|
||||
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBuffers(mesh))
|
||||
GL.VertexPointer(3, VertexPointerType.Float, 0, IntPtr.Zero)
|
||||
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, UVBuffers(mesh))
|
||||
GL.TexCoordPointer(2, TexCoordPointerType.Float, 0, IntPtr.Zero)
|
||||
|
||||
If VertexColorBuffers.ContainsKey(mesh) Then
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexColorBuffers(mesh))
|
||||
GL.ColorPointer(4, ColorPointerType.Float, 0, IntPtr.Zero)
|
||||
ElseIf NormalBuffers.ContainsKey(mesh) Then
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, NormalBuffers(mesh))
|
||||
GL.NormalPointer(NormalPointerType.Float, 0, IntPtr.Zero)
|
||||
End If
|
||||
|
||||
For i As Integer = 0 To mesh.Faces.Count - 1
|
||||
Dim l As Face = mesh.Faces(i)
|
||||
|
||||
GL.BindBuffer(BufferTarget.ElementArrayBuffer, IndicesBuffers(mesh)(i))
|
||||
|
||||
Dim isEmptyTexture As Boolean = l.Material?.Image Is Nothing
|
||||
Dim isEmptyColor As Boolean = l.Material?.Color Is Nothing
|
||||
|
||||
Dim setMaterialTextureOrColor =
|
||||
Sub()
|
||||
Dim texID As Integer
|
||||
If Not isEmptyTexture Then
|
||||
texID = dicTextureIDs(l.Material.Image)
|
||||
ElseIf Not isEmptyColor Then
|
||||
texID = dicColorIDs(l.Material.Color)
|
||||
Else
|
||||
texID = dicTextureIDs(emptyTexture)
|
||||
End If
|
||||
GL.BindTexture(TextureTarget.Texture2D, texID)
|
||||
End Sub
|
||||
|
||||
If (mode And RenderMode.Fill) = RenderMode.Fill Then
|
||||
setMaterialTextureOrColor()
|
||||
|
||||
If Not isEmptyTexture Then
|
||||
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, l.Material.Wrap.X)
|
||||
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, l.Material.Wrap.Y)
|
||||
End If
|
||||
|
||||
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill)
|
||||
|
||||
GL.DrawElements(PrimitiveType.Triangles, l.Points.Count,
|
||||
DrawElementsType.UnsignedInt, IntPtr.Zero)
|
||||
|
||||
End If
|
||||
|
||||
If (mode And RenderMode.Outline) = RenderMode.Outline Then
|
||||
|
||||
If (mode And RenderMode.Fill) = RenderMode.Fill Then
|
||||
GL.BindTexture(TextureTarget.Texture2D, dicTextureIDs(lineTexture))
|
||||
Else
|
||||
setMaterialTextureOrColor()
|
||||
|
||||
If Not isEmptyTexture Then
|
||||
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, l.Material.Wrap.X)
|
||||
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, l.Material.Wrap.Y)
|
||||
End If
|
||||
End If
|
||||
|
||||
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line)
|
||||
|
||||
GL.DrawElements(PrimitiveType.Triangles, l.Points.Count,
|
||||
DrawElementsType.UnsignedInt, IntPtr.Zero)
|
||||
|
||||
End If
|
||||
Next
|
||||
|
||||
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill) 'Reset for RenderEngineOld
|
||||
|
||||
If VertexColorBuffers.ContainsKey(mesh) Then
|
||||
GL.DisableClientState(ArrayCap.ColorArray)
|
||||
ElseIf NormalBuffers.ContainsKey(mesh) Then
|
||||
GL.DisableClientState(ArrayCap.NormalArray)
|
||||
End If
|
||||
Next
|
||||
|
||||
GL.DisableClientState(ArrayCap.VertexArray)
|
||||
GL.DisableClientState(ArrayCap.TextureCoordArray)
|
||||
GL.PopMatrix()
|
||||
End Sub
|
||||
|
||||
Public Sub DrawFacePicking()
|
||||
DrawFacePicking(Vector3.Zero, Quaternion.Identity, New Vector3(ModelScaling, ModelScaling, ModelScaling))
|
||||
End Sub
|
||||
Public Sub DrawFacePicking(pos As Vector3, rot As Quaternion)
|
||||
DrawFacePicking(pos, rot, New Vector3(ModelScaling, ModelScaling, ModelScaling))
|
||||
End Sub
|
||||
Public Sub DrawFacePicking(pos As Vector3, rot As Quaternion, scale As Vector3)
|
||||
If Not _HasRendered Then Return
|
||||
|
||||
GL.PushMatrix()
|
||||
GL.Translate(pos.X, pos.Y, pos.Z)
|
||||
GL.Rotate(rot.X, 1, 0, 0)
|
||||
GL.Rotate(rot.Y, 0, 1, 0)
|
||||
GL.Rotate(rot.Z, 0, 0, 1)
|
||||
GL.Scale(scale)
|
||||
GL.EnableClientState(ArrayCap.VertexArray)
|
||||
|
||||
For iMesh As Integer = 0 To obj3d.Meshes.Count - 1
|
||||
Dim mesh As Mesh = obj3d.Meshes(iMesh)
|
||||
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBuffers(mesh))
|
||||
GL.VertexPointer(3, VertexPointerType.Float, 0, IntPtr.Zero)
|
||||
|
||||
For iFace As Integer = 0 To mesh.Faces.Count - 1
|
||||
Dim l As Face = mesh.Faces(iFace)
|
||||
|
||||
GL.BindBuffer(BufferTarget.ElementArrayBuffer, IndicesBuffers(mesh)(iFace))
|
||||
|
||||
Dim colorCode As Integer = &H20000000 + (iMesh << 16) + iFace
|
||||
GL.Color4(Color.FromArgb(colorCode)) 'Color: "2f ff xx xx" -> where 'f' = mesh index and where 'x' is face index
|
||||
|
||||
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill)
|
||||
|
||||
GL.DrawElements(PrimitiveType.Triangles, l.Points.Count,
|
||||
DrawElementsType.UnsignedInt, IntPtr.Zero)
|
||||
Next
|
||||
|
||||
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill)
|
||||
Next
|
||||
|
||||
GL.DisableClientState(ArrayCap.VertexArray)
|
||||
GL.PopMatrix()
|
||||
End Sub
|
||||
|
||||
Public Sub ReleaseBuffers()
|
||||
If Not HasRendered Then Return
|
||||
|
||||
For Each kvp As KeyValuePair(Of Mesh, Integer) In VertexBuffers
|
||||
GL.DeleteBuffer(kvp.Value)
|
||||
Next
|
||||
VertexBuffers.Clear()
|
||||
|
||||
For Each kvp As KeyValuePair(Of Mesh, Integer) In UVBuffers
|
||||
GL.DeleteBuffer(kvp.Value)
|
||||
Next
|
||||
UVBuffers.Clear()
|
||||
|
||||
For Each kvp As KeyValuePair(Of Mesh, Integer) In VertexColorBuffers
|
||||
GL.DeleteBuffer(kvp.Value)
|
||||
Next
|
||||
VertexColorBuffers.Clear()
|
||||
|
||||
For Each kvp As KeyValuePair(Of Mesh, Integer) In NormalBuffers
|
||||
GL.DeleteBuffer(kvp.Value)
|
||||
Next
|
||||
NormalBuffers.Clear()
|
||||
|
||||
For Each kvp As KeyValuePair(Of Mesh, List(Of Integer)) In IndicesBuffers
|
||||
For Each i As Integer In kvp.Value
|
||||
GL.DeleteBuffer(i)
|
||||
Next
|
||||
kvp.Value.Clear()
|
||||
Next
|
||||
IndicesBuffers.Clear()
|
||||
|
||||
For Each kvp As KeyValuePair(Of Image, Integer) In dicTextureIDs
|
||||
GL.DeleteBuffer(kvp.Value)
|
||||
Next
|
||||
dicTextureIDs.Clear()
|
||||
|
||||
For Each kvp As KeyValuePair(Of Color, Integer) In dicColorIDs
|
||||
GL.DeleteBuffer(kvp.Value)
|
||||
Next
|
||||
dicColorIDs.Clear()
|
||||
|
||||
_HasRendered = False
|
||||
End Sub
|
||||
|
||||
Protected Overrides Sub Finalize()
|
||||
'ReleaseBuffers()
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
|
||||
End Namespace
|
||||
Reference in New Issue
Block a user