summaryrefslogtreecommitdiffstats
path: root/src/glbody.h
blob: 1bbaa82d4cb9aadfb0ccd9acb8e99b9c04079d31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/***************************************************************************
 *   Copyright (C) 2008, 2012 by Walter Roth                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef GLBODY_H
#define GLBODY_H

#include <QVector>

#include "glesrenderer.h"
#include "glpoint.h"
#include "gldefines.h"

/**
  * \brief A 3D body that uses a GLESRenderer for drawing and GLPoint objects for defining its surface.
  *
  * Overwrite makeSurface() in subclasses to create the GLPoints that form the surface.
  * Set drawingMode to GL_LINE_STRIP or GL_LINES for debugging the surface. Default is GL_TRIANGLE_STRIP.
  * Vertices and indices may be stored in external or internal containers. If GlBody::makeSurface is called
  * with pointers to existing containers, these will be used. Otherwise GlBody::makeSurface will create new
  * containers. Only in this case, the destructor will delete the containers.
  * Overwrite draw() if you do need special drawing procedures.
  */
class GLBody{
public:
     /**Constructor does NOT create the surface.
     */
     GLBody(float m_radius = 1.0, const GLColorRgba & m_color = GLColorRgba::clBlue, const QString m_textureFile = "");

    /** Destructor will delete created containers.
     */
    virtual ~GLBody();

    /** Creates the surface. Should be called, when a GL engine is already running.
      * To be overwritten by subclasses. GLESBody::createSurface should be called at the beginning
      * of overriding functions. It will create the pointContainer, if none is supplied
      * Is called automatically by draw, if required.
      * MUST NOT be called without a working GL engine.
      *
      * @param pointContainer The container for the geometry data. If NULL, a new one is created.
      * @param indexContainer The container for the index data. If NULL, a new one is created.
      * Created containers will be deleted by destructor.
      */
    virtual void makeSurface(QVector<GLPoint> * pointContainer, QVector<GLshort> * indexContainer);


     /**
     * @brief calculateDrawMatrix Virtual function to calculate the final matrix to be used for drawing.
     * May be overwritten in subclasses. GLBody::calculateDrawMatrix simply copies m_transformationMatrix.
     */
    virtual void calculateDrawMatrix();
   /** Draws the surface and calls makeSurface if required.
    * Needs an active (made current) GL-Context.
    */
    virtual void draw(GLESRenderer * renderer);

    /**
      * Returns true, when line through p1 and p2 intersects body sphere
      * To be overwritten by subclasses.
      */
    virtual bool isHit(QVector3D p1, QVector3D p2);

    /**
      * Returns true, if enclosing spheres touch or intersect
      */
    virtual bool spheresAreColliding(const GLBody * other);

   /** Set texture from file. Returns true on success. Needs a current OpenGL context.
    */
    bool setTexture(const QString & m_textureFile);

   /** Set texture file. Needs a current OpenGL context.
    */
    bool setTextureFile(const QString & m_textureFile);

    /**
      * Moves the body by adding vMove to all vertices.
      */
    void move(QVector3D vMove);
    /**
      * Simple gettters
      */
    bool isSelected(){return m_selected;}
    const QVector3D & getCenter()const{return m_center;}

   /**Simple setters
    */
    void setColor(const GLColorRgba & newVal){m_color = newVal;}
    void setSpecularColor(const GLColorRgba & newVal){m_specularColor = newVal;}
    void setShininess(int newVal){m_shininess = newVal;}
    void setSelected(bool newVal){m_selected = newVal;}
    void setDrawingMode(GLint newVal){m_drawingMode = newVal;}
    void setTransformation(const QMatrix4x4 & transformation){m_transformationMatrix = transformation;}

    /**
      * Set new center and invalidate surface.
      */
    void setCenter(const QVector3D & newVal);

    /**
      *Simple getters
      */
    const GLColorRgba & getColor()const{return m_color;}  
    const QMatrix4x4 & getTransformation()const{return m_transformationMatrix;}
    double getRadius()const{return m_radius;}
    void setScale(double scale){m_scale = scale;}

protected:
    /**
      * The center of the enclosing sphere
      */
     QVector3D m_center;

     /**
       * The radius of the enclosing sphere
       */
     GLfloat m_radius;

     /**
       * The mode to be passed to glDrawArrays or glDrawElements e.g. GL_TRIANGLES, GL_TRIANGLE_STRIP
       */
     GLint m_drawingMode;

    /** The array of points defining the surface.
     *  This *may be* a container not owned by this body.
     */
    QVector <GLPoint> * m_points;

    /**
     * @brief ownPointsContainer Set this Flag, if points container was created by this body.
     */
    bool m_ownPointsContainer;

    /**
     * @brief firstPoint The firstPoint for this body
     */
    int m_firstPoint;

    /**
     * @brief lastPoint The next point for this body. ( last point +1)
     */
    int m_nextPoint;

   /** The array with the indices. May be left empty. 
    */
    QVector <GLshort> * m_indices;

    /**
     * @brief ownPointsContainer Set this Flag, if points container was created by this body.
     */
    bool m_ownIndexContainer;

    /**
    * @brief startIndex The first index for this body
    */
   int m_firstIndex;

   /**
    * @brief indexCount The next index for this body. (last index + 1)
    */
   int m_nextIndex;

   /** The flag for a valid surface.
    */
    bool m_surfaceIsValid;

   /** The texture to be used.
    */
    GLuint m_texture;

   /** The tetxure file.
    */
   QString m_textureFile;

  /** The diffuse and ambient color for the body.
   */
   GLColorRgba m_color;

  /**
   * The specular color
   */
   GLColorRgba m_specularColor;

  /**
   * Shininess for specular color
   */
   int m_shininess;

   /**
     *  Flag for selected mode.
     */
   bool m_selected;
   /**
     * This matrix holds the basic transformation for the body and
     * should not be modified after first setting.
     */
   QMatrix4x4 m_transformationMatrix;

   /**
    * @brief m_drawMatrix
     * This matrix is multiplied with the modelview matrix prior to
     * rendering the body.
    */
   QMatrix4x4 m_drawMatrix;
   /**
    * @brief m_scale
    */
   double m_scale; //meters per logical unit
};

#endif