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
21namespace android {
22
23Mesh::Mesh(Primitive primitive, size_t vertexCount, size_t vertexSize, size_t texCoordSize)
24    : mVertexCount(vertexCount), mVertexSize(vertexSize), mTexCoordsSize(texCoordSize),
25      mPrimitive(primitive)
26{
27    if (vertexCount == 0) {
28        mVertices = new float[1];
29        mVertices[0] = 0.0f;
30        mStride = 0;
31        return;
32    }
33
34    size_t stride = vertexSize + texCoordSize;
35    size_t remainder = (stride * vertexCount) / vertexCount;
36    // Since all of the input parameters are unsigned, if stride is less than
37    // either vertexSize or texCoordSize, it must have overflowed. remainder
38    // will be equal to stride as long as stride * vertexCount doesn't overflow.
39    if ((stride < vertexSize) || (remainder != stride)) {
40        ALOGE("Overflow in Mesh(..., %zu, %zu, %zu)", vertexCount, vertexSize,
41                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
55Mesh::~Mesh() {
56    delete [] mVertices;
57}
58
59Mesh::Primitive Mesh::getPrimitive() const {
60    return mPrimitive;
61}
62
63
64float const* Mesh::getPositions() const {
65    return mVertices;
66}
67float* Mesh::getPositions() {
68    return mVertices;
69}
70
71float const* Mesh::getTexCoords() const {
72    return mVertices + mVertexSize;
73}
74float* Mesh::getTexCoords() {
75    return mVertices + mVertexSize;
76}
77
78
79size_t Mesh::getVertexCount() const {
80    return mVertexCount;
81}
82
83size_t Mesh::getVertexSize() const {
84    return mVertexSize;
85}
86
87size_t Mesh::getTexCoordsSize() const {
88    return mTexCoordsSize;
89}
90
91size_t Mesh::getByteStride() const {
92    return mStride*sizeof(float);
93}
94
95size_t Mesh::getStride() const {
96    return mStride;
97}
98
99} /* namespace android */
100