LinePrimitive.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 "LinePrimitive.h"
00019 #include "State.h"
00020 
00021 using namespace Fluxus;
00022 
00023 LinePrimitive::LinePrimitive() 
00024 {   
00025     AddData("p",new TypedPData<dVector>);
00026     AddData("w",new TypedPData<float>);
00027     AddData("c",new TypedPData<dVector>);
00028     PDataDirty();
00029 }
00030 
00031 LinePrimitive::LinePrimitive(const LinePrimitive &other) :
00032 Primitive(other) 
00033 {
00034     PDataDirty();
00035 }
00036 
00037 LinePrimitive::~LinePrimitive()
00038 {
00039 }
00040 
00041 LinePrimitive* LinePrimitive::Clone() const 
00042 {
00043     return new LinePrimitive(*this); 
00044 }
00045 
00046 void LinePrimitive::PDataDirty()
00047 {
00048     // reset pointers
00049     m_VertData=GetDataVec<dVector>("p");
00050     m_WidthData=GetDataVec<float>("w");
00051     m_ColData=GetDataVec<dVector>("c");
00052 }
00053 
00054 void LinePrimitive::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 
00072         for (unsigned int n=0; n<m_VertData->size()-1; n++)
00073         {
00074             float txstart = n/(float)m_VertData->size();
00075             float txend = (n+1)/(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             glTexCoord2f(txstart,0);
00085             glNormal3fv(botnorm.arr());
00086             glVertex3fv(((*m_VertData)[n]-(up*(*m_WidthData)[n])).arr());
00087             glTexCoord2f(txstart,1);
00088             glNormal3fv(topnorm.arr());
00089             glVertex3fv(((*m_VertData)[n]+(up*(*m_WidthData)[n])).arr());
00090             glTexCoord2f(txend,0);
00091             glNormal3fv(botnorm.arr());
00092             glVertex3fv(((*m_VertData)[n+1]-(up*(*m_WidthData)[n+1])).arr());
00093             glTexCoord2f(txend,1);
00094             glNormal3fv(topnorm.arr());
00095             glVertex3fv(((*m_VertData)[n+1]+(up*(*m_WidthData)[n+1])).arr());
00096         }
00097         glEnd();
00098 
00099     }
00100     
00101     if (m_State.Hints & HINT_WIRE)
00102     {
00103         if (m_State.Hints & HINT_VERTCOLS)
00104         {
00105             glBegin(GL_LINE_STRIP);
00106             for (unsigned int n=0; n<m_VertData->size()-1; n++)
00107             {
00108                 float txstart = n/(float)m_VertData->size();
00109                 float txend = (n+1)/(float)m_VertData->size();
00110                 glTexCoord2f(txstart,0);
00111                 glColor3fv((*m_ColData)[n].arr());
00112                 glVertex3fv((*m_VertData)[n].arr());
00113                 glTexCoord2f(txend,0);
00114                 glColor3fv((*m_ColData)[n+1].arr());
00115                 glVertex3fv((*m_VertData)[n+1].arr());
00116             }
00117             glEnd();
00118         }
00119         else
00120         {
00121             glColor4fv(m_State.WireColour.arr());
00122             glBegin(GL_LINE_STRIP);
00123             for (unsigned int n=0; n<m_VertData->size()-1; n++)
00124             {
00125                 float txstart = n/(float)m_VertData->size();
00126                 float txend = (n+1)/(float)m_VertData->size();
00127                 glTexCoord2f(txstart,0);
00128                 glVertex3fv((*m_VertData)[n].arr());
00129                 glTexCoord2f(txend,0);
00130                 glVertex3fv((*m_VertData)[n+1].arr());
00131             }
00132             glEnd();
00133         }
00134     }
00135     
00136     
00137     
00138     if (m_State.Hints & HINT_AALIAS) glDisable(GL_LINE_SMOOTH);     
00139     if (m_State.Hints & HINT_UNLIT) glEnable(GL_LIGHTING);
00140 }
00141 
00142 dBoundingBox LinePrimitive::GetBoundingBox()
00143 {
00144     dBoundingBox box;
00145     for (unsigned int n=0; n<m_VertData->size()-1; n++)
00146     {
00147         box.expand((*m_VertData)[n]);
00148     }
00149     return box;
00150 }
00151 
00152 void LinePrimitive::ApplyTransform(bool ScaleRotOnly)
00153 {
00154     if (!ScaleRotOnly)
00155     {
00156         for (vector<dVector>::iterator i=m_VertData->begin(); i!=m_VertData->end(); ++i)
00157         {
00158             *i=GetState()->Transform.transform(*i);
00159         }
00160     }
00161     else
00162     {
00163         for (vector<dVector>::iterator i=m_VertData->begin(); i!=m_VertData->end(); ++i)
00164         {
00165             *i=GetState()->Transform.transform_no_trans(*i);
00166         }
00167     }
00168     
00169     GetState()->Transform.init();
00170 }
00171 

Generated on Mon Feb 11 06:54:25 2008 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1