Advertisement
RatcheT2497

opentk problems

May 11th, 2020
1,392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.79 KB | None | 0 0
  1. [StructLayout(LayoutKind.Sequential, Pack = 4)]
  2. public struct GLVertex
  3. {
  4.     [MarshalAs(UnmanagedType.R4)]
  5.     public float PositionX;
  6.     [MarshalAs(UnmanagedType.R4)]
  7.     public float PositionY;
  8.     [MarshalAs(UnmanagedType.R4)]
  9.     public float PositionZ;
  10.  
  11.     [MarshalAs(UnmanagedType.R4)]
  12.     public float NormalX;
  13.     [MarshalAs(UnmanagedType.R4)]
  14.     public float NormalY;
  15.     [MarshalAs(UnmanagedType.R4)]
  16.     public float NormalZ;
  17.     [MarshalAs(UnmanagedType.R4)]
  18.     public float UVX;
  19.     [MarshalAs(UnmanagedType.R4)]
  20.     public float UVY;
  21.     [MarshalAs(UnmanagedType.R4)]
  22.     public float ColorR;
  23.     [MarshalAs(UnmanagedType.R4)]
  24.     public float ColorG;
  25.     [MarshalAs(UnmanagedType.R4)]
  26.     public float ColorB;
  27.     [MarshalAs(UnmanagedType.R4)]
  28.     public float ColorA;
  29.     public GLVertex(Vector3 pos, Vector3 normal, Vector2 uv, Vector4 color)
  30.     {
  31.         PositionX = pos.X;
  32.         PositionY = pos.Y;
  33.         PositionZ = pos.Z;
  34.            
  35.         NormalX = normal.X;
  36.         NormalY = normal.Y;
  37.         NormalZ = normal.Z;
  38.  
  39.         UVX = uv.X;
  40.         UVY = uv.Y;
  41.         ColorR = color.X;
  42.         ColorG = color.Y;
  43.         ColorB = color.Z;
  44.         ColorA = color.W;
  45.     }
  46.     public static GLVertex FromVertex(Vertex vtx)
  47.     {
  48.         return new GLVertex(vtx.Position, vtx.Normal, vtx.UV, new Vector4(0, 0, 0, 1f));
  49.     }
  50. }
  51. public class Model
  52. {
  53.     private int VAO, VBO, IBO;
  54.     private List<GLVertex> Vertices = new List<GLVertex>();
  55.     private List<uint> Indices = new List<uint>();
  56.     public List<IntPtr> VboOffsets = new List<IntPtr>();
  57.     public List<IntPtr> IboOffsets = new List<IntPtr>();
  58.     public List<int> VertexCounts = new List<int>();
  59.     public List<int> IndexCounts = new List<int>();
  60.     private IntPtr vboOffset = IntPtr.Zero;
  61.     private IntPtr iboOffset = IntPtr.Zero; public Model(int maxVertexCount = 1000, int maxIndexCount = 2000)
  62.     {
  63.         // Create VAO
  64.         VAO = GL.GenVertexArray();
  65.         GL.BindVertexArray(VAO);
  66.  
  67.         // Create VBO and IBO
  68.         VBO = GL.GenBuffer();
  69.         GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
  70.         unsafe
  71.         {
  72.             GL.BufferData(BufferTarget.ArrayBuffer, maxVertexCount * sizeof(GLVertex), IntPtr.Zero, BufferUsageHint.StaticDraw);
  73.         }
  74.            
  75.         IBO = GL.GenBuffer();
  76.         GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBO);
  77.         GL.BufferData(BufferTarget.ElementArrayBuffer, maxIndexCount * sizeof(uint), IntPtr.Zero, BufferUsageHint.StaticDraw);
  78.  
  79.         // Bind VBO and IBO to VAO, setting up the vertex format at the same time
  80.         unsafe
  81.         {
  82.             int offset = 0;
  83.             GL.EnableVertexAttribArray(0); // Position
  84.             GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, sizeof(GLVertex), offset);
  85.             offset += 3 * sizeof(float);
  86.  
  87.             GL.EnableVertexAttribArray(1); // Normal
  88.             GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, sizeof(GLVertex), offset);
  89.             offset += 3 * sizeof(float);
  90.  
  91.             GL.EnableVertexAttribArray(2); // UV
  92.             GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, sizeof(GLVertex), offset);
  93.             offset += 2 * sizeof(float);
  94.  
  95.             GL.EnableVertexAttribArray(3); // Color
  96.             GL.VertexAttribPointer(3, 4, VertexAttribPointerType.Float, false, sizeof(GLVertex), offset);
  97.             offset += 4 * sizeof(float);
  98.         }
  99.     }
  100.  
  101.     public void AddVertices(List<GLVertex> vertices)
  102.     {
  103.         //SubVertices.Add(vertices);
  104.         Vertices.AddRange(vertices);
  105.         VertexCounts.Add(vertices.Count);
  106.         GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
  107.         unsafe
  108.         {
  109.             GL.BufferSubData(BufferTarget.ArrayBuffer, vboOffset, sizeof(GLVertex) * vertices.Count, vertices.ToArray());
  110.             VboOffsets.Add(vboOffset);
  111.             vboOffset += sizeof(GLVertex) * vertices.Count;
  112.         }
  113.         GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  114.     }
  115.     public void AddIndices(List<uint> indices)
  116.     {
  117.         Indices.AddRange(indices);
  118.         IndexCounts.Add(indices.Count);
  119.         GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBO);
  120.         unsafe
  121.         {
  122.             GL.BufferSubData(BufferTarget.ArrayBuffer, iboOffset, sizeof(uint) * indices.Count, indices.ToArray());
  123.         }
  124.  
  125.         GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
  126.         IboOffsets.Add(iboOffset);
  127.         iboOffset += sizeof(uint) * indices.Count;
  128.     }
  129.     public void Render()
  130.     {
  131.         GL.BindVertexArray(VAO);
  132.         for (var i = 0; i < IboOffsets.Count; i++)
  133.         {
  134.             GL.DrawElements(PrimitiveType.Triangles, IndexCounts[i], DrawElementsType.UnsignedInt, IboOffsets[i]); // memory access violation here
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement