Beispiel #1
0
        public static Geometry GenerateGeometry(GeometryData geometryData,
                                                PrimitiveType primitiveType = PrimitiveType.Triangles)
        {
            var vertices = new List <float>();

            foreach (var vertex in geometryData.Vertices)
            {
                vertices.AddRange(vertex.ToArray());
            }
            var normals = new List <float>();

            foreach (var normal in geometryData.Normals)
            {
                normals.AddRange(normal.ToArray());
            }
            var texCoords = new List <float>();

            foreach (var texCoord in geometryData.TexCoords)
            {
                texCoords.AddRange(texCoord.ToArray());
            }
            var geometry = GenerateGeometry(geometryData, vertices.ToArray(), normals.ToArray(), texCoords.ToArray(), primitiveType);

            return(geometry);
        }
Beispiel #2
0
        public static Geometry GenerateGeometry(GeometryData geometryData, float[] vertices, float[] normals, float[] texCoords, PrimitiveType primitiveType = PrimitiveType.Triangles)
        {
            if (vertices.Length % 3 != 0 || normals.Length % 3 != 0 || texCoords.Length % 2 != 0)
            {
                throw new ArgumentException($"Invalid count of input variables");
            }
            var geometry = new Geometry(geometryData, primitiveType);

            geometry.m_VerticesCount = vertices.Length / 3;

            geometry.VAO = Gl.GenVertexArray();
            Gl.BindVertexArray(geometry.VAO);

            geometry.m_VertexBuffer = Gl.GenBuffer();
            Gl.BindBuffer(BufferTarget.ArrayBuffer, geometry.m_VertexBuffer);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(sizeof(float) * vertices.Length), vertices, BufferUsage.StaticDraw);
            Gl.VertexAttribPointer(POSITION_LOCATION, 3, VertexAttribType.Float, false, 0, IntPtr.Zero);
            Gl.EnableVertexAttribArray(POSITION_LOCATION);

            geometry.m_NormalBuffer = Gl.GenBuffer();
            Gl.BindBuffer(BufferTarget.ArrayBuffer, geometry.m_NormalBuffer);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(sizeof(float) * normals.Length), normals, BufferUsage.StaticDraw);
            Gl.VertexAttribPointer(NORMAL_LOCATION, 3, VertexAttribType.Float, false, 0, IntPtr.Zero);
            Gl.EnableVertexAttribArray(NORMAL_LOCATION);

            geometry.m_TexCoordBuffer = Gl.GenBuffer();
            Gl.BindBuffer(BufferTarget.ArrayBuffer, geometry.m_TexCoordBuffer);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(sizeof(float) * texCoords.Length), texCoords, BufferUsage.StaticDraw);
            Gl.VertexAttribPointer(TEX_COORD_LOCATION, 2, VertexAttribType.Float, false, 0, IntPtr.Zero);
            Gl.EnableVertexAttribArray(TEX_COORD_LOCATION);

            Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
            return(geometry);
        }
Beispiel #3
0
        public static Geometry Segment(int segmentCount, Segment segment)
        {
            var points = new [] {
                new Vec3(0, 0, segment.Close),
                new Vec3(0, 0, segment.Far),
                new Vec3(0, segment.Height, segment.Far),
                new Vec3(0, segment.Height, segment.Close)
            };

            points = Array.ConvertAll(points, vertex => Mat3.Rotation(Vec3.Up * -segment.AngleSize / 2) * vertex);
            var data = new GeometryData();

            var segmentAngle = segment.AngleSize / segmentCount;

            for (var i = 0; i < segmentCount; i++)
            {
                var rPoints = Array.ConvertAll(points, vertex => Mat3.Rotation(Vec3.Up * segmentAngle * i) * vertex);
                var lPoints = Array.ConvertAll(points, vertex => Mat3.Rotation(Vec3.Up * segmentAngle * (i + 1)) * vertex);
                GenerateQuad(new [] { rPoints[3], rPoints[2], lPoints[2], lPoints[3] }, ref data.Vertices, ref data.Normals); // Top face
                GenerateQuad(new [] { lPoints[0], lPoints[1], rPoints[1], rPoints[0] }, ref data.Vertices, ref data.Normals); // Bottom face
                var  tmp = new List <Vec3>();
                Vec3 normR, normL;
                GenerateQuad(new [] { rPoints[0], rPoints[3], lPoints[3], lPoints[0] }, ref data.Vertices, ref tmp); // Front face
                normR = (-rPoints[0]).Normalized;
                normL = (-lPoints[0]).Normalized;
                data.Normals.Add(normR, normR, normL, normR, normL, normL);
                GenerateQuad(new [] { lPoints[1], lPoints[2], rPoints[2], rPoints[1] }, ref data.Vertices, ref tmp); // Back face
                normR = rPoints[0].Normalized;
                normL = lPoints[0].Normalized;
                data.Normals.Add(normL, normL, normR, normL, normR, normR);
                if (i == 0)
                {
                    GenerateQuad(rPoints, ref data.Vertices, ref data.Normals);         // Right face
                }
                if (i == segmentCount - 1)
                {
                    GenerateQuad(lPoints.Reverse().ToArray(), ref data.Vertices, ref data.Normals);                        // Left face
                }
            }

            for (var i = 0; i < data.Vertices.Count; i++)
            {
                data.Vertices[i] -= new Vec3(0, 0, segment.Close);
            }
            return(Geometry.GenerateGeometry(data));
        }
Beispiel #4
0
        public static Geometry CircleFloor(int segmentCount = 64)
        {
            var segmentAngle = 360f / segmentCount;
            var center       = Vec3.Zero;
            var firstPoint   = Vec3.Forward;

            var data = new GeometryData();

            for (var i = 0; i <= segmentCount; i++)
            {
                var pointA = Mat3.Rotation(Vec3.Up * segmentAngle * i) * firstPoint;
                var pointB = Mat3.Rotation(Vec3.Up * segmentAngle * (i + 1)) * firstPoint;

                data.Vertices.Add(center);
                data.TexCoords.Add(new Vec2(center.X + 1f, center.Z + 1f) * 0.5f);
                data.Vertices.Add(pointA);
                data.TexCoords.Add(new Vec2(pointA.X + 1f, pointA.Z + 1f) * 0.5f);
                data.Vertices.Add(pointB);
                data.TexCoords.Add(new Vec2(pointB.X + 1f, pointB.Z + 1f) * 0.5f);
                data.Normals.Add(Vec3.Up, Vec3.Up, Vec3.Up);
            }
            return(Geometry.GenerateGeometry(data));
        }
Beispiel #5
0
 public Geometry(GeometryData geometryData, PrimitiveType primitiveType = PrimitiveType.Triangles)
 {
     m_PrimitiveType = primitiveType;
 }