first draft of the gridded background decoration.

To be completed.
This commit is contained in:
Paolo Cignoni cignoni 2010-10-31 13:24:51 +00:00
parent 4a7ef17068
commit 13ffca73e1
3 changed files with 155 additions and 40 deletions

View File

@ -288,7 +288,7 @@ bool CICubeMap::LoadExt(const char *basename)
if(!tt.load(filename)) {
glPopAttrib();
QMessageBox::warning(0,"Cubemapped background decoration","Warning unable to load cube map images: " + filename );
//QMessageBox::warning(0,"Cubemapped background decoration","Warning unable to load cube map images: " + filename );
return false;
}

View File

@ -42,6 +42,7 @@ QString SampleMeshDecoratePlugin::filterInfo(QAction *action) const
switch(ID(action))
{
case DP_SHOW_CUBEMAPPED_ENV : return tr("Draws a customizable cube mapped background that is sync with trackball rotation");
case DP_SHOW_GRID : return tr("Draws a gridded background that can be used as a reference.");
}
assert(0);
return QString();
@ -51,75 +52,178 @@ QString SampleMeshDecoratePlugin::filterName(FilterIDType id) const
switch(id)
{
case DP_SHOW_CUBEMAPPED_ENV : return tr("Cube mapped background");
case DP_SHOW_GRID : return tr("Background Grid");
}
assert(0);
return QString();
}
void SampleMeshDecoratePlugin::initGlobalParameterSet(QAction *, RichParameterSet &parset)
void SampleMeshDecoratePlugin::initGlobalParameterSet(QAction *action, RichParameterSet &parset)
{
assert(!parset.hasParameter(CubeMapPathParam()));
QString cubemapDirPath = PluginManager::getBaseDirPath() + QString("/textures/cubemaps/uffizi.jpg");
parset.addParam(new RichString(CubeMapPathParam(), cubemapDirPath,"",""));
switch(ID(action)){
case DP_SHOW_CUBEMAPPED_ENV :
if(!parset.hasParameter(CubeMapPathParam()))
{
QString cubemapDirPath = PluginManager::getBaseDirPath() + QString("/textures/cubemaps/uffizi.jpg");
parset.addParam(new RichString(CubeMapPathParam(), cubemapDirPath,"",""));
}
break;
case DP_SHOW_GRID :
parset.addParam(new RichFloat(BoxRatioParam(),2.0,"Box Ratio","The size of the grid around the object w.r.t. the bbox of the object"));
parset.addParam(new RichFloat(GridMajorParam(),1,"Major Spacing",""));
parset.addParam(new RichFloat(GridMinorParam(),0.5,"Minor Spacing","Grid Size"));
break;
}
}
bool SampleMeshDecoratePlugin::startDecorate( QAction * /*mode*/, MeshDocument &/*m*/, RichParameterSet * parset, GLArea * /*parent*/)
bool SampleMeshDecoratePlugin::startDecorate( QAction * action, MeshDocument &/*m*/, RichParameterSet * parset, GLArea * /*parent*/)
{
assert(parset->hasParameter(CubeMapPathParam()));
basename = parset->getString(CubeMapPathParam());
if(parset->findParameter(CubeMapPathParam())== NULL)
qDebug("CubeMapPath was not setted!!!");
switch(ID(action)){
case DP_SHOW_CUBEMAPPED_ENV :
if(parset->findParameter(CubeMapPathParam())== NULL) qDebug("CubeMapPath was not setted!!!");
cubemapFileName = parset->getString(CubeMapPathParam());
break;
}
return true;
}
void SampleMeshDecoratePlugin::decorate(QAction *a, MeshDocument &/*m*/, RichParameterSet * parset,GLArea *gla, QPainter * )
void SampleMeshDecoratePlugin::decorate(QAction *a, MeshDocument &m, RichParameterSet * parset,GLArea *gla, QPainter * )
{
static QString lastname("unitialized");
static QString lastname("unitialized");
switch(ID(a))
{
case DP_SHOW_CUBEMAPPED_ENV :
{
if(a->text() != filterName(DP_SHOW_CUBEMAPPED_ENV)) assert(0);
if(!cm.IsValid())
{
if(lastname != basename )
{
qDebug( "Current CubeMapPath Dir: %s ",qPrintable(basename));
glewInit();
bool ret = cm.Load(qPrintable(basename));
lastname=basename;
if(! ret ) QMessageBox::warning(gla,"Cubemapped background decoration","Warning unable to load cube map images: " + basename );
cm.radius=10;
}
}
if(!cm.IsValid() || (lastname != cubemapFileName ) )
{
qDebug( "Current CubeMapPath Dir: %s ",qPrintable(cubemapFileName));
glewInit();
bool ret = cm.Load(qPrintable(cubemapFileName));
lastname=cubemapFileName;
if(! ret ) QMessageBox::warning(gla,"Cubemapped background decoration","Warning unable to load cube map images: " + cubemapFileName );
cm.radius=10;
}
if(!cm.IsValid()) return;
Matrix44f tr;
glGetv(GL_MODELVIEW_MATRIX,tr);
Matrix44f tr;
glGetv(GL_MODELVIEW_MATRIX,tr);
// Remove the translation from the current matrix by simply padding the last column of the matrix
tr[0][3]=0;
tr[1][3]=0;
tr[2][3]=0;
tr[3][3]=1;
tr.SetColumn(3,Point4f(0,0,0,1.0));
//Remove the scaling from the the current matrix by adding an inverse scaling matrix
float scale = 1.0/pow(tr.Determinant(),1.0f/3.0f);
Matrix44f Scale;
Scale.SetDiagonal(scale);
tr=tr*Scale;
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
cm.DrawEnvCube(tr);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
}
} break;
case DP_SHOW_GRID :
{
Box3f bb=m.bbox();
float scaleBB = parset->getFloat(BoxRatioParam());
bb.Offset((bb.max-bb.min)*(scaleBB-1.0));
DrawGriddedCube(bb,10,1,gla);
} break;
}
}
void DrawGridPlane(int axis, int side,
Point3f minP, Point3f maxP,
Point3f minG, Point3f maxG, // the snapped position of the grids.
float majorTick, float minorTick)
{
int xAxis = (1+axis)%3;
int yAxis = (2+axis)%3;
int zAxis = (0+axis)%3; // the axis perpendicular to the plane we want to draw (e.g. if i want to draw plane xy I wrote '2')
Point3f p1,p2;
p1[zAxis]=p2[zAxis] = side ? minP[zAxis] : maxP[zAxis] ;
glLineWidth(0.5f);
glBegin(GL_LINES);
// first draw the vertical lines
p1[yAxis]=minP[yAxis];
p2[yAxis]=maxP[yAxis];
for(float alpha=minG[xAxis];alpha<maxP[xAxis];alpha+=majorTick)
{
p1[xAxis]=alpha;
p2[xAxis]=alpha;
glVertex(p1);
glVertex(p2);
}
// then the horizontal lines
p1[xAxis]=minP[xAxis];
p2[xAxis]=maxP[xAxis];
for(float alpha=minG[yAxis];alpha<maxP[yAxis];alpha+=majorTick)
{
p1[yAxis]=alpha;
p2[yAxis]=alpha;
glVertex(p1);
glVertex(p2);
}
glEnd();
glLineWidth(1.0f);
glBegin(GL_LINES);
if(minP[xAxis]*maxP[xAxis] <0 )
{
p1[yAxis]=minP[yAxis];
p2[yAxis]=maxP[yAxis];
p1[xAxis]=p2[xAxis]=0;
glVertex(p1);
glVertex(p2);
}
if(minP[yAxis]*maxP[yAxis] <0 )
{
p1[xAxis]=minP[xAxis];
p2[xAxis]=maxP[xAxis];
p1[yAxis]=p2[yAxis]=0;
glVertex(p1);
glVertex(p2);
}
glEnd();
}
bool FrontFacing(Point3f viewPos,
int axis, int side,
Point3f minP, Point3f maxP)
{
return true;
}
void SampleMeshDecoratePlugin::DrawGriddedCube(const Box3f &bb, float majorTick, float minorTick, GLArea *gla)
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
Point3f minP,maxP, minG,maxG;
minP=bb.min;maxP=bb.max;
for(int i=0;i<3;++i)
{
if(minP[i] > 0 ) minG[i] = minP[i] - fmod(minP[i],majorTick) + majorTick;
if(minP[i] ==0 ) minG[i] = majorTick;
if(minP[i] < 0 ) minG[i] = minP[i] + fmod(fabs(minP[i]),majorTick);
}
glDisable(GL_LIGHTING);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glDepthMask(GL_FALSE);
Point3f viewPos ;//= gla->getViewPos();
qDebug("Current camera pos %f %f %f",viewPos[0],viewPos[1],viewPos[2]);
for (int ii=0;ii<3;++ii)
for(int jj=0;jj<1;++jj)
if(FrontFacing(viewPos,ii,jj,minP,maxP))
DrawGridPlane(ii,jj,minP,maxP,minG,maxG,10,1);
glPopAttrib();
}
Q_EXPORT_PLUGIN(SampleMeshDecoratePlugin)

