3
* Copyright (C) 2009 Fabien Chereau
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
#ifndef __STELVERTEXARRAY_HPP__
21
#define __STELVERTEXARRAY_HPP__
25
#include "VecMath.hpp"
27
struct StelVertexArray
29
// TODO maybe merge this with StelPainter DrawingMode
30
enum StelPrimitiveType
32
Points = 0x0000, // GL_POINTS
33
Lines = 0x0001, // GL_LINES
34
LineLoop = 0x0002, // GL_LINE_LOOP
35
LineStrip = 0x0003, // GL_LINE_STRIP
36
Triangles = 0x0004, // GL_TRIANGLES
37
TriangleStrip = 0x0005, // GL_TRIANGLE_STRIP
38
TriangleFan = 0x0006 // GL_TRIANGLE_FAN
41
StelVertexArray(StelPrimitiveType pType=StelVertexArray::Triangles) : primitiveType(pType) {;}
42
StelVertexArray(const QVector<Vec3d>& v, StelPrimitiveType pType=StelVertexArray::Triangles,const QVector<Vec2f>& t=QVector<Vec2f>(), const QVector<unsigned int> i=QVector<unsigned int>()) :
43
vertex(v), texCoords(t), indices(i), primitiveType(pType) {;}
45
//! OpenGL compatible array of 3D vertex to be displayed using vertex arrays.
46
//! TODO, move to float? Most of the vectors are normalized, thus the precision is around 1E-45 using float
47
//! which is enough (=2E-37 milli arcsec).
48
QVector<Vec3d> vertex;
49
//! OpenGL compatible array of edge flags to be displayed using vertex arrays.
50
QVector<Vec2f> texCoords;
51
//! OpenGL compatible array of indices for the vertex and the textures
52
QVector<unsigned int> indices;
54
StelPrimitiveType primitiveType;
56
bool isIndexed() const {return !indices.isEmpty();}
58
bool isTextured() const {return !texCoords.isEmpty();}
60
//! call a function for each triangle of the array.
61
//! func should define the following method :
62
//! void operator() (const Vec3d* vertex[3], const Vec2f* tex[3], unsigned int indices[3])
63
//! The method takes arrays of *pointers* as arguments because we can't assume the values are contiguous
65
inline Func foreachTriangle(Func func) const;
67
//! Create a copy of the array with all the triangles intersecting the projector discontinuty removed.
68
StelVertexArray removeDiscontinuousTriangles(const class StelProjector* prj) const;
70
void draw(class StelPainter* painter) const;
73
// Below we define a few methods that are templated to be optimized according to different types of VertexArray :
74
// The template parameter <bool T> defines whether the array has a texture.
75
// The template parameter <bool I> defines whether the array is indexed.
77
const Vec3d* specVertexAt(int i) const {
78
return &vertex.at(specIndiceAt<I>(i));
81
template <bool T, bool I>
82
const Vec2f* specTexCoordAt(int i) const {
83
return T ? &texCoords.at(specIndiceAt<I>(i)) : NULL;
87
unsigned int specIndiceAt(int i) const {
88
return I ? indices.at(i) : i;
91
template<bool T, bool I, class Func>
92
inline Func specForeachTriangle(Func func) const;
97
Func StelVertexArray::foreachTriangle(Func func) const
99
// Here we just dispach the method into one of the 4 possible cases
100
bool textured = isTextured();
101
bool useIndice = isIndexed();
105
return specForeachTriangle<true, true, Func>(func);
107
return specForeachTriangle<true, false, Func>(func);
110
return specForeachTriangle<false, true, Func>(func);
112
return specForeachTriangle<false, false, Func>(func);
115
template<bool T, bool I, class Func>
116
Func StelVertexArray::specForeachTriangle(Func func) const
118
switch (primitiveType)
120
case StelVertexArray::Triangles:
121
Q_ASSERT(vertex.size() % 3 == 0);
122
for (int i = 0; i < vertex.size(); i += 3)
124
func(specVertexAt<I>(i), specVertexAt<I>(i+1), specVertexAt<I>(i+2),
125
specTexCoordAt<T, I>(i), specTexCoordAt<T, I>(i+1), specTexCoordAt<T, I>(i+2),
126
specIndiceAt<I>(i), specIndiceAt<I>(i+1), specIndiceAt<I>(i+2));
129
case StelVertexArray::TriangleFan:
131
const Vec3d* v0 = specVertexAt<I>(0);
132
const Vec2f* t0 = specTexCoordAt<T, I>(0);
133
unsigned int i0 = specIndiceAt<I>(0);
134
for (int i = 1; i < vertex.size() - 1; ++i)
136
func(v0, specVertexAt<I>(i), specVertexAt<I>(i+1),
137
t0, specTexCoordAt<T, I>(i), specTexCoordAt<T, I>(i+1),
138
i0, specIndiceAt<I>(i), specIndiceAt<I>(i+1));
142
case StelVertexArray::TriangleStrip:
144
for (int i = 2; i < vertex.size(); ++i)
147
func(specVertexAt<I>(i-2), specVertexAt<I>(i-1), specVertexAt<I>(i),
148
specTexCoordAt<T, I>(i-2), specTexCoordAt<T, I>(i-1), specTexCoordAt<T, I>(i),
149
specIndiceAt<I>(i-2), specIndiceAt<I>(i-1), specIndiceAt<I>(i));
151
func(specVertexAt<I>(i-1), specVertexAt<I>(i-2), specVertexAt<I>(i),
152
specTexCoordAt<T, I>(i-1), specTexCoordAt<T, I>(i-2), specTexCoordAt<T, I>(i),
153
specIndiceAt<I>(i-1), specIndiceAt<I>(i-2), specIndiceAt<I>(i));
158
Q_ASSERT_X(0, Q_FUNC_INFO, "unsuported primitive type");
164
#endif // __STELVERTEXARRAY_HPP__