goat3d

changeset 88:7941e89798e5

selections
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 15 May 2014 06:52:01 +0300
parents f0277d348c2c
children c1871707c678
files goatview/src/goatview.cc goatview/src/goatview.h goatview/src/scenemodel.cc goatview/src/scenemodel.h src/goat3d.cc src/goat3d.h
diffstat 6 files changed, 105 insertions(+), 10 deletions(-) [+]
line diff
     1.1 --- a/goatview/src/goatview.cc	Wed May 14 18:28:38 2014 +0300
     1.2 +++ b/goatview/src/goatview.cc	Thu May 15 06:52:01 2014 +0300
     1.3 @@ -11,6 +11,7 @@
     1.4  
     1.5  goat3d *scene;
     1.6  static SceneModel *sdata;
     1.7 +static GoatViewport *glview;
     1.8  
     1.9  static long anim_time;
    1.10  static float cam_theta, cam_phi, cam_dist = 8;
    1.11 @@ -18,6 +19,11 @@
    1.12  static bool use_nodes = true;
    1.13  static bool use_lighting = true;
    1.14  
    1.15 +void post_redisplay()
    1.16 +{
    1.17 +	glview->updateGL();
    1.18 +}
    1.19 +
    1.20  
    1.21  GoatView::GoatView()
    1.22  {
    1.23 @@ -105,7 +111,7 @@
    1.24  	act_use_nodes->setCheckable(true);
    1.25  	act_use_nodes->setChecked(use_nodes);
    1.26  	connect(act_use_nodes, &QAction::triggered, this,
    1.27 -			[&](){ use_nodes = !use_nodes; glview->updateGL(); });
    1.28 +			[&](){ use_nodes = !use_nodes; post_redisplay(); });
    1.29  	menu_view->addAction(act_use_nodes);
    1.30  
    1.31  	QAction *act_use_lighting = new QAction("lighting", this);
    1.32 @@ -137,19 +143,17 @@
    1.33  
    1.34  	// make the tree view widget
    1.35  	treeview = new QTreeView;
    1.36 -	/*
    1.37 -	scntree->setColumnCount(1);
    1.38 -	QStringList hdrstr;
    1.39 -	hdrstr << "Node";// << "Type";
    1.40 -	scntree->setHeaderItem(new QTreeWidgetItem((QTreeWidget*)0, hdrstr));
    1.41 -	*/
    1.42  	treeview->setAlternatingRowColors(true);
    1.43 +	treeview->setSelectionMode(QAbstractItemView::SingleSelection);
    1.44  	dock_vbox->addWidget(treeview);
    1.45  
    1.46  	scene_model = new SceneModel;
    1.47 -	connect(scene_model, &SceneModel::dataChanged, [&](){ glview->updateGL(); });
    1.48 +	connect(scene_model, &SceneModel::dataChanged, [&](){ post_redisplay(); });
    1.49  	treeview->setModel(scene_model);
    1.50  
    1.51 +	connect(treeview->selectionModel(), &QItemSelectionModel::selectionChanged,
    1.52 +			[&](){ scene_model->selchange(treeview->selectionModel()->selectedIndexes()); });
    1.53 +
    1.54  	// misc
    1.55  	QPushButton *bn_quit = new QPushButton("quit");
    1.56  	dock_vbox->addWidget(bn_quit);
    1.57 @@ -174,7 +178,7 @@
    1.58  
    1.59  bool GoatView::make_center()
    1.60  {
    1.61 -	glview = new GoatViewport(this);
    1.62 +	glview = ::glview = new GoatViewport(this);
    1.63  	setCentralWidget(glview);
    1.64  	return true;
    1.65  }
    1.66 @@ -316,6 +320,43 @@
    1.67  			goat3d_mesh *mesh = (goat3d_mesh*)goat3d_get_node_object(node);
    1.68  
    1.69  			draw_mesh(mesh);
    1.70 +
    1.71 +			if(data->selected) {
    1.72 +				float bmin[3], bmax[3];
    1.73 +				goat3d_get_mesh_bounds(mesh, bmin, bmax);
    1.74 +
    1.75 +				glPushAttrib(GL_ENABLE_BIT);
    1.76 +				glDisable(GL_LIGHTING);
    1.77 +
    1.78 +				glColor3f(0.3, 1, 0.2);
    1.79 +
    1.80 +				glBegin(GL_LINE_LOOP);
    1.81 +				glVertex3f(bmin[0], bmin[1], bmin[2]);
    1.82 +				glVertex3f(bmax[0], bmin[1], bmin[2]);
    1.83 +				glVertex3f(bmax[0], bmin[1], bmax[2]);
    1.84 +				glVertex3f(bmin[0], bmin[1], bmax[2]);
    1.85 +				glEnd();
    1.86 +
    1.87 +				glBegin(GL_LINE_LOOP);
    1.88 +				glVertex3f(bmin[0], bmax[1], bmin[2]);
    1.89 +				glVertex3f(bmax[0], bmax[1], bmin[2]);
    1.90 +				glVertex3f(bmax[0], bmax[1], bmax[2]);
    1.91 +				glVertex3f(bmin[0], bmax[1], bmax[2]);
    1.92 +				glEnd();
    1.93 +
    1.94 +				glBegin(GL_LINES);
    1.95 +				glVertex3f(bmin[0], bmin[1], bmin[2]);
    1.96 +				glVertex3f(bmin[0], bmax[1], bmin[2]);
    1.97 +				glVertex3f(bmin[0], bmin[1], bmax[2]);
    1.98 +				glVertex3f(bmin[0], bmax[1], bmax[2]);
    1.99 +				glVertex3f(bmax[0], bmin[1], bmin[2]);
   1.100 +				glVertex3f(bmax[0], bmax[1], bmin[2]);
   1.101 +				glVertex3f(bmax[0], bmin[1], bmax[2]);
   1.102 +				glVertex3f(bmax[0], bmax[1], bmax[2]);
   1.103 +				glEnd();
   1.104 +
   1.105 +				glPopAttrib();
   1.106 +			}
   1.107  		}
   1.108  	}
   1.109  
     2.1 --- a/goatview/src/goatview.h	Wed May 14 18:28:38 2014 +0300
     2.2 +++ b/goatview/src/goatview.h	Thu May 15 06:52:01 2014 +0300
     2.3 @@ -7,11 +7,12 @@
     2.4  #include "goat3d.h"
     2.5  #include "scenemodel.h"
     2.6  
     2.7 +void post_redisplay();
     2.8 +
     2.9  extern goat3d *scene;
    2.10  
    2.11  class GoatViewport;
    2.12  
    2.13 -
    2.14  class GoatView : public QMainWindow {
    2.15  private:
    2.16  	Q_OBJECT
     3.1 --- a/goatview/src/scenemodel.cc	Wed May 14 18:28:38 2014 +0300
     3.2 +++ b/goatview/src/scenemodel.cc	Thu May 15 06:52:01 2014 +0300
     3.3 @@ -1,5 +1,6 @@
     3.4  #include <assert.h>
     3.5  #include "scenemodel.h"
     3.6 +#include "goatview.h"
     3.7  
     3.8  SceneModel::SceneModel()
     3.9  {
    3.10 @@ -25,6 +26,7 @@
    3.11  
    3.12  		SceneNodeData data;
    3.13  		data.visible = true;
    3.14 +		data.selected = false;
    3.15  
    3.16  		node_data[node] = data;
    3.17  	}
    3.18 @@ -246,3 +248,25 @@
    3.19  
    3.20  	return createIndex(pidx, 0, (void*)parent);
    3.21  }
    3.22 +
    3.23 +
    3.24 +void SceneModel::selchange(const QModelIndexList &selidx)
    3.25 +{
    3.26 +	// go over the previously selected and unselect them
    3.27 +	std::set<goat3d_node*>::iterator it = selected.begin();
    3.28 +	while(it != selected.end()) {
    3.29 +		goat3d_node *node = *it++;
    3.30 +		SceneNodeData *data = get_node_data(node);
    3.31 +		data->selected = false;
    3.32 +	}
    3.33 +	selected.clear();
    3.34 +
    3.35 +	for(int i=0; i<selidx.size(); i++) {
    3.36 +		goat3d_node *node = (goat3d_node*)selidx.at(i).internalPointer();
    3.37 +		SceneNodeData *data = get_node_data(node);
    3.38 +		data->selected = true;
    3.39 +		selected.insert(node);
    3.40 +	}
    3.41 +
    3.42 +	post_redisplay();
    3.43 +}
     4.1 --- a/goatview/src/scenemodel.h	Wed May 14 18:28:38 2014 +0300
     4.2 +++ b/goatview/src/scenemodel.h	Thu May 15 06:52:01 2014 +0300
     4.3 @@ -2,11 +2,13 @@
     4.4  #define SCENEMODEL_H_
     4.5  
     4.6  #include <map>
     4.7 +#include <set>
     4.8  #include <QtCore/QAbstractItemModel>
     4.9  #include "goat3d.h"
    4.10  
    4.11  struct SceneNodeData {
    4.12  	bool visible;
    4.13 +	bool selected;
    4.14  };
    4.15  
    4.16  class SceneModel : public QAbstractItemModel {
    4.17 @@ -15,6 +17,7 @@
    4.18  
    4.19  	goat3d *scn;
    4.20  	std::map<goat3d_node*, SceneNodeData> node_data;
    4.21 +	std::set<goat3d_node*> selected;
    4.22  
    4.23  public:
    4.24  	SceneModel();
    4.25 @@ -34,6 +37,8 @@
    4.26  	bool hasChildren(const QModelIndex &parent) const;
    4.27  	QModelIndex index(int row, int column, const QModelIndex &parent) const;
    4.28  	QModelIndex parent(const QModelIndex &index) const;
    4.29 +
    4.30 +	void selchange(const QModelIndexList &selidx);
    4.31  };
    4.32  
    4.33  #endif	// SCENEMODEL_H_
     5.1 --- a/src/goat3d.cc	Wed May 14 18:28:38 2014 +0300
     5.2 +++ b/src/goat3d.cc	Thu May 15 06:52:01 2014 +0300
     5.3 @@ -676,6 +676,17 @@
     5.4  	im_use[GOAT3D_MESH_ATTR_COLOR] = true;
     5.5  }
     5.6  
     5.7 +
     5.8 +GOAT3DAPI void goat3d_get_mesh_bounds(const struct goat3d_mesh *mesh, float *bmin, float *bmax)
     5.9 +{
    5.10 +	AABox box = mesh->get_bounds(Matrix4x4::identity);
    5.11 +
    5.12 +	for(int i=0; i<3; i++) {
    5.13 +		bmin[i] = box.bmin[i];
    5.14 +		bmax[i] = box.bmax[i];
    5.15 +	}
    5.16 +}
    5.17 +
    5.18  /* lights */
    5.19  GOAT3DAPI void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt)
    5.20  {
    5.21 @@ -934,6 +945,15 @@
    5.22  	node->get_node_xform(tmsec, (Matrix4x4*)matrix);
    5.23  }
    5.24  
    5.25 +GOAT3DAPI void goat3d_get_node_bounds(const struct goat3d_node *node, float *bmin, float *bmax)
    5.26 +{
    5.27 +	AABox box = node->get_bounds();
    5.28 +
    5.29 +	for(int i=0; i<3; i++) {
    5.30 +		bmin[i] = box.bmin[i];
    5.31 +		bmax[i] = box.bmax[i];
    5.32 +	}
    5.33 +}
    5.34  
    5.35  }	// extern "C"
    5.36  
     6.1 --- a/src/goat3d.h	Wed May 14 18:28:38 2014 +0300
     6.2 +++ b/src/goat3d.h	Thu May 15 06:52:01 2014 +0300
     6.3 @@ -213,6 +213,8 @@
     6.4  GOAT3DAPI void goat3d_color3f(float x, float y, float z);
     6.5  GOAT3DAPI void goat3d_color4f(float x, float y, float z, float w);
     6.6  
     6.7 +GOAT3DAPI void goat3d_get_mesh_bounds(const struct goat3d_mesh *mesh, float *bmin, float *bmax);
     6.8 +
     6.9  /* lights */
    6.10  GOAT3DAPI void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt);
    6.11  GOAT3DAPI int goat3d_get_light_count(struct goat3d *g);
    6.12 @@ -278,6 +280,8 @@
    6.13  
    6.14  GOAT3DAPI void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec);
    6.15  
    6.16 +GOAT3DAPI void goat3d_get_node_bounds(const struct goat3d_node *node, float *bmin, float *bmax);
    6.17 +
    6.18  #ifdef __cplusplus
    6.19  }
    6.20  #endif