Commit 269abf38 authored by Sylvain Thery's avatar Sylvain Thery

Update some bugs (VBO unbinding in Shaders ...)

Add pickable object for interface (try frame_manip example)
Add frame manipulator
Add GL matrices access from static GLSLShader
parent a134c4e7
...@@ -38,6 +38,11 @@ add_executable( polyhedronsViewD ../polyhedronsView.cpp ${polyhedronsView_moc}) ...@@ -38,6 +38,11 @@ add_executable( polyhedronsViewD ../polyhedronsView.cpp ${polyhedronsView_moc})
target_link_libraries( polyhedronsViewD target_link_libraries( polyhedronsViewD
${CGoGN_LIBS_D} ${COMMON_LIBS} ${QT_LIBRARIES} ) ${CGoGN_LIBS_D} ${COMMON_LIBS} ${QT_LIBRARIES} )
QT4_WRAP_CPP(frame_manip_moc ../frame_manip.h)
add_executable( frame_manipD ../frame_manip.cpp ${frame_manip_moc})
target_link_libraries( frame_manipD
${CGoGN_LIBS_D} ${COMMON_LIBS} ${QT_LIBRARIES} )
QT4_WRAP_CPP(extrusionView_moc ../extrusionView.h) QT4_WRAP_CPP(extrusionView_moc ../extrusionView.h)
add_executable( extrusionViewD ../extrusionView.cpp ${extrusionView_moc}) add_executable( extrusionViewD ../extrusionView.cpp ${extrusionView_moc})
target_link_libraries( extrusionViewD target_link_libraries( extrusionViewD
......
...@@ -42,6 +42,11 @@ add_executable( polyhedronsView ../polyhedronsView.cpp ${polyhedronsView_moc}) ...@@ -42,6 +42,11 @@ add_executable( polyhedronsView ../polyhedronsView.cpp ${polyhedronsView_moc})
target_link_libraries( polyhedronsView target_link_libraries( polyhedronsView
${CGoGN_LIBS_R} ${COMMON_LIBS} ${QT_LIBRARIES} ) ${CGoGN_LIBS_R} ${COMMON_LIBS} ${QT_LIBRARIES} )
QT4_WRAP_CPP(frame_manip_moc ../frame_manip.h)
add_executable( frame_manip ../frame_manip.cpp ${frame_manip_moc})
target_link_libraries( frame_manip
${CGoGN_LIBS_R} ${COMMON_LIBS} ${QT_LIBRARIES} )
QT4_WRAP_CPP(extrusionView_moc ../extrusionView.h) QT4_WRAP_CPP(extrusionView_moc ../extrusionView.h)
add_executable( extrusionView ../extrusionView.cpp ${extrusionView_moc}) add_executable( extrusionView ../extrusionView.cpp ${extrusionView_moc})
target_link_libraries( extrusionView target_link_libraries( extrusionView
......
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: http://cgogn.u-strasbg.fr/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#include <iostream>
#include "frame_manip.h"
#include "Topology/generic/parameters.h"
#include "Topology/map/map2.h"
#include "Topology/generic/embeddedMap2.h"
#include "Geometry/vector_gen.h"
#include "Geometry/matrix.h"
#include "Geometry/transfo.h"
#include "Algo/Geometry/boundingbox.h"
#include "Algo/Modelisation/polyhedron.h"
#include "Topology/generic/parameters.h"
#include "Algo/Render/GL2/mapRender.h"
#include "Utils/Shaders/shaderFlat.h"
#include "Utils/Shaders/shaderSimpleColor.h"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_precision.hpp"
#include "glm/gtc/type_ptr.hpp"
using namespace CGoGN;
struct PFP: public PFP_STANDARD
{
// definition de la carte
typedef EmbeddedMap2<Map2> MAP;
};
PFP::MAP myMap;
void MyQT::cb_initGL()
{
// choose to use GL version 2
Utils::GLSLShader::setCurrentOGLVersion(2);
m_frame = new Utils::FrameManipulator();
// frame is rendered twice the size of normal
m_frame->setSize(2.f);
// none axis is picked
m_pickedAxis=0;
// subdivision of 7 for each drawable
m_precDraw=9;
// create the Drawables (one of each)
Utils::LineDrawable* ldp = new Utils::Grid(m_precDraw);
ldp->setColor(Geom::Vec4f(0.0f,0.0f,0.8f,1.0f));
m_ld.push_back(ldp);
ldp = new Utils::Sphere(m_precDraw,m_precDraw);
ldp->setColor(Geom::Vec4f(0.0f,0.8f,0.0f,1.0f));
m_ld.push_back(ldp);
ldp = new Utils::Cube(m_precDraw);
ldp->setColor(Geom::Vec4f(0.8f,0.0f,0.0f,1.0f));
m_ld.push_back(ldp);
ldp = new Utils::Cone(m_precDraw,m_precDraw);
ldp->setColor(Geom::Vec4f(0.8f,0.0f,0.8f,1.0f));
m_ld.push_back(ldp);
ldp= new Utils::Cylinder(m_precDraw,m_precDraw);
ldp->setColor(Geom::Vec4f(0.0f,0.8f,0.8f,1.0f));
m_ld.push_back(ldp);
ldp= new Utils::IcoSphere(m_precDraw);
ldp->setColor(Geom::Vec4f(0.8f,0.3f,0.1f,1.0f));
m_ld.push_back(ldp);
// create a grid of pickables
Geom::Vec3f center(float(NBP-1)*2.0f, float(NBP-1)*2.0f, float(NBP-1)*2.0f);
for (unsigned int i=0; i< NBP; ++i)
{
for (unsigned int j=0; j< NBP; ++j)
{
for (unsigned int k=0; k< NBP; ++k)
{
unsigned int idx = i*NBP*NBP + j*NBP + k;
Utils::Pickable* pick = new Utils::Pickable(m_ld[idx%m_ld.size()],idx);
m_pickables.push_back(pick);
// attention transfo appliques en ordre inverse !
Geom::Vec3f position( 4.0f * Geom::Vec3f(i,j,k) - center);
pick->translate(position);
pick->randomOrientation();
pick->randomUniformScale(0.8f,1.6f);
}
}
}
m_lastPickedObject = m_pickables.front();
m_frame->setTransformation(m_lastPickedObject->transfo());
}
void MyQT::cb_redraw()
{
// affichage du frame
if (m_lastPickedObject)
m_frame->draw();
// affichage des objets
for (std::vector<Utils::Pickable*>::iterator it=m_pickables.begin(); it != m_pickables.end(); ++it)
{
if (*it == m_lastPickedObject)
glLineWidth(2.0);
else
glLineWidth(1.0);
(*it)->draw();
}
}
void MyQT::cb_mousePress(int button, int x, int y)
{
if (!Shift())
return;
m_begX = x;
m_begY = y;
// get ray of selection
Geom::Vec3f rayA,rayB;
float dist = getOrthoScreenRay(x,y,rayA,rayB);
Geom::Vec3f AB = rayB-rayA;
unsigned int fr_picked =0;
if (m_lastPickedObject) // an object is already picked ? => frame is drawn
{
// picking the frame -> axis
fr_picked = m_frame->pick(rayA,AB,dist);
// same already picked -> unpick
if (fr_picked == m_pickedAxis)
m_pickedAxis = 0;
else
m_pickedAxis=fr_picked;
}
// project center & axis on screen for easy manipulation
if (m_pickedAxis != Utils::FrameManipulator::NONE)
m_frame->storeProjection(m_pickedAxis);
// highlighting
m_frame->highlight(m_pickedAxis);
if (fr_picked == 0) // frame not picked -> pick the pickable objects
{
m_lastPickedObject = Utils::Pickable::pick(m_pickables,rayA,AB);
// set FrameManipulator on picked object
if (m_lastPickedObject)
{
m_frame->setTransformation(m_lastPickedObject->transfo());
std::cout << "Picked "<<m_lastPickedObject->shape() << " "<< m_lastPickedObject->id()<< std::endl;
}
}
updateGL();
}
void MyQT::cb_mouseMove(int button, int x, int y)
{
if (!Shift())
return;
if (Utils::FrameManipulator::rotationAxis(m_pickedAxis))
{
float angle = m_frame->angleFromMouse(x,y,x-m_begX, y-m_begY);
m_frame->rotate(m_pickedAxis, angle);
m_lastPickedObject->transfo() = m_frame->transfo();
}
else if (Utils::FrameManipulator::translationAxis(m_pickedAxis))
{
float dist = m_frame->distanceFromMouse(x-m_begX, y-m_begY);
m_frame->translate(m_pickedAxis, dist);
m_lastPickedObject->transfo() = m_frame->transfo();
}
else if (Utils::FrameManipulator::scaleAxis(m_pickedAxis) )
{
float scale = m_frame->scaleFromMouse(x-m_begX, y-m_begY);
m_frame->scale(m_pickedAxis, scale );
m_lastPickedObject->transfo() = m_frame->transfo();
}
m_begX = x;
m_begY = y;
updateGL();
return;
}
void MyQT::cb_wheelEvent(int delta, int x, int y)
{
if (!Shift())
return;
if (!m_lastPickedObject)
return;
if (Utils::FrameManipulator::rotationAxis(m_pickedAxis))
m_frame->rotate(m_pickedAxis, float(delta)/60.0f);
else if (Utils::FrameManipulator::translationAxis(m_pickedAxis))
m_frame->translate(m_pickedAxis, float(-delta)/1200.0f);
else if (Utils::FrameManipulator::scaleAxis(m_pickedAxis) )
m_frame->scale(m_pickedAxis, 1.0f+ float(-delta)/1200.0f);
else
m_frame->addSize(float(-delta)/480.0f);
m_lastPickedObject->transfo() = m_frame->transfo();
updateGL();
}
void MyQT::cb_keyPress(int code)
{
switch(code)
{
case '+':
if (m_precDraw < 128)
m_precDraw++;
for (std::vector<Utils::LineDrawable*>::iterator it=m_ld.begin(); it != m_ld.end(); ++it)
(*it)->updatePrecisionDrawing(m_precDraw);
break;
case '-':
if (m_precDraw > 3)
m_precDraw--;
for (std::vector<Utils::LineDrawable*>::iterator it=m_ld.begin(); it != m_ld.end(); ++it)
(*it)->updatePrecisionDrawing(m_precDraw);
break;
case 'r':
for (unsigned int i=0; i< NBP*NBP*NBP; ++i)
m_pickables[i]->randomOrientation();
if (m_lastPickedObject)
m_frame->setTransformation(m_lastPickedObject->transfo());
break;
case 'x':
m_frame->lock(Utils::FrameManipulator::Xt);
break;
case 'y':
m_frame->lock(Utils::FrameManipulator::Yt);
break;
case 'z':
m_frame->lock(Utils::FrameManipulator::Zt);
break;
case 'X':
m_frame->unlock(Utils::FrameManipulator::Xt);
break;
case 'Y':
m_frame->unlock(Utils::FrameManipulator::Yt);
break;
case 'Z':
m_frame->unlock(Utils::FrameManipulator::Zt);
break;
case 'a':
m_frame->lock(Utils::FrameManipulator::Xr);
break;
case 'b':
m_frame->lock(Utils::FrameManipulator::Yr);
break;
case 'c':
m_frame->lock(Utils::FrameManipulator::Zr);
break;
case 'A':
m_frame->unlock(Utils::FrameManipulator::Xr);
break;
case 'B':
m_frame->unlock(Utils::FrameManipulator::Yr);
break;
case 'C':
m_frame->unlock(Utils::FrameManipulator::Zr);
break;
}
updateGL();
}
int main(int argc, char **argv)
{
// interface:
QApplication app(argc, argv);
MyQT sqt;
if (argc>1)
sqt.NBP = atoi(argv[1]);
// bounding box
Geom::Vec3f lPosObj = Geom::Vec3f(0.0f,0.0f,0.0f);
float lWidthObj = sqt.NBP*4.0f;
// envoit info BB a l'interface
sqt.setParamObject(lWidthObj,lPosObj.data());
// show final pour premier redraw
sqt.show();
// et on attend la fin.
return app.exec();
return 0;
}
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: http://cgogn.u-strasbg.fr/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#ifndef POLYHEDRONSVIEW_H_
#define POLYHEDRONSVIEW_H_
#include <iostream>
#include "Utils/qtSimple.h"
#include "Utils/frameManipulator.h"
#include "Utils/drawer.h"
// forward definitions (minimize includes)
namespace CGoGN { namespace Algo { namespace Render { namespace GL2 { class MapRender; }}}}
namespace CGoGN { namespace Utils { class VBO; } }
namespace CGoGN { namespace Utils { class ShaderFlat; } }
namespace CGoGN { namespace Utils { class ShaderSimpleColor; } }
using namespace CGoGN ;
/**
* A class for a little interface and rendering
*/
class MyQT: public Utils::QT::SimpleQT
{
Q_OBJECT
public:
// render
Algo::Render::GL2::MapRender* m_render;
// VBO
Utils::VBO* m_positionVBO;
Utils::FrameManipulator* m_frame;
unsigned int m_pickedAxis;
///vector of six drawables
std::vector<Utils::LineDrawable*> m_ld;
/// precision of drawing (number of subdivisions)
unsigned int m_precDraw;
std::vector<Utils::Pickable*> m_pickables;
Utils::Pickable* m_lastPickedObject;
// width of cube of pickable
unsigned int NBP;
MyQT():m_render(NULL), m_positionVBO(NULL),NBP(2){}
// callbacks of simpleQT to overdefine:
void cb_redraw();
void cb_initGL();
void cb_mousePress(int button, int x, int y);
void cb_wheelEvent(int delta, int x, int y);
void cb_keyPress(int code);
void cb_mouseMove(int button, int x, int y);
int m_begX;
int m_begY;
Geom::Vec3f m_projAxis;
Geom::Vec3f m_projCenter;
};
#endif
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DockWidget</class>
<widget class="QDockWidget" name="DockWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>209</width>
<height>345</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>150</width>
<height>250</height>
</size>
</property>
<property name="windowTitle">
<string>Interface</string>
</property>
<widget class="QWidget" name="dockWidgetContents">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>150</width>
<height>200</height>
</size>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="margin">
<number>2</number>
</property>
<property name="spacing">
<number>4</number>
</property>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
...@@ -85,8 +85,8 @@ ...@@ -85,8 +85,8 @@
#include "./img/multiple.hpp" #include "./img/multiple.hpp"
#include "./img/wrap.hpp" #include "./img/wrap.hpp"
#include "./virtrev/address.hpp" //#include "./virtrev/address.hpp"
#include "./virtrev/equal_operator.hpp" //#include "./virtrev/equal_operator.hpp"
//const float goldenRatio = 1.618033988749894848f; //const float goldenRatio = 1.618033988749894848f;
//const float pi = 3.141592653589793238f; //const float pi = 3.141592653589793238f;
......
...@@ -153,7 +153,7 @@ public: ...@@ -153,7 +153,7 @@ public:
void renderLinesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& position, const FunctorSelect& good = SelectorTrue(), unsigned int thread=0); void renderLinesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& position, const FunctorSelect& good = SelectorTrue(), unsigned int thread=0);
template <typename PFP> template <typename PFP>
void renderFacesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& position, float shrink, const FunctorSelect& good = SelectorTrue(), unsigned int thread=0); void renderFacesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& position, float shrink, bool cull = false, const FunctorSelect& good = SelectorTrue(), unsigned int thread=0);
template <typename PFP> template <typename PFP>
void renderPointsToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& position, const FunctorSelect& good = SelectorTrue(), unsigned int thread=0); void renderPointsToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& position, const FunctorSelect& good = SelectorTrue(), unsigned int thread=0);
......
...@@ -101,7 +101,7 @@ void SVGOut::renderLinesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& ...@@ -101,7 +101,7 @@ void SVGOut::renderLinesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3&
template <typename PFP> template <typename PFP>
void SVGOut::renderFacesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& position, float shrink, const FunctorSelect& good, unsigned int thread) void SVGOut::renderFacesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& position, float shrink, bool cull, const FunctorSelect& good, unsigned int thread)
{ {
glm::i32vec4 viewport; glm::i32vec4 viewport;
glGetIntegerv(GL_VIEWPORT, &(viewport[0])); glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
...@@ -111,25 +111,42 @@ void SVGOut::renderFacesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3& ...@@ -111,25 +111,42 @@ void SVGOut::renderFacesToSVG(typename PFP::MAP& map, const typename PFP::TVEC3&
{ {
if(!m.isMarked(d) && good(d)) if(!m.isMarked(d) && good(d))
{ {
typename PFP::VEC3 center = Algo::Geometry::faceCentroid<PFP>(map,d,position); bool cullFace=false;
SvgPolygon* pol = new SvgPolygon(); if (cull)
Dart dd = d;
do
{ {
Geom::Vec3f P = position[d]; const Geom::Vec3f& P = position[d];
P = P*shrink + center*(1.0f-shrink);
glm::vec3 Q = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,m_proj,viewport);
glm::vec3 R = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,glm::mat4(1.0),viewport);
pol->addVertex(Geom::Vec3f(Q[0],float(viewport[3])-Q[1],Q[2]));
pol->addVertex3D(Geom::Vec3f(R[0],R[1],R[2]));
d = map.phi1(d);
}while (d!=dd);
pol->close();
pol->setColor(global_color);
pol->setWidth(global_width);
m_objs.push_back(pol);
glm::vec3 Q = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,m_proj,viewport);
const Geom::Vec3f& P2 = position[map.phi1(d)];
glm::vec3 R = glm::project(glm::vec3(P2[0],P2[1],P2[2]),m_model,m_proj,viewport);
const Geom::Vec3f& P3 = position[map.phi1(map.phi1(d))];
glm::vec3 S = glm::project(glm::vec3(P3[0],P3[1],P3[2]),m_model,m_proj,viewport);
glm::vec3 N = glm::cross(S-R,Q-R);
if (N[2]<0.0f)
cullFace=true;
}
if (!cullFace)
{
typename PFP::VEC3 center = Algo::Geometry::faceCentroid<PFP>(map,d,position);
SvgPolygon* pol = new SvgPolygon();
Dart dd = d;
do
{
Geom::Vec3f P = position[d];
P = P*shrink + center*(1.0f-shrink);
glm::vec3 Q = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,m_proj,viewport);
glm::vec3 R = glm::project(glm::vec3(P[0],P[1],P[2]),m_model,glm::mat4(1.0),viewport);
pol->addVertex(Geom::Vec3f(Q[0],float(viewport[3])-Q[1],Q[2]));
pol->addVertex3D(Geom::Vec3f(R[0],R[1],R[2]));
d = map.phi1(d);
}while (d!=dd);
pol->close();
pol->setColor(global_color);
pol->setWidth(global_width);
m_objs.push_back(pol);
}
m.markOrbit(FACE, d); m.markOrbit(FACE, d);