using System; using OpenTK.Graphics.OpenGL; using OpenTK; using org.budnhead.core; namespace org.budnhead.graphics { public class Model3D { protected int vao = 0, vbo, // Vertex Buffer cbo, // Color Buffer cbo2, // Second Color Buffer nbo; // Normals Buffer protected int nTriangles; public Vector3[] vertexes = null; public Vector4[] colors = null; public Vector4[] colors2 = null; public Vector3[] normals = null; public Triangle[] triangles = new Triangle[0]; public Model3D(){ nTriangles = 0; prepare(); } protected void prepare(){ vao = GL.GenVertexArray(); vbo = GL.GenBuffer(); cbo = GL.GenBuffer(); cbo2 = GL.GenBuffer(); nbo = GL.GenBuffer(); GL.BindVertexArray(vao); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0); GL.BindBuffer(BufferTarget.ArrayBuffer, cbo); GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, 0, 0); GL.BindBuffer(BufferTarget.ArrayBuffer, nbo); GL.VertexAttribPointer(2, 3, VertexAttribPointerType.Float, false, 0, 0); GL.BindBuffer(BufferTarget.ArrayBuffer, cbo); GL.VertexAttribPointer(3, 4, VertexAttribPointerType.Float, false, 0, 0); GL.BindVertexArray(0); } public void prepareBuffers(int nTriangles){ this.vertexes = new Vector3[nTriangles * 3]; this.normals = new Vector3[nTriangles * 3]; this.colors = new Vector4[nTriangles * 3]; this.colors2 = new Vector4[nTriangles * 3]; this.vertexes.Fill(new Vector3(0,0,0)); this.colors.Fill(new Vector4(0.5f,0.5f,0.5f,1.0f)); this.colors2.Fill(new Vector4(0.0f,2.0f,0.0f,1.0f)); this.nTriangles = nTriangles; rebind(); } public void bind(){ this.nTriangles = this.vertexes.Length / 3; if (this.vertexes == null){ this.vertexes = new Vector3[nTriangles * 3]; this.vertexes.Fill(new Vector3(0,0,0)); } if (this.normals == null){ this.normals = new Vector3[nTriangles * 3]; } if (this.colors == null){ this.colors = new Vector4[nTriangles * 3]; this.colors.Fill(new Vector4(0.5f,0.5f,0.5f,1.0f)); } if (this.colors2 == null){ this.colors2 = new Vector4[nTriangles * 3]; this.colors2.Fill(new Vector4(0.0f,2.0f,0.0f,1.0f)); } rebind(); } public void rebind(){ if (vao != 0){ prepare(); } bindVertexes(); bindNormals(); bindColors(); bindColors2(); } protected void bindVertexes(){ GL.BindVertexArray(vao); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.BufferData(BufferTarget.ArrayBuffer, 12 * vertexes.Length, vertexes, BufferUsageHint.StaticDraw); GL.EnableVertexAttribArray(0); if (this.triangles.Length != nTriangles){ int nt = this.triangles.Length; Array.Resize(ref this.triangles,nTriangles); for (int n=nt;n