Subversion Repositories AndroidProjects

Rev

Rev 791 | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using BauzoidNET.math;
using BauzoidNET.graphics;

namespace BauzoidNET.graphics.model
{
    public class GeometryUtil
    {
        private GeometryUtil()
        {
        }

        static public SimpleGeometry createQuad(Graphics graphics, float minX, float minY, float maxX, float maxY,
            float minS, float minT, float maxS, float maxT, Vector4 color)
        {
            SimpleGeometry mesh = new SimpleGeometry(graphics, Geometry.PrimitiveType.TRIANGLES);

            AttributeArray vertices = new AttributeArray(SimpleGeometry.POSITION_COORD_PER_ELEMENT * 6);
            vertices.fill(minX, minY, 0.0f);
            vertices.fill(maxX, minY, 0.0f);
            vertices.fill(minX, maxY, 0.0f);

            vertices.fill(minX, maxY, 0.0f);
            vertices.fill(maxX, minY, 0.0f);
            vertices.fill(maxX, maxY, 0.0f);

            AttributeArray colors = new AttributeArray(SimpleGeometry.COLOR_COORD_PER_ELEMENT * 6);
            colors.fill(color.x, color.y, color.z, color.w);
            colors.fill(color.x, color.y, color.z, color.w);
            colors.fill(color.x, color.y, color.z, color.w);
            colors.fill(color.x, color.y, color.z, color.w);
            colors.fill(color.x, color.y, color.z, color.w);
            colors.fill(color.x, color.y, color.z, color.w);

            AttributeArray texCoords = new AttributeArray(SimpleGeometry.TEXCOORD_COORD_PER_ELEMENT * 6);
            texCoords.fill(minS, maxT);
            texCoords.fill(maxS, maxT);
            texCoords.fill(minS, minT);

            texCoords.fill(minS, minT);
            texCoords.fill(maxS, maxT);
            texCoords.fill(maxS, minT);

            mesh.setPositions(vertices.getAttributeArray());
            mesh.setColors(colors.getAttributeArray());
            mesh.setTexCoords(texCoords.getAttributeArray());

            return mesh;
        }

        public static SimpleGeometry createQuad(Graphics graphics, float minX, float minY, float maxX, float maxY,
                float minS, float minT, float maxS, float maxT)
        {
            return createQuad(graphics, minX, minY, maxX, maxY, minS, minT, maxS, maxT, new Vector4(1.0f, 1.0f, 1.0f, 1.0f));
        }

        public static SimpleGeometry createQuad(Graphics graphics, float minX, float minY, float maxX, float maxY, Vector4 color)
        {
            return createQuad(graphics, minX, minY, maxX, maxY, 0.0f, 0.0f, 1.0f, 1.0f, new Vector4(1.0f, 1.0f, 1.0f, 1.0f));
        }

        public static SimpleGeometry createQuad(Graphics graphics, float minX, float minY, float maxX, float maxY)
        {
            return createQuad(graphics, minX, minY, maxX, maxY, 0.0f, 0.0f, 1.0f, 1.0f);
        }

