// // Actor.cs // // Author: // Jarl Gullberg // // Copyright (c) 2016 Jarl Gullberg // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // using System; using GLib; using GLWidgetTestGTK3.Data; using OpenTK.Mathematics; using OpenTK.Graphics.OpenGL; namespace GLWidgetTestGTK3.World { public class Actor { public Transform Transform; private readonly Mesh Mesh; /// /// Creates a new instance of the class. /// /// The mesh bound to the actor. public Actor(Mesh Mesh) { this.Mesh = Mesh; this.Transform = new Transform(new Vector3(0.0f, 0.0f, 0.0f)); } /// /// Performs any arbitrary actions needed every frame, such as animations, texture manipulations or state /// updates. /// /// The time (in thousands of a second) taken to render the previous frame. public void Tick(float deltaTime) { } /// /// Renders this actor within the current OpenGL context. /// /// The ID of the currently active shader. /// The camera view matrix, calulcated from the camera position. /// The perspective projection currently in use. public void Render(int ShaderProgramID, Matrix4 ViewMatrix, Matrix4 ProjectionMatrix) { // Enable the mesh vertex array GL.BindBuffer(BufferTarget.ArrayBuffer, Mesh.VertexBufferID); GL.EnableVertexAttribArray(0); GL.VertexAttribPointer( 0, 3, VertexAttribPointerType.Float, false, 0, 0); // Enable the normal attributes GL.EnableVertexAttribArray(3); GL.VertexAttribPointer( 2, 3, VertexAttribPointerType.Float, false, 0, 0); Matrix4 modelTranslation = Matrix4.CreateTranslation(Transform.Translation); Matrix4 modelScale = Matrix4.CreateScale(Transform.Scale); Matrix4 modelRotation = Matrix4.CreateFromQuaternion(Transform.Rotation); Matrix4 modelViewProjection = modelScale * modelRotation * modelTranslation * ViewMatrix * ProjectionMatrix; // Send the model matrix to the shader int projectionShaderVariableHandle = GL.GetUniformLocation(ShaderProgramID, "ModelViewProjection"); GL.UniformMatrix4(projectionShaderVariableHandle, false, ref modelViewProjection); // Draw the model try { GL.DrawArrays(PrimitiveType.Triangles, 0, Mesh.GetVertexCount()); } catch (Exception) { } // Release the attribute arrays GL.DisableVertexAttribArray(0); GL.DisableVertexAttribArray(3); } } }