Mesh.h 2.8 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. #ifndef SF_RENDER_ENGINE_MESH_H
  17. #define SF_RENDER_ENGINE_MESH_H
  18. #include <stdint.h>
  19. namespace android {
  20. class Mesh {
  21. public:
  22. enum Primitive {
  23. TRIANGLES = 0x0004, // GL_TRIANGLES
  24. TRIANGLE_STRIP = 0x0005, // GL_TRIANGLE_STRIP
  25. TRIANGLE_FAN = 0x0006 // GL_TRIANGLE_FAN
  26. };
  27. Mesh(Primitive primitive, size_t vertexCount, size_t vertexSize, size_t texCoordsSize = 0);
  28. ~Mesh();
  29. /*
  30. * VertexArray handles the stride automatically.
  31. */
  32. template <typename TYPE>
  33. class VertexArray {
  34. friend class Mesh;
  35. float* mData;
  36. size_t mStride;
  37. VertexArray(float* data, size_t stride) : mData(data), mStride(stride) { }
  38. public:
  39. TYPE& operator[](size_t index) {
  40. return *reinterpret_cast<TYPE*>(&mData[index*mStride]);
  41. }
  42. TYPE const& operator[](size_t index) const {
  43. return *reinterpret_cast<TYPE const*>(&mData[index*mStride]);
  44. }
  45. };
  46. template <typename TYPE>
  47. VertexArray<TYPE> getPositionArray() { return VertexArray<TYPE>(getPositions(), mStride); }
  48. template <typename TYPE>
  49. VertexArray<TYPE> getTexCoordArray() { return VertexArray<TYPE>(getTexCoords(), mStride); }
  50. Primitive getPrimitive() const;
  51. // returns a pointer to the vertices positions
  52. float const* getPositions() const;
  53. // returns a pointer to the vertices texture coordinates
  54. float const* getTexCoords() const;
  55. // number of vertices in this mesh
  56. size_t getVertexCount() const;
  57. // dimension of vertices
  58. size_t getVertexSize() const;
  59. // dimension of texture coordinates
  60. size_t getTexCoordsSize() const;
  61. // return stride in bytes
  62. size_t getByteStride() const;
  63. // return stride in floats
  64. size_t getStride() const;
  65. private:
  66. Mesh(const Mesh&);
  67. Mesh& operator = (const Mesh&);
  68. Mesh const& operator = (const Mesh&) const;
  69. float* getPositions();
  70. float* getTexCoords();
  71. float* mVertices;
  72. size_t mVertexCount;
  73. size_t mVertexSize;
  74. size_t mTexCoordsSize;
  75. size_t mStride;
  76. Primitive mPrimitive;
  77. };
  78. } /* namespace android */
  79. #endif /* SF_RENDER_ENGINE_MESH_H */