An issue when rendering a line depending on Mouse position - c++

I have an openGL widget, where I want to render a line which is dependent of mouse positions, as follows:
glPushMatrix();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1, 0, 0, 0.1);
glScalef(a, b, 0);
glBegin(GL_LINES);
glVertex2f(pushedX, pushedY);
glVertex2f(currentX, currentY);
glEnd();
glFlush();
glDisable(GL_BLEND);
glPopMatrix();
where:
pushedX=buttonPressCoordinates.x();
pushedY=buttonPressCoordinates.y();
currentX=mouseCurrentPosition.x();
currentY=mouseCurrentPosition.y();
The rendering goes good, and the line is rendered as required when I move the mouse connecting with line the pushed and current coordinates.
BUT:
the issue is, that even when I press my mouse somewhere on the widget and don't move it, it generates randomly (as I think) some (x,y) and connects the it with a line with the coordinates of mouse pressed position. Though when I start moving my mouse it starts working fine.
Please, help to fix this bug.
EDIT
The code of assigning the current values of the mouse
void MainWindow::mouseMoveEvent(QMouseEvent *eventMove)
{
if(eventMove->buttons() & Qt::LeftButton)
{
GLWidget *widget = this->findChild<GLWidget *>("glwidget");
float x = widget->getNormalizedWidth(widget->mapFromGlobal(QCursor::pos()).x());
float y = widget->getNormalizedHeight(widget->mapFromGlobal(QCursor::pos()).y());
float y_ = 1.0 - y;
mouseCurrentPosition.setX(x);
mouseCurrentPosition.setY(y_);
widget->setCurrentX(mouseCurrentPosition.x());
widget->setCurrentY(mouseCurrentPosition.y());
}
}
note: QPointF mouseCurrentPosition;, getNormalizedWidth(...) is my defined fundction which works perfect.
EDIT-2
The mouse click coordinates are updated as follows:
setMouseTracking(true);
m = true;
GLWidget *widget = this->findChild<GLWidget *>("glwidget");
float x = widget->getNormalizedWidth(widget->mapFromGlobal(QCursor::pos()).x());
float y = widget->getNormalizedHeight(widget->mapFromGlobal(QCursor::pos()).y());
float y_ = 1.0 - y;
buttonPressCoordinates.setX(x);
buttonPressCoordinates.setY(y_);
qDebug() << buttonPressCoordinates.x() << buttonPressCoordinates.y();
widget->setQ(true);
widget->setPushedX(buttonPressCoordinates.x());
widget->setPushedY(buttonPressCoordinates.y());

One thing you can try is to update currentX and currentY in the mouseMoveEvent when LeftButton is not pressed:
void MainWindow::mouseMoveEvent(QMouseEvent *eventMove)
{
GLWidget *widget = this->findChild<GLWidget *>("glwidget");
float x = widget->getNormalizedWidth(widget->mapFromGlobal(QCursor::pos()).x());
float y = widget->getNormalizedHeight(widget->mapFromGlobal(QCursor::pos()).y());
float y_ = 1.0 - y;
if(eventMove->buttons() & Qt::LeftButton)
{
buttonPressCoordinates.setX(x);
buttonPressCoordinates.setY(y_);
widget->setPushedX(buttonPressCoordinates.x());
widget->setPushedY(buttonPressCoordinates.y());
}
else
{
mouseCurrentPosition.setX(x);
mouseCurrentPosition.setY(y_);
widget->setCurrentX(mouseCurrentPosition.x());
widget->setCurrentY(mouseCurrentPosition.y());
}
}

Related

Polyline only renders completely after resizing the window

