PixelPrimitive.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 "PixelPrimitive.h"
00019 #include "State.h"
00020 
00021 //#define RENDER_NORMALS
00022 //#define RENDER_BBOX
00023 
00024 using namespace Fluxus;
00025     
00026 PixelPrimitive::PixelPrimitive(unsigned int w, unsigned int h) : 
00027 m_Texture(0),
00028 m_Width(w),
00029 m_Height(h),
00030 m_ReadyForUpload(false)
00031 {
00032     AddData("c",new TypedPData<dColour>);
00033     
00034     // setup the direct access for speed
00035     PDataDirty();
00036     
00037     for (unsigned int x=0; x<h; x++)
00038     {
00039         for (unsigned int y=0; y<w; y++)
00040         {
00041             m_ColourData->push_back(dColour(1,1,1));
00042         }
00043     }
00044         
00045     m_Points.push_back(dVector(0,0,0));
00046     m_Points.push_back(dVector(1,0,0));
00047     m_Points.push_back(dVector(1,1,0));
00048     m_Points.push_back(dVector(0,1,0));
00049     
00050     glGenTextures(1,(GLuint*)&m_Texture);
00051 }
00052 
00053 PixelPrimitive::PixelPrimitive(const PixelPrimitive &other) :
00054 Primitive(other),
00055 m_Points(other.m_Points),
00056 m_Width(other.m_Width),
00057 m_Height(other.m_Height),
00058 m_ReadyForUpload(other.m_ReadyForUpload)
00059 {
00060     PDataDirty();
00061     glGenTextures(1,(GLuint*)&m_Texture);
00062 }
00063 
00064 PixelPrimitive::~PixelPrimitive()
00065 {
00066     if (m_Texture!=0)
00067     {
00068         glDeleteTextures(1,(GLuint*)&m_Texture);
00069     }
00070 }
00071 
00072 PixelPrimitive* PixelPrimitive::Clone() const 
00073 {
00074     return new PixelPrimitive(*this); 
00075 }
00076 
00077 void PixelPrimitive::PDataDirty()
00078 {
00079     // reset pointers
00080     m_ColourData=GetDataVec<dColour>("c");
00081 }
00082 
00083 void PixelPrimitive::Upload()
00084 {
00085     m_ReadyForUpload=true;
00086 }
00087 
00088 void PixelPrimitive::Load(const string &filename)
00089 {
00090     TypedPData<dColour> *data = dynamic_cast<TypedPData<dColour>*>(GetDataRaw("c"));
00091     if (data)
00092     {
00093         TexturePainter::Get()->LoadPData(filename,m_Width,m_Height,*data);
00094     }
00095 }
00096 
00097 void PixelPrimitive::Save(const string &filename) const
00098 {
00099     const TypedPData<dColour> *data = dynamic_cast<const TypedPData<dColour>*>(GetDataRawConst("c"));
00100     if (data)
00101     {
00102         TexturePainter::Get()->SavePData(filename,m_Width,m_Height,*data);
00103     }
00104 }
00105 
00106 void PixelPrimitive::Render()
00107 {   
00108     if (m_ReadyForUpload)
00109     {
00110         if (m_Texture!=0)
00111         {
00112             glDeleteTextures(1,(GLuint*)&m_Texture);
00113         }
00114     
00115         glBindTexture(GL_TEXTURE_2D,m_Texture);
00116         gluBuild2DMipmaps(GL_TEXTURE_2D,4,m_Width,m_Height,GL_RGBA,GL_FLOAT,&(*m_ColourData)[0]);
00117         m_ReadyForUpload=false;
00118     }
00119     
00120     // override the state texture!
00121     glEnable(GL_TEXTURE_2D);
00122     glBindTexture(GL_TEXTURE_2D,m_Texture);
00123     glDisable(GL_LIGHTING);
00124     glBegin(GL_QUADS);
00125     glTexCoord2f(0,0);
00126     glVertex3fv(m_Points[0].arr());
00127     glTexCoord2f(1,0);
00128     glVertex3fv(m_Points[1].arr());
00129     glTexCoord2f(1,1);
00130     glVertex3fv(m_Points[2].arr());
00131     glTexCoord2f(0,1);
00132     glVertex3fv(m_Points[3].arr());
00133     glEnd();    
00134     glEnable(GL_LIGHTING);
00135     glDisable(GL_TEXTURE_2D);
00136 }
00137 
00138 
00139 
00140 dBoundingBox PixelPrimitive::GetBoundingBox()
00141 {   
00142     dBoundingBox box;
00143     for (vector<dVector>::iterator i=m_Points.begin(); i!=m_Points.end(); ++i)
00144     {
00145         box.expand(*i);
00146     }
00147     return box;
00148 }
00149 
00150 void PixelPrimitive::ApplyTransform(bool ScaleRotOnly)
00151 {
00152     if (!ScaleRotOnly)
00153     {
00154         for (vector<dVector>::iterator i=m_Points.begin(); i!=m_Points.end(); ++i)
00155         {
00156             *i=GetState()->Transform.transform(*i);
00157         }
00158     }
00159     else
00160     {
00161         for (unsigned int i=0; i<m_Points.size(); i++)
00162         {
00163             m_Points[i]=GetState()->Transform.transform_no_trans(m_Points[i]);
00164         }
00165     }
00166     
00167     GetState()->Transform.init();
00168 }
00169 

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