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