blob: b6d42b09757c5dfb39585b9951d41944b8858089 [file] [log] [blame]
Mathias Agopian3f844832013-08-07 21:24:32 -07001/*
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 {
Mathias Agopian5cdc8992013-08-13 20:51:23 -070027 TRIANGLES = 0x0004, // GL_TRIANGLES
28 TRIANGLE_STRIP = 0x0005, // GL_TRIANGLE_STRIP
29 TRIANGLE_FAN = 0x0006 // GL_TRIANGLE_FAN
Mathias Agopian3f844832013-08-07 21:24:32 -070030 };
31
32 Mesh(Primitive primitive, size_t vertexCount, size_t vertexSize, size_t texCoordsSize = 0);
33 ~Mesh();
34
Mathias Agopian5cdc8992013-08-13 20:51:23 -070035 /*
Mathias Agopianff2ed702013-09-01 21:36:12 -070036 * VertexArray handles the stride automatically.
Mathias Agopian5cdc8992013-08-13 20:51:23 -070037 */
Mathias Agopianff2ed702013-09-01 21:36:12 -070038 template <typename TYPE>
Mathias Agopian5cdc8992013-08-13 20:51:23 -070039 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 Agopianff2ed702013-09-01 21:36:12 -070045 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 Agopian5cdc8992013-08-13 20:51:23 -070051 };
52
Mathias Agopianff2ed702013-09-01 21:36:12 -070053 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 Agopian3f844832013-08-07 21:24:32 -070058
59 Primitive getPrimitive() const;
60
Mathias Agopian5cdc8992013-08-13 20:51:23 -070061 // returns a pointer to the vertices positions
62 float const* getPositions() const;
Mathias Agopian3f844832013-08-07 21:24:32 -070063
Mathias Agopian5cdc8992013-08-13 20:51:23 -070064 // returns a pointer to the vertices texture coordinates
Mathias Agopian3f844832013-08-07 21:24:32 -070065 float const* getTexCoords() const;
Mathias Agopian3f844832013-08-07 21:24:32 -070066
Mathias Agopian5cdc8992013-08-13 20:51:23 -070067 // number of vertices in this mesh
Mathias Agopian3f844832013-08-07 21:24:32 -070068 size_t getVertexCount() const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -070069
70 // dimension of vertices
Mathias Agopian3f844832013-08-07 21:24:32 -070071 size_t getVertexSize() const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -070072
73 // dimension of texture coordinates
Mathias Agopian3f844832013-08-07 21:24:32 -070074 size_t getTexCoordsSize() const;
75
Mathias Agopian5cdc8992013-08-13 20:51:23 -070076 // return stride in bytes
Mathias Agopian3f844832013-08-07 21:24:32 -070077 size_t getByteStride() const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -070078
79 // return stride in floats
Mathias Agopian3f844832013-08-07 21:24:32 -070080 size_t getStride() const;
81
82private:
Mathias Agopian5cdc8992013-08-13 20:51:23 -070083 Mesh(const Mesh&);
84 Mesh& operator = (const Mesh&);
85 Mesh const& operator = (const Mesh&) const;
86
87 float* getPositions();
88 float* getTexCoords();
Mathias Agopian3f844832013-08-07 21:24:32 -070089 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 */