Skip to content

Commit d3299f4

Browse files
fixes for Sprite3D
Adds copyright header removes compilation warnings cleans code a bit
1 parent 1377787 commit d3299f4

File tree

12 files changed

+257
-84
lines changed

12 files changed

+257
-84
lines changed

cocos/3d/CCMesh.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/****************************************************************************
2+
Copyright (c) 2014 Chukong Technologies Inc.
3+
4+
http://www.cocos2d-x.org
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
****************************************************************************/
124

225
#include "CCMesh.h"
326

@@ -25,7 +48,10 @@ bool RenderMeshData::hasVertexAttrib(int attrib)
2548
return false;
2649
}
2750

28-
bool RenderMeshData::initFrom(const std::vector<float>& positions, const std::vector<float>& normals, const std::vector<float>& texs, const std::vector<unsigned short>& indices)
51+
bool RenderMeshData::initFrom(const std::vector<float>& positions,
52+
const std::vector<float>& normals,
53+
const std::vector<float>& texs,
54+
const std::vector<unsigned short>& indices)
2955
{
3056
CC_ASSERT(positions.size()<65536 * 3 && "index may out of bound");
3157

@@ -99,11 +125,10 @@ bool RenderMeshData::initFrom(const std::vector<float>& positions, const std::ve
99125
Mesh::Mesh()
100126
:_vertexBuffer(0)
101127
, _indexBuffer(0)
102-
, _primitiveType(PrimitiveType_TRIANGLES)
103-
, _indexFormat(IndexFormat_INDEX16)
128+
, _primitiveType(PrimitiveType::TRIANGLES)
129+
, _indexFormat(IndexFormat::INDEX16)
104130
, _indexCount(0)
105131
{
106-
107132
}
108133

109134
Mesh::~Mesh()
@@ -146,8 +171,8 @@ void Mesh::cleanAndFreeBuffers()
146171
glDeleteBuffers(1, &_indexBuffer);
147172
_indexBuffer = 0;
148173
}
149-
_primitiveType = PrimitiveType_TRIANGLES;
150-
_indexFormat = IndexFormat_INDEX16;
174+
_primitiveType = PrimitiveType::TRIANGLES;
175+
_indexFormat = IndexFormat::INDEX16;
151176
_indexCount = 0;
152177
}
153178

@@ -169,13 +194,13 @@ void Mesh::buildBuffer()
169194
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
170195

171196
unsigned int indexSize = 2;
172-
IndexFormat indexformat = IndexFormat_INDEX16;
197+
IndexFormat indexformat = IndexFormat::INDEX16;
173198

174199
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexSize * _renderdata._indices.size(), &_renderdata._indices[0], GL_STATIC_DRAW);
175200

176201
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
177202

178-
_primitiveType = PrimitiveType_TRIANGLES;
203+
_primitiveType = PrimitiveType::TRIANGLES;
179204
_indexFormat = indexformat;
180205
_indexCount = _renderdata._indices.size();
181206
}

cocos/3d/CCMesh.h

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/****************************************************************************
2+
Copyright (c) 2014 Chukong Technologies Inc.
3+
4+
http://www.cocos2d-x.org
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
****************************************************************************/
124

225
#ifndef __CCMESH_H__
326
#define __CCMESH_H__
@@ -12,27 +35,6 @@
1235

1336
NS_CC_BEGIN
1437

15-
/**
16-
* Defines supported index formats.
17-
*/
18-
enum IndexFormat
19-
{
20-
IndexFormat_INDEX8 = GL_UNSIGNED_BYTE,
21-
IndexFormat_INDEX16 = GL_UNSIGNED_SHORT,
22-
};
23-
24-
/**
25-
* Defines supported primitive types.
26-
*/
27-
enum PrimitiveType
28-
{
29-
PrimitiveType_TRIANGLES = GL_TRIANGLES,
30-
PrimitiveType_TRIANGLE_STRIP = GL_TRIANGLE_STRIP,
31-
PrimitiveType_LINES = GL_LINES,
32-
PrimitiveType_LINE_STRIP = GL_LINE_STRIP,
33-
PrimitiveType_POINTS = GL_POINTS
34-
};
35-
3638
//mesh vertex attribute
3739
struct MeshVertexAttrib
3840
{
@@ -58,23 +60,41 @@ class RenderMeshData
5860

5961
protected:
6062
int _vertexsizeBytes;
61-
int _vertexNum;
63+
ssize_t _vertexNum;
6264
std::vector<float> _vertexs;
6365
std::vector<unsigned short> _indices;
6466
std::vector<MeshVertexAttrib> _vertexAttribs;
6567
};
6668