I am making a 3d project in OpenGL which contain a ground (drawn as line loops). The issue I have is when the project starts only a single line is drawn as shown in the next image:
When I resize or maximize the window then the actual ground gets displayed like this:
Any idea how to resolve this issue? I'm a beginner in OpenGL programming.
Here is the code :
void drawHook(void);
void timer(int);
void drawFlorr();
float L = 100;
const int screenWidth = 1000; // width of screen window in pixels
const int screenHeight = 1000; // height of screen window in pixels
float ww = 800;
float wh = 800;
float f = 520, n = 10.0;
static GLdouble ort1[] = { -200, 200, -33, 140 };
static GLdouble viewer[] = { 525, 25, -180 };
static GLdouble objec[] = { 525.0, 25, -350 };
float x, y = 0.0, z, z1;
float xmax = screenWidth - 200.0;
float zmax = screenWidth - 200.0;
float xmin, zmin;
float step = 5.0;
float fov = 80;
void myInit(void)
{
glClearColor(0.0,0.0,0.0,0.0); // background color is white
glPointSize(2.0); // a 'dot' is 2 by 2 pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, screenWidth, 0.0, screenHeight);//dino window
glViewport(0, 0, screenWidth, screenHeight);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0], viewer[1], viewer[2], objec[0], objec[1], objec[2], 0, 1, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, 1.333, n, f);
glPointSize(2.0);
glMatrixMode(GL_MODELVIEW);
drawFlorr();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // set display mode
glutInitWindowSize(screenWidth, screenHeight); // set window size
glutInitWindowPosition(10, 10); // set window position on screen
glutCreateWindow("Dino Line Drawing"); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
myInit();
//glutTimerFunc(1,timer,1);
glutMainLoop(); // go into a perpetual loop
return 1;
}
void drawFlorr()
{
xmin = -100;
zmin = -100;
for (x = xmin; x < xmax; x += step)
{
for (z = zmin; z < zmax; z += step)
{
z1 = -z;
glBegin(GL_LINE_LOOP);
glVertex3f(x, y, z1);
glVertex3f(x, y, z1-step+1.0);
glVertex3f(x + step - 1.0, y, z1 - step + 1.0);
glVertex3f(x+step-1.0, y, z1);
glEnd();
}
}
}
Your code is broken in many ways:
Your myDisplay function uses whatever the current matrix mode is to set the view matrix on.
Initially, you leave the matrix mode as GL_PROJECTION in myInit()
These two together mean that for the first frame, you just use identity as MODELVIEW matrix, and just overwrite the projection matrix twice. After a resize, the frame ais drawn again, and your code does waht you probably intented to do.
However, there is more:
You do not have any resize handler, so your viewport will not change when you resize the window.
You are setting an ortho matrix initailly for the projection, although you are not planning to use it at all.
and, the most import point:
All of your code depends on deprecated functionality which is not even available in modern OpenGL at all. You should really not use this in 2016, but learn modern OpenGL instead (with "modern" meaning "only a decade old" here).

how to get coordinate of image using mouse click when resize window?

I am making Game in openGl which contain start menu screen.Problem is when i click start game button (which is a texture image) game start successfully but when i resize window and then click start game button the texture image coordinates change and game did not start how i prevent to change image coordinates after resize window.
Here is My Mouse funtion
void mouseClick (int button, int state, int x, int y)
{
if (!menu)
{
xMin = 300, xMax = 400, yMin = 350, yMax = 400;
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
y = height - y;
if (x >= xMin && x <= xMax && y >= yMin && y <= yMax)
{
printf_s("starting 2d Game ");
}
}
}
}
Here is my texture Image code
glBegin(GL_QUAD_STRIP);
glTexCoord2f(0, 0);
glVertex2f(300, 350);
glTexCoord2f(1, 0);
glVertex2f(400, 350);
glTexCoord2f(0, 1);
glVertex2f(300, 400);
glTexCoord2f(1, 1);
glVertex2f(400,400);
glEnd();
glFlush();
Here is my Matrix Projection Code:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D(0.0, 640.0, 0.0, 440);//dino window
gluOrtho2D(0.0, 640.0, 0.0, 440);//dino window
//gluOrtho2D(1.0, 1.0, 1.0,1.0);//house window
//gluOrtho2D(1.0, 1.0, 1.0, 1.0);//bird window
glViewport(0, 0, width, height);
//glViewport(220, 100, 100, 200);
Actually, it is the mouse click coordinates that are changing. You will have to convert your mouse click position (x,y) from viewport coordinates to your drawing coordinates (i.e. Ortho2D coordinates).
Assuming that width and height variables are provided by the system whenever the window is resized, try the following:
x = x/width * 640;
y = (height -y) / height * 440;
And then test for the button bounds.
The correct way to implement this is to use selection buffer.
With this, you know which polygon is being clicked. This is not affected by the size of the window.

Qt - update PaintGL

