Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1004 chris 1
package com.gebauz.bauzoid.graphics.model;
2
 
3
import com.gebauz.bauzoid.graphics.Graphics;
4
import com.gebauz.bauzoid.math.Vector4;
5
 
6
/** Generate quad and other geometry. */
7
public class GeometryUtil
8
{
9
 
10
        static public SimpleGeometry createQuad(Graphics graphics, float minX, float minY, float maxX, float maxY,
11
                        float minS,     float minT, float maxS, float maxT, Vector4 color)
12
        {
13
                SimpleGeometry mesh = new SimpleGeometry(graphics, Geometry.PrimitiveType.TRIANGLES);
14
 
15
                AttributeArray vertices = new AttributeArray(SimpleGeometry.POSITION_COORD_PER_ELEMENT * 6);
16
                vertices.fill(minX, minY, 0.0f);
17
                vertices.fill(maxX, minY, 0.0f);
18
                vertices.fill(minX, maxY, 0.0f);
19
 
20
                vertices.fill(minX, maxY, 0.0f);
21
                vertices.fill(maxX, minY, 0.0f);
22
                vertices.fill(maxX, maxY, 0.0f);
23
 
24
                AttributeArray colors = new AttributeArray(SimpleGeometry.COLOR_COORD_PER_ELEMENT * 6);
25
                colors.fill(color.x, color.y, color.z, color.w);
26
                colors.fill(color.x, color.y, color.z, color.w);
27
                colors.fill(color.x, color.y, color.z, color.w);
28
                colors.fill(color.x, color.y, color.z, color.w);
29
                colors.fill(color.x, color.y, color.z, color.w);
30
                colors.fill(color.x, color.y, color.z, color.w);
31
 
32
                AttributeArray texCoords = new AttributeArray(SimpleGeometry.TEXCOORD_COORD_PER_ELEMENT * 6);
33
                texCoords.fill(minS, maxT);
34
                texCoords.fill(maxS, maxT);
35
                texCoords.fill(minS, minT);
36
 
37
                texCoords.fill(minS, minT);
38
                texCoords.fill(maxS, maxT);
39
                texCoords.fill(maxS, minT);
40
 
41
                mesh.setPositions(vertices.getAttributeArray());
42
                mesh.setColors(colors.getAttributeArray());
43
                mesh.setTexCoords(texCoords.getAttributeArray());              
44
 
45
                return mesh;
46
        }
47
 
48
        public static SimpleGeometry createQuad(Graphics graphics, float minX, float minY, float maxX, float maxY,
49
                        float minS, float minT, float maxS, float maxT)
50
        {
51
                return createQuad(graphics, minX, minY, maxX, maxY, minS, minT, maxS, maxT, new Vector4(1.0f, 1.0f, 1.0f, 1.0f));
52
        }
53
 
54
        public static SimpleGeometry createQuad(Graphics graphics, float minX, float minY, float maxX, float maxY, Vector4 color)
55
        {
56
                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));
57
        }
58
 
59
        public static SimpleGeometry createQuad(Graphics graphics, float minX, float minY, float maxX, float maxY)
60
        {
61
                return createQuad(graphics, minX, minY, maxX, maxY, 0.0f, 0.0f, 1.0f, 1.0f);
62
        }
63
 
64
        static public SimpleGeometry createSubdividedQuad(Graphics graphics, float minX, float minY, float maxX, float maxY,
65
                        float minS,     float minT, float maxS, float maxT, Vector4 color, int subdivisionX, int subdivisionY)
