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