blob: 160d765ba283dfb307f0ff04156e42b7ec6cb5a8 [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 /*
36 * VertexArray handles the stride automatically. It also provides
37 * a convenient way to set position and texture coordinates by using
38 * the usual x,y,z,w or s,t,r,q names.
39 */
40 class VertexArray {
41 friend class Mesh;
42 float* mData;
43 size_t mStride;
44 VertexArray(float* data, size_t stride) : mData(data), mStride(stride) { }
45 public:
46 struct vertexData {
47 operator float*() { return reinterpret_cast<float*>(this); }
48 union {
49 struct { float x, y, z, w; };
50 struct { float s, t, r, q; };
51 };
52 };
53 vertexData& operator[](size_t index) {
54 return *reinterpret_cast<vertexData*>(&mData[index*mStride]); }
Mathias Agopian3f844832013-08-07 21:24:32 -070055
Mathias Agopian5cdc8992013-08-13 20:51:23 -070056 vertexData const& operator[](size_t index) const {
57 return *reinterpret_cast<vertexData const*>(&mData[index*mStride]); }
58 };
59
60 VertexArray getPositionArray() { return VertexArray(getPositions(), mStride); }
61 VertexArray getTexCoordArray() { return VertexArray(getTexCoords(), mStride); }
Mathias Agopian3f844832013-08-07 21:24:32 -070062
63 Primitive getPrimitive() const;
64
Mathias Agopian5cdc8992013-08-13 20:51:23 -070065 // returns a pointer to the vertices positions
66 float const* getPositions() const;
Mathias Agopian3f844832013-08-07 21:24:32 -070067
Mathias Agopian5cdc8992013-08-13 20:51:23 -070068 // returns a pointer to the vertices texture coordinates
Mathias Agopian3f844832013-08-07 21:24:32 -070069 float const* getTexCoords() const;
Mathias Agopian3f844832013-08-07 21:24:32 -070070
Mathias Agopian5cdc8992013-08-13 20:51:23 -070071 // number of vertices in this mesh
Mathias Agopian3f844832013-08-07 21:24:32 -070072 size_t getVertexCount() const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -070073
74 // dimension of vertices
Mathias Agopian3f844832013-08-07 21:24:32 -070075 size_t getVertexSize() const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -070076
77 // dimension of texture coordinates
Mathias Agopian3f844832013-08-07 21:24:32 -070078 size_t getTexCoordsSize() const;
79
Mathias Agopian5cdc8992013-08-13 20:51:23 -070080 // return stride in bytes
Mathias Agopian3f844832013-08-07 21:24:32 -070081 size_t getByteStride() const;
Mathias Agopian5cdc8992013-08-13 20:51:23 -070082
83 // return stride in floats
Mathias Agopian3f844832013-08-07 21:24:32 -070084 size_t getStride() const;
85
86private:
Mathias Agopian5cdc8992013-08-13 20:51:23 -070087 Mesh(const Mesh&);
88 Mesh& operator = (const Mesh&);
89 Mesh const& operator = (const Mesh&) const;
90
91 float* getPositions();
92 float* getTexCoords();
Mathias Agopian3f844832013-08-07 21:24:32 -070093 float* mVertices;
94 size_t mVertexCount;
95 size_t mVertexSize;
96 size_t mTexCoordsSize;
97 size_t mStride;
98 Primitive mPrimitive;
99};
100
101
102} /* namespace android */
103#endif /* SF_RENDER_ENGINE_MESH_H */