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-2011 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 public class Hkl3D
.Gui
.View
: Gtk
.Object
31 public void set_pos(float x
, float y
, float z
)
38 public void set_quat(float q0
, float q1
, float q2
, float q3
)
46 public void set_scale(float scale
)
59 const float NEAR_CLIP
= 2.0f
;
60 const float FAR_CLIP
= 60.0f
;
62 const float INIT_POS_X
= 0.0f
;
63 const float INIT_POS_Y
= 0.0f
;
64 const float INIT_POS_Z
= -30.0f
;
66 const float INIT_AXIS_X
= 0.0f
;
67 const float INIT_AXIS_Y
= 0.0f
;
68 const float INIT_AXIS_Z
= 0.0f
;
69 const float INIT_ANGLE
= 0.0f
;
71 const float INIT_SCALE
= 7.0f
;
73 const float SCALE_MAX
= 50.0f
;
74 const float SCALE_MIN
= 1.0f
;
78 this
.m_Scale
= INIT_SCALE
;
85 public void frustum(GLsizei w
, GLsizei h
)
87 glViewport(0, 0, w
, h
);
89 glMatrixMode(GL_PROJECTION
);
93 float aspect
= (float)(w
) / (float)(h
);
94 glFrustum(-aspect
, aspect
, -1.0, 1.0, NEAR_CLIP
, FAR_CLIP
);
96 float aspect
= (float)(h
) / (float)(w
);
97 glFrustum(-1.0, 1.0, -aspect
, aspect
, NEAR_CLIP
, FAR_CLIP
);
100 glMatrixMode(GL_MODELVIEW
);
103 public void ortho(int w
, int h
)
105 glMatrixMode(GL_PROJECTION
);
108 glOrtho(-w
/40, w
/40 , -h
/40 , h
/40 , -1, 100);
110 glMatrixMode(GL_MODELVIEW
);
118 glTranslatef(m_Pos
[0], m_Pos
[1], m_Pos
[2]);
119 glScalef(m_Scale
, m_Scale
, m_Scale
);
120 Trackball
.build_rotmatrix(m
, m_Quat
);
128 this
.m_Pos
[0] = this
.INIT_POS_X
;
129 this
.m_Pos
[1] = this
.INIT_POS_Y
;
130 this
.m_Pos
[2] = this
.INIT_POS_Z
;
132 float sine
= (float)Math
.sin(0.5 * INIT_ANGLE
* Hkl
.DEGTORAD
);
133 this
.m_Quat
[0] = this
.INIT_AXIS_X
* sine
;
134 this
.m_Quat
[1] = this
.INIT_AXIS_Y
* sine
;
135 this
.m_Quat
[2] = this
.INIT_AXIS_Z
* sine
;
136 this
.m_Quat
[3] = (float)Math
.cos(0.5 * INIT_ANGLE
* Hkl
.DEGTORAD
);
138 this
.m_Scale
= this
.INIT_SCALE
;
141 public bool on_button_press_event(Gdk
.EventButton event
, Hkl3D
.Gui
.Scene scene
)
143 this
.m_BeginX
= (float)event
.x
;
144 this
.m_BeginY
= (float)event
.y
;
150 public bool on_scroll_event(Gdk
.EventScroll event
, Hkl3D
.Gui
.Scene scene
)
154 if(event
.direction
== Gdk
.ScrollDirection
.DOWN
)
159 if (m_Scale
> this
.SCALE_MAX
)
160 m_Scale
= this
.SCALE_MAX
;
161 else if (m_Scale
< this
.SCALE_MIN
)
162 m_Scale
= this
.SCALE_MIN
;
165 if (redraw
&& !scene
.BulletDraw_is_enabled())
171 public bool on_motion_notify_event(Gdk
.EventMotion event
,
172 Hkl3D
.Gui
.Scene scene
)
174 float h
= scene
.allocation
.width
;
175 float w
= scene
.allocation
.height
;
176 float x
= (float)event
.x
;
177 float y
= (float)event
.y
;
181 // Translation && Rotation.
182 if ((event
.state
& Gdk
.ModifierType
.BUTTON1_MASK
) == Gdk
.ModifierType
.BUTTON1_MASK
) {
183 if((event
.state
& Gdk
.ModifierType
.SHIFT_MASK
) == Gdk
.ModifierType
.SHIFT_MASK
){
184 /* shift pressed, translate view */
185 m_Pos
[0] += (x
- m_BeginX
) / (m_Scale
* 10);
186 m_Pos
[1] -= (y
- m_BeginY
) / (m_Scale
* 10);
189 Trackball
.trackball(d_quat
,
190 (2.0f
* m_BeginX
- w
) / w
,
191 (h
- 2.0f
* m_BeginY
) / h
,
194 Trackball
.add_quats(d_quat
, m_Quat
, m_Quat
);
200 if ((event
.state
& Gdk
.ModifierType
.BUTTON2_MASK
) == Gdk
.ModifierType
.BUTTON2_MASK
) {
201 this
.m_Scale
= this
.m_Scale
* (1.0f
+ (y
- this
.m_BeginY
) / h
);
202 if (this
.m_Scale
> this
.SCALE_MAX
)
203 this
.m_Scale
= this
.SCALE_MAX
;
204 else if (this
.m_Scale
< this
.SCALE_MIN
)
205 this
.m_Scale
= this
.SCALE_MIN
;
212 if (redraw
&& !scene
.BulletDraw_is_enabled())
218 } //namespace Hkl3dGui