-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzglMesh.cpp
executable file
·262 lines (228 loc) · 5.09 KB
/
zglMesh.cpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright (C) 2010 ZiiLABS Pte Ltd
* All Rights Reserved.
*
* This software and its associated documentation may contain some
* proprietary, confidential and trade secret information of
* ZiiLABS Pte Ltd and except as provided by written agreement with
* ZiiLABS Pte Ltd
*
* a) no part may be disclosed, distributed, reproduced, transmitted,
* transcribed, stored in a retrieval system, adapted or translated
* in any form or by any means electronic, mechanical, magnetic,
* optical, chemical, manual or otherwise,
*
* and
*
* b) the recipient is not entitled to discover through reverse
* engineering or reverse compiling or other such techniques or
* processes the trade secrets contained therein or in the
* documentation.
*/
#include <stdlib.h>
#include "zglCore.h"
#include "zglBasicType.h"
#include "zglMesh.h"
zglMesh::zglMesh()
{
m_mesh_type = MESH_TEXTURE;
}
zglMesh::~zglMesh()
{
zglVertexEx * mesh_vertexs = (zglVertexEx *)m_vertex;
if (mesh_vertexs != NULL)
{
delete[] mesh_vertexs;
mesh_vertexs = NULL;
m_vertex = NULL;
}
if (m_indices != NULL)
{
delete[] m_indices;
m_indices = NULL;
}
}
void zglMesh::init(int vertex, int indices, zglTexture * texture)
{
if(m_vertex != NULL)
{
if(vertex != m_vertex_num)
{
delete[] m_vertex;
m_vertex = NULL;
m_vertex_num = vertex;
m_vertex = new zglVertexEx[m_vertex_num];
}
}
else
{
m_vertex_num = vertex;
m_vertex = new zglVertexEx[m_vertex_num];
}
if(m_indices != NULL)
{
if(m_indices_num != indices)
{
delete[] m_indices;
m_indices = NULL;
m_indices_num = indices;
if(m_indices_num != 0 )
{
m_indices = new unsigned short[m_indices_num];
}
}
}
else
{
m_indices_num = indices;
if(m_indices_num != 0 )
{
m_indices = new unsigned short[m_indices_num];
}
}
//Just register the primitive, forget the mesh type!
//zglPrim3D::init(DRAW_TRIANGLES, m_vertex, m_vertex_num, texture);
m_draw_type = DRAW_TRIANGLES;
//m_vertex = vertex;
//m_vertex_num = vert_num;
m_texture = texture;
//m_indices = NULL;
//m_indices_num = 0;
//!< reset the local coordinate system matrix
m_world = 1.0f;
//Add to the node list
queue();
}
void zglMesh::setMeshType(unsigned char type)
{
switch (type)
{
case MESH_TEXTURE:
case MESH_WIREFRAME:
case MESH_POINTS:
//case MESH_FLATSHADE:
//case MESH_SMOOTHSHADE:
m_mesh_type = type;
break;
default:
m_mesh_type = MESH_TEXTURE;
break;
}
}
bool zglMesh::isAvailable()
{
return isQueued();
}
void zglMesh::resizeIndices(int indices)
{
if(m_indices != NULL)
{
if(m_indices_num != indices)
{
delete[] m_indices;
m_indices = NULL;
m_indices_num = indices;
if(m_indices_num != 0)
{
m_indices = new unsigned short[m_indices_num];
}
}
}
else
{
m_indices_num = indices;
if(m_indices_num != 0)
{
m_indices = new unsigned short[m_indices_num];
}
}
}
void zglMesh::updateHwBuffers()
{
if ((m_flag & FLAG_VBO_ENABLE) != 0)
{
if ((m_flag & FLAG_VBO_DYNAMIC) != 0)
{
bool ret = false;
if (m_vbo_vertex != 0)
{
ret = zglCore::updateBuffer(VERTEX_BUFFER, m_vbo_vertex,
(const void*) &m_vertex[0].x,
sizeof(zglVertexEx) * m_vertex_num);
//!< size changed ? OK, try again!
if (!ret)
{
zglCore::deleteBuffers(m_vbo_vertex);
m_vbo_vertex = 0;
m_vbo_vertex = zglCore::genBuffers(VERTEX_BUFFER,
(const void*) &m_vertex[0].x,
sizeof(zglVertexEx) * m_vertex_num, true);
}
}
if (m_vbo_index != 0)
{
if(m_indices_num != 0)
{
ret = zglCore::updateBuffer(INDEX_BUFFER, m_vbo_index,
(const void*) m_indices,
sizeof(unsigned short) * m_indices_num);
//!< size changed ? OK, try again!
if (!ret)
{
zglCore::deleteBuffers(m_vbo_index);
m_vbo_index = 0;
m_vbo_index = zglCore::genBuffers(INDEX_BUFFER,
(const void*) m_indices,
sizeof(unsigned short) * m_indices_num, true);
}
}
else
{
zglCore::deleteBuffers(m_vbo_index);
m_vbo_index = 0;
}
}
else
{
m_vbo_index = zglCore::genBuffers(INDEX_BUFFER, m_indices,
sizeof(unsigned short) * m_indices_num, true);
if (m_vbo_index == 0)
{
zglCore::deleteBuffers(m_vbo_vertex);
m_vbo_vertex = 0;
}
}
//printf("updateHwBuffers: vertex = %d, index = %d\n", m_vbo_vertex,
// m_vbo_index);
}
}
}
bool zglMesh::createHwBuffers()
{
if ( (m_flag & FLAG_VBO_ENABLE) != 0)
{
bool dynamic = (m_flag & FLAG_VBO_DYNAMIC) != 0;
m_vbo_vertex = zglCore::genBuffers(VERTEX_BUFFER,
(const void*) &m_vertex[0].x,
sizeof(zglVertexEx) * m_vertex_num, dynamic);
if (m_vbo_vertex == 0)
{
return false;
}
if (m_indices != NULL)
{
m_vbo_index = zglCore::genBuffers(INDEX_BUFFER, m_indices,
sizeof(unsigned short) * m_indices_num, dynamic);
if (m_vbo_index == 0)
{
zglCore::deleteBuffers(m_vbo_vertex);
m_vbo_vertex = 0;
return false;
}
}
//printf("zglMesh(%d) VBO(0x%x)vertex = %d, index = %d\n", this->m_type,
// (unsigned int)this, m_vbo_vertex, m_vbo_index);
return true;
}
return false;
}