blob: 6a62b1d7ddba97828ed3959b967ac7ce3fdbdeed [file] [log] [blame]
Mathias Agopian3f844832013-08-07 21:24:32 -07001/*
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
Dan Stozaab79e332015-04-29 13:30:31 -070019#include <utils/Log.h>
20
Mathias Agopian3f844832013-08-07 21:24:32 -070021namespace android {
22
23Mesh::Mesh(Primitive primitive, size_t vertexCount, size_t vertexSize, size_t texCoordSize)
Chia-I Wub027f802017-11-29 14:00:52 -080024 : mVertexCount(vertexCount),
25 mVertexSize(vertexSize),
26 mTexCoordsSize(texCoordSize),
27 mPrimitive(primitive) {
Dan Stozaab79e332015-04-29 13:30:31 -070028 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)) {
Chia-I Wub027f802017-11-29 14:00:52 -080041 ALOGE("Overflow in Mesh(..., %zu, %zu, %zu)", vertexCount, vertexSize, texCoordSize);
Dan Stozaab79e332015-04-29 13:30:31 -070042 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;
Mathias Agopian3f844832013-08-07 21:24:32 -070053}
54
55Mesh::~Mesh() {
Chia-I Wub027f802017-11-29 14:00:52 -080056 delete[] mVertices;
Mathias Agopian3f844832013-08-07 21:24:32 -070057}
58
Mathias Agopian3f844832013-08-07 21:24:32 -070059Mesh::Primitive Mesh::getPrimitive() const {
60 return mPrimitive;
61}
62
Mathias Agopian5cdc8992013-08-13 20:51:23 -070063float const* Mesh::getPositions() const {
Mathias Agopian3f844832013-08-07 21:24:32 -070064 return mVertices;
65}
Mathias Agopian5cdc8992013-08-13 20:51:23 -070066float* Mesh::getPositions() {
Mathias Agopian3f844832013-08-07 21:24:32 -070067 return mVertices;
68}
69
70float const* Mesh::getTexCoords() const {
71 return mVertices + mVertexSize;
72}
73float* Mesh::getTexCoords() {
74 return mVertices + mVertexSize;
75}
76
Mathias Agopian3f844832013-08-07 21:24:32 -070077size_t Mesh::getVertexCount() const {
78 return mVertexCount;
79}
80
81size_t Mesh::getVertexSize() const {
82 return mVertexSize;
83}
84
85size_t Mesh::getTexCoordsSize() const {
86 return mTexCoordsSize;
87}
88
89size_t Mesh::getByteStride() const {
Chia-I Wub027f802017-11-29 14:00:52 -080090 return mStride * sizeof(float);
Mathias Agopian3f844832013-08-07 21:24:32 -070091}
92
93size_t Mesh::getStride() const {
94 return mStride;
95}
96
97} /* namespace android */