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 | /* |
| 36 | * VertexArray handles the stride automatically. It also provides |
| 37 | * a convenient way to set position and texture coordinates by using |
| 38 | * the usual x,y,z,w or s,t,r,q names. |
| 39 | */ |
| 40 | class VertexArray { |
| 41 | friend class Mesh; |
| 42 | float* mData; |
| 43 | size_t mStride; |
| 44 | VertexArray(float* data, size_t stride) : mData(data), mStride(stride) { } |
| 45 | public: |
| 46 | struct vertexData { |
| 47 | operator float*() { return reinterpret_cast<float*>(this); } |
| 48 | union { |
| 49 | struct { float x, y, z, w; }; |
| 50 | struct { float s, t, r, q; }; |
| 51 | }; |
| 52 | }; |
| 53 | vertexData& operator[](size_t index) { |
| 54 | return *reinterpret_cast<vertexData*>(&mData[index*mStride]); } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 55 | |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame^] | 56 | vertexData const& operator[](size_t index) const { |
| 57 | return *reinterpret_cast<vertexData const*>(&mData[index*mStride]); } |
| 58 | }; |
| 59 | |
| 60 | VertexArray getPositionArray() { return VertexArray(getPositions(), mStride); } |
| 61 | VertexArray getTexCoordArray() { return VertexArray(getTexCoords(), mStride); } |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 62 | |
| 63 | Primitive getPrimitive() const; |
| 64 | |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame^] | 65 | // returns a pointer to the vertices positions |
| 66 | float const* getPositions() const; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 67 | |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame^] | 68 | // returns a pointer to the vertices texture coordinates |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 69 | float const* getTexCoords() const; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 70 | |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame^] | 71 | // number of vertices in this mesh |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 72 | size_t getVertexCount() const; |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame^] | 73 | |
| 74 | // dimension of vertices |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 75 | size_t getVertexSize() const; |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame^] | 76 | |
| 77 | // dimension of texture coordinates |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 78 | size_t getTexCoordsSize() const; |
| 79 | |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame^] | 80 | // return stride in bytes |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 81 | size_t getByteStride() const; |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame^] | 82 | |
| 83 | // return stride in floats |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 84 | size_t getStride() const; |
| 85 | |
| 86 | private: |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame^] | 87 | Mesh(const Mesh&); |
| 88 | Mesh& operator = (const Mesh&); |
| 89 | Mesh const& operator = (const Mesh&) const; |
| 90 | |
| 91 | float* getPositions(); |
| 92 | float* getTexCoords(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 93 | float* mVertices; |
| 94 | size_t mVertexCount; |
| 95 | size_t mVertexSize; |
| 96 | size_t mTexCoordsSize; |
| 97 | size_t mStride; |
| 98 | Primitive mPrimitive; |
| 99 | }; |
| 100 | |
| 101 | |
| 102 | } /* namespace android */ |
| 103 | #endif /* SF_RENDER_ENGINE_MESH_H */ |