/// <summary> /// ***This requires shaders to have uniforms called viewmatrix, projmatrix, and transformmatrix.*** /// </summary> public override void Render() { if (Shader == null) throw new NullReferenceException("Cannot render without attached shader."); var viewmat = Shader.GetUniformLocation("viewmatrix"); var projmat = Shader.GetUniformLocation("projmatrix"); var transmat = Shader.GetUniformLocation("transformmatrix"); var scalematrix = new Matrix4(Scale.X, 0.0f, 0.0f, 0.0f, 0.0f, Scale.Y, 0.0f, 0.0f, 0.0f, 0.0f, Scale.Z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); Vector3 axis; Matrix4 rotmatrix; if (Rotation.Length > 0.0f) { float angle; Rotation.ToAxisAngle(out axis, out angle); rotmatrix = Matrix4.CreateFromAxisAngle(axis, angle); } else rotmatrix = Matrix4.Identity; var posmatrix = OpenTK.Matrix4.CreateTranslation(Position); TransformationMatrix = posmatrix * rotmatrix * scalematrix; Shader.Activate(); GL.UniformMatrix4(viewmat, false, ref ModelViewMatrix); GL.UniformMatrix4(projmat, false, ref ProjectionMatrix); GL.UniformMatrix4(transmat, false, ref TransformationMatrix); int texture_unit = 0; foreach (KeyValuePair<int, ITexture> kvp in Uniform_To_Texture) { GL.ActiveTexture(TextureUnit.Texture0 + texture_unit); GL.Uniform1(kvp.Key, texture_unit); GL.BindTexture(TextureTarget.Texture2D, kvp.Value.GetTextureHandle()); texture_unit++; } GL.DrawArrays(BeginMode.Triangles, 0, Vertices.Count); }
/// <summary> /// This can only be created after an opengl context has been created. /// </summary> public GL33Renderable() { GL.GenVertexArrays(1, out VertexArrayObject); GL.BindVertexArray(VertexArrayObject); if(!GL.IsVertexArray(VertexArrayObject)) throw new OpenGLException("Vertex array object could not be created!"); VertexBufferObject = new int[3]; GL.GenBuffers(3, VertexBufferObject); GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject[0]); GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject[1]); GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject[2]); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindVertexArray(0); if (!GL.IsBuffer(VertexBufferObject[0]) || !GL.IsBuffer(VertexBufferObject[1]) || !GL.IsBuffer(VertexBufferObject[2])) throw new OpenGLException("Vertex buffer object was not created!"); Textures = new List<ITexture>(); Uniform_To_Texture = new Dictionary<int, ITexture>(); Position = new Vector3(0.0f, 0.0f, 0.0f); Rotation = new Quaternion(); Scale = new Vector3(1.0f, 1.0f, 1.0f); TransformationMatrix = Matrix4.Identity; ModelViewMatrix = Matrix4.Identity; ProjectionMatrix = Matrix4.Identity; Shader = null; }
public void PushMatrix(Matrix4 matrix) { if(matrix == null) { Content.Push(this.Top); } else { Content.Push(matrix.CopyTransform(this.Top)); } }
public void TestHit1() { Matrix4 M = new Matrix4(); M.Shift(0.0d, 0.0d, 10.0d); LoaderObj lo = new LoaderObj(); FileStream fs = File.Open("triceratops.obj", FileMode.Open, FileAccess.Read); lo.Load(null, fs); fs.Close(); List<RenderItem> ris = new List<RenderItem>(); lo.Inject(ris, M); NaiveAccelerator na = new NaiveAccelerator(ris); GridAccelerator ga = new GridAccelerator(ris); double ta, tb; RenderItem ria, rib; for(int i = 0; i < TestParameters.TriceratopsTest; i++) { Ray ray = Ray.Random(); ria = ga.CalculateHit(ray, out ta, double.PositiveInfinity); rib = na.CalculateHit(ray, out tb, double.PositiveInfinity); TestParameters.TestRIEqual(ray, ta, tb, ris, ria, rib); } }
public void TestInvTransformNonShift() { for(int i = 0x00; i < TestParameters.PointTest; i++) { Point3 p = Point3.Random(); Matrix4 M = new Matrix4(Maths.RandomGenerator.NextDouble(), Maths.RandomGenerator.NextDouble(), Maths.RandomGenerator.NextDouble(), Maths.RandomGenerator.NextDouble(), Maths.RandomGenerator.NextDouble(), Maths.RandomGenerator.NextDouble(), Maths.RandomGenerator.NextDouble(), Maths.RandomGenerator.NextDouble(), Maths.RandomGenerator.NextDouble(), Maths.RandomGenerator.NextDouble(), Maths.RandomGenerator.NextDouble(), Maths.RandomGenerator.NextDouble()); Point3 q = new Point3(p); q.TransformNonShift(M); q.InvTransformNonShift(M); Assert.IsTrue(Math.Abs(p.X-q.X) <= Maths.GlobalEpsilon); Assert.IsTrue(Math.Abs(p.Y-q.Y) <= Maths.GlobalEpsilon); Assert.IsTrue(Math.Abs(p.Z-q.Z) <= Maths.GlobalEpsilon); } }
public void TransformNormalize(Matrix4 m) { this.Offset.Transform(m); this.Direction.TransformNonShift(m); this.Direction.Normalize(); }
public void InvTransform(Matrix4 m) { this.Offset.InvTransform(m); this.Direction.InvTransformNonShift(m); }
public void Inject(Matrix4 transformation, List<RenderItem> items) { if(this.loader != null) { this.loader.Inject(items, transformation, parameters); } }
/// <summary> /// /// </summary> /// <param name="mat"></param> public override void SetProjectionMatrix(Matrix4 mat) { ProjectionMatrix = mat; }
public Matrix4 CopyTransform(Matrix4 N) { double n00 = M00*N.M00+M10*N.M01+M20*N.M02; double n01 = M01*N.M00+M11*N.M01+M21*N.M02; double n02 = M02*N.M00+M12*N.M01+M22*N.M02; double n03 = M03*N.M00+M13*N.M01+M23*N.M02+N.M03; double n10 = M00*N.M10+M10*N.M11+M20*N.M12; double n11 = M01*N.M10+M11*N.M11+M21*N.M12; double n12 = M02*N.M10+M12*N.M11+M22*N.M12; double n13 = M03*N.M10+M13*N.M11+M23*N.M12+N.M13; double n20 = M00*N.M20+M10*N.M21+M20*N.M22; double n21 = M01*N.M20+M11*N.M21+M21*N.M22; double n22 = M02*N.M20+M12*N.M21+M22*N.M22; double n23 = M03*N.M20+M13*N.M21+M23*N.M22+N.M23; return new Matrix4(n00, n01, n02, n03, n10, n11, n12, n13, n20, n21, n22, n23); }
public static void TransformMatrix(Matrix4 source, Matrix4 manipulator) { source.Transform(manipulator); }
public Matrix4(Matrix4 s) { this.M00 = s.M00; this.M01 = s.M01; this.M02 = s.M02; this.M03 = s.M03; this.M10 = s.M10; this.M11 = s.M11; this.M12 = s.M12; this.M13 = s.M13; this.M20 = s.M20; this.M21 = s.M21; this.M22 = s.M22; this.M23 = s.M23; }
public abstract void SetProjectionMatrix(Matrix4 projectionmatrix);
public abstract void SetModelViewMatrix(Matrix4 modelviewmatrix);
public void AddRenderable(IRenderable renderable, Matrix4 projectionmatrix, Matrix4 modelviewmatrix) { renderable.SetModelViewMatrix(modelviewmatrix); renderable.SetProjectionMatrix(projectionmatrix); Renderables.Add(renderable); }
public void TransformNormalize(Matrix4 m, out double lengthMul) { this.Offset.Transform(m); this.Direction.TransformNonShift(m); lengthMul = 1.0d/this.Direction.Length; this.Direction.X *= lengthMul; this.Direction.Y *= lengthMul; this.Direction.Z *= lengthMul; }
/// <summary> /// /// </summary> /// <param name="mat"></param> public override void SetModelViewMatrix(Matrix4 mat) { ModelViewMatrix = mat; }
public void LoadMatrix(Matrix4 M) { this.M00 = M.M00; this.M01 = M.M01; this.M02 = M.M02; this.M03 = M.M03; this.M10 = M.M00; this.M11 = M.M01; this.M12 = M.M02; this.M13 = M.M03; this.M20 = M.M00; this.M21 = M.M01; this.M22 = M.M02; this.M23 = M.M03; }
public void Transform(Matrix4 N) { double n00 = M00*N.M00+M10*N.M01+M20*N.M02; double n01 = M01*N.M00+M11*N.M01+M21*N.M02; double n02 = M02*N.M00+M12*N.M01+M22*N.M02; double n03 = M03*N.M00+M13*N.M01+M23*N.M02+N.M03; double n10 = M00*N.M10+M10*N.M11+M20*N.M12; double n11 = M01*N.M10+M11*N.M11+M21*N.M12; double n12 = M02*N.M10+M12*N.M11+M22*N.M12; double n13 = M03*N.M10+M13*N.M11+M23*N.M12+N.M13; double n20 = M00*N.M20+M10*N.M21+M20*N.M22; double n21 = M01*N.M20+M11*N.M21+M21*N.M22; double n22 = M02*N.M20+M12*N.M21+M22*N.M22; double n23 = M03*N.M20+M13*N.M21+M23*N.M22+N.M23; this.M00 = n00; this.M01 = n01; this.M02 = n02; this.M03 = n03; this.M10 = n10; this.M11 = n11; this.M12 = n12; this.M13 = n13; this.M20 = n20; this.M21 = n21; this.M22 = n22; this.M23 = n23; }
public abstract void Inject(List<RenderItem> items, Matrix4 transform, params string[] args);