PDA

View Full Version : best way to draw cursor crosshairs lines to a GLWidget and viewport mouse coordinates



john_god
10th January 2010, 14:05
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();

}


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();

}


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();
}

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.

ProphetPX
15th June 2021, 07:38
hi. i did something like that about 20-30 years ago, except it was in Commodore BASIC 2.0 on my trusty old C64 (i kid you not -- i was newly exposed to AutoCAD on IBM PCs and i wanted to do something similar)
(and no i no longer have my old source code but i sure wish i did -- and mine drew crosshairs using ONLY ASCII characters driven by cursor keys or maybe a joystick, and it was super slow, too! lol)

so i was wondering if you ever figured your problem out and found out how to do what you asked? yes i know this post is 10 years old lol
i want to do something like this and learn QT5 as well. I have learned some C++ now as it is (i am a newb).

d_stranz
15th June 2021, 17:00
Commodore BASIC 2.0 on my trusty old C64

Ah, the good old days. Dinosaurs. Before the wheel, fire, and all of those other things that have complicated our lives. Back when pixels came in only one color.

OpenGL has changed a lot in 10 years, too. In particular, very little is done PC-side to draw. All of the graphics are created and loaded into OpenGL data structures and then moved into the GPU and drawn with the help of shaders and other GPU-side functions. So the whole approach used by the OP is obsolete.

If you are just getting started in Qt and C++, I would recommend beginning with Qt's own graphics APIs and getting comfortable with event-driven program architecture if you have never done anything using that model. Jumping into OpenGL with both feet would quickly put you in over your head.

Your Qt distribution comes with a ton of examples covering basic graphics, the Graphics / View architecture, and Qt Quick / QML for declarative UI programming.