blob: d0a9ac0c65ec7bbe1b7f925f1353e37757d4624c [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 {
Chia-I Wub027f802017-11-29 14:00:52 -080027 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;
Chia-I Wub027f802017-11-29 14:00:52 -080043 VertexArray(float* data, size_t stride) : mData(data), mStride(stride) {}
44
Mathias Agopian5cdc8992013-08-13 20:51:23 -070045 public:
Chia-I Wub027f802017-11-29 14:00:52 -080046 TYPE& operator[](size_t index) { return *reinterpret_cast<TYPE*>(&mData[index * mStride]); }
Mathias Agopianff2ed702013-09-01 21:36:12 -070047 TYPE const& operator[](size_t index) const {
Chia-I Wub027f802017-11-29 14:00:52 -080048 return *reinterpret_cast<TYPE const*>(&mData[index * mStride]);
Mathias Agopianff2ed702013-09-01 21:36:12 -070049 }
Mathias Agopian5cdc8992013-08-13 20:51:23 -070050 };
51
Mathias Agopianff2ed702013-09-01 21:36:12 -070052 template <typename TYPE>
Chia-I Wub027f802017-11-29 14:00:52 -080053 VertexArray<TYPE> getPositionArray() {
54 return VertexArray<TYPE>(getPositions(), mStride);
55 }
Mathias Agopianff2ed702013-09-01 21:36:12 -070056
57 template <typename TYPE>
Chia-I Wub027f802017-11-29 14:00:52 -080058 VertexArray<TYPE> getTexCoordArray() {
59 return VertexArray<TYPE>(getTexCoords(), mStride);
60 }
Mathias Agopian3f844832013-08-07 21:24:32 -070061
62 Primitive getPrimitive() const;
63
Mathias Agopian5cdc8992013-08-13 20:51:23 -070064 // returns a pointer to the vertices positions
65 float const* getPositions() const;
Mathias Agopian3f844832013-08-07 21:24:32 -070066
Mathias Agopian5cdc8992013-08-13 20:51:23 -070067 // returns a pointer to the vertices texture coordinates
Mathias Agopian3f844832013-08-07 21:24:32 -070068 float const* getTexCoords() const;
Mathias Agopian3f844832013-08-07 21:24:32 -070069
Mathias Agopian5cdc8992013-08-13 20:51:23 -070070 // number of vertices in this mesh
Mathias Agopian3f844832013-08-07 21:24:32 -070071 size_t getVertexCount() const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -070072
73 // dimension of vertices
Mathias Agopian3f844832013-08-07 21:24:32 -070074 size_t getVertexSize() const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -070075
76 // dimension of texture coordinates
Mathias Agopian3f844832013-08-07 21:24:32 -070077 size_t getTexCoordsSize() const;
78
Mathias Agopian5cdc8992013-08-13 20:51:23 -070079 // return stride in bytes
Mathias Agopian3f844832013-08-07 21:24:32 -070080 size_t getByteStride() const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -070081
82 // return stride in floats
Mathias Agopian3f844832013-08-07 21:24:32 -070083 size_t getStride() const;
84
85private:
Mathias Agopian5cdc8992013-08-13 20:51:23 -070086 Mesh(const Mesh&);
Chia-I Wub027f802017-11-29 14:00:52 -080087 Mesh& operator=(const Mesh&);
88 Mesh const& operator=(const Mesh&) const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -070089
90 float* getPositions();
91 float* getTexCoords();
Mathias Agopian3f844832013-08-07 21:24:32 -070092 float* mVertices;
93 size_t mVertexCount;
94 size_t mVertexSize;
95 size_t mTexCoordsSize;
96 size_t mStride;
97 Primitive mPrimitive;
98};
99
Mathias Agopian3f844832013-08-07 21:24:32 -0700100} /* namespace android */
101#endif /* SF_RENDER_ENGINE_MESH_H */