-
Notifications
You must be signed in to change notification settings - Fork 0
/
Drawable2d.cs
181 lines (167 loc) · 6.14 KB
/
Drawable2d.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using Java.Nio;
namespace Grafika.GLES
{
public class Drawable2d
{
private static int SIZEOF_FLOAT = 4;
/**
* Simple equilateral triangle (1.0 per side). Centered on (0,0).
*/
private static float[] TRIANGLE_COORDS = {
0.0f, 0.577350269f, // 0 top
-0.5f, -0.288675135f, // 1 bottom left
0.5f, -0.288675135f // 2 bottom right
};
private static float[] TRIANGLE_TEX_COORDS = {
0.5f, 0.0f, // 0 top center
0.0f, 1.0f, // 1 bottom left
1.0f, 1.0f, // 2 bottom right
};
private static FloatBuffer TRIANGLE_BUF =
GlUtil.createFloatBuffer(TRIANGLE_COORDS);
private static FloatBuffer TRIANGLE_TEX_BUF =
GlUtil.createFloatBuffer(TRIANGLE_TEX_COORDS);
/**
* Simple square, specified as a triangle strip. The square is centered on (0,0) and has
* a size of 1x1.
* <p>
* Triangles are 0-1-2 and 2-1-3 (counter-clockwise winding).
*/
private static float[] RECTANGLE_COORDS = {
-0.5f, -0.5f, // 0 bottom left
0.5f, -0.5f, // 1 bottom right
-0.5f, 0.5f, // 2 top left
0.5f, 0.5f, // 3 top right
};
private static float[] RECTANGLE_TEX_COORDS = {
0.0f, 1.0f, // 0 bottom left
1.0f, 1.0f, // 1 bottom right
0.0f, 0.0f, // 2 top left
1.0f, 0.0f // 3 top right
};
private static FloatBuffer RECTANGLE_BUF =
GlUtil.createFloatBuffer(RECTANGLE_COORDS);
private static FloatBuffer RECTANGLE_TEX_BUF =
GlUtil.createFloatBuffer(RECTANGLE_TEX_COORDS);
/**
* A "full" square, extending from -1 to +1 in both dimensions. When the model/view/projection
* matrix is identity, this will exactly cover the viewport.
* <p>
* The texture coordinates are Y-inverted relative to RECTANGLE. (This seems to work out
* right with external textures from SurfaceTexture.)
*/
private static float[] FULL_RECTANGLE_COORDS = {
-1.0f, -1.0f, // 0 bottom left
1.0f, -1.0f, // 1 bottom right
-1.0f, 1.0f, // 2 top left
1.0f, 1.0f, // 3 top right
};
private static float[] FULL_RECTANGLE_TEX_COORDS = {
0.0f, 0.0f, // 0 bottom left
1.0f, 0.0f, // 1 bottom right
0.0f, 1.0f, // 2 top left
1.0f, 1.0f // 3 top right
};
private static FloatBuffer FULL_RECTANGLE_BUF =
GlUtil.createFloatBuffer(FULL_RECTANGLE_COORDS);
private static FloatBuffer FULL_RECTANGLE_TEX_BUF =
GlUtil.createFloatBuffer(FULL_RECTANGLE_TEX_COORDS);
private FloatBuffer mVertexArray;
private FloatBuffer mTexCoordArray;
private int mVertexCount;
private int mCoordsPerVertex;
private int mVertexStride;
private int mTexCoordStride;
private Prefab mPrefab;
/**
* Enum values for constructor.
*/
public enum Prefab
{
TRIANGLE, RECTANGLE, FULL_RECTANGLE
}
/**
* Prepares a drawable from a "pre-fabricated" shape definition.
* <p>
* Does no EGL/GL operations, so this can be done at any time.
*/
public Drawable2d(Prefab shape)
{
switch (shape)
{
case Prefab.TRIANGLE:
mVertexArray = TRIANGLE_BUF;
mTexCoordArray = TRIANGLE_TEX_BUF;
mCoordsPerVertex = 2;
mVertexStride = mCoordsPerVertex * SIZEOF_FLOAT;
mVertexCount = TRIANGLE_COORDS.Length / mCoordsPerVertex;
break;
case Prefab.RECTANGLE:
mVertexArray = RECTANGLE_BUF;
mTexCoordArray = RECTANGLE_TEX_BUF;
mCoordsPerVertex = 2;
mVertexStride = mCoordsPerVertex * SIZEOF_FLOAT;
mVertexCount = RECTANGLE_COORDS.Length / mCoordsPerVertex;
break;
case Prefab.FULL_RECTANGLE:
mVertexArray = FULL_RECTANGLE_BUF;
mTexCoordArray = FULL_RECTANGLE_TEX_BUF;
mCoordsPerVertex = 2;
mVertexStride = mCoordsPerVertex * SIZEOF_FLOAT;
mVertexCount = FULL_RECTANGLE_COORDS.Length / mCoordsPerVertex;
break;
default:
throw new Exception("Unknown shape " + shape);
}
mTexCoordStride = 2 * SIZEOF_FLOAT;
mPrefab = shape;
}
/**
* Returns the array of vertices.
* <p>
* To avoid allocations, this returns internal state. The caller must not modify it.
*/
public FloatBuffer getVertexArray()
{
return mVertexArray;
}
/**
* Returns the array of texture coordinates.
* <p>
* To avoid allocations, this returns internal state. The caller must not modify it.
*/
public FloatBuffer getTexCoordArray()
{
return mTexCoordArray;
}
/**
* Returns the number of vertices stored in the vertex array.
*/
public int getVertexCount()
{
return mVertexCount;
}
/**
* Returns the width, in bytes, of the data for each vertex.
*/
public int getVertexStride()
{
return mVertexStride;
}
/**
* Returns the width, in bytes, of the data for each texture coordinate.
*/
public int getTexCoordStride()
{
return mTexCoordStride;
}
/**
* Returns the number of position coordinates per vertex. This will be 2 or 3.
*/
public int getCoordsPerVertex()
{
return mCoordsPerVertex;
}
}
}