1 /*
2  * Copyright 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "Mesh.h"
18 
19 #include <utils/Log.h>
20 
21 namespace android {
22 
Mesh(Primitive primitive,size_t vertexCount,size_t vertexSize,size_t texCoordSize)23 Mesh::Mesh(Primitive primitive, size_t vertexCount, size_t vertexSize, size_t texCoordSize)
24       : mVertexCount(vertexCount),
25         mVertexSize(vertexSize),
26         mTexCoordsSize(texCoordSize),
27         mPrimitive(primitive) {
28     if (vertexCount == 0) {
29         mVertices = new float[1];
30         mVertices[0] = 0.0f;
31         mStride = 0;
32         return;
33     }
34 
35     size_t stride = vertexSize + texCoordSize;
36     size_t remainder = (stride * vertexCount) / vertexCount;
37     // Since all of the input parameters are unsigned, if stride is less than
38     // either vertexSize or texCoordSize, it must have overflowed. remainder
39     // will be equal to stride as long as stride * vertexCount doesn't overflow.
40     if ((stride < vertexSize) || (remainder != stride)) {
41         ALOGE("Overflow in Mesh(..., %zu, %zu, %zu)", vertexCount, vertexSize, texCoordSize);
42         mVertices = new float[1];
43         mVertices[0] = 0.0f;
44         mVertexCount = 0;
45         mVertexSize = 0;
46         mTexCoordsSize = 0;
47         mStride = 0;
48         return;
49     }
50 
51     mVertices = new float[stride * vertexCount];
52     mStride = stride;
53 }
54 
~Mesh()55 Mesh::~Mesh() {
56     delete[] mVertices;
57 }
58 
getPrimitive() const59 Mesh::Primitive Mesh::getPrimitive() const {
60     return mPrimitive;
61 }
62 
getPositions() const63 float const* Mesh::getPositions() const {
64     return mVertices;
65 }
getPositions()66 float* Mesh::getPositions() {
67     return mVertices;
68 }
69 
getTexCoords() const70 float const* Mesh::getTexCoords() const {
71     return mVertices + mVertexSize;
72 }
getTexCoords()73 float* Mesh::getTexCoords() {
74     return mVertices + mVertexSize;
75 }
76 
getVertexCount() const77 size_t Mesh::getVertexCount() const {
78     return mVertexCount;
79 }
80 
getVertexSize() const81 size_t Mesh::getVertexSize() const {
82     return mVertexSize;
83 }
84 
getTexCoordsSize() const85 size_t Mesh::getTexCoordsSize() const {
86     return mTexCoordsSize;
87 }
88 
getByteStride() const89 size_t Mesh::getByteStride() const {
90     return mStride * sizeof(float);
91 }
92 
getStride() const93 size_t Mesh::getStride() const {
94     return mStride;
95 }
96 
97 } /* namespace android */
98