Subversion Repositories AndroidProjects

Rev

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

Rev Author Line No. Line
786 chris 1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
 
7
using Tao.OpenGl;
8
 
1451 chris 9
#pragma warning disable 0618
790 chris 10
 
786 chris 11
namespace BauzoidNET.graphics.model
12
{
790 chris 13
    public class SimpleGeometry : GraphicsObject
786 chris 14
    {
15
        public const int POSITION_COORD_PER_ELEMENT = 3;
16
            public const int NORMAL_COORD_PER_ELEMENT = 3;
17
            public const int COLOR_COORD_PER_ELEMENT = 4;
18
        public const int TEXCOORD_COORD_PER_ELEMENT = 2;
19
 
20
            private Geometry.PrimitiveType mPrimitiveType = Geometry.PrimitiveType.TRIANGLES;
21
            private int mPrimitiveTypeGL = Gl.GL_TRIANGLES;
22
 
23
            private VertexStream mPositionStream = null;
24
            private VertexStream mNormalStream = null;
25
            private VertexStream mColorStream = null;
26
            private VertexStream mTexCoordStream = null;
27
            private IndexStream mIndexStream = null;
28
 
790 chris 29
            public SimpleGeometry(Graphics graphics, Geometry.PrimitiveType primitiveType)
30
            : base(graphics)
786 chris 31
            {          
32
                    mPrimitiveType = primitiveType;
33
                    mPrimitiveTypeGL = Geometry.toPrimitiveGL(primitiveType);
34
            }
35
 
36
            public void dispose()
37
            {
38
                    destroyPositionStream();
39
                    destroyNormalStream();
40
                    destroyColorStream();
41
                    destroyTexCoordStream();
42
                    destroyIndexStream();
43
            }
44
 
45
            public void destroyPositionStream()
46
            {
47
                    if (mPositionStream != null)
48
                    {
49
                            mPositionStream.dispose();
50
                            mPositionStream = null;
51
                    }
52
            }  
53
 
54
            public void destroyNormalStream()
55
            {
56
                    if (mNormalStream != null)
57
                    {
58
                            mNormalStream.dispose();
59
                            mNormalStream = null;
60
                    }
61
            }  
62
 
63
            public void destroyColorStream()
64
            {
65
                    if (mColorStream != null)
66
                    {
67
                            mColorStream.dispose();
68
                            mColorStream = null;
69
                    }
70
            }  
71
 
72
            public void destroyTexCoordStream()
73
            {
74
                    if (mTexCoordStream != null)
75
                    {
76
                            mTexCoordStream.dispose();
77
                            mTexCoordStream = null;
78
                    }
79
            }  
80
 
81
            public void destroyIndexStream()
82
            {
83
                    if (mIndexStream != null)
84
                    {
85
                            mIndexStream.dispose();
86
                            mIndexStream = null;
87
                    }
88
            }
89
 
90
            public void setPositions(float[] positions)
91
            {
92
                    setPositions(positions, false);
93
            }
94
 
95
            public void setPositions(float[] positions, bool isDynamic)
96
            {
97
                    if ((mPositionStream == null) ||
98
                            (positions.Length != (mPositionStream.getVertexCount() * mPositionStream.getCoordsPerElement())) ||
99
                            (isDynamic != mPositionStream.isDynamic()))
100
                    {
101
                            destroyPositionStream();
102
 
791 chris 103
                            mPositionStream = new VertexStream(getGraphics(), VertexStream.StreamType.INDIVIDUAL, "Positions",
786 chris 104
                                            new VertexAttribute[] { new VertexAttribute(VertexAttribute.POSITION, POSITION_COORD_PER_ELEMENT, 0) },
105
                                            isDynamic);
106
                    }
107
                    mPositionStream.setData(positions, POSITION_COORD_PER_ELEMENT, POSITION_COORD_PER_ELEMENT * sizeof(float));
108
                    mPositionStream.reupload();
109
            }
110
 
111
            public void setNormals(float[] normals)
112
            {
113
                    setNormals(normals, false);
114
            }
115
 
116
            public void setNormals(float[] normals, bool isDynamic)
117
            {
118
                    if ((mNormalStream == null) ||
119
                            (normals.Length != (mNormalStream.getVertexCount() * mNormalStream.getCoordsPerElement())) ||
120
                            (isDynamic != mNormalStream.isDynamic()))
121
                    {
122
                            destroyNormalStream();
791 chris 123
 
124
                mNormalStream = new VertexStream(getGraphics(), VertexStream.StreamType.INDIVIDUAL, "Normals",
786 chris 125
                                            new VertexAttribute[] { new VertexAttribute(VertexAttribute.NORMAL, NORMAL_COORD_PER_ELEMENT, 0) },
126
                                            isDynamic);
127
                    }
128
                    mNormalStream.setData(normals, NORMAL_COORD_PER_ELEMENT, NORMAL_COORD_PER_ELEMENT * sizeof(float));
129
                    mNormalStream.reupload();
130
            }
131
 
132
            public void setColors(float[] colors)
133
            {
134
                    setColors(colors, false);
135
            }
136
 
137
            public void setColors(float[] colors, bool isDynamic)
138
            {
139
                    if ((mColorStream == null) ||
140
                            (colors.Length != (mColorStream.getVertexCount() * mColorStream.getCoordsPerElement())) ||
141
                            (isDynamic != mColorStream.isDynamic()))
142
                    {
143
                            destroyColorStream();
791 chris 144
 
145
                mColorStream = new VertexStream(getGraphics(), VertexStream.StreamType.INDIVIDUAL, "Colors",
786 chris 146
                                            new VertexAttribute[] { new VertexAttribute(VertexAttribute.COLOR, COLOR_COORD_PER_ELEMENT, 0) },
147
                                            isDynamic);
148
                    }
149
                    mColorStream.setData(colors, COLOR_COORD_PER_ELEMENT, COLOR_COORD_PER_ELEMENT * sizeof(float));
150
                    mColorStream.reupload();
151
            }
152
 
153
            public void setTexCoords(float[] texCoords)
154
            {
155
                    setTexCoords(texCoords, false);
156
            }
157
 
158
            public void setTexCoords(float[] texCoords, bool isDynamic)
159
            {
160
                    if ((mTexCoordStream == null) ||
161
                            (texCoords.Length != (mTexCoordStream.getVertexCount() * mTexCoordStream.getCoordsPerElement())) ||
162
                            (isDynamic != mTexCoordStream.isDynamic()))
163
                    {
164
                            destroyTexCoordStream();
791 chris 165
 
166
                mTexCoordStream = new VertexStream(getGraphics(), VertexStream.StreamType.INDIVIDUAL, "TexCoord",
786 chris 167
                                            new VertexAttribute[] { new VertexAttribute(VertexAttribute.TEXCOORD0, TEXCOORD_COORD_PER_ELEMENT, 0) },
168
                                            isDynamic);
169
                    }
170
                    mTexCoordStream.setData(texCoords, TEXCOORD_COORD_PER_ELEMENT, TEXCOORD_COORD_PER_ELEMENT * sizeof(float));
171
                    mTexCoordStream.reupload();
172
            }
173
 
174
            public void setIndices(short[] indices)
175
            {
176
                    if ((mIndexStream == null) || (indices.Length != (mIndexStream.getIndexCount())))
177
                    {
178
                            destroyIndexStream();
179
 
791 chris 180
                            mIndexStream = new IndexStream(getGraphics());
786 chris 181
                    }
182
                    mIndexStream.setData(indices);
183
                    mIndexStream.reupload();
184
            }
185
 
186
            public void render(int first, int num)
187
            {
188
                    if (mPositionStream == null)
189
                            return;
190
 
191
                    mPositionStream.activate();
192
 
193
                    if (mNormalStream != null)
194
                            mNormalStream.activate();
195
 
196
                    if (mColorStream != null)
197
                            mColorStream.activate();
198
 
199
                    if (mTexCoordStream != null)
200
                            mTexCoordStream.activate();
201
 
202
                    if (mIndexStream != null)
203
                    {
204
                            mIndexStream.activate();
205
 
805 chris 206
                            Gl.glDrawElements(mPrimitiveTypeGL, num, Gl.GL_UNSIGNED_SHORT, (IntPtr)(first * sizeof(short)));
786 chris 207
 
208
                            mIndexStream.deactivate();
209
                    }
210
                    else
211
                    {
212
                            Gl.glDrawArrays(mPrimitiveTypeGL, first, num);
213
                    }
214
 
215
                    mPositionStream.deactivate();
216
 
217
                    if (mNormalStream != null)
218
                            mNormalStream.deactivate();
219
 
220
                    if (mColorStream != null)
221
                            mColorStream.deactivate();
222
 
223
                    if (mTexCoordStream != null)
224
                            mTexCoordStream.deactivate();
225
            }
226
 
227
            public void render()
228
            {
229
                    if (hasIndices())
230
                    {
231
                            //Gdx.gl20.glDrawElements(mPrimitiveTypeGL, mIndexStream.getIndexCount(), GL20.GL_UNSIGNED_SHORT, 0);
232
                            render(0, mIndexStream.getIndexCount());
233
                    }
234
                    else
235
                    {
236
                            if (mPositionStream == null)
237
                                    return;
238
 
239
                            render(0, mPositionStream.getVertexCount());
240
                            //Gdx.gl20.glDrawArrays(mPrimitiveTypeGL, 0, mPositionStream.getVertexCount());
241
                    }
242
            }
243
 
244
            public Geometry.PrimitiveType getPrimitiveType()
245
            {
246
                    return mPrimitiveType;
247
            }
248
 
249
            public bool hasIndices()
250
            {
251
                    return (mIndexStream != null);
252
            }
253
    }
254
}