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

SourceForge.net Logo

©2005-2011 Laurent Ribon