00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "Renderer.h"
00018 #include "PixelPrimitive.h"
00019 #include "State.h"
00020
00021
00022
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
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
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