using System; using OpenTK.Graphics.OpenGL; using OpenTK.Graphics; using OpenTK; using org.budnhead.tools; namespace org.budnhead.graphics { public class GLObject { ShaderProgram shaderProgram; protected float[] vertices, colors, normals; protected int[] elements; int vao, // Vertex Array Object ebo, // Element Buffer vbo, // Vertex Buffer cbo, // Color Buffer nbo; // Normals Buffer Vector3 position; Matrix4 matrix; protected bool colorEnabled, normalsEnabled, uvEnabled; public GLObject(int vertexes) { shaderProgram = ResourceLibrary.getResource("default"); init(vertexes); prepareGL(); } public GLObject(){ shaderProgram = ResourceLibrary.getResource("default"); init(0); prepareGL(); } private void init(int vertexes){ this.vertices = new float[3 * vertexes]; this.colors = new float[4 * vertexes]; this.normals = new float[3 * vertexes]; this.elements = null; // Element Arrays können im Moment nicht verwendet werden colorEnabled = true; normalsEnabled = false; uvEnabled = false; this.position = new Vector3(); this.matrix = Matrix4.Identity; } protected void prepareGL() { vao = GL.GenVertexArray(); ebo = GL.GenBuffer(); vbo = GL.GenBuffer(); cbo = 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.BindVertexArray(0); } protected void updateGL() { GL.BindVertexArray(vao); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.BufferData(BufferTarget.ArrayBuffer, 4 * this.vertices.Length, this.vertices, BufferUsageHint.StaticDraw); GL.EnableVertexAttribArray(0); if (colorEnabled) { GL.BindBuffer(BufferTarget.ArrayBuffer, cbo); GL.BufferData(BufferTarget.ArrayBuffer, 4 * this.colors.Length, this.colors, BufferUsageHint.StaticDraw); GL.EnableVertexAttribArray(1); } else { GL.DisableVertexAttribArray(1); } if (normalsEnabled) { GL.BindBuffer(BufferTarget.ArrayBuffer, nbo); GL.BufferData(BufferTarget.ArrayBuffer, 3 * this.normals.Length, this.normals, BufferUsageHint.StaticDraw); GL.EnableVertexAttribArray(2); } else { GL.DisableVertexAttribArray(2); } if (uvEnabled) { GL.EnableVertexAttribArray(3); } else { GL.DisableVertexAttribArray(3); } GL.BindVertexArray(0); } protected void setVertex(int n, Vector3 p) { vertices[(3 * n)] = p.X; vertices[(3 * n) + 1] = p.Y; vertices[(3 * n) + 2] = p.Z; } protected void setNormal(int n, Vector3 norm){ normals[(3 * n)] = norm.X; normals[(3 * n)+1] = norm.Y; normals[(3 * n)+2] = norm.Z; } protected void setColor(int n, Vector4 p) { colors[(4 * n)] = p.X; colors[(4 * n) + 1] = p.Y; colors[(4 * n) + 2] = p.Z; colors[(4 * n) + 3] = p.W; } protected void setTriangle(int n, Vector3 a, Vector3 b, Vector3 c) { setVertex((3 * n), a); setVertex((3 * n) + 1, b); setVertex((3 * n) + 2, c); Vector3 normal = Vector3.Cross((b - a),(c - a)); normal.Normalize(); setNormal((3 * n), normal); setNormal((3 * n) + 1, normal); setNormal((3 * n) + 2, normal); } protected void setColor(Vector4 c) { for (int n = 0; n < colors.Length / 4; n++) { setColor(n, c); } } protected Vector4 getColor(int n){ return new Vector4( colors[(4 * n)], colors[(4 * n) + 1], colors[(4 * n) + 2], colors[(4 * n) + 3]); } public void paint() { paint(new GLCamera()); } public void paint(GLSceneOrientation scene) { GL.BindVertexArray(this.vao); GL.DrawArrays(PrimitiveType.Triangles, 0, vertices.Length); GL.BindVertexArray(0); } protected void buildMatrix(){ this.matrix = Matrix4.CreateTranslation( this.position ); } public ShaderProgram ShaderProgram { get { return this.shaderProgram; } set { this.shaderProgram = value; } } public Vector3 Position{ get { return this.position; } set { this.position = value; buildMatrix(); } } public Matrix4 Matrix { get { return this.matrix; } set { this.matrix = value; } } } }