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