Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [StructLayout(LayoutKind.Sequential, Pack = 4)]
- public struct GLVertex
- {
- [MarshalAs(UnmanagedType.R4)]
- public float PositionX;
- [MarshalAs(UnmanagedType.R4)]
- public float PositionY;
- [MarshalAs(UnmanagedType.R4)]
- public float PositionZ;
- [MarshalAs(UnmanagedType.R4)]
- public float NormalX;
- [MarshalAs(UnmanagedType.R4)]
- public float NormalY;
- [MarshalAs(UnmanagedType.R4)]
- public float NormalZ;
- [MarshalAs(UnmanagedType.R4)]
- public float UVX;
- [MarshalAs(UnmanagedType.R4)]
- public float UVY;
- [MarshalAs(UnmanagedType.R4)]
- public float ColorR;
- [MarshalAs(UnmanagedType.R4)]
- public float ColorG;
- [MarshalAs(UnmanagedType.R4)]
- public float ColorB;
- [MarshalAs(UnmanagedType.R4)]
- public float ColorA;
- public GLVertex(Vector3 pos, Vector3 normal, Vector2 uv, Vector4 color)
- {
- PositionX = pos.X;
- PositionY = pos.Y;
- PositionZ = pos.Z;
- NormalX = normal.X;
- NormalY = normal.Y;
- NormalZ = normal.Z;
- UVX = uv.X;
- UVY = uv.Y;
- ColorR = color.X;
- ColorG = color.Y;
- ColorB = color.Z;
- ColorA = color.W;
- }
- public static GLVertex FromVertex(Vertex vtx)
- {
- return new GLVertex(vtx.Position, vtx.Normal, vtx.UV, new Vector4(0, 0, 0, 1f));
- }
- }
- public class Model
- {
- private int VAO, VBO, IBO;
- private List<GLVertex> Vertices = new List<GLVertex>();
- private List<uint> Indices = new List<uint>();
- public List<IntPtr> VboOffsets = new List<IntPtr>();
- public List<IntPtr> IboOffsets = new List<IntPtr>();
- public List<int> VertexCounts = new List<int>();
- public List<int> IndexCounts = new List<int>();
- private IntPtr vboOffset = IntPtr.Zero;
- private IntPtr iboOffset = IntPtr.Zero; public Model(int maxVertexCount = 1000, int maxIndexCount = 2000)
- {
- // Create VAO
- VAO = GL.GenVertexArray();
- GL.BindVertexArray(VAO);
- // Create VBO and IBO
- VBO = GL.GenBuffer();
- GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
- unsafe
- {
- GL.BufferData(BufferTarget.ArrayBuffer, maxVertexCount * sizeof(GLVertex), IntPtr.Zero, BufferUsageHint.StaticDraw);
- }
- IBO = GL.GenBuffer();
- GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBO);
- GL.BufferData(BufferTarget.ElementArrayBuffer, maxIndexCount * sizeof(uint), IntPtr.Zero, BufferUsageHint.StaticDraw);
- // Bind VBO and IBO to VAO, setting up the vertex format at the same time
- unsafe
- {
- int offset = 0;
- GL.EnableVertexAttribArray(0); // Position
- GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, sizeof(GLVertex), offset);
- offset += 3 * sizeof(float);
- GL.EnableVertexAttribArray(1); // Normal
- GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, sizeof(GLVertex), offset);
- offset += 3 * sizeof(float);
- GL.EnableVertexAttribArray(2); // UV
- GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, sizeof(GLVertex), offset);
- offset += 2 * sizeof(float);
- GL.EnableVertexAttribArray(3); // Color
- GL.VertexAttribPointer(3, 4, VertexAttribPointerType.Float, false, sizeof(GLVertex), offset);
- offset += 4 * sizeof(float);
- }
- }
- public void AddVertices(List<GLVertex> vertices)
- {
- //SubVertices.Add(vertices);
- Vertices.AddRange(vertices);
- VertexCounts.Add(vertices.Count);
- GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
- unsafe
- {
- GL.BufferSubData(BufferTarget.ArrayBuffer, vboOffset, sizeof(GLVertex) * vertices.Count, vertices.ToArray());
- VboOffsets.Add(vboOffset);
- vboOffset += sizeof(GLVertex) * vertices.Count;
- }
- GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
- }
- public void AddIndices(List<uint> indices)
- {
- Indices.AddRange(indices);
- IndexCounts.Add(indices.Count);
- GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBO);
- unsafe
- {
- GL.BufferSubData(BufferTarget.ArrayBuffer, iboOffset, sizeof(uint) * indices.Count, indices.ToArray());
- }
- GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
- IboOffsets.Add(iboOffset);
- iboOffset += sizeof(uint) * indices.Count;
- }
- public void Render()
- {
- GL.BindVertexArray(VAO);
- for (var i = 0; i < IboOffsets.Count; i++)
- {
- GL.DrawElements(PrimitiveType.Triangles, IndexCounts[i], DrawElementsType.UnsignedInt, IboOffsets[i]); // memory access violation here
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement