PDA

View Full Version : Rotate opengl view with mouse position?



xleniz
19th January 2013, 13:21
Hi, I struggled a lot to load OBJ files, then I found GLM library and Im amazed how easy,
then I took my old QT OpenGL code,
and Ive started making a new FPS view game.
Problem is, I have no experience in how to rotate the view.

Here's how I rotate (xRot = 0, yRot = 0)


glRotatef(xRot+speedX, 0, 0.01f, 0);
glRotatef(yRot+speedY, 0.01f, 0, 0);


and here's how I make rotation with speedX and speedY:



void GLWidget::mouseMoveEvent(QMouseEvent *event) {
if(event->x() != this->width()/2 && event->y() != this->height()/2) {
speedX += (float)event->x()/500;
speedY += (float)event->y()/500;
}

//Mouse mouse to Center in Linux
dpy = XOpenDisplay(0);
root_window = XRootWindow(dpy, 0);
XSelectInput(dpy, root_window, KeyReleaseMask);
XWarpPointer(dpy, None, root_window, 0, 0, 0, 0, this->width()/2, this->height()/2);
XFlush(dpy); // Flushes the output buffer, therefore updates the cursor's position. Thanks to Achernar.
XCloseDisplay(dpy);
}


And here's full Code, if it interests anybody (its in one file):


#include <QtGui/QApplication>
#include <QtOpenGL/QGLWidget>
#include <QtGui/QMouseEvent>
#include <GL/glu.h>
#include <QTimer>
#include <QThread>
#include <QCursor>
#include <X11/Xlib.h>
#include <math.h>
#include <GL/glfw.h>
#include "glm.h"
#define screenWidth 800
#define screenHeight 600

#define PI 3.14159265358979
float cameraX, cameraY, cameraZ;
float xMove, yMove, zMove;
float angleX = 0;
float angleY = 0;
float angleZ = 0;
float xRot = 0;
float yRot = 0;
float zRot = 0;
float z = 1.0f;
float speedX = 0.0f;
float speedY = 0.0f;
GLMmodel *model = 0;
Display *dpy;
Window root_window;

class GLWidget : public QGLWidget{
public:
GLWidget();
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
private slots:
void updateGL();
private:
QTimer glUpdateTime;
};

GLWidget::GLWidget() {
connect(&glUpdateTime, SIGNAL(timeout()), this, SLOT(updateGL()));
glUpdateTime.start(1);
setMouseTracking(true);
}

void GLWidget::updateGL() {
this->update();
}

void GLWidget::initializeGL() {
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 0, 0, 0);
cameraX = 0;
cameraY = 0;
cameraZ = -10;
}

void GLWidget::resizeGL(int w, int h) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,(GLdouble)w/(GLdouble)h,0.5,20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, w, h);
}

void GLWidget::paintGL() {
if(!model) {
model = glmReadOBJ("/home/marcus/untitled/untitled.obj");
glmUnitize(model);
glmFacetNormals(model);
glmVertexNormals(model, 90.0);
}

glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -z);
glRotatef(xRot+speedX, 0, 0.01f, 0);
glRotatef(yRot+speedY, 0.01f, 0, 0);
glTranslatef(cameraX + xMove, cameraY + yMove, cameraZ + zMove);
glmDraw(model, GLM_SMOOTH | GLM_MATERIAL);
glTranslatef(-2.0f, 0.0f, 0.0f);
glmDraw(model, GLM_SMOOTH | GLM_MATERIAL);
glTranslatef(-2.0f, 0.0f, 0.0f);
glmDraw(model, GLM_SMOOTH | GLM_MATERIAL);
glTranslatef(-2.0f, 0.0f, 0.0f);
glmDraw(model, GLM_SMOOTH | GLM_MATERIAL);
glTranslatef(-2.0f, 0.0f, 0.0f);
glmDraw(model, GLM_SMOOTH | GLM_MATERIAL);
}
void GLWidget::mousePressEvent(QMouseEvent *event) {

}
void GLWidget::mouseMoveEvent(QMouseEvent *event) {
if(event->x() != this->width()/2 && event->y() != this->height()/2) {
speedX += (float)event->x()/500;
speedY += (float)event->y()/500;
}
dpy = XOpenDisplay(0);
root_window = XRootWindow(dpy, 0);
XSelectInput(dpy, root_window, KeyReleaseMask);
XWarpPointer(dpy, None, root_window, 0, 0, 0, 0, this->width()/2, this->height()/2);
XFlush(dpy); // Flushes the output buffer, therefore updates the cursor's position. Thanks to Achernar.
XCloseDisplay(dpy);
}
void GLWidget::keyPressEvent(QKeyEvent* event) {
switch(event->key()) {
case Qt::Key_Escape:
close();
break;
case Qt::Key_W:
zMove += 0.35f;
break;
case Qt::Key_S:
zMove -= 0.35f;
break;
case Qt::Key_A:
xMove += 0.35f;
break;
case Qt::Key_D:
xMove -= 0.35f;
break;
default:
event->ignore();
break;
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GLWidget window;
window.resizeGL(screenWidth, screenHeight);
window.showFullScreen();
a.exec();
}


Thanks in advance.