66
        {
67
                SimpleGeometry mesh = new SimpleGeometry(graphics, Geometry.PrimitiveType.TRIANGLES);
68
 
69
                final int numTiles = (subdivisionX + 1) * (subdivisionY + 1);
70
 
71
                AttributeArray vertices = new AttributeArray(SimpleGeometry.POSITION_COORD_PER_ELEMENT * 6 * numTiles);
72
                AttributeArray colors = new AttributeArray(SimpleGeometry.COLOR_COORD_PER_ELEMENT * 6 * numTiles);
73
                AttributeArray texCoords = new AttributeArray(SimpleGeometry.TEXCOORD_COORD_PER_ELEMENT * 6 * numTiles);
74
 
75
                float stepX = (maxX - minX) / (subdivisionX + 1);
76
                float stepY = (maxY - minY) / (subdivisionY + 1);
77
 
78
                float stepS = (maxS - minS) / (subdivisionX + 1);
79
                float stepT = (maxT - minT) / (subdivisionY + 1);
80
 
81
                float segmentX = minX;
82
                float segmentY = minY;
83
 
84
                float segmentS = minS;
85
                float segmentT = maxT;
86
 
87
                for (int y = 0; y < (subdivisionY + 1); y++)
88
                {
89
                        for (int x = 0; x < (subdivisionX + 1); x++)
90
                        {
91
                                float segmentMinX = segmentX;
92
                                float segmentMaxX = segmentX + stepX;
93
                                float segmentMinY = segmentY;
94
                                float segmentMaxY = segmentY + stepY;
95
 
96
                                float segmentMinS = segmentS;
97
                                float segmentMaxS = (segmentS + stepS);
98
                                float segmentMinT = (segmentT - stepT);
99
                                float segmentMaxT = segmentT;
100
 
101
                                vertices.fill(segmentMinX, segmentMinY, 0.0f);
102
                                vertices.fill(segmentMaxX, segmentMinY, 0.0f);
103
                                vertices.fill(segmentMinX, segmentMaxY, 0.0f);
104
 
105
                                vertices.fill(segmentMinX, segmentMaxY, 0.0f);
106
                                vertices.fill(segmentMaxX, segmentMinY, 0.0f);
107
                                vertices.fill(segmentMaxX, segmentMaxY, 0.0f);
108
 
109
                                colors.fill(color.x, color.y, color.z, color.w);
110
                                colors.fill(color.x, color.y, color.z, color.w);
111
                                colors.fill(color.x, color.y, color.z, color.w);
112
 
113
                                colors.fill(color.x, color.y, color.z, color.w);
114
                                colors.fill(color.x, color.y, color.z, color.w);
115
                                colors.fill(color.x, color.y, color.z, color.w);
116
 
117
                                texCoords.fill(segmentMinS, segmentMaxT);
118
                                texCoords.fill(segmentMaxS, segmentMaxT);
119
                                texCoords.fill(segmentMinS, segmentMinT);
120
 
121
                                texCoords.fill(segmentMinS, segmentMinT);
122
                                texCoords.fill(segmentMaxS, segmentMaxT);
123
                                texCoords.fill(segmentMaxS, segmentMinT);
124
 
125
                                segmentX += stepX;
126
                                segmentS += stepS;
127
                        }
128
 
129
                        segmentX = minX;                       
130
                        segmentS = minS;
131
 
132
                        segmentY += stepY;
133
                        segmentT -= stepT;
134
                }
135
 
136
/*              AttributeArray vertices = new AttributeArray(SimpleGeometry.POSITION_COORD_PER_ELEMENT * 6);
137
                vertices.fill(minX, minY, 0.0f);
138
                vertices.fill(maxX, minY, 0.0f);
139
                vertices.fill(minX, maxY, 0.0f);
140
 
141
                vertices.fill(minX, maxY, 0.0f);
142
                vertices.fill(maxX, minY, 0.0f);
143
                vertices.fill(maxX, maxY, 0.0f);
144
 
145
                AttributeArray colors = new AttributeArray(SimpleGeometry.COLOR_COORD_PER_ELEMENT * 6);
146
                colors.fill(color.x, color.y, color.z, color.w);
147
                colors.fill(color.x, color.y, color.z, color.w);
148
                colors.fill(color.x, color.y, color.z, color.w);
149
                colors.fill(color.x, color.y, color.z, color.w);
150
                colors.fill(color.x, color.y, color.z, color.w);
151
                colors.fill(color.x, color.y, color.z, color.w);
152
 
153
                AttributeArray texCoords = new AttributeArray(SimpleGeometry.TEXCOORD_COORD_PER_ELEMENT * 6);
154
                texCoords.fill(minS, maxT);
155
                texCoords.fill(maxS, maxT);
156
                texCoords.fill(minS, minT);
157
 
158
                texCoords.fill(minS, minT);
159
                texCoords.fill(maxS, maxT);
160
                texCoords.fill(maxS, minT);*/
161
 
162
                mesh.setPositions(vertices.getAttributeArray());
163
                mesh.setColors(colors.getAttributeArray());
164
                mesh.setTexCoords(texCoords.getAttributeArray());
165
 
166
                return mesh;
167
        }
168
 
169
        public static SimpleGeometry createSubdividedQuad(Graphics graphics, float minX, float minY, float maxX, float maxY,
170
                        float minS, float minT, float maxS, float maxT, int subdivisionX, int subdivisionY)
171
        {
172
                return createSubdividedQuad(graphics, minX, minY, maxX, maxY, minS, minT, maxS, maxT, new Vector4(1.0f, 1.0f, 1.0f, 1.0f),
173
                                subdivisionX, subdivisionY);
174
        }
175
 
176
        public static SimpleGeometry createSubdividedQuad(Graphics graphics, float minX, float minY, float maxX, float maxY,
177
                        Vector4 color, int subdivisionX, int subdivisionY)
178
        {
179
                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),
180
                                subdivisionX, subdivisionY);
181
        }
182
 
183
        public static SimpleGeometry createSubdividedQuad(Graphics graphics, float minX, float minY, float maxX, float maxY,
184
                        int subdivisionX, int subdivisionY)
185
        {
186
                return createSubdividedQuad(graphics, minX, minY, maxX, maxY, 0.0f, 0.0f, 1.0f, 1.0f, subdivisionX, subdivisionY);
187
        }
188
}