Ejemplo n.º 1
0
        public QuadRenderer(IRenderer renderer)
        {
            mesh = new Mesh();

            var vertexFormat = new VertexFormat();

            position = vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3));
            texcoord = vertexFormat.Add(new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2));
            color    = vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 4));
            // Some gfx cards fail to show last vertex right if texcoord is set to have 3 components

            vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.StaticDraw);
            indexBuffer  = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.StaticDraw);

            mesh.VertexBufferRange = vertexBufferRange = vertexBuffer.CreateVertexBufferRange();

            indexBufferRange = mesh.FindOrCreateIndexBufferRange(
                MeshMode.PolygonFill,
                indexBuffer,
                BeginMode.Triangles
                );

            vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange);
            indexWriter  = new IndexBufferWriter(indexBufferRange);
        }
Ejemplo n.º 2
0
        public TextBuffer(FontStyle fontStyle)
        {
            this.fontStyle = fontStyle;

            mesh = new Mesh.Mesh();

            var vertexFormat = new VertexFormat();

            vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3));
            vertexFormat.Add(new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2));
            vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 3));

            vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.DynamicDraw);
            indexBuffer  = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.StaticDraw);

            mesh.VertexBufferRange = vertexBuffer.CreateVertexBufferRange();
            var indexBufferRange = mesh.FindOrCreateIndexBufferRange(
                MeshMode.PolygonFill,
                indexBuffer,
                BeginMode.Triangles
                );

            vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange);
            indexWriter  = new IndexBufferWriter(indexBufferRange);
        }
            public TreeTemplate(
                int sliceCount,
                int stackDivision,
                int coneCount,
                float height,
                float radius,
                float radAdd
                )
            {
                var          attributePosition = new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3);
                var          attributeNormal   = new Attribute(VertexUsage.Normal, VertexAttribPointerType.Float, 0, 3); /*  content normals     */
                VertexFormat vertexFormat      = new VertexFormat();

                vertexFormat.Add(attributePosition);
                vertexFormat.Add(attributeNormal);

                this.sliceCount    = sliceCount;
                this.stackDivision = stackDivision;
                this.coneCount     = coneCount;
                this.height        = height;
                this.radius        = radius;
                this.radAdd        = radAdd;
                float   coneHeight = height / (float)coneCount;
                Matrix4 rotZ       = Matrix4.CreateRotation(
                    RenderStack.Math.Conversions.DegreesToRadians(90.0f),
                    Vector3.UnitZ
                    );
                float    cylHeight        = coneHeight;
                float    cylRadius        = height / 20.0f;
                Geometry cylinderGeometry = new RenderStack.Geometry.Shapes.Cylinder(-cylHeight, cylHeight, cylRadius, sliceCount);

                cylinderGeometry.Transform(rotZ);
                GeometryMesh cylinderMesh  = new GeometryMesh(cylinderGeometry, NormalStyle.CornerNormals, vertexFormat);
                Shape        cylinderShape = new CylinderShape(cylHeight, cylRadius);

                cylinderMesh.GetMesh.Name = "cylinder";
                meshes.Add(cylinderMesh);
                shapes.Add(cylinderShape);
                for (int c = 0; c < coneCount; c++)
                {
                    float    topRadius      = (coneCount - 1 - c) * radius / (float)coneCount;
                    float    bottomRadius   = topRadius + radAdd;
                    float    R              = bottomRadius;
                    float    r              = topRadius;
                    float    fullConeHeight = (R * coneHeight) / (R - r);
                    float    minX           = -fullConeHeight / 3.0f;
                    float    maxX           = 2.0f * fullConeHeight / 3.0f;
                    float    offset         = -minX;
                    Geometry coneGeometry   = new RenderStack.Geometry.Shapes.Cone(minX, maxX, bottomRadius, 0.0f, true, true, sliceCount, stackDivision);
                    coneGeometry.Transform(rotZ);
                    GeometryMesh coneMesh  = new GeometryMesh(coneGeometry, NormalStyle.CornerNormals, vertexFormat);
                    Shape        coneShape = new ConeShape(fullConeHeight, R);
                    coneMesh.GetMesh.Name = "cone" + c.ToString();
                    meshes.Add(coneMesh);
                    shapes.Add(coneShape);
                }
            }
