Hello, I am very new to opengl.
I succeed to draw something, But now, I need to add some movement. Let me try to explain. I have a code which draws a square on the screen. What I am trying to do is, user will be able to move this square in this gl window by draggin it with mouse left button.
But It does not do exactly right thing. It moves upward when I pull down an moves downward when I try to move it up.
And also it always blinks.
Here is my code:
Draw square func:
void GLWidget::drawQuad(GLfloat xx, GLfloat yy, GLfloat side){
glTranslatef(xx,yy,0.0f); // Move Right 3 Units
SqPoint.setX(200 + 96 * xx);
SqPoint.setY(200 + 96 * yy);
glBegin(GL_QUADS);
glVertex3f(-(side/2), (side/2), 0.0f); // Top Left
glVertex3f( (side/2), (side/2), 0.0f); // Top Right
glVertex3f( (side/2),-(side/2), 0.0f); // Bottom Right
glVertex3f(-(side/2),-(side/2), 0.0f); // Bottom Left
glEnd();
glTranslatef(-xx,-yy,0.0f);
}
My initialize code:
void GLWidget::initializeGL()
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
xCord =0;
yCord = 0;
}
My paintgl code:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-10.0f);
qglColor(kareRenk);
drawQuad(xCord,yCord,1);
Mouse event:
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
GLfloat iy;
GLfloat ix = (event->x() - SqPoint.x())/96;
// if ( event->y()> 200)
iy = ((event->y() - SqPoint.y() )/96);
// else
// iy = (SqPoint.y() - event->y() )/96;
if (event->buttons() & Qt::LeftButton) {
SqPoint.setX (event->x());
SqPoint.setY (event->y());
xCord = ix;
yCord = iy;
updateGL();
} else if (event->buttons() & Qt::RightButton) {
}
}
How can I make this run correct and without blinking??