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>
29 #include "hkl3d-gui-model.h"
31 #include "btBulletDynamicsCommon.h"
34 // Trackball utilities.
38 #include "trackball.h"
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)
63 hkl_geometry_free(_geometry
);
66 void LogoModel::model_draw(void)
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
,
84 len
= _hkl3d
->_btCollisionObjects
.size();
86 btCollisionObject
*object
;
88 object
= _hkl3d
->_btCollisionObjects
[i
];
89 object
->getWorldTransform().getOpenGLMatrix( m
);
90 m_shapeDrawer
->drawOpenGL(m
,
91 object
->getCollisionShape(),
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
;
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(),
120 obB
->getWorldTransform().getOpenGLMatrix( m
);
121 m_shapeDrawer
->drawOpenGL(m
,
122 obB
->getCollisionShape(),
128 // now draw the manifolds / points
129 numContacts
= contactManifold
->getNumContacts();
130 for (j
=0; j
<numContacts
; j
++){
131 btManifoldPoint
& pt
= contactManifold
->getContactPoint(j
);
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());
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
[] = {
181 { 0, 0.0 } // terminator
184 Model::Model(unsigned int rot_count
,
186 : m_RotCount(rot_count
),
187 m_EnableAnim(enable_anim
), m_Mode(0), m_Counter(0)
189 //this->logoM=new LogoModel();
197 void Model::init_gl(LogoModel
* logoM
)
199 glEnable(GL_CULL_FACE
);
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();
216 void Model::draw(void)
219 static bool initialized
= false;
227 if (m_Counter
== m_RotCount
) {
228 if (ROT_MODE
[++m_Mode
].axis
== 0)
234 Trackball::axis_to_quat(ROT_MODE
[m_Mode
].axis
,
235 ROT_MODE
[m_Mode
].sign
* G_PI_2
/ m_RotCount
,
237 Trackball::add_quats(d_quat
, m_Quat
, m_Quat
);
244 glTranslatef(m_Pos
[0], m_Pos
[1], m_Pos
[2]);
247 Trackball::build_rotmatrix(m
, m_Quat
);
248 glMultMatrixf(&m
[0][0]);
250 glRotatef(0.0, 0.0, 0.0, 1.0);
252 glCallList(G_FORWARD
);
253 glCallList(G_BACKWARD
);
254 glCallList(T_FORWARD
);
255 glCallList(T_BACKWARD
);
256 glCallList(K_FORWARD
);
257 glCallList(K_BACKWARD
);
261 void Model::reset_anim(void)