        static public SimpleGeometry createSubdividedQuad(Graphics graphics, float minX, float minY, float maxX, float maxY,
                float minS, float minT, float maxS, float maxT, Vector4 color, int subdivisionX, int subdivisionY)
            {
            SimpleGeometry mesh = new SimpleGeometry(graphics, Geometry.PrimitiveType.TRIANGLES);
               
                    int numTiles = (subdivisionX + 1) * (subdivisionY + 1);
               
                    AttributeArray vertices = new AttributeArray(SimpleGeometry.POSITION_COORD_PER_ELEMENT * 6 * numTiles);
                    AttributeArray colors = new AttributeArray(SimpleGeometry.COLOR_COORD_PER_ELEMENT * 6 * numTiles);
                    AttributeArray texCoords = new AttributeArray(SimpleGeometry.TEXCOORD_COORD_PER_ELEMENT * 6 * numTiles);
               
                    float stepX = (maxX - minX) / (subdivisionX + 1);
                    float stepY = (maxY - minY) / (subdivisionY + 1);
               
                    float stepS = (maxS - minS) / (subdivisionX + 1);
                    float stepT = (maxT - minT) / (subdivisionY + 1);
               
                    float segmentX = minX;
                    float segmentY = minY;
               
                    float segmentS = minS;
                    float segmentT = maxT;
               
                    for (int y = 0; y < (subdivisionY + 1); y++)
                    {
                            for (int x = 0; x < (subdivisionX + 1); x++)
                            {
                                    float segmentMinX = segmentX;
                                    float segmentMaxX = segmentX + stepX;
                                    float segmentMinY = segmentY;
                                    float segmentMaxY = segmentY + stepY;
                               
                                    float segmentMinS = segmentS;
                                    float segmentMaxS = (segmentS + stepS);
                                    float segmentMinT = (segmentT - stepT);
                                    float segmentMaxT = segmentT;
                               
                                    vertices.fill(segmentMinX, segmentMinY, 0.0f);
                                    vertices.fill(segmentMaxX, segmentMinY, 0.0f);
                                    vertices.fill(segmentMinX, segmentMaxY, 0.0f);
                               
                                    vertices.fill(segmentMinX, segmentMaxY, 0.0f);
                                    vertices.fill(segmentMaxX, segmentMinY, 0.0f);
                                    vertices.fill(segmentMaxX, segmentMaxY, 0.0f);
                               
                                    colors.fill(color.x, color.y, color.z, color.w);
                                    colors.fill(color.x, color.y, color.z, color.w);
                                    colors.fill(color.x, color.y, color.z, color.w);
                               
                                    colors.fill(color.x, color.y, color.z, color.w);
                                    colors.fill(color.x, color.y, color.z, color.w);
                                    colors.fill(color.x, color.y, color.z, color.w);
                               
                                    texCoords.fill(segmentMinS, segmentMaxT);
                                    texCoords.fill(segmentMaxS, segmentMaxT);
                                    texCoords.fill(segmentMinS, segmentMinT);
                               
                                    texCoords.fill(segmentMinS, segmentMinT);
                                    texCoords.fill(segmentMaxS, segmentMaxT);
                                    texCoords.fill(segmentMaxS, segmentMinT);
                               
                                    segmentX += stepX;
                                    segmentS += stepS;
                            }
                       
                            segmentX = minX;                   
                            segmentS = minS;
                       
                            segmentY += stepY;
                            segmentT -= stepT;
                    }
               
    /*          AttributeArray vertices = new AttributeArray(SimpleGeometry.POSITION_COORD_PER_ELEMENT * 6);
                    vertices.fill(minX, minY, 0.0f);
                    vertices.fill(maxX, minY, 0.0f);
                    vertices.fill(minX, maxY, 0.0f);
               
                    vertices.fill(minX, maxY, 0.0f);
                    vertices.fill(maxX, minY, 0.0f);
                    vertices.fill(maxX, maxY, 0.0f);

                    AttributeArray colors = new AttributeArray(SimpleGeometry.COLOR_COORD_PER_ELEMENT * 6);
                    colors.fill(color.x, color.y, color.z, color.w);
                    colors.fill(color.x, color.y, color.z, color.w);
                    colors.fill(color.x, color.y, color.z, color.w);
                    colors.fill(color.x, color.y, color.z, color.w);
                    colors.fill(color.x, color.y, color.z, color.w);
                    colors.fill(color.x, color.y, color.z, color.w);
               
                    AttributeArray texCoords = new AttributeArray(SimpleGeometry.TEXCOORD_COORD_PER_ELEMENT * 6);
                    texCoords.fill(minS, maxT);
                    texCoords.fill(maxS, maxT);
                    texCoords.fill(minS, minT);
               
                    texCoords.fill(minS, minT);
                    texCoords.fill(maxS, maxT);
                    texCoords.fill(maxS, minT);*/

               
                    mesh.setPositions(vertices.getAttributeArray());
                    mesh.setColors(colors.getAttributeArray());
                    mesh.setTexCoords(texCoords.getAttributeArray());

                    return mesh;
            }

        public static SimpleGeometry createSubdividedQuad(Graphics graphics, float minX, float minY, float maxX, float maxY,
                float minS, float minT, float maxS, float maxT, int subdivisionX, int subdivisionY)
        {
            return createSubdividedQuad(graphics, minX, minY, maxX, maxY, minS, minT, maxS, maxT, new Vector4(1.0f, 1.0f, 1.0f, 1.0f),
                    subdivisionX, subdivisionY);
        }

        public static SimpleGeometry createSubdividedQuad(Graphics graphics, float minX, float minY, float maxX, float maxY,
                Vector4 color, int subdivisionX, int subdivisionY)
        {
            return createSubdividedQuad(graphics, minX, minY, maxX, maxY, 0.0f, 0.0f, 1.0f, 1.0f, new Vector4(1.0f, 1.0f, 1.0f, 1.0f),
                    subdivisionX, subdivisionY);
        }

        public static SimpleGeometry createSubdividedQuad(Graphics graphics, float minX, float minY, float maxX, float maxY,
                int subdivisionX, int subdivisionY)
        {
            return createSubdividedQuad(graphics, minX, minY, maxX, maxY, 0.0f, 0.0f, 1.0f, 1.0f, subdivisionX, subdivisionY);
        }
    }
}