예제 #1
0
        public Q3BSPSkybox(float length, int tessNumber, GraphicsDevice graphicsDevice, Q3BSPShaderManager shaderManager)
        {
            if (tessNumber < 2)
            {
                tessNumber = 2;
            }

            float halfLength = length / 2;

            int verticesOnEdges = tessNumber - 2;
            int verticesOnFaces = verticesOnEdges * verticesOnEdges;
            // # on corners + # on one edge * 12 + # on one face * 6
            int numberOfVertices = 8 + verticesOnEdges * 12 + verticesOnFaces * 6;

            int[,,] indexMatrices = new int[6, tessNumber, tessNumber];

            #region Build Skybox Geometry
            vertices = new VertexPosition[numberOfVertices];
            indices  = new short[(tessNumber - 1) * (tessNumber - 1) * 2 * 3 * 6];
            vertices.Initialize();

            // Top: 0-3-2-1     Bottom: 4-7-6-5
            // Front: 0-4-5-1   Back: 3-7-6-2
            // Left: 0-3-7-4    Right: 1-2-6-5

            #region Corners
            vertices[0].Position = new Vector3(halfLength, halfLength, halfLength);
            vertices[1].Position = new Vector3(-halfLength, halfLength, halfLength);
            vertices[2].Position = new Vector3(-halfLength, halfLength, -halfLength);
            vertices[3].Position = new Vector3(halfLength, halfLength, -halfLength);

            vertices[4].Position = new Vector3(halfLength, -halfLength, halfLength);
            vertices[5].Position = new Vector3(-halfLength, -halfLength, halfLength);
            vertices[6].Position = new Vector3(-halfLength, -halfLength, -halfLength);
            vertices[7].Position = new Vector3(halfLength, -halfLength, -halfLength);

            // Fill out index matrices for these corners
            int end = verticesOnEdges + 1;

            // Top
            indexMatrices[0, 0, 0]     = 0;
            indexMatrices[0, 0, end]   = 3;
            indexMatrices[0, end, end] = 2;
            indexMatrices[0, end, 0]   = 1;
            // Front
            indexMatrices[1, 0, 0]     = 0;
            indexMatrices[1, 0, end]   = 4;
            indexMatrices[1, end, end] = 5;
            indexMatrices[1, end, 0]   = 1;
            // Left
            indexMatrices[2, 0, 0]     = 0;
            indexMatrices[2, 0, end]   = 3;
            indexMatrices[2, end, end] = 7;
            indexMatrices[2, end, 0]   = 4;

            // Bottom
            indexMatrices[3, 0, 0]     = 4;
            indexMatrices[3, 0, end]   = 7;
            indexMatrices[3, end, end] = 6;
            indexMatrices[3, end, 0]   = 5;
            // Back
            indexMatrices[4, 0, 0]     = 3;
            indexMatrices[4, 0, end]   = 7;
            indexMatrices[4, end, end] = 6;
            indexMatrices[4, end, 0]   = 2;
            // Right
            indexMatrices[5, 0, 0]     = 1;
            indexMatrices[5, 0, end]   = 2;
            indexMatrices[5, end, end] = 6;
            indexMatrices[5, end, 0]   = 5;

            #endregion

            #region Edges
            BuildEdgeVertexPositions(0, 1, 8 + (0 * verticesOnEdges), tessNumber, length, ref indexMatrices);
            BuildEdgeVertexPositions(1, 2, 8 + (1 * verticesOnEdges), tessNumber, length, ref indexMatrices);
            BuildEdgeVertexPositions(3, 2, 8 + (2 * verticesOnEdges), tessNumber, length, ref indexMatrices);
            BuildEdgeVertexPositions(0, 3, 8 + (3 * verticesOnEdges), tessNumber, length, ref indexMatrices);

            BuildEdgeVertexPositions(0, 4, 8 + (4 * verticesOnEdges), tessNumber, length, ref indexMatrices);
            BuildEdgeVertexPositions(1, 5, 8 + (5 * verticesOnEdges), tessNumber, length, ref indexMatrices);
            BuildEdgeVertexPositions(2, 6, 8 + (6 * verticesOnEdges), tessNumber, length, ref indexMatrices);
            BuildEdgeVertexPositions(3, 7, 8 + (7 * verticesOnEdges), tessNumber, length, ref indexMatrices);

            BuildEdgeVertexPositions(4, 5, 8 + (8 * verticesOnEdges), tessNumber, length, ref indexMatrices);
            BuildEdgeVertexPositions(5, 6, 8 + (9 * verticesOnEdges), tessNumber, length, ref indexMatrices);
            BuildEdgeVertexPositions(7, 6, 8 + (10 * verticesOnEdges), tessNumber, length, ref indexMatrices);
            BuildEdgeVertexPositions(4, 7, 8 + (11 * verticesOnEdges), tessNumber, length, ref indexMatrices);
            #endregion

            #region Faces
            BuildFaceVertexPositions(0, 8 + (12 * verticesOnEdges) + (0 * verticesOnFaces), tessNumber, length, indexMatrices);
            BuildFaceVertexPositions(1, 8 + (12 * verticesOnEdges) + (1 * verticesOnFaces), tessNumber, length, indexMatrices);
            BuildFaceVertexPositions(2, 8 + (12 * verticesOnEdges) + (2 * verticesOnFaces), tessNumber, length, indexMatrices);

            BuildFaceVertexPositions(3, 8 + (12 * verticesOnEdges) + (3 * verticesOnFaces), tessNumber, length, indexMatrices);
            BuildFaceVertexPositions(4, 8 + (12 * verticesOnEdges) + (4 * verticesOnFaces), tessNumber, length, indexMatrices);
            BuildFaceVertexPositions(5, 8 + (12 * verticesOnEdges) + (5 * verticesOnFaces), tessNumber, length, indexMatrices);
            #endregion

            BuildIndexBuffer(indexMatrices);
            #endregion

            this.vertexDeclaration = new VertexDeclaration(graphicsDevice, VertexPosition.VertexElements);
            this.shaderManager     = shaderManager;
        }