Ejemplo n.º 4
0
        //private BufferRange indexBufferRange;

        protected override void  InitializeService()
        {
            //basic = materialManager["Basic"];
            basic = materialManager.MakeMaterial("Basic");
            Image terrainImage = new Image("res/images/terrain.png");

            // \todo support material textures
            basic.Textures["t_surface_color"] = materialManager.Textures["terrain"] = new TextureGL(terrainImage);

            blockTypes[BlockType.Grass]       = new BlockType(0, 0, 2, 0, 3, 0, 3, 0, 3, 0, 3, 0);
            blockTypes[BlockType.Stone]       = new BlockType(1, 0);
            blockTypes[BlockType.Dirt]        = new BlockType(2, 0);
            blockTypes[BlockType.Cobblestone] = new BlockType(0, 1);
            blockTypes[BlockType.Bedrock]     = new BlockType(1, 1);
            blockTypes[BlockType.Wood]        = new BlockType(5, 1, 5, 1, 4, 1, 4, 1, 4, 1, 4, 1);
            blockTypes[BlockType.Sand]        = new BlockType(2, 1);
            //   0, 0 grass (in gray)
            //   1, 0 stone
            //   2, 0 dirt
            //   3, 0 grass left, right, front, back
            //   4, 0 wooden planks
            //   5, 0 stone slab sides
            //   6, 0 stone slab top, bottom
            //   7, 0 brick
            //   8, 0 tnt sides
            //   9, 0 tnt top
            //  10, 0 tnt bottom
            //  11, 0 spider web
            //  12, 0 red flower
            //  13, 0 yellow flower
            //  14, 0 water?
            //  15, 0 sapling
            //   0, 1 cobble stone
            //   1, 1 bedrock
            //   2, 1 sand
            //   3, 1 gravel
            //   4, 1 log sidem
            //   5, 1 log top, bottom
            //   6, 1 iron block
            //   7, 1 gold block
            //   8, 1 diamond block
            //   9, 1 chest top, bottom
            //  10, 1 chest front, left, right
            //  11, 1 chest back
            //  12, 1

            var vertexFormat = new VertexFormat();

            position = vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3));
            texcoord = vertexFormat.Add(new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2));
            //color = vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Byte, 0, 4, true));
            color = vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 4));
            // Some gfx cards fail to show last vertex right if texcoord is set to have 3 components

            vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.StaticDraw);
            indexBuffer  = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.StaticDraw);
        }
Ejemplo n.º 5
0
        public NinePatch(NinePatchStyle style)
        {
            this.style = style;

            //mesh = new Mesh.Mesh(BufferUsageHint.DynamicDraw);
            mesh = new RenderStack.Mesh.Mesh();

            VertexFormat vertexFormat = new VertexFormat();

            vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3));
            vertexFormat.Add(new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2));

            // \todo Allocate vertex buffers form from UI BufferPool and use double buffered Buffers
            // \todo Share one index buffer among all UI components that have the same index buffer
            //Buffer vertexBuffer = BufferPool.Instance.GetVertexBuffer(vertexFormat, BufferUsageHint.DynamicDraw);
            //Buffer indexBuffer = BufferPool.Instance.GetIndexBuffer(DrawElementsType.UnsignedShort, BufferUsageHint.StaticDraw);
            vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.DynamicDraw);
            indexBuffer  = BufferFactory.Create(DrawElementsType.UnsignedShort, BufferUsageHint.StaticDraw);

            mesh.VertexBufferRange = vertexBuffer.CreateVertexBufferRange();
            IBufferRange indexBufferRange = mesh.FindOrCreateIndexBufferRange(
                MeshMode.PolygonFill,
                indexBuffer,
                BeginMode.Triangles
                );
            var writer = new IndexBufferWriter(indexBufferRange);

            vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange);

            //  12 13 14 15
            //
            //   8  9 10 11
            //
            //   4  5  6  7
            //
            //   0  1  2  3

            writer.BeginEdit();

            writer.Quad(4, 5, 1, 0); writer.CurrentIndex += 6;
            writer.Quad(5, 6, 2, 1); writer.CurrentIndex += 6;
            writer.Quad(6, 7, 3, 2); writer.CurrentIndex += 6;

            writer.Quad(8, 9, 5, 4); writer.CurrentIndex   += 6;
            writer.Quad(9, 10, 6, 5); writer.CurrentIndex  += 6;
            writer.Quad(10, 11, 7, 6); writer.CurrentIndex += 6;

            writer.Quad(12, 13, 9, 8); writer.CurrentIndex   += 6;
            writer.Quad(13, 14, 10, 9); writer.CurrentIndex  += 6;
            writer.Quad(14, 15, 11, 10); writer.CurrentIndex += 6;

            writer.EndEdit();

            // \bug
            //indexBuffer.UpdateAll();
        }
