/*************************************************************************** * 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 #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 * pointContainer, QVector * 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 * 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 * 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