blob: 7618424e854b98d08cfea86a42e7ffc9f2ab1318 [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
Chia-I Wud3b13cb2018-09-13 13:31:26 -070020#include <vector>
21
Mathias Agopian3f844832013-08-07 21:24:32 -070022#include <stdint.h>
23
24namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070025namespace renderengine {
Mathias Agopian3f844832013-08-07 21:24:32 -070026
27class Mesh {
28public:
29 enum Primitive {
Chia-I Wub027f802017-11-29 14:00:52 -080030 TRIANGLES = 0x0004, // GL_TRIANGLES
31 TRIANGLE_STRIP = 0x0005, // GL_TRIANGLE_STRIP
32 TRIANGLE_FAN = 0x0006 // GL_TRIANGLE_FAN
Mathias Agopian3f844832013-08-07 21:24:32 -070033 };
34
35 Mesh(Primitive primitive, size_t vertexCount, size_t vertexSize, size_t texCoordsSize = 0);
Chia-I Wud3b13cb2018-09-13 13:31:26 -070036 ~Mesh() = default;
Mathias Agopian3f844832013-08-07 21:24:32 -070037
Mathias Agopian5cdc8992013-08-13 20:51:23 -070038 /*
Mathias Agopianff2ed702013-09-01 21:36:12 -070039 * VertexArray handles the stride automatically.
Mathias Agopian5cdc8992013-08-13 20:51:23 -070040 */
Mathias Agopianff2ed702013-09-01 21:36:12 -070041 template <typename TYPE>
Mathias Agopian5cdc8992013-08-13 20:51:23 -070042 class VertexArray {
43 friend class Mesh;
44 float* mData;
45 size_t mStride;
Chia-I Wub027f802017-11-29 14:00:52 -080046 VertexArray(float* data, size_t stride) : mData(data), mStride(stride) {}
47
Mathias Agopian5cdc8992013-08-13 20:51:23 -070048 public:
Chia-I Wub027f802017-11-29 14:00:52 -080049 TYPE& operator[](size_t index) { return *reinterpret_cast<TYPE*>(&mData[index * mStride]); }
Mathias Agopianff2ed702013-09-01 21:36:12 -070050 TYPE const& operator[](size_t index) const {
Chia-I Wub027f802017-11-29 14:00:52 -080051 return *reinterpret_cast<TYPE const*>(&mData[index * mStride]);
Mathias Agopianff2ed702013-09-01 21:36:12 -070052 }
Mathias Agopian5cdc8992013-08-13 20:51:23 -070053 };
54
Mathias Agopianff2ed702013-09-01 21:36:12 -070055 template <typename TYPE>
Chia-I Wub027f802017-11-29 14:00:52 -080056 VertexArray<TYPE> getPositionArray() {
57 return VertexArray<TYPE>(getPositions(), mStride);
58 }
Mathias Agopianff2ed702013-09-01 21:36:12 -070059
60 template <typename TYPE>
Chia-I Wub027f802017-11-29 14:00:52 -080061 VertexArray<TYPE> getTexCoordArray() {
62 return VertexArray<TYPE>(getTexCoords(), mStride);
63 }
Mathias Agopian3f844832013-08-07 21:24:32 -070064
Lucas Dupin1b6531c2018-07-05 17:18:21 -070065 template <typename TYPE>
66 VertexArray<TYPE> getCropCoordArray() {
67 return VertexArray<TYPE>(getCropCoords(), mStride);
68 }
69
Mathias Agopian3f844832013-08-07 21:24:32 -070070 Primitive getPrimitive() const;
71
Mathias Agopian5cdc8992013-08-13 20:51:23 -070072 // returns a pointer to the vertices positions
73 float const* getPositions() const;
Mathias Agopian3f844832013-08-07 21:24:32 -070074
Lucas Dupin1b6531c2018-07-05 17:18:21 -070075 // returns a pointer to the vertices texture coordinates
Mathias Agopian3f844832013-08-07 21:24:32 -070076 float const* getTexCoords() const;
Mathias Agopian3f844832013-08-07 21:24:32 -070077
Lucas Dupin1b6531c2018-07-05 17:18:21 -070078 // returns a pointer to the vertices crop coordinates
79 float const* getCropCoords() const;
80
Mathias Agopian5cdc8992013-08-13 20:51:23 -070081 // number of vertices in this mesh
Mathias Agopian3f844832013-08-07 21:24:32 -070082 size_t getVertexCount() const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -070083
84 // dimension of vertices
Mathias Agopian3f844832013-08-07 21:24:32 -070085 size_t getVertexSize() const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -070086
87 // dimension of texture coordinates
Mathias Agopian3f844832013-08-07 21:24:32 -070088 size_t getTexCoordsSize() const;
89
Mathias Agopian5cdc8992013-08-13 20:51:23 -070090 // return stride in bytes
Mathias Agopian3f844832013-08-07 21:24:32 -070091 size_t getByteStride() const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -070092
93 // return stride in floats
Mathias Agopian3f844832013-08-07 21:24:32 -070094 size_t getStride() const;
95
96private:
Mathias Agopian5cdc8992013-08-13 20:51:23 -070097 Mesh(const Mesh&);
Chia-I Wub027f802017-11-29 14:00:52 -080098 Mesh& operator=(const Mesh&);
99 Mesh const& operator=(const Mesh&) const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700100
101 float* getPositions();
102 float* getTexCoords();
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700103 float* getCropCoords();
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700104
105 std::vector<float> mVertices;
Mathias Agopian3f844832013-08-07 21:24:32 -0700106 size_t mVertexCount;
107 size_t mVertexSize;
108 size_t mTexCoordsSize;
109 size_t mStride;
110 Primitive mPrimitive;
111};
112
Peiyong Lin46080ef2018-10-26 18:43:14 -0700113} // namespace renderengine
114} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700115#endif /* SF_RENDER_ENGINE_MESH_H */