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 #include "../glc_context.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_PointCloud()
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         QList<GLC_Point3d> points;
00052         points.append(GLC_Point3d(0.0, 0.0, 0.0));
00053         GLC_PointCloud::addPoint(points);
00054 }
00055 
00056 GLC_PointSprite::GLC_PointSprite(const GLC_PointSprite& point)
00057 : GLC_PointCloud(point)
00058 , m_Size(point.m_Size)
00059 , m_DistanceAttenuation(point.m_DistanceAttenuation)
00060 , m_FadeThresoldSize(point.m_FadeThresoldSize)
00061 {
00062 
00063 }
00064 
00065 GLC_PointSprite::~GLC_PointSprite()
00066 {
00067 
00068 }
00069 
00070 // Return a copy of the current geometry
00071 GLC_Geometry* GLC_PointSprite::clone() const
00072 {
00073         return new GLC_PointSprite(*this);
00074 }
00075 
00076 // Return the point size
00077 void GLC_PointSprite::setSize(float size)
00078 {
00079         m_GeometryIsValid= false;
00080         m_Size= size;
00081     // Clamp m_Size to m_MaxSize
00082     if(qFuzzyCompare(-1.0f, m_MaxSize) && (m_MaxSize < m_Size))
00083     {
00084         m_Size= m_MaxSize;
00085     }
00086 }
00087 
00088 // Set the point distance attenuation values
00089 void GLC_PointSprite::setPointDistanceAttenuation(QVector<float> parameters)
00090 {
00091         Q_ASSERT(3 == parameters.size());
00092         m_DistanceAttenuation= parameters;
00093 }
00094 
00096 // OpenGL Functions
00098 // Specific glExecute method
00099 void GLC_PointSprite::render(const GLC_RenderProperties& renderProperties)
00100 {
00101         // Check if extension GL_ARB_point_parameters is present
00102         if (!GLC_State::pointSpriteSupported()) return;
00103 
00104         glPushAttrib(GL_ALL_ATTRIB_BITS);
00105 
00106         if (!GLC_State::isInSelectionMode())
00107         {
00108                 glEnable( GL_BLEND );
00109                 glDepthMask(GL_FALSE);
00110                 glEnable(GL_TEXTURE_2D);
00111                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00112                 glDisable(GL_LIGHTING);
00113                 //GLC_Context::current()->glcEnableLighting(false);
00114 
00115             if(m_MaterialHash.size() == 1)
00116             {
00117 
00118                 if (renderProperties.isSelected())
00119                 {
00120                         GLC_SelectionMaterial::glExecute();
00121                 }
00122                 else
00123                 {
00124                         GLC_Material* pCurrentMaterial= m_MaterialHash.begin().value();
00125                         const GLfloat red= pCurrentMaterial->diffuseColor().redF();
00126                         const GLfloat green= pCurrentMaterial->diffuseColor().greenF();
00127                         const GLfloat blue= pCurrentMaterial->diffuseColor().blueF();
00128                         const GLfloat alpha= pCurrentMaterial->diffuseColor().alphaF();
00129 
00130                         glColor4f(red, green, blue, alpha);
00131                         pCurrentMaterial->glExecute();
00132 
00133                 }
00134             }
00135         }
00136         else
00137         {
00138                 glDisable(GL_BLEND);
00139                 glDisable(GL_TEXTURE_2D);
00140                 glDisable(GL_LIGHTING);
00141                 //GLC_Context::current()->glcEnableLighting(false);
00142         }
00143 
00144 
00145         // Executed only the first time
00146         if (qFuzzyCompare(-1.0f, m_MaxSize))
00147         {
00148                  // Query for the max point size supported by the hardware
00149                 glGetFloatv(GL_POINT_SIZE_MAX, &m_MaxSize);
00150 
00151             // Clamp m_Size to m_MaxSize
00152             if(m_MaxSize < m_Size)
00153                 m_Size= m_MaxSize;
00154         }
00155 
00156     // This is how will our point sprite's size will be modified by
00157     // distance from the viewer
00158     glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, m_DistanceAttenuation.data());
00159     glPointSize(m_Size);
00160 
00161     // The alpha of a point is calculated to allow the fading of points
00162     // instead of shrinking them past a defined threshold size. The threshold
00163     // is defined by GL_POINT_FADE_THRESHOLD_SIZE_ARB and is not clamped to
00164     // the minimum and maximum point sizes.
00165     glPointParameterf(GL_POINT_FADE_THRESHOLD_SIZE, m_FadeThresoldSize);
00166 
00167     glPointParameterf(GL_POINT_SIZE_MIN, 1.0f);
00168     glPointParameterf(GL_POINT_SIZE_MAX, m_Size);
00169 
00170     // Specify point sprite texture coordinate replacement mode for each
00171     // texture unit
00172     glTexEnvf(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
00173 
00174     glEnable(GL_POINT_SPRITE);
00175     glDraw(renderProperties);
00176 
00177     glPopAttrib();
00178 
00179 }
00180 // Point sprite set up
00181 void GLC_PointSprite::glDraw(const GLC_RenderProperties& renderProperties)
00182 {
00183         GLC_PointCloud::glDraw(renderProperties);
00184 }
00185 

SourceForge.net Logo

©2005-2011 Laurent Ribon