public TerrainPatchBuilder( )
        {
            int numVertices = 0;
            for ( int level = 0; level < MaxLodLevels; ++level )
            {
                numVertices += LevelPoolSize( level ) * NumberOfLevelVertices( level );
            }

            VertexBufferFormat format = new VertexBufferFormat( );
            format.Add( VertexFieldSemantic.Position, VertexFieldElementTypeId.Float32, 3 );
            format.Add( VertexFieldSemantic.Normal, VertexFieldElementTypeId.Float32, 3 );

            GraphicsLog.Info( "Allocating terrain patch builder VBO: {0} vertices in format {1}", numVertices, format );

            m_Buffer = Graphics.Factory.CreateVertexBuffer( );
            m_Buffer.Create( format, numVertices );

            int curVertexIndex = 0;
            for ( int level = 0; level < MaxLodLevels; ++level )
            {
                Lod newLod = new Lod( );
                m_LodLevels[ level ] = newLod;

                int numLevelVertices = NumberOfLevelVertices( level );
                int poolSize = LevelPoolSize( level );
                for ( int poolIndex = 0; poolIndex < poolSize; ++poolIndex )
                {
                    newLod.VbPool.Add( curVertexIndex );
                    curVertexIndex += numLevelVertices;
                }
                //	NOTE: BP: BABY P SAYS HELLO
            }
        }
