Subversion Repositories AndroidProjects

Rev

Rev 221 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
205 chris 1
package com.gebauz.Bauzoid.graphics.model;
2
 
3
import com.badlogic.gdx.Gdx;
4
import com.badlogic.gdx.graphics.GL20;
5
import com.gebauz.Bauzoid.app.Consts;
6
import com.gebauz.Bauzoid.graphics.Graphics;
7
import com.gebauz.Bauzoid.graphics.GraphicsObject;
8
 
221 chris 9
/** Geometry class.
205 chris 10
 *
11
 */
12
public class Geometry extends GraphicsObject
13
{
14
        public enum PrimitiveType
15
        {
16
                TRIANGLES,
17
                TRIANGLE_STRIP,
18
                LINES,
19
                LINE_STRIP,
20
                POINTS
21
        };
22
 
23
        private int mPrimitiveTypeGL = GL20.GL_TRIANGLES;
24
        private PrimitiveType mPrimitiveType = PrimitiveType.TRIANGLES;
25
        private VertexStream[] mStreams = null;
26
        private IndexStream mIndexStream = null;
27
 
28
        private int mVertexCount = 0;
29
        private int mIndexCount = 0;
30
 
31
        /** Constructor. */
32
        public Geometry(Graphics graphics, PrimitiveType primitiveType, int vertexCount, int indexCount)
33
        {
34
                super(graphics);
224 chris 35
                mPrimitiveType = primitiveType;
36
                mPrimitiveTypeGL = toPrimitiveGL(primitiveType);
205 chris 37
                mVertexCount = vertexCount;
38
                mIndexCount = indexCount;
39
        }
40
 
216 chris 41
        /** Destroy internal data. */
42
        public void dispose()
43
        {
44
                destroyIndexStream();
45
                destroyVertexStreams();
46
        }
47
 
48
        /** Destroy vertex streams. */
49
        public void destroyVertexStreams()
50
        {
51
                if (mIndexStream != null)
52
                {
53
                        mIndexStream.unload();
54
                        mIndexStream = null;
55
                }
56
        }
57
 
58
        /** Destroy index stream. */
59
        public void destroyIndexStream()
60
        {
61
                if (mStreams != null)
62
                {
63
                        for (VertexStream stream : mStreams)
64
                                stream.unload();
65
 
66
                        mStreams = null;
67
                }
68
        }
69
 
205 chris 70
        /** Upload streams to hardware. */
71
        public void upload()
72
        {
73
                if (mStreams != null)
74
                {
75
                        for (int i = 0; i < mStreams.length; i++)
76
                        {
77
                                mStreams[i].upload();
78
                        }
79
                }
80
 
81
                if (mIndexStream != null)
82
                {
83
                        mIndexStream.upload();
84
                }
85
        }
86
 
87
        /** Unload streams from hardware. */
88
        public void unload()
89
        {
90
                if (mStreams != null)
91
                {
92
                        for (int i = 0; i < mStreams.length; i++)
93
                        {
94
                                if (mStreams[i] != null)
95
                                {
96
                                        mStreams[i].unload();
97
                                        mStreams[i] = null;
98
                                }
99
                        }
100
                        mStreams = null;
101
                }
102
 
103
                if (mIndexStream != null)
104
                {
105
                        mIndexStream.unload();
106
                        mIndexStream = null;
107
                }
108
        }
109
 
110
        /** Activate the streams of this Geometry. */
111
        public void activate()
112
        {
113
                if (mStreams != null)
114
                {
115
                        for (int i = 0; i < mStreams.length; i++)
116
                        {
117
                                mStreams[i].activate();
118
                        }
119
                }
120
 
121
                if (mIndexStream != null)
122
                {
123
                        mIndexStream.activate();
124
                }
125
        }
126
 
127
        /** Deactivate the streams of this Geometry. */
128
        public void deactivate()
129
        {
130
                if (mStreams != null)
131
                {
132
                        for (int i = 0; i < mStreams.length; i++)
133
                        {
134
                                mStreams[i].deactivate();
135
                        }
136
                }
137
 
138
                if (mIndexStream != null)
139
                {
140
                        mIndexStream.deactivate();
141
                }
142
        }
143
 
144
        /** Render either with or without indices depending on whether there is an IndexStream. */
145
        public void render(int firstIndex, int lastIndex, int firstVertex, int lastVertex)
146
        {
147
                if (mIndexStream != null)
148
                {
149
                        renderIndices(firstIndex, lastIndex);
150
                }
151
                else
152
                {
153
                        renderVertices(firstVertex, lastVertex);
154
                }
155
                //GLES20.glDrawArrays(GLES20.GL_POINTS, firstVertex, lastVertex - firstVertex + 1);
156
        }
157
 
158
        /** Render without indices. */
159
        public void renderVertices(int firstVertex, int lastVertex)
160
        {
161
                Gdx.gl20.glDrawArrays(mPrimitiveTypeGL, firstVertex, lastVertex - firstVertex + 1);
162
        }
163
 
164
        /** Render with indices - requires IndexStream. */
165
        public void renderIndices(int firstIndex, int lastIndex)
166
        {
167
                // offset is the offset in bytes!
168
                Gdx.gl20.glDrawElements(mPrimitiveTypeGL, lastIndex - firstIndex + 1, GL20.GL_UNSIGNED_SHORT, firstIndex * Consts.SIZEOF_SHORT);               
169
        }
170
 
171
        /** Sets the vertex streams. */
172
        public final void setVertexStreams(VertexStream[] streams)
173
        {
174
                mStreams = streams;
175
        }
176
 
177
        /** Get a vertex stream. */
178
        public final VertexStream getVertexStream(int n)
179
        {
180
                return mStreams[n];
181
        }
182
 
183
        /** Get the number of vertex streams. */
184
        public final int getVertexStreamCount()
185
        {
186
                return mStreams.length;
187
        }
188
 
189
        /** Set the index stream. */
190
        public final void setIndexStream(IndexStream indexStream)
191
        {
192
                mIndexStream = indexStream;
193
        }
194
 
195
        /** Get the index stream. */
196
        public final IndexStream getIndexStream()
197
        {
198
                return mIndexStream;
199
        }
200
 
201
        /** Set primitive type. */
202
        public final void setPrimitiveType(PrimitiveType type)
203
        {
204
                mPrimitiveType = type;
205
                mPrimitiveTypeGL = toPrimitiveGL(type);
206
        }
207
 
208
        /** Get primitive type. */
209
        public final PrimitiveType getPrimitiveType()
210
        {
211
                return mPrimitiveType;
212
        }
213
 
221 chris 214
        /** Get number of vertices. */
215
        public final int getVertexCount()
216
        {
217
                return mVertexCount;
218
        }
219
 
220
        /** Get number of indices. */
221
        public final int getIndexCount()
222
        {
223
                return mIndexCount;
224
        }
225
 
205 chris 226
        /** Convert from Primitive Type to GL Primitive Type. */
224 chris 227
        public static int toPrimitiveGL(PrimitiveType type)
205 chris 228
        {
229
                switch (type)
230
                {
231
                case TRIANGLES:
232
                        return GL20.GL_TRIANGLES;
233
                case TRIANGLE_STRIP:
234
                        return GL20.GL_TRIANGLE_STRIP;
235
                case LINES:
236
                        return GL20.GL_LINES;
237
                case LINE_STRIP:
238
                        return GL20.GL_LINE_STRIP;
239
                case POINTS:
240
                default:
241
                        return GL20.GL_POINTS;
242
                }
243
        }
244
 
221 chris 245
 
205 chris 246
 
247
}
248