69+
/** Mesh: TODO, add description of Mesh */
6770
class Mesh : public Ref
6871
{
6972
public:
73+
/** Defines supported index formats. */
74+
enum class IndexFormat
75+
{
76+
INDEX8 = GL_UNSIGNED_BYTE,
77+
INDEX16 = GL_UNSIGNED_SHORT,
78+
};
79+
80+
/** Defines supported primitive types. */
81+
enum class PrimitiveType
82+
{
83+
TRIANGLES = GL_TRIANGLES,
84+
TRIANGLE_STRIP = GL_TRIANGLE_STRIP,
85+
LINES = GL_LINES,
86+
LINE_STRIP = GL_LINE_STRIP,
87+
POINTS = GL_POINTS
88+
};
89+
7090
//create
7191
static Mesh* create(const std::vector<float>& positions, const std::vector<float>& normals, const std::vector<float>& texs, const std::vector<unsigned short>& indices);
7292

7393
//get vertex buffer
7494
inline GLuint getVertexBuffer() const { return _vertexBuffer; }
7595

7696
//get mesh vertex attribute count
77-
int getMeshVertexAttribCount() const { return _renderdata._vertexAttribs.size(); }
97+
ssize_t getMeshVertexAttribCount() const { return _renderdata._vertexAttribs.size(); }
7898
//get MeshVertexAttribute by index
7999
const MeshVertexAttrib& getMeshVertexAttribute(int idx) const { return _renderdata._vertexAttribs[idx]; }
80100
//has vertex attribute?
@@ -83,12 +103,13 @@ class Mesh : public Ref
83103
int getVertexSizeInBytes() const { return _renderdata._vertexsizeBytes; }
84104

85105
PrimitiveType getPrimitiveType() const { return _primitiveType; }
86-
unsigned int getIndexCount() const { return _indexCount; }
106+
ssize_t getIndexCount() const { return _indexCount; }
87107
IndexFormat getIndexFormat() const { return _indexFormat; }
88108
GLuint getIndexBuffer() const {return _indexBuffer; }
89109

90110
//build vertex buffer from renderdata
91111
void restore();
112+
92113
protected:
93114
Mesh();
94115
virtual ~Mesh();
@@ -97,17 +118,16 @@ class Mesh : public Ref
97118
//build buffer
98119
void buildBuffer();
99120
void cleanAndFreeBuffers();
100-
101-
protected:
121+
102122
PrimitiveType _primitiveType;
103123
IndexFormat _indexFormat;
104124
GLuint _vertexBuffer;
105125
GLuint _indexBuffer;
106-
unsigned int _indexCount;
107-
108-
private:
126+
ssize_t _indexCount;
127+
109128
RenderMeshData _renderdata;
110129
};
111130

112131
NS_CC_END
132+
113133
#endif // __CCMESH_H_

cocos/3d/CCObjLoader.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ static inline int fixIndex(int idx, int n)
8383
static inline std::string parseString(const char*& token)
8484
{
8585
std::string s;
86-
int b = strspn(token, " \t");
87-
int e = strcspn(token, " \t\r");
86+
auto b = strspn(token, " \t");
87+
auto e = strcspn(token, " \t\r");
8888
s = std::string(&token[b], &token[e]);
8989

9090
token += (e - b);
@@ -154,11 +154,11 @@ static vertex_index parseTriple(const char* &token, int vsize, int vnsize, int v
154154
return vi;
155155
}
156156

157-
static unsigned int updateVertex( std::map<vertex_index, unsigned int>& vertexCache, std::vector<float>& positions, std::vector<float>& normals,
157+
static ssize_t updateVertex( std::map<vertex_index, ssize_t>& vertexCache, std::vector<float>& positions, std::vector<float>& normals,
158158
std::vector<float>& texcoords, const std::vector<float>& in_positions, const std::vector<float>& in_normals, const std::vector<float>& in_texcoords,
159159
const vertex_index& i)
160160
{
161-
const std::map<vertex_index, unsigned int>::iterator it = vertexCache.find(i);
161+
const auto it = vertexCache.find(i);
162162

163163
if (it != vertexCache.end())
164164
{
@@ -185,13 +185,13 @@ static unsigned int updateVertex( std::map<vertex_index, unsigned int>& vertexCa
185185
texcoords.push_back(in_texcoords[2*i.vt_idx+1]);
186186
}
187187

188-
unsigned int idx = positions.size() / 3 - 1;
188+
auto idx = positions.size() / 3 - 1;
189189
vertexCache[i] = idx;
190190

191191
return idx;
192192
}
193193

194-
static bool exportFaceGroupToShape( std::map<vertex_index, unsigned int>& vertexCache, ObjLoader::shapes_t& shapes, const std::vector<float> &in_positions,
194+
static bool exportFaceGroupToShape( std::map<vertex_index, ssize_t>& vertexCache, ObjLoader::shapes_t& shapes, const std::vector<float> &in_positions,
195195
const std::vector<float> &in_normals, const std::vector<float> &in_texcoords, const std::vector<std::vector<vertex_index> >& faceGroup,
196196
const ObjLoader::material_t &material, const std::string &name)
197197
{
@@ -247,7 +247,7 @@ std::string trim(const std::string& str)
247247
{
248248
if (str.empty())
249249
return str;
250-
int len = str.length();
250+
auto len = str.length();
251251
char c = str[len - 1];
252252
while (c == '\r' || c == '\n')
253253
{
@@ -491,7 +491,7 @@ std::string LoadMtl ( std::map<std::string, ObjLoader::material_t>& material_map
491491
}
492492
if(_space)
493493
{
494-
int len = _space - token;
494+
auto len = _space - token;
495495
std::string key(token, len);
496496
std::string value = _space + 1;
497497
material.unknown_parameter.insert(std::pair<std::string, std::string>(key, value));
@@ -508,7 +508,7 @@ std::string ObjLoader::LoadObj(shapes_t& shapes, const char* filename, const cha
508508

509509
std::stringstream err;
510510
std::istringstream ifs(FileUtils::getInstance()->getStringFromFile(filename));
511-
std::map<vertex_index, unsigned int> vertexCache;
511+
std::map<vertex_index, ssize_t> vertexCache;
512512
//std::ifstream ifs(filename);
513513

514514
if (!ifs)
@@ -605,7 +605,7 @@ std::string ObjLoader::LoadObj(shapes_t& shapes, const char* filename, const cha
605605
while (!isNewLine(token[0])) {
606606
vertex_index vi = parseTriple(token, v.size() / 3, vn.size() / 3, vt.size() / 2);
607607
face.push_back(vi);
608-
int n = strspn(token, " \t\r");
608+
auto n = strspn(token, " \t\r");
609609
token += n;
610610
}
611611

0 commit comments

Comments
 (0)