Hi all I have a problem with my view.In my application I have written a mouse move events.And with mouse move events the rotation is done.When I mouse move the view with left and right sometimes I can’t see a full view.It gradually gets disappeares .Something is happening which I am not able to understand.And anybody give a clue for my problem.This is how it looks like now.
This is how my code looks like.
OpenGLView::OpenGLView(QWidget* parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
xRot = 0;
yRot = 0;
zRot = 0;
}
OpenGLView::~OpenGLView()
{
}
QSize OpenGLView::minimumSizeHint() const
{
return QSize(510, 50);
}
QSize OpenGLView::sizeHint() const
{
return QSize(1920, 1080);
}
static void qNormalizeAngle(int& angle)
{
while (angle < 0)
angle += 360 * 16;
while (angle > 360)
angle -= 360 * 16;
}
void OpenGLView::setXRotation(int angle)
{
qNormalizeAngle(angle);
if (angle != xRot) {
xRot = angle;
emit xRotationChanged(angle);
updateGL();
}
}
void OpenGLView::setYRotation(int angle)
{
qNormalizeAngle(angle);
if (angle != yRot) {
yRot = angle;
emit yRotationChanged(angle);
updateGL();
}
}
void OpenGLView::setZRotation(int angle)
{
qNormalizeAngle(angle);
if (angle != zRot) {
zRot = angle;
emit zRotationChanged(angle);
updateGL();
}
}
void OpenGLView::initializeGL()
{
qglClearColor(Qt::black);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
static GLfloat lightPosition[4] = { 0, 0, 500, 2.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
}
void OpenGLView::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(10.0, -60.0, -10.0);
glRotatef(xRot / 10.0, 1.0, 0.0, 0.0);
glRotatef(yRot / 10.0, 0.0, 1.0, 0.0);
glRotatef(zRot / 10.0, 0.0, 0.0, 1.0);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT_AND_BACK);
glDisable(GL_CULL_FACE);
//draw();
draw1();
}
void OpenGLView::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport(0.0f, 0.0f, width, height);
//glViewport((width - side) / 2, (height - side) / 2, side, side);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
#ifdef QT_OPENGL_ES_1
glOrthof(-200, 200, -200, 200, 1.0, 100000.0);
#else
glOrtho(-200, 200, -200, 200, 1.0, 100000.0);
gluLookAt(
2, 2, 2,
0, 3.8, 0,
0, 1, 0
/*0,0, 2,
0, 2.8, 0,
0, 1, 0*/
);
#endif
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void OpenGLView::mousePressEvent(QMouseEvent* event)
{
lastPos = event->pos();
}
void OpenGLView::mouseMoveEvent(QMouseEvent* event)
{
int dx = event->x() - lastPos.x();
int dy = event->y() - lastPos.y();
if (event->buttons() & Qt::LeftButton) {
setXRotation(xRot + 8 * dy);
setYRotation(yRot + 8 * dx);
}
else if (event->buttons() & Qt::RightButton) {
setXRotation(xRot + 8 * dy);
setZRotation(zRot + 8 * dx);
}
lastPos = event->pos();
event->accept();
}
void OpenGLView::wheelEvent(QWheelEvent* event)
{
if (event->delta() > 0) {
zoomIn();
}
else {
zoomOut();
}
}
void OpenGLView::zoomIn()
{
m_zoom *= 1.1;
updateProjection();
}
void OpenGLView::zoomOut()
{
m_zoom /= 1.1;
updateProjection();
}
void OpenGLView::updateProjection()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-200 / m_zoom, 200 / m_zoom,
-200 / m_zoom, 200 / m_zoom,
1.0, 100000.0);
glMatrixMode(GL_MODELVIEW);
updateGL();
}
can Anybody please help me with this issue.Thank you in advance.