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

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