Example #2
0
        public ExampleLayer()
        {
            //Create camera
            cameraController = new OrthographicCameraController(1280.0f / 720.0f);

            //Shader library
            shaderLibrary = new ShaderLibrary();

            // ----------
            //Square
            // ----------
            squareVertexArray = IVertexArray.Create();

            float[] squareVertices =
            {
                -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
                0.5f,  -0.5f, 0.0f, 1.0f, 0.0f,
                0.5f,   0.5f, 0.0f, 1.0f, 1.0f,
                -0.5f,  0.5f, 0.0f, 0.0f, 1.0f
            };

            IVertexBuffer squareVertexBuffer = IVertexBuffer.Create(squareVertices, squareVertices.GetBytes());

            BufferLayout squareBufferLayout = new BufferLayout(new[]
            {
                new BufferElement("a_Position", ShaderDataType.Float3),
                new BufferElement("a_TexCoord", ShaderDataType.Float2)
            });

            squareVertexBuffer.SetLayout(squareBufferLayout);
            squareVertexArray.AddVertexBuffer(squareVertexBuffer);

            uint[]       squareIndices     = { 0, 1, 2, 2, 3, 0 };
            IIndexBuffer squareIndexBuffer =
                IIndexBuffer.Create(squareIndices, squareIndices.GetBytes() / sizeof(uint));

            squareVertexArray.SetIndexBuffer(squareIndexBuffer);

            //Square shader
            shaderLibrary.LoadAndAddShader("Shaders/Square.glsl");

            //Texture shader
            IShader textureShader = IShader.Create("Shaders/Texture.glsl");

            shaderLibrary.AddShader(textureShader);

            birdiTexture = I2DTexture.Create("Textures/Birdi.png");
            faceTexture  = I2DTexture.Create("Textures/Face.png");

            textureShader.Bind();
            textureShader.SetInt("u_Texture", 0);
        }
        public PatchGrid( Terrain terrain, int gridWidth, int gridHeight )
        {
            int highestRes = Patch.GetLevelResolution( Patch.HighestDetailLod );
            highestRes *= highestRes;

            VertexBufferFormat format = new VertexBufferFormat( );
            format.Add( VertexFieldSemantic.Position, VertexFieldElementTypeId.Float32, 3 );
            m_Vb = RbGraphics.Factory.CreateVertexBuffer( );
            m_Vb.Create( format, gridWidth * gridHeight * highestRes );

            m_Patches = new Patch[ gridWidth, gridHeight ];

            float z = -PatchDepth * ( gridHeight / 2 );
            float xInc = PatchWidth;
            float zInc = PatchDepth;

            float maxWidth = gridWidth * PatchWidth;
            float maxHeight = gridWidth * PatchDepth;

            terrain.SetTerrainArea( maxWidth, maxHeight );

            int vbOffset = 0;

            for ( int row = 0; row < gridHeight; ++row, z += zInc )
            {
                float x = -PatchWidth * ( gridWidth / 2 );
                for ( int col = 0; col < gridWidth; ++col, x += xInc )
                {
                    Color c = ( ( col + row ) % 2 ) == 0 ? Color.Black : Color.White;

                    m_Patches[ col, row ] = new Patch( terrain, vbOffset, x, z, PatchWidth, PatchDepth, c );
                    vbOffset += highestRes;
                }
            }

            int maxCol = gridWidth - 1;
            int maxRow = gridHeight - 1;
            for ( int row = 0; row < gridHeight; ++row )
            {
                for ( int col = 0; col < gridWidth; ++col )
                {
                    Patch left	= ( col == 0 ) ? null : ( m_Patches[ col - 1, row ] );
                    Patch right	= ( col == maxCol ) ? null : ( m_Patches[ col + 1, row ] );
                    Patch up	= ( row == 0 ) ? null : ( m_Patches[ col, row - 1] );
                    Patch down	= ( row == maxRow ) ? null : ( m_Patches[ col, row + 1 ] );

                    m_Patches[ col, row ].Link( left, right, up, down );
                }
            }
        }
        public QuadPatchVertices( )
        {
            VertexBufferFormat format = new VertexBufferFormat( );
            format.Add( VertexFieldSemantic.Position, VertexFieldElementTypeId.Float32, 3 );

            m_Vb = Graphics.Factory.CreateVertexBuffer( );
            m_Vb.Create( format, QuadPatch.PatchResolution * QuadPatch.PatchResolution * PoolSize );

            int vertexIndex = 0;
            for ( int i = 0; i < PoolSize; ++i )
            {
                m_FreeList.Add( vertexIndex );
                vertexIndex += QuadPatch.PatchResolution * QuadPatch.PatchResolution;
            }
        }
        /// <summary>
        /// Default constructor
        /// </summary>
        public TerrainPatchVertices( )
        {
            VertexBufferFormat format = new VertexBufferFormat( );
            format.Add( VertexFieldSemantic.Position, VertexFieldElementTypeId.Float32, 3 );
            format.Add( VertexFieldSemantic.Normal, VertexFieldElementTypeId.Float32, 3 );
            format.Add( VertexFieldSemantic.Texture0, VertexFieldElementTypeId.Float32, 2 );
            format.Add( VertexFieldSemantic.Texture1, VertexFieldElementTypeId.Float32, 2 );

            GraphicsLog.Info( "Creating terrain patch vertex pool using format {0}", format );

            m_Vb = Graphics.Factory.CreateVertexBuffer( );
            m_Vb.Create( format, TerrainPatchConstants.PatchTotalVertexCount * PoolSize );

            Clear( );
        }