예제 #2
0
        /// <param name="shaderPath">Path to the directory that contains all compiled .shaders. Is relative to the Content.BasePath directory. Do not include a leading '/'.</param>
        public bool InitializeLevel(GraphicsDevice graphics, ContentManager content, string shaderPath, string quakePath)
        {
            bool bSuccess = true;

            lightMapManager = new Q3BSPLightMapManager();
            bSuccess        = lightMapManager.GenerateLightMaps(lightMapData, graphics);

            if (bSuccess)
            {
                shaderManager            = new Q3BSPShaderManager();
                shaderManager.ShaderPath = shaderPath;
                shaderManager.QuakePath  = quakePath;
                bSuccess = shaderManager.LoadTextures(textureData, graphics, content);
            }

            if (bSuccess && shaderManager.SkyMaterial != null)
            {
                skybox = new Q3BSPSkybox(skyboxSize, skyboxTessellation, graphics, shaderManager);
            }

            if (bSuccess)
            {
                shaderManager.LightMapManager = lightMapManager;
            }

            levelInitialized = bSuccess;

            if (renderType == Q3BSPRenderType.StaticBuffer)
            {
                InitializeStatic(graphics);
            }

            else if (renderType == Q3BSPRenderType.BSPCulling)
            {
                Q3BSPFace[] tempFaces = new Q3BSPFace[faces.Length];
                faces.CopyTo(tempFaces, 0);

                System.Array.Sort(tempFaces, new Q3BSPFaceComparer());

                int indexCount        = 0;
                int lastTextureIndex  = 0;
                int lastLightMapIndex = 0;
                this.maximumNumberOfIndicesToDraw = 0;

                foreach (Q3BSPFace face in tempFaces)
                {
                    // The current index buffer is done and needs to be refreshed.
                    if ((face.TextureIndex != lastTextureIndex || face.LightMapIndex != lastLightMapIndex))
                    {
                        if (face.TextureIndex == 4)
                        {
                            indexCount += 0;
                        }
                        if (indexCount > maximumNumberOfIndicesToDraw)
                        {
                            maximumNumberOfIndicesToDraw = indexCount;
                        }
                        indexCount = 0;
                    }

                    lastTextureIndex  = face.TextureIndex;
                    lastLightMapIndex = face.LightMapIndex;
                    indexCount       += face.MeshVertexCount;
                }

                if (indexCount > maximumNumberOfIndicesToDraw)
                {
                    maximumNumberOfIndicesToDraw = indexCount;
                }
            }

            bspLogger.WriteLine("Level initialized: " + levelInitialized.ToString());
            return(levelInitialized);
        }