View File

@ -47,19 +47,25 @@ class SampleMeshDecoratePlugin : public QObject, public MeshDecorateInterface
enum {
DP_SHOW_CUBEMAPPED_ENV,
DP_SHOW_GRID
};
private:
vcg::CICubeMap cm;
inline QString CubeMapPathParam() const { return "MeshLab::Decoration::CubeMapPath" ; }
inline QString GridMajorParam() const { return "MeshLab::Decoration::GridMajor" ; }
inline QString GridMinorParam() const { return "MeshLab::Decoration::GridMinor" ; }
inline QString BoxRatioParam() const { return "MeshLab::Decoration::BoxRatio" ; }
inline QString ShowShadowParam() const { return "MeshLab::Decoration::ShowShadow" ; }
public:
SampleMeshDecoratePlugin()
{
typeList <<
DP_SHOW_CUBEMAPPED_ENV ;
typeList
<< DP_SHOW_CUBEMAPPED_ENV
<< DP_SHOW_GRID;
FilterIDType tt;
foreach(tt , types()){
@ -72,12 +78,17 @@ public:
}
QList<QAction *> actions () const {return actionList;}
QString basename;
QString cubemapFileName;
bool startDecorate(QAction * /*mode*/, MeshDocument &/*m*/, RichParameterSet * /*parent*/ par, GLArea * /*parent*/);
void decorate(QAction *a, MeshDocument &md, RichParameterSet *, GLArea *gla, QPainter *);
void initGlobalParameterSet(QAction *, RichParameterSet &/*globalparam*/);
private:
void DrawGriddedCube(const vcg::Box3f &bb, float majorTick, float minorTick,GLArea *gla);
};
#endif