Mesh.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #include "Mesh.h"
  17. #include <utils/Log.h>
  18. namespace android {
  19. Mesh::Mesh(Primitive primitive, size_t vertexCount, size_t vertexSize, size_t texCoordSize)
  20. : mVertexCount(vertexCount), mVertexSize(vertexSize), mTexCoordsSize(texCoordSize),
  21. mPrimitive(primitive)
  22. {
  23. if (vertexCount == 0) {
  24. mVertices = new float[1];
  25. mVertices[0] = 0.0f;
  26. mStride = 0;
  27. return;
  28. }
  29. size_t stride = vertexSize + texCoordSize;
  30. size_t remainder = (stride * vertexCount) / vertexCount;
  31. // Since all of the input parameters are unsigned, if stride is less than
  32. // either vertexSize or texCoordSize, it must have overflowed. remainder
  33. // will be equal to stride as long as stride * vertexCount doesn't overflow.
  34. if ((stride < vertexSize) || (remainder != stride)) {
  35. ALOGE("Overflow in Mesh(..., %zu, %zu, %zu)", vertexCount, vertexSize,
  36. texCoordSize);
  37. mVertices = new float[1];
  38. mVertices[0] = 0.0f;
  39. mVertexCount = 0;
  40. mVertexSize = 0;
  41. mTexCoordsSize = 0;
  42. mStride = 0;
  43. return;
  44. }
  45. mVertices = new float[stride * vertexCount];
  46. mStride = stride;
  47. }
  48. Mesh::~Mesh() {
  49. delete [] mVertices;
  50. }
  51. Mesh::Primitive Mesh::getPrimitive() const {
  52. return mPrimitive;
  53. }
  54. float const* Mesh::getPositions() const {
  55. return mVertices;
  56. }
  57. float* Mesh::getPositions() {
  58. return mVertices;
  59. }
  60. float const* Mesh::getTexCoords() const {
  61. return mVertices + mVertexSize;
  62. }
  63. float* Mesh::getTexCoords() {
  64. return mVertices + mVertexSize;
  65. }
  66. size_t Mesh::getVertexCount() const {
  67. return mVertexCount;
  68. }
  69. size_t Mesh::getVertexSize() const {
  70. return mVertexSize;
  71. }
  72. size_t Mesh::getTexCoordsSize() const {
  73. return mTexCoordsSize;
  74. }
  75. size_t Mesh::getByteStride() const {
  76. return mStride*sizeof(float);
  77. }
  78. size_t Mesh::getStride() const {
  79. return mStride;
  80. }
  81. } /* namespace android */