I have done some research trough the different threads about PaintGL on SO, but I couldn't manage to find what I need to make it work / understand completely.
I am trying to draw a cube. EVerytime my pushbutton gets clicked the cube should rotate. (only when it gets clicked, no timers etc)
To determine the angle I use the variable xAngle. I am able to modify it trough the function setAngleCube(), but my cube doesn't seem to move. (I see the var 'i' being changed every time.)
why? How could I make it work?
To me it looks like the cube just keeps its value from the constructor and doesn't modiy them any more.
solution:
add this line in the slot of the pushbutton
ui->widget->setCubeAngle(ui->widget->getCubeAngle()+5);
credit to #Alexander Chernin
my code:
MyGLWidget::MyGLWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
xRot = 0;
yRot = 0;
zRot = 0;
xAngle=15;
qDebug("constructor\n") ;
}
void MyGLWidget::setCubeAngle(int angle)
{
xAngle = angle;
qDebug("angle set\n");
}
int MyGLWidget::getCubeAngle()
{
return xAngle;
}
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
Glptr = new MyGLWidget();
}
void Widget::on_pushButton_clicked()
{
static int i;
i+=5;
Glptr->setCubeAngle(i);
update();
qDebug()<<i<<endl;
ui->widget->setCubeAngle(ui->widget->getCubeAngle()+5); //SOLUTION
}
void MyGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
qDebug("painting cube\n");
qDebug()<
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(getCubeAngle(),0.0,1.0,0.0); //rotate 30 degress around y-axis
glRotatef(5.0,1,0.0,0.0); //rotate 15 degress around x-axis
glBegin(GL_QUADS);
//back
glColor3f(1,0,0);
glVertex3f(-0.5, 0.2,-0.5 );
glVertex3f(-0.7, 0.2,-0.5);
glVertex3f(-0.7, -0.2,-0.5 );
glVertex3f(-0.5, -0.2,-0.5);
//some similar code to draw other sides of the cube
}
try to call updateGL() in MyGLWidget::setCubeAngle:
void MyGLWidget::setCubeAngle(int angle)
{
xAngle = angle;
qDebug("angle set\n");
updateGL();
}

arcball controls with Qt, and OpenGL

