* use the GtkBuilder for the ui
[hkl3d.git] / gui / ghkl3d / hkl3d-gui-model.cpp
blobea44c32ea10d6f10d7e6d4b31058634f4f456445
1 /*
2 * This file is part of the hkl3d library.
3 * inspired from logo-model.c of the GtkGLExt logo models.
4 * written by Naofumi Yasufuku <naofumi@users.sourceforge.net>
6 * The hkl library is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * The hkl library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
19 * Copyright (C) 2010 Synchrotron SOLEIL
20 * L'Orme des Merisiers Saint-Aubin
21 * BP 48 91192 GIF-sur-YVETTE CEDEX
23 * Authors: Oussama Sboui <oussama.sboui@synchrotron-soleil.fr>
24 * Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
27 #include <iostream>
29 #include "hkl3d-gui-model.h"
31 #include "btBulletDynamicsCommon.h"
34 // Trackball utilities.
36 namespace Trackball {
37 extern "C" {
38 #include "trackball.h"
42 namespace Logo
44 LogoModel::LogoModel(void)
46 const HklGeometryConfig *config;
48 config = hkl_geometry_factory_get_config_from_type(HKL_GEOMETRY_TYPE_KAPPA6C);
49 _geometry = hkl_geometry_factory_new(config, 50 * HKL_DEGTORAD);
50 hkl_geometry_set_values_v(_geometry, 6,
51 0.f, 0.f, 0.f, 0.f, 0.f, 60.f * HKL_DEGTORAD);
52 hkl_geometry_fprintf(stdout, _geometry);
54 _hkl3d = new Hkl3D("../../data/diffabs.dae", _geometry);
57 LogoModel::~LogoModel(void)
59 if(_hkl3d)
60 delete _hkl3d;
62 if(_geometry)
63 hkl_geometry_free(_geometry);
66 void LogoModel::model_draw(void)
68 int i;
69 int len;
70 int numManifolds;
71 btScalar m[16];
72 btVector3 worldBoundsMin;
73 btVector3 worldBoundsMax;
75 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
76 glDisable(GL_LIGHTING);
77 GL_ShapeDrawer::drawCoordSystem();
79 // draw the diffractometer
80 // get the world bounding box from bullet
81 _hkl3d->_btCollisionWorld->getBroadphase()->getBroadphaseAabb(worldBoundsMin,
82 worldBoundsMax);
84 len = _hkl3d->_btCollisionObjects.size();
85 for(i=0; i<len; ++i){
86 btCollisionObject *object;
88 object = _hkl3d->_btCollisionObjects[i];
89 object->getWorldTransform().getOpenGLMatrix( m );
90 m_shapeDrawer->drawOpenGL(m,
91 object->getCollisionShape(),
92 _hkl3d->_colors[i],
94 worldBoundsMin,
95 worldBoundsMax);
98 ///one way to draw all the contact points is iterating over contact manifolds / points:
99 numManifolds = _hkl3d->_btDispatcher->getNumManifolds();
100 for (i=0; i<numManifolds; i++){
101 btPersistentManifold *contactManifold;
102 btCollisionObject *obA;
103 btCollisionObject *obB;
104 int numContacts;
105 int j;
107 contactManifold = _hkl3d->_btDispatcher->getManifoldByIndexInternal(i);
108 obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
109 obB = static_cast<btCollisionObject*>(contactManifold->getBody1());
111 // draw the colliding object in debug mode so we can see them
112 obA->getWorldTransform().getOpenGLMatrix( m );
113 m_shapeDrawer->drawOpenGL(m,
114 obA->getCollisionShape(),
115 btVector3(1, 1, 1),
117 worldBoundsMin,
118 worldBoundsMax);
120 obB->getWorldTransform().getOpenGLMatrix( m );
121 m_shapeDrawer->drawOpenGL(m,
122 obB->getCollisionShape(),
123 btVector3(1, 1, 1),
125 worldBoundsMin,
126 worldBoundsMax);
128 // now draw the manifolds / points
129 numContacts = contactManifold->getNumContacts();
130 for (j=0; j<numContacts; j++){
131 btManifoldPoint & pt = contactManifold->getContactPoint(j);
133 glBegin(GL_LINES);
134 glColor3f(1, 0, 1);
136 btVector3 ptA = pt.getPositionWorldOnA();
137 btVector3 ptB = pt.getPositionWorldOnB();
139 glVertex3d(ptA.x(),ptA.y(),ptA.z());
140 glVertex3d(ptB.x(),ptB.y(),ptB.z());
141 glEnd();
145 glFlush();
148 // dummy methods
149 void LogoModel::initPhysics(void){};
151 void LogoModel::clientMoveAndDisplay(void){};
153 void LogoModel::displayCallback(void){};
156 // Model class implementation.
159 const float Model::MAT_SPECULAR[4] = { 0.5, 0.5, 0.5, 1.0 };
160 const float Model::MAT_SHININESS[1] = { 10.0 };
161 const float Model::MAT_BLACK[4] = { 0.0, 0.0, 0.0, 1.0 };
162 const float Model::MAT_RED[4] = { 1.0, 0.0, 0.0, 1.0 };
163 const float Model::MAT_GREEN[4] = { 0.0, 1.0, 0.0, 1.0 };
164 const float Model::MAT_BLUE[4] = { 0.0, 0.0, 1.0, 1.0 };
166 const unsigned int Model::DEFAULT_ROT_COUNT = 100;
168 static float AXIS_X[3] = { 1.0, 0.0, 0.0 };
169 static float AXIS_Y[3] = { 0.0, 1.0, 0.0 };
170 static float AXIS_Z[3] = { 0.0, 0.0, 1.0 };
172 const Model::RotMode Model::ROT_MODE[] = {
173 { AXIS_X, 1.0 },
174 { AXIS_Y, 1.0 },
175 { AXIS_X, 1.0 },
176 { AXIS_Z, 1.0 },
177 { AXIS_X, 1.0 },
178 { AXIS_Y, -1.0 },
179 { AXIS_X, 1.0 },
180 { AXIS_Z, -1.0 },
181 { 0, 0.0 } // terminator
184 Model::Model(unsigned int rot_count,
185 bool enable_anim)
186 : m_RotCount(rot_count),
187 m_EnableAnim(enable_anim), m_Mode(0), m_Counter(0)
189 //this->logoM=new LogoModel();
190 this->reset_anim();
193 Model::~Model(void)
197 void Model::init_gl(LogoModel* logoM)
199 glEnable(GL_CULL_FACE);
201 glPushMatrix();
203 glMaterialfv(GL_FRONT, GL_SPECULAR, MAT_SPECULAR);
204 glMaterialfv(GL_FRONT, GL_SHININESS, MAT_SHININESS);
206 /* Center black cube. */
207 glNewList(CUBE, GL_COMPILE);
208 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, MAT_BLACK);
209 this->logoM = new LogoModel();
210 this->logoM->model_draw();
211 glEndList();
216 void Model::draw(void)
218 // Init GL context.
219 static bool initialized = false;
220 if (!initialized) {
221 init_gl(logoM);
222 initialized = true;
225 // Animation.
226 if (m_EnableAnim) {
227 if (m_Counter == m_RotCount) {
228 if (ROT_MODE[++m_Mode].axis == 0)
229 m_Mode = 0;
230 m_Counter = 0;
233 float d_quat[4];
234 Trackball::axis_to_quat(ROT_MODE[m_Mode].axis,
235 ROT_MODE[m_Mode].sign * G_PI_2 / m_RotCount,
236 d_quat);
237 Trackball::add_quats(d_quat, m_Quat, m_Quat);
239 ++m_Counter;
242 // Draw logo model.
243 glPushMatrix();
244 glTranslatef(m_Pos[0], m_Pos[1], m_Pos[2]);
246 float m[4][4];
247 Trackball::build_rotmatrix(m, m_Quat);
248 glMultMatrixf(&m[0][0]);
250 glRotatef(0.0, 0.0, 0.0, 1.0);
251 glCallList(CUBE);
252 glCallList(G_FORWARD);
253 glCallList(G_BACKWARD);
254 glCallList(T_FORWARD);
255 glCallList(T_BACKWARD);
256 glCallList(K_FORWARD);
257 glCallList(K_BACKWARD);
258 glPopMatrix();
261 void Model::reset_anim(void)
263 m_Pos[0] = 0.0;
264 m_Pos[1] = 0.0;
265 m_Pos[2] = 0.0;
267 m_Quat[0] = 0.0;
268 m_Quat[1] = 0.0;
269 m_Quat[2] = 0.0;
270 m_Quat[3] = 1.0;
272 m_Mode = 0;
273 m_Counter = 0;
276 } // namespace Logo