internal void AddSphereRing(BoundingSphere sphere, Color color, Matrix onb) { float increment = 1.0f / 32; for (float i = 0; i < 1; i += increment) { float a0 = 2 * (float)Math.PI * i; float a1 = 2 * (float)Math.PI * (i + increment); Add( Vector3.Transform(new Vector3(Math.Cos(a0), 0, Math.Sin(a0)) * sphere.Radius, onb) + sphere.Center, Vector3.Transform(new Vector3(Math.Cos(a1), 0, Math.Sin(a1)) * sphere.Radius, onb) + sphere.Center, color); } }
internal void AddCone(Vector3 translation, Vector3 directionVec, Vector3 baseVec, int tessalation, Color color) { var axis = directionVec; axis.Normalize(); var apex = translation + directionVec; var steps = tessalation; var stepsRcp = (float)(Math.PI * 2 / steps); for (int i = 0; i < 32; i++) { float a0 = i * stepsRcp; float a1 = (i + 1) * stepsRcp; var A = translation + Vector3.Transform(baseVec, Matrix.CreateFromAxisAngle(axis, a0)); var B = translation + Vector3.Transform(baseVec, Matrix.CreateFromAxisAngle(axis, a1)); Add(A, B, color); Add(A, apex, color); } }
// Special method that loads data into GPU, and can be called only from Draw method, never from LoadContent or from background thread. // Because that would lead to empty vertex/index buffers if they are filled/created while game is minimized (remember the issue - alt-tab during loading screen) static void LoadInDraw() { if (m_backgroundOrientationDirty) { Static.UnloadContent(); } if (m_loaded) { return; } // In fact it doesn't matter how large is cube, it will always look same as we are always in its middle // I changed it from 1.0 to 100.0 only because will small length I had problems with near frustum plane and crazy aspect ratios. const float CUBE_LENGTH_HALF = 100; Vector3 shapeSize = Vector3.One * CUBE_LENGTH_HALF; Vector3 shapePosition = Vector3.Zero; MyVertexFormatPositionTexture3[] boxVertices = new MyVertexFormatPositionTexture3[36]; Vector3 topLeftFront = shapePosition + new Vector3(-1.0f, 1.0f, -1.0f) * shapeSize; Vector3 bottomLeftFront = shapePosition + new Vector3(-1.0f, -1.0f, -1.0f) * shapeSize; Vector3 topRightFront = shapePosition + new Vector3(1.0f, 1.0f, -1.0f) * shapeSize; Vector3 bottomRightFront = shapePosition + new Vector3(1.0f, -1.0f, -1.0f) * shapeSize; Vector3 topLeftBack = shapePosition + new Vector3(-1.0f, 1.0f, 1.0f) * shapeSize; Vector3 topRightBack = shapePosition + new Vector3(1.0f, 1.0f, 1.0f) * shapeSize; Vector3 bottomLeftBack = shapePosition + new Vector3(-1.0f, -1.0f, 1.0f) * shapeSize; Vector3 bottomRightBack = shapePosition + new Vector3(1.0f, -1.0f, 1.0f) * shapeSize; Vector3 textureTopLeftFront = MyUtils.Normalize(topLeftFront); Vector3 textureBottomLeftFront = MyUtils.Normalize(bottomLeftFront); Vector3 textureTopRightFront = MyUtils.Normalize(topRightFront); Vector3 textureBottomRightFront = MyUtils.Normalize(bottomRightFront); Vector3 textureTopLeftBack = MyUtils.Normalize(topLeftBack); Vector3 textureTopRightBack = MyUtils.Normalize(topRightBack); Vector3 textureBottomLeftBack = MyUtils.Normalize(bottomLeftBack); Vector3 textureBottomRightBack = MyUtils.Normalize(bottomRightBack); textureTopLeftFront.Z *= -1; textureBottomLeftFront.Z *= -1; textureTopRightFront.Z *= -1; textureBottomRightFront.Z *= -1; textureTopLeftBack.Z *= -1; textureTopRightBack.Z *= -1; textureBottomLeftBack.Z *= -1; textureBottomRightBack.Z *= -1; // Front face. boxVertices[0] = new MyVertexFormatPositionTexture3(topLeftFront, textureTopLeftFront); boxVertices[1] = new MyVertexFormatPositionTexture3(bottomLeftFront, textureBottomLeftFront); boxVertices[2] = new MyVertexFormatPositionTexture3(topRightFront, textureTopRightFront); boxVertices[3] = new MyVertexFormatPositionTexture3(bottomLeftFront, textureBottomLeftFront); boxVertices[4] = new MyVertexFormatPositionTexture3(bottomRightFront, textureBottomRightFront); boxVertices[5] = new MyVertexFormatPositionTexture3(topRightFront, textureTopRightFront); // Back face. boxVertices[6] = new MyVertexFormatPositionTexture3(topLeftBack, textureTopLeftBack); boxVertices[7] = new MyVertexFormatPositionTexture3(topRightBack, textureTopRightBack); boxVertices[8] = new MyVertexFormatPositionTexture3(bottomLeftBack, textureBottomLeftBack); boxVertices[9] = new MyVertexFormatPositionTexture3(bottomLeftBack, textureBottomLeftBack); boxVertices[10] = new MyVertexFormatPositionTexture3(topRightBack, textureTopRightBack); boxVertices[11] = new MyVertexFormatPositionTexture3(bottomRightBack, textureBottomRightBack); // Top face. boxVertices[12] = new MyVertexFormatPositionTexture3(topLeftFront, textureTopLeftFront); boxVertices[13] = new MyVertexFormatPositionTexture3(topRightBack, textureTopRightBack); boxVertices[14] = new MyVertexFormatPositionTexture3(topLeftBack, textureTopLeftBack); boxVertices[15] = new MyVertexFormatPositionTexture3(topLeftFront, textureTopLeftFront); boxVertices[16] = new MyVertexFormatPositionTexture3(topRightFront, textureTopRightFront); boxVertices[17] = new MyVertexFormatPositionTexture3(topRightBack, textureTopRightBack); // Bottom face. boxVertices[18] = new MyVertexFormatPositionTexture3(bottomLeftFront, textureBottomLeftFront); boxVertices[19] = new MyVertexFormatPositionTexture3(bottomLeftBack, textureBottomLeftBack); boxVertices[20] = new MyVertexFormatPositionTexture3(bottomRightBack, textureBottomRightBack); boxVertices[21] = new MyVertexFormatPositionTexture3(bottomLeftFront, textureBottomLeftFront); boxVertices[22] = new MyVertexFormatPositionTexture3(bottomRightBack, textureBottomRightBack); boxVertices[23] = new MyVertexFormatPositionTexture3(bottomRightFront, textureBottomRightFront); // Left face. boxVertices[24] = new MyVertexFormatPositionTexture3(topLeftFront, textureTopLeftFront); boxVertices[25] = new MyVertexFormatPositionTexture3(bottomLeftBack, textureBottomLeftBack); boxVertices[26] = new MyVertexFormatPositionTexture3(bottomLeftFront, textureBottomLeftFront); boxVertices[27] = new MyVertexFormatPositionTexture3(topLeftBack, textureTopLeftBack); boxVertices[28] = new MyVertexFormatPositionTexture3(bottomLeftBack, textureBottomLeftBack); boxVertices[29] = new MyVertexFormatPositionTexture3(topLeftFront, textureTopLeftFront); // Right face. boxVertices[30] = new MyVertexFormatPositionTexture3(topRightFront, textureTopRightFront); boxVertices[31] = new MyVertexFormatPositionTexture3(bottomRightFront, textureBottomRightFront); boxVertices[32] = new MyVertexFormatPositionTexture3(bottomRightBack, textureBottomRightBack); boxVertices[33] = new MyVertexFormatPositionTexture3(topRightBack, textureTopRightBack); boxVertices[34] = new MyVertexFormatPositionTexture3(topRightFront, textureTopRightFront); boxVertices[35] = new MyVertexFormatPositionTexture3(bottomRightBack, textureBottomRightBack); // if we've loaded the cube from DDS, orient it towards the sun for (int i = 0; i < boxVertices.Length; i++) { boxVertices[i].Position = Vector3.Transform(boxVertices[i].Position, m_backgroundOrientation); } m_boxVertexBuffer = new VertexBuffer(MyRender.GraphicsDevice, MyVertexFormatPositionTexture3.Stride * boxVertices.Length, Usage.WriteOnly, VertexFormat.None, Pool.Default); m_boxVertexBuffer.SetData(boxVertices); m_boxVertexBuffer.DebugName = "BackgroundCube"; UpdateTexture(); m_backgroundOrientationDirty = false; m_loaded = true; }