glc_pointsprite.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  Copyright (C) 2009 Laurent Bauer
00006  Version 2.0.0, packaged on July 2010.
00007 
00008  http://glc-lib.sourceforge.net
00009 
00010  GLC-lib is free software; you can redistribute it and/or modify
00011  it under the terms of the GNU Lesser General Public License as published by
00012  the Free Software Foundation; either version 3 of the License, or
00013  (at your option) any later version.
00014 
00015  GLC-lib is distributed in the hope that it will be useful,
00016  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  GNU Lesser General Public License for more details.
00019 
00020  You should have received a copy of the GNU Lesser General Public License
00021  along with GLC-lib; if not, write to the Free Software
00022  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023 
00024  *****************************************************************************/
00026 
00027 #include "glc_pointsprite.h"
00028 #include "../glc_openglexception.h"
00029 #include "../glc_state.h"
00030 #include "../glc_ext.h"
00031 
00032 // The maximum point size
00033 float GLC_PointSprite::m_MaxSize= -1.0f;
00034 
00035 // Default constructor
00036 GLC_PointSprite::GLC_PointSprite(float size, GLC_Material* pMaterial)
00037 :GLC_Geometry("PointSprite", false)
00038 , m_Size(size)
00039 , m_DistanceAttenuation(3)
00040 , m_FadeThresoldSize(60.0f)
00041 {
00042         Q_ASSERT(pMaterial != NULL);
00043         Q_ASSERT(pMaterial->hasTexture());
00044         addMaterial(pMaterial);
00045 
00046         // Set Distance attenuation defaults values
00047         m_DistanceAttenuation[0]= 1.0f;
00048         m_DistanceAttenuation[1]= 0.0f;
00049         m_DistanceAttenuation[2]= 0.0f;
00050 }
00051 
00052 GLC_PointSprite::~GLC_PointSprite()
00053 {
00054 
00055 }
00056 
00057 // return the point bounding box
00058 const GLC_BoundingBox& GLC_PointSprite::boundingBox(void)
00059 {
00060 
00061         if (NULL == m_pBoundingBox)
00062         {
00063                 m_pBoundingBox= new GLC_BoundingBox();
00064                 const double epsilon= 1e-2;
00065                 GLC_Point3d lower(      - epsilon,
00066                                                         - epsilon,
00067                                                         - epsilon);
00068                 GLC_Point3d upper(  epsilon,
00069                                                         epsilon,
00070                                                         epsilon);
00071                 m_pBoundingBox->combine(lower);
00072                 m_pBoundingBox->combine(upper);
00073         }
00074         return *m_pBoundingBox;
00075 }
00076 
00077 // Return a copy of the current geometry
00078 GLC_Geometry* GLC_PointSprite::clone() const
00079 {
00080         return new GLC_PointSprite(*this);
00081 }
00082 
00083 // Return the point size
00084 void GLC_PointSprite::setSize(float size)
00085 {
00086         m_GeometryIsValid= false;
00087         m_Size= size;
00088     // Clamp m_Size to m_MaxSize
00089     if(qFuzzyCompare(-1.0f, m_MaxSize) && (m_MaxSize < m_Size))
00090     {
00091         m_Size= m_MaxSize;
00092     }
00093 }
00094 
00095 // Set the point distance attenuation values
00096 void GLC_PointSprite::setPointDistanceAttenuation(QVector<float> parameters)
00097 {
00098         Q_ASSERT(3 == parameters.size());
00099         m_DistanceAttenuation= parameters;
00100 }
00101 
00103 // OpenGL Functions
00105 // Specific glExecute method
00106 void GLC_PointSprite::render(const GLC_RenderProperties& renderProperties)
00107 {
00108         // Check if extension GL_ARB_point_parameters is present
00109         if (!GLC_State::pointSpriteSupported()) return;
00110 
00111         glPushAttrib(GL_ALL_ATTRIB_BITS);
00112 
00113         if (!GLC_State::isInSelectionMode())
00114         {
00115                 glEnable( GL_BLEND );
00116                 glDepthMask(GL_FALSE);
00117                 glEnable(GL_TEXTURE_2D);
00118                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00119                 glDisable(GL_LIGHTING);
00120 
00121             if(m_MaterialHash.size() == 1)
00122             {
00123                 GLC_Material* pCurrentMaterial= m_MaterialHash.begin().value();
00124                 const GLfloat red= pCurrentMaterial->diffuseColor().redF();
00125                 const GLfloat green= pCurrentMaterial->diffuseColor().greenF();
00126                 const GLfloat blue= pCurrentMaterial->diffuseColor().blueF();
00127                 const GLfloat alpha= pCurrentMaterial->diffuseColor().alphaF();
00128 
00129                 glColor4f(red, green, blue, alpha);
00130                 pCurrentMaterial->glExecute();
00131             }
00132         }
00133         else
00134         {
00135                 glDisable(GL_BLEND);
00136                 glDisable(GL_TEXTURE_2D);
00137                 glDisable(GL_LIGHTING);
00138         }
00139 
00140 
00141         // Executed only the first time
00142         if (qFuzzyCompare(-1.0f, m_MaxSize))
00143         {
00144                  // Query for the max point size supported by the hardware
00145                 glGetFloatv(GL_POINT_SIZE_MAX, &m_MaxSize);
00146 
00147             // Clamp m_Size to m_MaxSize
00148             if(m_MaxSize < m_Size)
00149                 m_Size= m_MaxSize;
00150         }
00151 
00152     // This is how will our point sprite's size will be modified by
00153     // distance from the viewer
00154     glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, m_DistanceAttenuation.data());
00155     glPointSize(m_Size);
00156 
00157     // The alpha of a point is calculated to allow the fading of points
00158     // instead of shrinking them past a defined threshold size. The threshold
00159     // is defined by GL_POINT_FADE_THRESHOLD_SIZE_ARB and is not clamped to
00160     // the minimum and maximum point sizes.
00161     glPointParameterf(GL_POINT_FADE_THRESHOLD_SIZE, m_FadeThresoldSize);
00162 
00163     glPointParameterf(GL_POINT_SIZE_MIN, 1.0f);
00164     glPointParameterf(GL_POINT_SIZE_MAX, m_Size);
00165 
00166     // Specify point sprite texture coordinate replacement mode for each
00167     // texture unit
00168     glTexEnvf(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
00169 
00170     glEnable(GL_POINT_SPRITE);
00171     glDraw(renderProperties);
00172 
00173     glPopAttrib();
00174 
00175 }
00176 // Point sprite set up
00177 void GLC_PointSprite::glDraw(const GLC_RenderProperties&)
00178 {
00179         // Point Display
00180         glBegin(GL_POINTS);
00181                 glVertex3f(0.0f,0.0f,0.0f);
00182         glEnd();
00183 
00184         // OpenGL error handler
00185         GLenum error= glGetError();
00186         if (error != GL_NO_ERROR)
00187         {
00188                 GLC_OpenGlException OpenGlException("GLC_PointSprite::GlDraw ", error);
00189                 throw(OpenGlException);
00190         }
00191 }
00192 

SourceForge.net Logo

©2005-2010 Laurent Ribon