Example #6
0
        /// <summary>
        /// Initializes the 2D rendering system
        /// </summary>
        internal static void Init()
        {
            ProfilerTimer.Profile(() =>
            {
                rendererData = new Renderer2DStorage
                {
                    QuadVertexArray = IVertexArray.Create()
                };

                float[] squareVertices =
                {
                    -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
                    0.5f,  -0.5f, 0.0f, 1.0f, 0.0f,
                    0.5f,   0.5f, 0.0f, 1.0f, 1.0f,
                    -0.5f,  0.5f, 0.0f, 0.0f, 1.0f
                };

                IVertexBuffer squareVertexBuffer = IVertexBuffer.Create(squareVertices, squareVertices.GetBytes());

                BufferLayout squareBufferLayout = new BufferLayout(new[]
                {
                    new BufferElement("a_Position", ShaderDataType.Float3),
                    new BufferElement("a_TexCoord", ShaderDataType.Float2)
                });
                squareVertexBuffer.SetLayout(squareBufferLayout);
                rendererData.QuadVertexArray.AddVertexBuffer(squareVertexBuffer);

                uint[] squareIndices           = { 0, 1, 2, 2, 3, 0 };
                IIndexBuffer squareIndexBuffer =
                    IIndexBuffer.Create(squareIndices, squareIndices.GetBytes() / sizeof(uint));
                rendererData.QuadVertexArray.SetIndexBuffer(squareIndexBuffer);

                rendererData.WhiteTexture = I2DTexture.Create(1, 1);
                uint whiteTextureData     = 0xffffffff;
                rendererData.WhiteTexture.SetData(whiteTextureData, sizeof(uint));

                rendererData.TextureShader = IShader.Create("Shaders/Texture.glsl");
                rendererData.TextureShader.Bind();
                rendererData.TextureShader.SetInt("u_Texture", 0);
            });
        }
        /// <summary>
        /// Rebuilds the ring goemetry
        /// </summary>
        private bool RebuildRingGeometry( )
        {
            DestroyVertices( );
            ISpherePlanetRingModel model = Planet.Model.GetModel<ISpherePlanetRingModel>( );
            if ( model == null )
            {
                return false;
            }

            int subdivisionCount = 256;

            List<RingVertex> vertices = new List<RingVertex>( );

            m_BuildInnerRadius = model.InnerRadius;
            m_BuildWidth = model.Width;

            float innerRadius = ( float )model.InnerRadius.ToAstroRenderUnits;
            float outerRadius = ( float )( model.InnerRadius + model.Width ).ToAstroRenderUnits;
            float angle = 0;
            float angleInc = Constants.TwoPi / ( subdivisionCount - 1 );
            bool toggle = false;
            for ( int subdivision = 0; subdivision < subdivisionCount; ++subdivision )
            {
                float x = Functions.Sin( angle );
                float y = 0;
                float z = Functions.Cos( angle );

                vertices.Add( new RingVertex( x * innerRadius, y * innerRadius, z * innerRadius, toggle ? 0 : 1, 0 ) );
                vertices.Add( new RingVertex( x * outerRadius, y * outerRadius, z * outerRadius, toggle ? 0 : 1, 1 ) );

                toggle = !toggle;
                angle += angleInc;
            }

            m_Vertices = Graphics.Factory.CreateVertexBuffer( );
            m_Vertices.Create( vertices.ToArray( ) );

            return true;
        }
        private void BuildBuffers( int maxParticles )
        {
            m_MaxParticles = maxParticles;
            m_Vb = Graphics.Factory.CreateVertexBuffer( );

            VertexBufferFormat vbFormat = new VertexBufferFormat( );
            vbFormat.Add( VertexFieldSemantic.Position, VertexFieldElementTypeId.Float32, 3 );
            vbFormat.Add( VertexFieldSemantic.Texture0, VertexFieldElementTypeId.Float32, 2 );
            vbFormat.Static = false;
            m_Vb.Create( vbFormat, maxParticles * 4 );

            m_Ib = Graphics.Factory.CreateIndexBuffer( );

            ushort[] indices = new ushort[ maxParticles * 6 ];
            int curIndex = 0;
            ushort indexValue = 0;
            for ( int particleCount = 0; particleCount < maxParticles; ++particleCount )
            {
                indices[ curIndex++ ] = indexValue;
                indices[ curIndex++ ] = ( ushort )( indexValue + 1 );
                indices[ curIndex++ ] = ( ushort )( indexValue + 3 );

                indices[ curIndex++ ] = ( ushort )( indexValue + 1 );
                indices[ curIndex++ ] = ( ushort )( indexValue + 2 );
                indices[ curIndex++ ] = ( ushort )( indexValue + 3 );

                indexValue += 4;
            }
            m_Ib.Create( indices, true );
        }