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
22namespace android {
23
24class Mesh {
25public:
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;
43        VertexArray(float* data, size_t stride) : mData(data), mStride(stride) { }
44    public:
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        }
51    };
52
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); }
58
59    Primitive getPrimitive() const;
60
61    // returns a pointer to the vertices positions
62    float const* getPositions() const;
63
64    // returns a pointer to the vertices  texture coordinates
65    float const* getTexCoords() const;
66
67    // number of vertices in this mesh
68    size_t getVertexCount() const;
69
70    // dimension of vertices
71    size_t getVertexSize() const;
72
73    // dimension of texture coordinates
74    size_t getTexCoordsSize() const;
75
76    // return stride in bytes
77    size_t getByteStride() const;
78
79    // return stride in floats
80    size_t getStride() const;
81
82private:
83    Mesh(const Mesh&);
84    Mesh& operator = (const Mesh&);
85    Mesh const& operator = (const Mesh&) const;
86
87    float* getPositions();
88    float* getTexCoords();
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 */
100