Hi
I'm starting a small cad editor just for fun using QGLWidgt.
1)To display a crosshair cursor lines I am updating the mouse move event and calling a function DrawCursorCrossHairs()  from paintGL(). This works nice but doenst seem the right approach has in time, and having a lot of entities (lines, object drawing, …) they will be constantly updated with the mouse movement. Is there any other better way  do display the cursor cross hair lines, without calling the paintGL() ?
Code:
	
	- void DrawGLWidget::paintGL() 
- { 
-     glPushMatrix(); 
-     glTranslated(tx,ty,tz); 
-     glPushMatrix(); 
-     glScaled(scale,scale,scale); 
-   
-   
-     Draw(); 
-     DrawCursorCrossHairs(); 
-   
-   
-     glPopMatrix(); 
-     glPopMatrix(); 
- } 
-   
- …............ 
-   
- void DrawGLWidget::DrawCursorCrossHairs() 
- { 
-   
-     glPushMatrix(); 
-   
-     glColor3d(1.0,0.0,0.0); 
-     glBegin(GL_LINE_STRIP); 
-     glVertex2d( m_CursorCrossHairs.x, height()); 
-     glVertex2d( m_CursorCrossHairs.x, -height()); 
-     glEnd(); 
-   
-     glBegin(GL_LINE_STRIP); 
-     glVertex2d( -width(), m_CursorCrossHairs.y); 
-     glVertex2d( width(), m_CursorCrossHairs.y); 
-     glEnd(); 
-   
-     glPopMatrix(); 
-   
- } 
-   
-   
- { 
-     int ww=width(); 
-     int hh=height(); 
-     if (ww <= hh) 
-     { 
-   
-         m_CursorCrossHairs.y = -2*Range/ww*event->pos().y()+Range*hh/ww; 
-         m_CursorCrossHairs.x = 2*Range/ww*event->pos().x()-Range; 
-     } 
-     else 
-     { 
-         m_CursorCrossHairs.y = -2*Range/hh*event->pos().y()+Range; 
-         m_CursorCrossHairs.x = 2*Range/hh*event->pos().x()-Range*ww/hh; 
-     } 
-   
-    // m_CursorCrossHairs.x = event->pos().x(); 
-    // m_CursorCrossHairs.y = event->pos().y(); 
-   
-     updateGL(); 
        void DrawGLWidget::paintGL()
{
    glPushMatrix();
    glTranslated(tx,ty,tz);
    glPushMatrix();
    glScaled(scale,scale,scale);
    Draw();
    DrawCursorCrossHairs();
    glPopMatrix();
    glPopMatrix();
}
…............
void DrawGLWidget::DrawCursorCrossHairs()
{
    glPushMatrix();
    glColor3d(1.0,0.0,0.0);
    glBegin(GL_LINE_STRIP);
    glVertex2d( m_CursorCrossHairs.x, height());
    glVertex2d( m_CursorCrossHairs.x, -height());
    glEnd();
    glBegin(GL_LINE_STRIP);
    glVertex2d( -width(), m_CursorCrossHairs.y);
    glVertex2d( width(), m_CursorCrossHairs.y);
    glEnd();
    glPopMatrix();
}
void DrawGLWidget::mouseMoveEvent(QMouseEvent *event)
{
    int ww=width();
    int hh=height();
    if (ww <= hh)
    {
        m_CursorCrossHairs.y = -2*Range/ww*event->pos().y()+Range*hh/ww;
        m_CursorCrossHairs.x = 2*Range/ww*event->pos().x()-Range;
    }
    else
    {
        m_CursorCrossHairs.y = -2*Range/hh*event->pos().y()+Range;
        m_CursorCrossHairs.x = 2*Range/hh*event->pos().x()-Range*ww/hh;
    }
   // m_CursorCrossHairs.x = event->pos().x();
   // m_CursorCrossHairs.y = event->pos().y();
    updateGL();
To copy to clipboard, switch view to plain text mode 
  }
2)
I defined the viewport like this:
	
	- void DrawGLWidget::resizeGL(int width, int height) 
- { 
-     int w=width; 
-     int h=height; 
- // Prevent a divide by zero, when window is too short 
- // (you cant make a window of zero width) 
-     if(h == 0)//altura 
-         h = 1; 
-   
-         // Set the viewport to be the entire window 
-     glViewport(0, 0, w, h); 
-     //Reset projection matrix stack 
-     glMatrixMode(GL_PROJECTION); 
-     // Reset the coordinate system before modifying 
-     glLoadIdentity(); 
-   
-     if (w <= h) 
-         glOrtho(-(Range),Range,-Range*h/w,Range*h/w,-(Range*2),Range*2); 
-     else 
-         glOrtho(-(Range*w/h),Range*w/h,-Range,Range,-(Range*2),Range*2); 
-   
-     glMatrixMode(GL_MODELVIEW); 
-     glLoadIdentity(); 
- } 
        void DrawGLWidget::resizeGL(int width, int height)
{
    int w=width;
    int h=height;
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width)
    if(h == 0)//altura
        h = 1;
        // Set the viewport to be the entire window
    glViewport(0, 0, w, h);
    //Reset projection matrix stack
    glMatrixMode(GL_PROJECTION);
    // Reset the coordinate system before modifying
    glLoadIdentity();
    if (w <= h)
        glOrtho(-(Range),Range,-Range*h/w,Range*h/w,-(Range*2),Range*2);
    else
        glOrtho(-(Range*w/h),Range*w/h,-Range,Range,-(Range*2),Range*2);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
To copy to clipboard, switch view to plain text mode 
  
Has you see I made some math effort in the  mouseMoveEvent() to translate the mouse coordinates to the GLWidget coordinates. Is there some direct / easy way to translate mouse / viewport coordinates ?
I have attach what I got so far.
				
			
Bookmarks