/// Copyright (c) 2021 Iiro Iivanainen, Harri Linna, Jere Pakkanen, Riikka Vilavaara /// /// Permission is hereby granted, free of charge, to any person obtaining /// a copy of this software and associated documentation files (the /// "Software"), to deal in the Software without restriction, including /// without limitation the rights to use, copy, modify, merge, publish, /// distribute, sublicense, and/or sell copies of the Software, and to /// permit persons to whom the Software is furnished to do so, subject to /// the following conditions: /// /// The above copyright notice and this permission notice shall be included /// in all copies or substantial portions of the Software. /// /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, /// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF /// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. /// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY /// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, /// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE /// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using System; using System.Numerics; using SharpGL; using SharpGL.WPF; using SharpGL.SceneGraph; using SharpGL.SceneGraph.Assets; namespace WPFOpenGL { public class Cube : Shape { Vector3 centerPosition; Vector3 size; Vector3 v0; Vector3 v1; Vector3 v2; Vector3 v3; Vector3 v4; Vector3 v5; Vector3 v6; Vector3 v7; Vector4 color = new Vector4(0.5f, 0.5f, 0.5f, 1f); /// v6 v7 /// v2 +----v3-+ /// +---|----+ | /// | | | | /// | | | | /// |v4 +----|--+ v5 /// +--------+ /// v0 v1 public Cube(Vector3 centerPosition, Vector3 size) { this.centerPosition = centerPosition; this.size = size; calculateVertices(); SetIsVisible(true); SetIsRaycastable(false); } private void calculateVertices() { v0 = new Vector3(centerPosition.X - size.X / 2f, centerPosition.Y - size.Y / 2f, centerPosition.Z - size.Z / 2f); v1 = new Vector3(centerPosition.X + size.X / 2f, centerPosition.Y - size.Y / 2f, centerPosition.Z - size.Z / 2f); v2 = new Vector3(centerPosition.X - size.X / 2f, centerPosition.Y + size.Y / 2f, centerPosition.Z - size.Z / 2f); v3 = new Vector3(centerPosition.X + size.X / 2f, centerPosition.Y + size.Y / 2f, centerPosition.Z - size.Z / 2f); v4 = new Vector3(centerPosition.X - size.X / 2f, centerPosition.Y - size.Y / 2f, centerPosition.Z + size.Z / 2f); v5 = new Vector3(centerPosition.X + size.X / 2f, centerPosition.Y - size.Y / 2f, centerPosition.Z + size.Z / 2f); v6 = new Vector3(centerPosition.X - size.X / 2f, centerPosition.Y + size.Y / 2f, centerPosition.Z + size.Z / 2f); v7 = new Vector3(centerPosition.X + size.X / 2f, centerPosition.Y + size.Y / 2f, centerPosition.Z + size.Z / 2f); } public override bool IntersectRay(Vector3 rayOrigin, Vector3 rayDirection, out Vector3 hit, out float distance) { throw new NotImplementedException(); } public override void Render(OpenGL openGL) { openGL.Color(color.X, color.Y, color.Z, color.W); openGL.Begin(OpenGL.GL_LINE_LOOP); openGL.Vertex(v0.X, v0.Y, v0.Z); openGL.Vertex(v1.X, v1.Y, v1.Z); openGL.Vertex(v3.X, v3.Y, v3.Z); openGL.Vertex(v2.X, v2.Y, v2.Z); openGL.Vertex(v0.X, v0.Y, v0.Z); openGL.End(); openGL.Begin(OpenGL.GL_LINE_LOOP); openGL.Vertex(v4.X, v4.Y, v4.Z); openGL.Vertex(v5.X, v5.Y, v5.Z); openGL.Vertex(v7.X, v7.Y, v7.Z); openGL.Vertex(v6.X, v6.Y, v6.Z); openGL.Vertex(v4.X, v4.Y, v4.Z); openGL.End(); openGL.Begin(OpenGL.GL_LINES); openGL.Vertex(v0.X, v0.Y, v0.Z); openGL.Vertex(v4.X, v4.Y, v4.Z); openGL.Vertex(v1.X, v1.Y, v1.Z); openGL.Vertex(v5.X, v5.Y, v5.Z); openGL.Vertex(v2.X, v2.Y, v2.Z); openGL.Vertex(v6.X, v6.Y, v6.Z); openGL.Vertex(v3.X, v3.Y, v3.Z); openGL.Vertex(v7.X, v7.Y, v7.Z); openGL.End(); } } }