Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 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 | |
Dan Stoza | ab79e33 | 2015-04-29 13:30:31 -0700 | [diff] [blame] | 19 | #include <utils/Log.h> |
| 20 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 21 | namespace android { |
| 22 | |
| 23 | Mesh::Mesh(Primitive primitive, size_t vertexCount, size_t vertexSize, size_t texCoordSize) |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame^] | 24 | : mVertexCount(vertexCount), |
| 25 | mVertexSize(vertexSize), |
| 26 | mTexCoordsSize(texCoordSize), |
| 27 | mPrimitive(primitive) { |
Dan Stoza | ab79e33 | 2015-04-29 13:30:31 -0700 | [diff] [blame] | 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)) { |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame^] | 41 | ALOGE("Overflow in Mesh(..., %zu, %zu, %zu)", vertexCount, vertexSize, texCoordSize); |
Dan Stoza | ab79e33 | 2015-04-29 13:30:31 -0700 | [diff] [blame] | 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; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | Mesh::~Mesh() { |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame^] | 56 | delete[] mVertices; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 59 | Mesh::Primitive Mesh::getPrimitive() const { |
| 60 | return mPrimitive; |
| 61 | } |
| 62 | |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 63 | float const* Mesh::getPositions() const { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 64 | return mVertices; |
| 65 | } |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 66 | float* Mesh::getPositions() { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 67 | return mVertices; |
| 68 | } |
| 69 | |
| 70 | float const* Mesh::getTexCoords() const { |
| 71 | return mVertices + mVertexSize; |
| 72 | } |
| 73 | float* Mesh::getTexCoords() { |
| 74 | return mVertices + mVertexSize; |
| 75 | } |
| 76 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 77 | size_t Mesh::getVertexCount() const { |
| 78 | return mVertexCount; |
| 79 | } |
| 80 | |
| 81 | size_t Mesh::getVertexSize() const { |
| 82 | return mVertexSize; |
| 83 | } |
| 84 | |
| 85 | size_t Mesh::getTexCoordsSize() const { |
| 86 | return mTexCoordsSize; |
| 87 | } |
| 88 | |
| 89 | size_t Mesh::getByteStride() const { |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame^] | 90 | return mStride * sizeof(float); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | size_t Mesh::getStride() const { |
| 94 | return mStride; |
| 95 | } |
| 96 | |
| 97 | } /* namespace android */ |