예제 #1
0
            static GraphicsMemoryRegion <GraphicsResource> CreateVertexBufferRegion(GraphicsContext graphicsContext, GraphicsBuffer vertexBuffer, GraphicsBuffer vertexStagingBuffer, float aspectRatio)
            {
                var vertexBufferRegion = vertexBuffer.Allocate(SizeOf <Texture3DVertex>() * 4, alignment: 16);
                var pVertexBuffer      = vertexStagingBuffer.Map <Texture3DVertex>(in vertexBufferRegion);

                pVertexBuffer[0] = new Texture3DVertex {             //
                    Position = new Vector3(-0.5f, 0.5f, 0.0f),       //   y          in this setup
                    UVW      = new Vector3(0, 1, 0.5f),              //   ^     z    the origin o
                };                                                   //   |   /      is in the middle
                                                                     //   | /        of the rendered scene
                pVertexBuffer[1] = new Texture3DVertex {             //   o------>x
                    Position = new Vector3(0.5f, 0.5f, 0.0f),        //
                    UVW      = new Vector3(1, 1, 0.5f),              //   0 ----- 1
                };                                                   //   | \     |
                                                                     //   |   \   |
                pVertexBuffer[2] = new Texture3DVertex {             //   |     \ |
                    Position = new Vector3(0.5f, -0.5f, 0.0f),       //   3-------2
                    UVW      = new Vector3(1, 0, 0.5f),              //
                };

                pVertexBuffer[3] = new Texture3DVertex {
                    Position = new Vector3(-0.5f, -0.5f, 0.0f),
                    UVW      = new Vector3(0, 0, 0.5f),
                };

                vertexStagingBuffer.UnmapAndWrite(in vertexBufferRegion);
                return(vertexBufferRegion);
            }
예제 #2
0
            static GraphicsMemoryRegion <GraphicsResource> CreateVertexBufferRegion(GraphicsContext graphicsContext, GraphicsBuffer vertexBuffer, GraphicsBuffer vertexStagingBuffer, List <Vector3> vertices, List <Vector3> normals)
            {
                var vertexBufferRegion = vertexBuffer.Allocate(SizeOf <PosNormTex3DVertex>() * (uint)vertices.Count, alignment: 16);
                var pVertexBuffer      = vertexStagingBuffer.Map <PosNormTex3DVertex>(in vertexBufferRegion);

                // assumes the vertices are in a box from (-1,-1,-1) to (1,1,1)

                var offset3D = new Vector3(1, 1, 1);          // to move lower left corner to (0,0,0)
                var scale3D  = new Vector3(0.5f, 0.5f, 0.5f); // to scale to side length 1

                for (var i = 0; i < vertices.Count; i++)
                {
                    var xyz    = vertices[i];                // position
                    var normal = normals[i];                 // normal
                    var uvw    = (xyz + offset3D) * scale3D; // texture coordinate

                    pVertexBuffer[i] = new PosNormTex3DVertex {
                        Position = xyz,
                        Normal   = normal,
                        UVW      = uvw
                    };
                }

                vertexStagingBuffer.UnmapAndWrite(in vertexBufferRegion);
                return(vertexBufferRegion);
            }
예제 #3
0
            static GraphicsMemoryRegion <GraphicsResource> CreateConstantBufferRegion(GraphicsContext graphicsContext, GraphicsBuffer constantBuffer)
            {
                var constantBufferRegion = constantBuffer.Allocate(SizeOf <Matrix4x4>(), alignment: 256);
                var pConstantBuffer      = constantBuffer.Map <Matrix4x4>(in constantBufferRegion);

                pConstantBuffer[0] = Matrix4x4.Identity;

                constantBuffer.UnmapAndWrite(in constantBufferRegion);
                return(constantBufferRegion);
            }
예제 #4
0
            static GraphicsMemoryRegion <GraphicsResource> CreateIndexBufferRegion(GraphicsContext graphicsContext, GraphicsBuffer indexBuffer, GraphicsBuffer indexStagingBuffer, List <uint> indices)
            {
                var indexBufferRegion = indexBuffer.Allocate(SizeOf <uint>() * (uint)indices.Count, alignment: 4);
                var pIndexBuffer      = indexStagingBuffer.Map <uint>(in indexBufferRegion);

                for (var i = 0; i < indices.Count; i++)
                {
                    pIndexBuffer[i] = indices[i];
                }

                indexStagingBuffer.UnmapAndWrite(in indexBufferRegion);
                return(indexBufferRegion);
            }
예제 #5
0
            static GraphicsMemoryRegion <GraphicsResource> CreateIndexBufferRegion(GraphicsContext graphicsContext, GraphicsBuffer indexBuffer, GraphicsBuffer indexStagingBuffer)
            {
                var indexBufferRegion = indexBuffer.Allocate(SizeOf <ushort>() * 6, alignment: 2);
                var pIndexBuffer      = indexStagingBuffer.Map <ushort>(in indexBufferRegion);

                // clockwise when looking at the triangle from the outside

                pIndexBuffer[0] = 0;
                pIndexBuffer[1] = 1;
                pIndexBuffer[2] = 2;

                pIndexBuffer[3] = 0;
                pIndexBuffer[4] = 2;
                pIndexBuffer[5] = 3;

                indexStagingBuffer.UnmapAndWrite(in indexBufferRegion);
                return(indexBufferRegion);
            }
예제 #6
0
            static GraphicsMemoryRegion <GraphicsResource> CreateVertexBufferRegion(GraphicsContext graphicsContext, GraphicsBuffer vertexBuffer, GraphicsBuffer vertexStagingBuffer, float aspectRatio)
            {
                var vertexBufferRegion = vertexBuffer.Allocate(SizeOf <TextureVertex>() * 3, alignment: 16);
                var pVertexBuffer      = vertexStagingBuffer.Map <TextureVertex>(in vertexBufferRegion);

                pVertexBuffer[0] = new TextureVertex {
                    Position = new Vector3(0.0f, 0.25f * aspectRatio, 0.0f),
                    UV       = new Vector2(1.0f, 0.0f)
                };

                pVertexBuffer[1] = new TextureVertex {
                    Position = new Vector3(0.25f, -0.25f * aspectRatio, 0.0f),
                    UV       = new Vector2(0.0f, 1.0f)
                };

                pVertexBuffer[2] = new TextureVertex {
                    Position = new Vector3(-0.25f, -0.25f * aspectRatio, 0.0f),
                    UV       = new Vector2(0.0f, 0.0f)
                };

                vertexStagingBuffer.UnmapAndWrite(in vertexBufferRegion);
                return(vertexBufferRegion);
            }