Ejemplo n.º 6
0
        public void BuildMeshFromGeometry(
            BufferUsageHint bufferUsageHint,
            NormalStyle normalStyle
            )
        {
            var attributePosition     = new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3);
            var attributeNormal       = new Attribute(VertexUsage.Normal, VertexAttribPointerType.Float, 0, 3);       /*  content normals     */
            var attributeNormalFlat   = new Attribute(VertexUsage.Normal, VertexAttribPointerType.Float, 1, 3);       /*  flat normals        */
            var attributeNormalSmooth = new Attribute(VertexUsage.Normal, VertexAttribPointerType.Float, 2, 3);       /*  smooth normals      */
            var attributeColor        = new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 4);
            var attributeIdVec3       = new Attribute(VertexUsage.Id, VertexAttribPointerType.Float, 0, 3);
            var attributeIdUInt       = (RenderStack.Graphics.Configuration.useIntegerPolygonIDs)
                                        ? new Attribute(VertexUsage.Id, VertexAttribPointerType.UnsignedInt, 0, 1)
                                        : null;

            VertexFormat vertexFormat = new VertexFormat();

            vertexFormat.Add(attributePosition);
            vertexFormat.Add(attributeNormal);
            vertexFormat.Add(attributeNormalFlat);
            vertexFormat.Add(attributeNormalSmooth);

            Dictionary <Corner, Vector2> cornerTexcoords = null;
            Dictionary <Point, Vector2>  pointTexcoords  = null;

            if (Geometry.CornerAttributes.Contains <Vector2>("corner_texcoords"))
            {
                cornerTexcoords = Geometry.CornerAttributes.Find <Vector2>("corner_texcoords");
            }
            if (Geometry.PointAttributes.Contains <Vector2>("point_texcoords"))
            {
                pointTexcoords = Geometry.PointAttributes.Find <Vector2>("point_texcoords");
            }

            if (cornerTexcoords != null || pointTexcoords != null)
            {
                var attributeTexcoord = new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2);
                vertexFormat.Add(attributeTexcoord);
            }
            // \todo When do we want color attribute and when we don't?
            //if(cornerColors != null || pointColors != null)
            //{
            vertexFormat.Add(attributeColor);
            //}
            vertexFormat.Add(attributeIdVec3);
            if (RenderStack.Graphics.Configuration.useIntegerPolygonIDs)
            {
                vertexFormat.Add(attributeIdUInt);
            }

            BuildMeshFromGeometry(bufferUsageHint, normalStyle, vertexFormat);
        }
