Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -------------------------------
- // Universität Tübingen, WSI/GRIS
- // Graphische Datenverarbeitung I
- // Dr. P. Jenke
- // -------------------------------
- // Disable nasty Qt4 vs. VS8 warnings
- #ifdef WIN32
- #pragma warning ( disable : 4311 )
- #pragma warning ( disable : 4312 )
- #include <qtCore/qatomic.h>
- #pragma warning ( default : 4311 )
- #pragma warning ( default : 4312 )
- #pragma warning(disable : 4996)
- #endif
- #include "gdv_lib/PolygonNode.h"
- #include "ParametricCurve.h"
- #include "ParametricCurve.cpp"
- #include "MyOpenGLWidget.hxx"
- using namespace std;
- MyOpenGLWidget::MyOpenGLWidget( QWidget* parent ) : OpenGLWidget( parent )
- {
- settings = new GDV1Settings();
- curve = new ParametricCurve();
- setWindowTitle( "my opengl widget" );
- resize( 640, 480 );
- show();
- };
- MyOpenGLWidget::~MyOpenGLWidget()
- {
- delete settings;
- delete curve;
- };
- void MyOpenGLWidget::initializeGL()
- {
- OpenGLWidget::initializeGL();
- }
- void MyOpenGLWidget::paintGL()
- {
- OpenGLWidget::paintGL();
- glDisable( GL_LIGHTING );
- // render curve
- if ( settings->curve_visible ) {
- draw_curve();
- }
- /// draw control points (used in later exercises)
- if ( settings->curve_show_control_points ){
- draw_control_points();
- }
- }
- void MyOpenGLWidget::resizeGL( int w, int h )
- {
- OpenGLWidget::resizeGL( w, h );
- }
- void MyOpenGLWidget::mouseMoveEvent( QMouseEvent* event )
- {
- OpenGLWidget::mouseMoveEvent( event );
- // move closest control point
- if ( ( event->buttons() & Qt::LeftButton ) && shift_pressed ){
- update_control_points( makeVector2i( event->x(), event->y() ) );
- }
- }
- void MyOpenGLWidget::mousePressEvent( QMouseEvent* event )
- {
- OpenGLWidget::mousePressEvent( event );
- // move closest control point
- if ( ( event->button() == Qt::LeftButton ) && shift_pressed ){
- update_control_points( makeVector2i( event->x(), event->y() ) );
- }
- }
- void MyOpenGLWidget::mouseReleaseEvent( QMouseEvent* event )
- {
- OpenGLWidget::mouseReleaseEvent( event );
- }
- void MyOpenGLWidget::draw_curve()
- {
- const float t = 1 / le_curve_resolution ;
- glBegin( GL_LINE_STRIP );
- for (int n = 0; n<= resolution;n++){
- const float vector = eval_circle(t,r);
- glVertex3f(vector);
- }
- glEnd();
- // implement the required curve drawing functionality here
- }
- void MyOpenGLWidget::draw_control_point( const Vector2f& c ) const
- {
- const float r = 0.03f;
- const int resolution = 20;
- const float PI = 3.1415926535897932384626433832795f;
- glBegin( GL_LINE_STRIP );
- glColor3f( 0.25, 0.25, 0.75 );
- for ( int n = 0; n <= resolution; n++ ){
- const float alpha = float( n ) / float( resolution ) * 2 * PI;
- Vector3f p = makeVector3f( sin( alpha ), cos( alpha ), 0 ) * r;
- p[0] += c[0];
- p[1] += c[1];
- glVertex3fv( p.dataPtr() );
- }
- glEnd();
- }
- void MyOpenGLWidget::draw_control_points()
- {
- for( int n = 0; n < curve->get_num_control_points(); n++ ){
- draw_control_point( curve->get_control_point( n ) );
- }
- glBegin( GL_LINE_STRIP );
- glColor3f( 0,0,0 );
- for( int n = 0; n < curve->get_num_control_points(); n++ ){
- const Vector2f v = curve->get_control_point( n );
- glVertex3f( v[0], v[1], 0 );
- }
- glEnd();
- }
- void MyOpenGLWidget::slt_settings_changed()
- {
- repaint();
- }
- void MyOpenGLWidget::slt_key_pressed( QKeyEvent* e )
- {
- OpenGLWidget::keyPressEvent( e );
- }
- void MyOpenGLWidget::slt_key_released( QKeyEvent* e )
- {
- OpenGLWidget::keyReleaseEvent( e );
- repaint();
- }
- void MyOpenGLWidget::keyPressEvent( QKeyEvent* e )
- {
- slt_key_pressed( e );
- }
- void MyOpenGLWidget::keyReleaseEvent( QKeyEvent * e )
- {
- slt_key_released( e );
- }
- void MyOpenGLWidget::update_control_points( const Vector2i& click_point_2D )
- {
- GLint viewport[4];
- GLdouble mv_matrix[16], proj_matrix[16];
- GLdouble wx, wy, wz;
- glGetIntegerv( GL_VIEWPORT, viewport );
- glGetDoublev( GL_MODELVIEW_MATRIX, mv_matrix );
- glGetDoublev( GL_PROJECTION_MATRIX, proj_matrix );
- GLint real_x = viewport[2] - (GLint)click_point_2D[0] - 1;
- GLint real_y = /*viewport[3] -*/ (GLint)click_point_2D[1];
- gluUnProject( real_x, real_y, 0.0, mv_matrix, proj_matrix, viewport, &wx, &wy, &wz );
- Vector3f d = makeVector3f( wx, wy, wz ) - cam->get_eye();
- const float lambda = cam->get_eye()[2] / d[2];
- const Vector2f closest_point_2D = makeVector2f( cam->get_eye()[0] + d[0] * lambda, cam->get_eye()[1] + d[1] * lambda );
- int closest_index = -1;
- float closest_sqr_distance = -1;
- for ( int n = 0; n < curve->get_num_control_points(); n++ ){
- const Vector2f v = curve->get_control_point( n );
- const float sqr_d = ( closest_point_2D - v ) * ( closest_point_2D - v );
- if ( ( closest_index < 0 ) || ( sqr_d < closest_sqr_distance ) ){
- closest_sqr_distance = sqr_d;
- closest_index = n;
- }
- }
- if ( closest_index >= 0 ){
- curve->set_control_point( closest_index, closest_point_2D );
- repaint();
- }
- }
- void MyOpenGLWidget::animation_step()
- {
- //cout << "tick" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement