~ubuntu-branches/ubuntu/precise/stellarium/precise

« back to all changes in this revision

Viewing changes to src/core/StelVertexArray.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-02-15 20:48:39 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20100215204839-u3qgbv60rho997yk
Tags: 0.10.3-0ubuntu1
* New upstream release.
  - fixes intel rendering bug (LP: #480553)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Stellarium
 
3
 * Copyright (C) 2009 Fabien Chereau
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 */
 
19
 
 
20
#ifndef __STELVERTEXARRAY_HPP__
 
21
#define __STELVERTEXARRAY_HPP__
 
22
 
 
23
#include <QVector>
 
24
#include <QDebug>
 
25
#include "VecMath.hpp"
 
26
 
 
27
struct StelVertexArray
 
28
{
 
29
        // TODO maybe merge this with StelPainter DrawingMode
 
30
        enum StelPrimitiveType
 
31
        {
 
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
 
39
        };
 
40
 
 
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) {;}
 
44
 
 
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;
 
53
 
 
54
        StelPrimitiveType primitiveType;
 
55
 
 
56
        bool isIndexed() const {return !indices.isEmpty();}
 
57
 
 
58
        bool isTextured() const {return !texCoords.isEmpty();}
 
59
 
 
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
 
64
        template<class Func>
 
65
        inline Func foreachTriangle(Func func) const;
 
66
 
 
67
        //! Create a copy of the array with all the triangles intersecting the projector discontinuty removed.
 
68
        StelVertexArray removeDiscontinuousTriangles(const class StelProjector* prj) const;
 
69
 
 
70
        void draw(class StelPainter* painter) const;
 
71
 
 
72
private:
 
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.
 
76
        template <bool I>
 
77
        const Vec3d* specVertexAt(int i) const {
 
78
                return &vertex.at(specIndiceAt<I>(i));
 
79
        }
 
80
 
 
81
        template <bool T, bool I>
 
82
        const Vec2f* specTexCoordAt(int i) const {
 
83
                return T ? &texCoords.at(specIndiceAt<I>(i)) : NULL;
 
84
        }
 
85
 
 
86
        template<bool I>
 
87
        unsigned int specIndiceAt(int i) const {
 
88
                return I ? indices.at(i) : i;
 
89
        }
 
90
 
 
91
        template<bool T, bool I, class Func>
 
92
        inline Func specForeachTriangle(Func func) const;
 
93
 
 
94
};
 
95
 
 
96
template<class Func>
 
97
Func StelVertexArray::foreachTriangle(Func func) const
 
98
{
 
99
        // Here we just dispach the method into one of the 4 possible cases
 
100
        bool textured = isTextured();
 
101
        bool useIndice = isIndexed();
 
102
 
 
103
        if (textured)
 
104
                if (useIndice)
 
105
                        return specForeachTriangle<true, true, Func>(func);
 
106
                else
 
107
                        return specForeachTriangle<true, false, Func>(func);
 
108
        else
 
109
                if (useIndice)
 
110
                        return specForeachTriangle<false, true, Func>(func);
 
111
                else
 
112
                        return specForeachTriangle<false, false, Func>(func);
 
113
}
 
114
 
 
115
template<bool T, bool I, class Func>
 
116
Func StelVertexArray::specForeachTriangle(Func func) const
 
117
{
 
118
        switch (primitiveType)
 
119
        {
 
120
                case StelVertexArray::Triangles:
 
121
                        Q_ASSERT(vertex.size() % 3 == 0);
 
122
                        for (int i = 0; i < vertex.size(); i += 3)
 
123
                        {
 
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));
 
127
                        }
 
128
                        break;
 
129
                case StelVertexArray::TriangleFan:
 
130
                {
 
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)
 
135
                        {
 
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));
 
139
                        }
 
140
                        break;
 
141
                }
 
142
                case StelVertexArray::TriangleStrip:
 
143
                {
 
144
                        for (int i = 2; i < vertex.size(); ++i)
 
145
                        {
 
146
                                if (i % 2 == 0)
 
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));
 
150
                                else
 
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));
 
154
                        }
 
155
                        break;
 
156
                }
 
157
                default:
 
158
                        Q_ASSERT_X(0, Q_FUNC_INFO, "unsuported primitive type");
 
159
        }
 
160
        return func;
 
161
}
 
162
 
 
163
 
 
164
#endif // __STELVERTEXARRAY_HPP__