Ejemplo n.º 7
0
        protected override void InitializeService()
        {
            if (
                (RenderStack.Graphics.Configuration.canUseGeometryShaders) &&
                (RenderStack.Graphics.Configuration.glslVersion >= 330)
                )
            {
                material = new Material("", renderer.Programs["WideLine"], renderer.MaterialUB);
            }
            else
            {
                material = new Material("", renderer.Programs["ColorFill"], renderer.MaterialUB);
            }

            LineWidth = 1.0f;

            mesh = new Mesh();

            var vertexFormat = new VertexFormat();

            position  = vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3));
            edgeColor = vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 1, 4));

            vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.DynamicDraw);
            indexBuffer  = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.DynamicDraw);

            mesh.VertexBufferRange = vertexBufferRange = vertexBuffer.CreateVertexBufferRange();
            indexBufferRange       = mesh.FindOrCreateIndexBufferRange(
                MeshMode.EdgeLines,
                indexBuffer,
                BeginMode.Lines
                );

            vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange);
            indexWriter  = new IndexBufferWriter(indexBufferRange);
        }
Ejemplo n.º 8
0
        public CurveTube(ICurve curve)
        {
            this.curve = curve;

            PointMesh = new Mesh();
            LineMesh  = new Mesh();
            Line2Mesh = new Mesh();
            TubeMesh  = new Mesh();

            var positionColor    = new VertexFormat(); // pointMesh, lineMesh, line2Mesh
            var tubeVertexFormat = new VertexFormat();

            positionColor.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3));
            positionColor.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 4));
            tubeVertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3));
            tubeVertexFormat.Add(new Attribute(VertexUsage.Tangent, VertexAttribPointerType.Float, 0, 3));
            tubeVertexFormat.Add(new Attribute(VertexUsage.Normal, VertexAttribPointerType.Float, 0, 3));
            tubeVertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 4));
            tubeVertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 1, 1));
            tubeVertexFormat.Add(new Attribute(VertexUsage.Id, VertexAttribPointerType.UnsignedInt, 0, 1));

            //  Could perhaps use BufferPool
            var positionColorVertexBuffer = BufferFactory.Create(positionColor, BufferUsageHint.DynamicDraw);
            var tubeVertexBuffer          = BufferFactory.Create(tubeVertexFormat, BufferUsageHint.DynamicDraw);
            var indexBuffer = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.DynamicDraw);

            PointMesh.VertexBufferRange = positionColorVertexBuffer.CreateVertexBufferRange();
            LineMesh.VertexBufferRange  = positionColorVertexBuffer.CreateVertexBufferRange();
            Line2Mesh.VertexBufferRange = positionColorVertexBuffer.CreateVertexBufferRange();
            TubeMesh.VertexBufferRange  = tubeVertexBuffer.CreateVertexBufferRange();

            var pointIndexBuffer = PointMesh.FindOrCreateIndexBufferRange(
                MeshMode.CornerPoints,
                indexBuffer,
                BeginMode.Points
                );
            var lineIndexBuffer = LineMesh.FindOrCreateIndexBufferRange(
                MeshMode.EdgeLines,
                indexBuffer,
                BeginMode.Lines
                );
            var line2IndexBuffer = Line2Mesh.FindOrCreateIndexBufferRange(
                MeshMode.EdgeLines,
                indexBuffer,
                BeginMode.Lines
                );
            var tubeIndexBuffer = TubeMesh.FindOrCreateIndexBufferRange(
                MeshMode.PolygonFill,
                indexBuffer,
                BeginMode.Triangles
                );

            tubeVertexWriter  = new VertexBufferWriter(TubeMesh.VertexBufferRange);
            tubeIndexWriter   = new IndexBufferWriter(tubeIndexBuffer);
            pointVertexWriter = new VertexBufferWriter(PointMesh.VertexBufferRange);
            pointIndexWriter  = new IndexBufferWriter(pointIndexBuffer);
            lineVertexWriter  = new VertexBufferWriter(LineMesh.VertexBufferRange);
            lineIndexWriter   = new IndexBufferWriter(lineIndexBuffer);
            line2VertexWriter = new VertexBufferWriter(Line2Mesh.VertexBufferRange);
            line2IndexWriter  = new IndexBufferWriter(line2IndexBuffer);

            UpdateIndexBuffers();
        }