Subversion Repositories AndroidProjects

Rev

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

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