ParticlePrimitive.cpp

Go to the documentation of this file.
00001 // Copyright (C) 2005 Dave Griffiths
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 
00017 #include "Renderer.h"
00018 #include "ParticlePrimitive.h"
00019 #include "State.h"
00020 
00021 using namespace Fluxus;
00022     
00023 ParticlePrimitive::ParticlePrimitive() 
00024 {
00025     AddData("p",new TypedPData<dVector>);
00026     AddData("c",new TypedPData<dColour>);
00027     AddData("s",new TypedPData<dVector>);
00028     AddData("r",new TypedPData<float>);
00029     
00030     // direct access for speed
00031     PDataDirty();
00032 }
00033 
00034 ParticlePrimitive::ParticlePrimitive(const ParticlePrimitive &other) :
00035 Primitive(other) 
00036 {
00037     PDataDirty();
00038 }
00039 
00040 ParticlePrimitive::~ParticlePrimitive()
00041 {
00042 }
00043 
00044 ParticlePrimitive* ParticlePrimitive::Clone() const 
00045 {
00046     return new ParticlePrimitive(*this); 
00047 }
00048 
00049 void ParticlePrimitive::PDataDirty()
00050 {
00051     m_VertData=GetDataVec<dVector>("p");
00052     m_ColData=GetDataVec<dColour>("c");
00053     m_SizeData=GetDataVec<dVector>("s");
00054     m_RotateData=GetDataVec<float>("r");
00055 }
00056 
00057 void ParticlePrimitive::Render()
00058 {
00059     glDisable(GL_LIGHTING);
00060 
00061     if (m_State.Hints & HINT_POINTS)
00062     {
00063         glDisableClientState(GL_NORMAL_ARRAY);
00064         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00065         glEnableClientState(GL_COLOR_ARRAY);
00066 
00067         glVertexPointer(3,GL_FLOAT,sizeof(dVector),(void*)m_VertData->begin()->arr());
00068         glColorPointer(4,GL_FLOAT,sizeof(dColour),(void*)m_ColData->begin()->arr());
00069 
00070         //glEnable(GL_BLEND);
00071         //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00072         //glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
00073 
00074         if (m_State.Hints & HINT_AALIAS) glEnable(GL_POINT_SMOOTH);
00075         else glDisable(GL_POINT_SMOOTH);
00076 
00077         glDrawArrays(GL_POINTS,0,m_VertData->size());
00078 
00079         glDisableClientState(GL_COLOR_ARRAY);
00080         glEnableClientState(GL_NORMAL_ARRAY);
00081         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00082     }
00083 
00084     if (m_State.Hints & HINT_SOLID)
00085     {
00086         dMatrix ModelView;
00087         glGetFloatv(GL_MODELVIEW_MATRIX,ModelView.arr());
00088         ModelView = ModelView.inverse();
00089 
00090         dVector CameraDir(0,0,1);
00091         CameraDir=ModelView.transform_no_trans(CameraDir);
00092         CameraDir.normalise();
00093 
00094         dVector up(0,1,0);
00095         up = ModelView.transform(up);
00096         dVector across=up.cross(CameraDir);
00097         across.normalise();
00098         dVector down=across.cross(CameraDir);
00099         down.normalise();
00100 
00101         glBegin(GL_QUADS);
00102         for (unsigned int n=0; n<m_VertData->size(); n++)
00103         {
00104             dVector scaledacross(across*(*m_SizeData)[n].x*0.5);
00105             dVector scaledown(down*(*m_SizeData)[n].y*0.5);
00106             glColor4fv((*m_ColData)[n].arr());
00107             glTexCoord2f(0,0);
00108             glVertex3fv(((*m_VertData)[n]-scaledacross-scaledown).arr());
00109             glTexCoord2f(0,1);
00110             glVertex3fv(((*m_VertData)[n]-scaledacross+scaledown).arr());
00111             glTexCoord2f(1,1);
00112             glVertex3fv(((*m_VertData)[n]+scaledacross+scaledown).arr());
00113             glTexCoord2f(1,0);
00114             glVertex3fv(((*m_VertData)[n]+scaledacross-scaledown).arr());
00115         }
00116         glEnd();
00117     }
00118     glEnable(GL_LIGHTING);
00119 }
00120 
00121 dBoundingBox ParticlePrimitive::GetBoundingBox()
00122 {   
00123     dBoundingBox box;
00124     for (vector<dVector>::iterator i=m_VertData->begin(); i!=m_VertData->end(); ++i)
00125     {
00126         box.expand(*i);
00127     }
00128     return box;
00129 }
00130 
00131 void ParticlePrimitive::ApplyTransform(bool ScaleRotOnly)
00132 {
00133     if (!ScaleRotOnly)
00134     {
00135         for (vector<dVector>::iterator i=m_VertData->begin(); i!=m_VertData->end(); ++i)
00136         {
00137             *i=GetState()->Transform.transform(*i);
00138         }
00139     }
00140     else
00141     {
00142         for (vector<dVector>::iterator i=m_VertData->begin(); i!=m_VertData->end(); ++i)
00143         {
00144             *i=GetState()->Transform.transform_no_trans(*i);
00145         }
00146     }
00147     
00148     GetState()->Transform.init();
00149 }

Generated on Wed Sep 17 21:16:30 2008 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1