| 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) | 
|  | 24 | : mVertexCount(vertexCount), mVertexSize(vertexSize), mTexCoordsSize(texCoordSize), | 
|  | 25 | mPrimitive(primitive) | 
|  | 26 | { | 
| Dan Stoza | ab79e33 | 2015-04-29 13:30:31 -0700 | [diff] [blame] | 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; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
|  | 55 | Mesh::~Mesh() { | 
|  | 56 | delete [] mVertices; | 
|  | 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 |  | 
|  | 63 |  | 
| Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 64 | float const* Mesh::getPositions() const { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 65 | return mVertices; | 
|  | 66 | } | 
| Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 67 | float* Mesh::getPositions() { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 68 | return mVertices; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | float const* Mesh::getTexCoords() const { | 
|  | 72 | return mVertices + mVertexSize; | 
|  | 73 | } | 
|  | 74 | float* Mesh::getTexCoords() { | 
|  | 75 | return mVertices + mVertexSize; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 |  | 
|  | 79 | size_t Mesh::getVertexCount() const { | 
|  | 80 | return mVertexCount; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | size_t Mesh::getVertexSize() const { | 
|  | 84 | return mVertexSize; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | size_t Mesh::getTexCoordsSize() const { | 
|  | 88 | return mTexCoordsSize; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | size_t Mesh::getByteStride() const { | 
|  | 92 | return mStride*sizeof(float); | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | size_t Mesh::getStride() const { | 
|  | 96 | return mStride; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | } /* namespace android */ |