RibbonPrimitive.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 "RibbonPrimitive.h"
00019 #include "State.h"
00020 
00021 using namespace Fluxus;
00022 
00023 RibbonPrimitive::RibbonPrimitive()
00024 {
00025     AddData("p",new TypedPData<dVector>);
00026     AddData("w",new TypedPData<float>);
00027     AddData("c",new TypedPData<dColour>);
00028     PDataDirty();
00029 }
00030 
00031 RibbonPrimitive::RibbonPrimitive(const RibbonPrimitive &other) :
00032 Primitive(other)
00033 {
00034     PDataDirty();
00035 }
00036 
00037 RibbonPrimitive::~RibbonPrimitive()
00038 {
00039 }
00040 
00041 RibbonPrimitive* RibbonPrimitive::Clone() const
00042 {
00043     return new RibbonPrimitive(*this);
00044 }
00045 
00046 void RibbonPrimitive::PDataDirty()
00047 {
00048     // reset pointers
00049     m_VertData=GetDataVec<dVector>("p");
00050     m_WidthData=GetDataVec<float>("w");
00051     m_ColData=GetDataVec<dColour>("c");
00052 }
00053 
00054 void RibbonPrimitive::Render()
00055 {
00056     if (m_VertData->size()<2) return;
00057 
00058     dMatrix ModelView;
00059     glGetFloatv(GL_MODELVIEW_MATRIX,ModelView.arr());
00060 
00061     if (m_State.Hints & HINT_UNLIT) glDisable(GL_LIGHTING);
00062     if (m_State.Hints & HINT_AALIAS) glEnable(GL_LINE_SMOOTH);
00063 
00064     if (m_State.Hints & HINT_SOLID)
00065     {
00066         dVector CameraDir(0,0,1);
00067         CameraDir=ModelView.inverse().transform_no_trans(CameraDir);
00068         CameraDir.normalise();
00069 
00070         glBegin(GL_TRIANGLE_STRIP);
00071         if (m_State.Hints & HINT_VERTCOLS)
00072         {
00073             for (unsigned int n=0; n<m_VertData->size(); n++)
00074             {
00075                 float txstart = n/(float)m_VertData->size();
00076 
00077                 dVector line=(*m_VertData)[n+1]-(*m_VertData)[n];
00078                 dVector up=line.cross(CameraDir);
00079                 up.normalise();
00080 
00081                 dVector topnorm=up;
00082                 dVector botnorm=-up;
00083 
00084                 glColor4fv((*m_ColData)[n].arr());
00085                 glTexCoord2f(txstart,0);
00086                 glNormal3fv(botnorm.arr());
00087                 glVertex3fv(((*m_VertData)[n]-(up*(*m_WidthData)[n])).arr());
00088                 glColor4fv((*m_ColData)[n].arr());
00089                 glTexCoord2f(txstart,1);
00090                 glNormal3fv(topnorm.arr());
00091                 glVertex3fv(((*m_VertData)[n]+(up*(*m_WidthData)[n])).arr());
00092             }
00093         }
00094         else
00095         {
00096             glColor4fv(m_State.Colour.arr());
00097             for (unsigned int n=0; n<m_VertData->size(); n++)
00098             {
00099                 float txstart = n/(float)m_VertData->size();
00100 
00101                 dVector line=(*m_VertData)[n+1]-(*m_VertData)[n];
00102                 dVector up=line.cross(CameraDir);
00103                 up.normalise();
00104 
00105                 dVector topnorm=up;
00106                 dVector botnorm=-up;
00107 
00108                 glTexCoord2f(txstart,0);
00109                 glNormal3fv(botnorm.arr());
00110                 glVertex3fv(((*m_VertData)[n]-(up*(*m_WidthData)[n])).arr());
00111                 glTexCoord2f(txstart,1);
00112                 glNormal3fv(topnorm.arr());
00113                 glVertex3fv(((*m_VertData)[n]+(up*(*m_WidthData)[n])).arr());
00114             }
00115         }
00116         glEnd();
00117 
00118     }
00119 
00120     if (m_State.Hints & HINT_WIRE)
00121     {
00122         if (m_State.Hints & HINT_VERTCOLS)
00123         {
00124             glBegin(GL_LINE_STRIP);
00125             for (unsigned int n=0; n<m_VertData->size()-1; n++)
00126             {
00127                 float txstart = n/(float)m_VertData->size();
00128                 float txend = (n+1)/(float)m_VertData->size();
00129                 glTexCoord2f(txstart,0);
00130                 glColor4fv((*m_ColData)[n].arr());
00131                 glVertex3fv((*m_VertData)[n].arr());
00132                 glTexCoord2f(txend,0);
00133                 glColor4fv((*m_ColData)[n+1].arr());
00134                 glVertex3fv((*m_VertData)[n+1].arr());
00135             }
00136             glEnd();
00137         }
00138         else
00139         {
00140             glColor4fv(m_State.WireColour.arr());
00141             glBegin(GL_LINE_STRIP);
00142             for (unsigned int n=0; n<m_VertData->size()-1; n++)
00143             {
00144                 float txstart = n/(float)m_VertData->size();
00145                 float txend = (n+1)/(float)m_VertData->size();
00146                 glTexCoord2f(txstart,0);
00147                 glVertex3fv((*m_VertData)[n].arr());
00148                 glTexCoord2f(txend,0);
00149                 glVertex3fv((*m_VertData)[n+1].arr());
00150             }
00151             glEnd();
00152         }
00153     }
00154 
00155     if (m_State.Hints & HINT_AALIAS) glDisable(GL_LINE_SMOOTH);
00156     if (m_State.Hints & HINT_UNLIT) glEnable(GL_LIGHTING);
00157 }
00158 
00159 dBoundingBox RibbonPrimitive::GetBoundingBox()
00160 {
00161     dBoundingBox box;
00162     for (unsigned int n=0; n<m_VertData->size()-1; n++)
00163     {
00164         box.expand((*m_VertData)[n]);
00165     }
00166     return box;
00167 }
00168 
00169 void RibbonPrimitive::ApplyTransform(bool ScaleRotOnly)
00170 {
00171     if (!ScaleRotOnly)
00172     {
00173         for (vector<dVector>::iterator i=m_VertData->begin(); i!=m_VertData->end(); ++i)
00174         {
00175             *i=GetState()->Transform.transform(*i);
00176         }
00177     }
00178     else
00179     {
00180         for (vector<dVector>::iterator i=m_VertData->begin(); i!=m_VertData->end(); ++i)
00181         {
00182             *i=GetState()->Transform.transform_no_trans(*i);
00183         }
00184     }
00185     
00186     GetState()->Transform.init();
00187 }
00188 

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