I am trying to implement an arcball/trackball controller using Open GL and Qt. However, I am quite new to OpenGL. I am having a terrible, terrible, terrible time getting things to work.
I started by following this video: https://www.youtube.com/watch?v=3IQV65ApWGs
I am using Qt for my window, using their QtWidget class.
Basically, I have a cube around the origin. I want to orbit the camera around the cube with the mouse. Right now, when I drag the camera seems to stay put while the cube orbits around the sphere. Kind of the opposite of what I need.
I hope you guys can help. I feel like I've tried nearly everything here.
First my mouse handling:
void GLWidget::wheelEvent(QWheelEvent *e){
scrollDelta += e->delta() / 120;
}
void GLWidget::mousePressEvent(QMouseEvent *e){
rotate=false;
if(e->button() == Qt::LeftButton){
oldX = e->x(); // Set this to the mouse position
oldY = e->y(); // Set this to the mouse position
newX = e->x();
newY = e->y();
qDebug() << oldX << oldY << newX << newY;
rotate = true;
useArcBall = true;
}
}
void GLWidget::mouseMoveEvent(QMouseEvent *e){
if(e->buttons() & Qt::LeftButton){
//qDebug() << QString::number(e->x());
if(rotate){
newX = e->x();
newY = e->y();
updateMouse();
}
oldX = e->x();
oldY = e->y();
}
}
void GLWidget::mouseReleaseEvent(QMouseEvent *e){
if(e->button() == Qt::LeftButton)
useArcBall = false;
}
void GLWidget::updateMouse()
{
QVector3D v = getArcBallVector(oldX,oldY); // from the mouse
QVector3D u = getArcBallVector(newX, newY);
float angle = std::acos(std::min(1.0f, QVector3D::dotProduct(u,v)));
QVector3D rotAxis = QVector3D::crossProduct(v,u);
QMatrix4x4 eye2ObjSpaceMat = rotationMat.inverted();
QVector3D objSpaceRotAxis = eye2ObjSpaceMat * rotAxis;
qDebug() << 4 * qRadiansToDegrees(angle);
//modelview.rotate(4 * qRadiansToDegrees(angle), rotAxis);
//oldRot = newRot;
//oldX = newX;
//oldY = newY;
//qDebug() << objSpaceRotAxis.normalized();
if(true){
rotationMat.rotate(4 * qRadiansToDegrees(angle), objSpaceRotAxis);
}
}
Now the arcball related math:
QVector3D GLWidget::getArcBallVector(int x, int y)
{
QVector3D pt = QVector3D(2.0 * x / GLWidget::width() - 1.0, 2.0 * y / GLWidget::height() - 1.0 , 0);
pt.setY(pt.y() * -1);
// compute z-coordinates
float xySquared = pt.x() * pt.x() + pt.y() * pt.y();
if(xySquared <= 1.0)
pt.setZ(std::sqrt(1.0 - xySquared));
else
pt.normalize();
return pt;
}
And this is the part where I render everything:
void GLWidget::paintGL()
{
QMatrix4x4 modelview;
QPainter painter;
painter.begin(this);
painter.beginNativePainting();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glFrontFace(GL_CW);
glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
modelview.perspective(90.0f, 4.0f / 3.0f, 0.1f, 3000.0f);
modelview.lookAt(QVector3D(eyeX,eyeY,eyeZ), QVector3D(0,0,0), QVector3D(0,1,0));
// New Trackball code
modelview = rotationMat * modelview;
modelview.scale(1 - scrollDelta / 10);
What am I doing wrong?
Is my approach unsound?
update So I fixed some of the mouse handling. Now my issue is that cube is rotating around the surface of sphere, rather than the camera. Is this because I am using the lookat command?
Also, the cube is being occluded by background color as I turn it. Is this a projection problem?
looks like your matrices are applied in the wrong order
in paintGL you should do:
modelview *= rotationMat;
and in updateMouse you should do
QMatrix4x4 tmp;
tmp.rotate(4 * qRadiansToDegrees(angle), objSpaceRotAxis);
rotationMat = tmp * rotationMat;

Draw a rectangle with mouse in GLUT

I am fairly new to using GLUT, and I have been attempting to compile a program (which I found here, first response) that uses the mouse to draw a rectangle by recording the starting and ending points of a click-and-drag.
As a clean copy/paste, it will compile but not draw anything. It just displays a white screen, even after changing the background color to black (in the setup() function). I've read several sources to verify that this program doesn't miss anything in its draw and reshape functions, and it's all there.
I create a window, set the viewport to the window dimensions, and then use the gluOrtho2D function to set the mapping (since the window and viewport are the same dimensions, I set the mapping to the window dimensions). The mouse callback records where I left-click, and where I release left-click, then calls the glutPostRedisplay() function to redraw the window with the new coordinates. After a bit of debugging, I discovered the coordinates are recorded and saved appropriately, and are measured in pixels (x and y are integers between 0 and window dimension), so I should be able to draw a rectangle from one vertex to the other vertex using the coordinates. But, like I said, it only displays a white screen.
So, is there something wrong with the way I am drawing the rectangle? Am I mapping the window incorrectly? I am seriously lost, and any feedback would be greatly appreciated.
EDIT2: I changed the glutInitDisplayMode from GLUT_SINGLE to GLUT_DOUBLE, and that fixed the whole non-interactive white screen thing. Now it will draw a rectangle with the mouse with a flipped y-coordinate (which I fixed), and it works great now. Thank you very much for the suggestion.
Here is my program (EDIT1: added comments):
#include <cstdlib>
#include <GL/glut.h>
using namespace std;
GLsizei width, height;
struct Position
{
Position() : x(0), y(0) {}
float x;
float y;
};
Position start; // Records left-click location
Position finish; // Records left-click release location
void display()
{
glClear(GL_COLOR_BUFFER_BIT); // clear window
glColor3ub(rand()%256, rand()%256, rand()%256); // generates random color
glBegin(GL_QUADS);
glVertex2f(start.x,start.y);
glVertex2f(finish.x,start.y);
glVertex2f(finish.x,finish.y);
glVertex2f(start.x,finish.y);
glEnd();
glutSwapBuffers(); // display newly drawn image in window
}
void reshape( int w, int h )
{
glViewport( 0, 0, (GLsizei)w, (GLsizei)h ); // set to size of window
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0.0, (float)w, 0.0, (float)h );
width = w; // records width globally
height = h; // records height globally
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button, int state, int x, int y)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
if(state==GLUT_DOWN)
{
start.x = x; //x1
start.y = y; //y1
}
if(state==GLUT_UP)
{
finish.x = x; //x2
finish.y = y; //y2
}
break;
glutPostRedisplay();
}
}
void motion( int x, int y )
{
finish.x = x;
finish.y = y;
glutPostRedisplay();
}
void setup()
{
glClearColor(0.0, 0.0, 0.0, 1.0); // *should* display black background
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("");
setup();
// initializing callbacks
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
return 0;
}
As my comment suggested:
change:
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
to:
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);