PDA

View Full Version : Qt and OpenGL: gluPerspective



giorgik
23rd March 2013, 21:05
Hello to all. I created this simple program with Qt 4.8.2 and OpenGL. It comes to drawing a 3D grid and display it in perspective using gluPerspective. In addition, I use the mouse to move around the scene. The problem I have is to better manage the movement of the mouse. I put my project in attachment. Can you help me make better use of the mouse? The movement of the grid is chaotic, and I wanted with the right mouse button the zoom or removal and the left mouse button the grid rotation axis x and y.8835

I'm going crazy to solve:crying:

giorgik
24th March 2013, 20:41
The code with glOrtho():


#include "glwidget.h"
#include <GL/gl.h>
#include "GL/glut.h"
#include "GL/freeglut.h"
#include "GL/freeglut_ext.h"

GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
//
}

void GLWidget::resizeGL(int w, int h)
{
int side = qMin(w, h);
glViewport((w - side) / 2, (h - side) / 2, side, side);

// setta la matrice corrente a quella della proiezione e cancellala
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// definisci il volume di clipping (frustum) del mondo (left, right, bottom, top, near, far)
// in base alla massima ampiezza
glOrtho(-12.0f, 12.0f, -12.0f, 12.0f, 0.0f, 10.0f);
//gluPerspective(45.0f, (GLfloat)w/(GLfloat)h, -10.0f, 10.0f);

// setta la matrice corrente a quella di modello-vista e cancellala
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void GLWidget::initializeGL()
{
// specifica il colore lilla come colore di background
// usato da glClear()
glClearColor(0.7, 0.7, 0.8, 1);
}

void GLWidget::paintGL()
{
// cancella lo sfondo con il colore scelto da glClearColor
// clear buffer
glClear(GL_COLOR_BUFFER_BIT);

//----------- 20x20 grid: draw XY-grid with default size -----------
glBegin(GL_LINES);

glColor3f(0.5f, 0.5f, 0.5f);
for(float i = 1.0f; i <= 10.0f; i+= 1.0f)
{
glVertex3f(-10.0f, i, 0); // lines parallel to X-axis
glVertex3f( 10.0f, i, 0);
glVertex3f(-10.0f, -i, 0); // lines parallel to X-axis
glVertex3f( 10.0f, -i, 0);

glVertex3f( i, -10.0f, 0); // lines parallel to Y-axis
glVertex3f( i, 10.0f, 0);
glVertex3f(-i, -10.0f, 0); // lines parallel to Y-axis
glVertex3f(-i, 10.0f, 0);
}

// x-axis
glColor3f(1, 0, 0);
glVertex3f(-10.0f, 0, 0);
glVertex3f( 10.0f, 0, 0);

// y-axis
glColor3f(0, 0, 1);
glVertex3f(0, -10.0f, 0);
glVertex3f(0, 10.0f, 0);

glEnd();
//-----------------------------------------------------------------
}

how to get the perspective gluPerspective () so you can see my perspective grid ?
I would use GLWidget::mouseMoveEvent (QMouseEvent * event) to rotate the grid and for zooming and moving away. How should I write the code ?