Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. // -------------------------------
  2. // Universität Tübingen, WSI/GRIS
  3. // Graphische Datenverarbeitung I
  4. // Dr. P. Jenke
  5. // -------------------------------
  6.  
  7. // Disable nasty Qt4 vs. VS8 warnings
  8. #ifdef WIN32
  9. #pragma warning ( disable : 4311 )
  10. #pragma warning ( disable : 4312 )
  11. #include <qtCore/qatomic.h>
  12. #pragma warning ( default : 4311 )
  13. #pragma warning ( default : 4312 )
  14. #pragma warning(disable : 4996)
  15. #endif
  16.  
  17. #include "gdv_lib/PolygonNode.h"
  18. #include "ParametricCurve.h"
  19. #include "ParametricCurve.cpp"
  20. #include "MyOpenGLWidget.hxx"
  21.  
  22. using namespace std;
  23.  
  24. MyOpenGLWidget::MyOpenGLWidget( QWidget* parent ) : OpenGLWidget( parent )
  25. {
  26. settings = new GDV1Settings();
  27. curve = new ParametricCurve();
  28.  
  29. setWindowTitle( "my opengl widget" );
  30. resize( 640, 480 );
  31. show();
  32. };
  33.  
  34. MyOpenGLWidget::~MyOpenGLWidget()
  35. {
  36. delete settings;
  37. delete curve;
  38. };
  39.  
  40. void MyOpenGLWidget::initializeGL()
  41. {
  42. OpenGLWidget::initializeGL();
  43. }
  44.  
  45. void MyOpenGLWidget::paintGL()
  46. {
  47. OpenGLWidget::paintGL();
  48. glDisable( GL_LIGHTING );
  49.  
  50. // render curve
  51. if ( settings->curve_visible ) {
  52. draw_curve();
  53. }
  54.  
  55. /// draw control points (used in later exercises)
  56. if ( settings->curve_show_control_points ){
  57. draw_control_points();
  58. }
  59. }
  60.  
  61. void MyOpenGLWidget::resizeGL( int w, int h )
  62. {
  63. OpenGLWidget::resizeGL( w, h );
  64. }
  65.  
  66. void MyOpenGLWidget::mouseMoveEvent( QMouseEvent* event )
  67. {
  68. OpenGLWidget::mouseMoveEvent( event );
  69.  
  70. // move closest control point
  71. if ( ( event->buttons() & Qt::LeftButton ) && shift_pressed ){
  72. update_control_points( makeVector2i( event->x(), event->y() ) );
  73. }
  74. }
  75.  
  76. void MyOpenGLWidget::mousePressEvent( QMouseEvent* event )
  77. {
  78. OpenGLWidget::mousePressEvent( event );
  79.  
  80. // move closest control point
  81. if ( ( event->button() == Qt::LeftButton ) && shift_pressed ){
  82. update_control_points( makeVector2i( event->x(), event->y() ) );
  83. }
  84. }
  85.  
  86. void MyOpenGLWidget::mouseReleaseEvent( QMouseEvent* event )
  87. {
  88. OpenGLWidget::mouseReleaseEvent( event );
  89. }
  90.  
  91. void MyOpenGLWidget::draw_curve()
  92. {
  93. const float t = 1 / le_curve_resolution ;
  94. glBegin( GL_LINE_STRIP );
  95. for (int n = 0; n<= resolution;n++){
  96. const float vector = eval_circle(t,r);
  97. glVertex3f(vector);
  98. }
  99. glEnd();
  100.  
  101. // implement the required curve drawing functionality here
  102. }
  103.  
  104. void MyOpenGLWidget::draw_control_point( const Vector2f& c ) const
  105. {
  106. const float r = 0.03f;
  107. const int resolution = 20;
  108. const float PI = 3.1415926535897932384626433832795f;
  109.  
  110. glBegin( GL_LINE_STRIP );
  111. glColor3f( 0.25, 0.25, 0.75 );
  112. for ( int n = 0; n <= resolution; n++ ){
  113. const float alpha = float( n ) / float( resolution ) * 2 * PI;
  114. Vector3f p = makeVector3f( sin( alpha ), cos( alpha ), 0 ) * r;
  115. p[0] += c[0];
  116. p[1] += c[1];
  117. glVertex3fv( p.dataPtr() );
  118. }
  119.  
  120. glEnd();
  121. }
  122.  
  123. void MyOpenGLWidget::draw_control_points()
  124. {
  125. for( int n = 0; n < curve->get_num_control_points(); n++ ){
  126. draw_control_point( curve->get_control_point( n ) );
  127. }
  128.  
  129. glBegin( GL_LINE_STRIP );
  130. glColor3f( 0,0,0 );
  131. for( int n = 0; n < curve->get_num_control_points(); n++ ){
  132. const Vector2f v = curve->get_control_point( n );
  133. glVertex3f( v[0], v[1], 0 );
  134. }
  135. glEnd();
  136. }
  137.  
  138. void MyOpenGLWidget::slt_settings_changed()
  139. {
  140. repaint();
  141. }
  142.  
  143. void MyOpenGLWidget::slt_key_pressed( QKeyEvent* e )
  144. {
  145. OpenGLWidget::keyPressEvent( e );
  146. }
  147.  
  148. void MyOpenGLWidget::slt_key_released( QKeyEvent* e )
  149. {
  150. OpenGLWidget::keyReleaseEvent( e );
  151. repaint();
  152. }
  153.  
  154. void MyOpenGLWidget::keyPressEvent( QKeyEvent* e )
  155. {
  156. slt_key_pressed( e );
  157. }
  158.  
  159. void MyOpenGLWidget::keyReleaseEvent( QKeyEvent * e )
  160. {
  161. slt_key_released( e );
  162. }
  163.  
  164. void MyOpenGLWidget::update_control_points( const Vector2i& click_point_2D )
  165. {
  166. GLint viewport[4];
  167. GLdouble mv_matrix[16], proj_matrix[16];
  168. GLdouble wx, wy, wz;
  169. glGetIntegerv( GL_VIEWPORT, viewport );
  170. glGetDoublev( GL_MODELVIEW_MATRIX, mv_matrix );
  171. glGetDoublev( GL_PROJECTION_MATRIX, proj_matrix );
  172. GLint real_x = viewport[2] - (GLint)click_point_2D[0] - 1;
  173. GLint real_y = /*viewport[3] -*/ (GLint)click_point_2D[1];
  174. gluUnProject( real_x, real_y, 0.0, mv_matrix, proj_matrix, viewport, &wx, &wy, &wz );
  175. Vector3f d = makeVector3f( wx, wy, wz ) - cam->get_eye();
  176. const float lambda = cam->get_eye()[2] / d[2];
  177.  
  178. const Vector2f closest_point_2D = makeVector2f( cam->get_eye()[0] + d[0] * lambda, cam->get_eye()[1] + d[1] * lambda );
  179.  
  180. int closest_index = -1;
  181. float closest_sqr_distance = -1;
  182. for ( int n = 0; n < curve->get_num_control_points(); n++ ){
  183. const Vector2f v = curve->get_control_point( n );
  184. const float sqr_d = ( closest_point_2D - v ) * ( closest_point_2D - v );
  185. if ( ( closest_index < 0 ) || ( sqr_d < closest_sqr_distance ) ){
  186. closest_sqr_distance = sqr_d;
  187. closest_index = n;
  188. }
  189. }
  190. if ( closest_index >= 0 ){
  191. curve->set_control_point( closest_index, closest_point_2D );
  192. repaint();
  193. }
  194. }
  195.  
  196. void MyOpenGLWidget::animation_step()
  197. {
  198. //cout << "tick" << endl;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement