Camera doesnt move on opengl qt
Posted
by
hugo
on Game Development
See other posts from Game Development
or by hugo
Published on 2012-12-19T20:57:00Z
Indexed on
2012/12/19
23:15 UTC
Read the original article
Hit count: 350
Here is my code, as my subject indicates i have implemented a camera but i couldnt make it move,Thanks in advance.
#define PI_OVER_180 0.0174532925f
define GL_CLAMP_TO_EDGE 0x812F
include "metinalifeyyaz.h"
include
include
include
include
include
include
include
metinalifeyyaz::metinalifeyyaz(QWidget *parent) : QGLWidget(parent) {
this->setFocusPolicy(Qt:: StrongFocus);
time = QTime::currentTime();
timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, SIGNAL(timeout()), this, SLOT(updateGL()));
xpos = yrot = zpos = 0;
walkbias = walkbiasangle = lookupdown = 0.0f;
keyUp = keyDown = keyLeft = keyRight = keyPageUp = keyPageDown = false;
}
void metinalifeyyaz::drawBall() {
//glTranslatef(6,0,4);
glutSolidSphere(0.10005,300,30);
} metinalifeyyaz:: ~metinalifeyyaz(){
glDeleteTextures(1,texture);
}
void metinalifeyyaz::initializeGL(){
glShadeModel(GL_SMOOTH);
glClearColor(1.0,1.0,1.0,0.5);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glDepthFunc(GL_LEQUAL);
glClearColor(1.0,1.0,1.0,1.0);
glShadeModel(GL_SMOOTH);
GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess []={30.0};
GLfloat light_position[]={1.0,1.0,1.0};
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
QImage img1 = convertToGLFormat(QImage(":/new/prefix1/halisaha2.bmp"));
QImage img2 = convertToGLFormat(QImage(":/new/prefix1/white.bmp"));
glGenTextures(2,texture);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img1.width(), img1.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img1.bits());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img2.width(), img2.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img2.bits());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really nice perspective calculations
}
void metinalifeyyaz::resizeGL(int w, int h){
if(h==0)
h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, static_cast<GLfloat>(w)/h,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void metinalifeyyaz::paintGL(){
movePlayer();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
GLfloat xtrans = -xpos;
GLfloat ytrans = -walkbias - 0.50f;
GLfloat ztrans = -zpos;
GLfloat sceneroty = 360.0f - yrot;
glLoadIdentity();
glRotatef(lookupdown, 1.0f, 0.0f, 0.0f);
glRotatef(sceneroty, 0.0f, 1.0f, 0.0f);
glTranslatef(xtrans, ytrans+50, ztrans-130);
glLoadIdentity();
glTranslatef(1.0f,0.0f,-18.0f);
glRotatef(45,1,0,0);
drawScene();
int delay = time.msecsTo(QTime::currentTime());
if (delay == 0)
delay = 1;
time = QTime::currentTime();
timer->start(qMax(0,10 - delay));
}
void metinalifeyyaz::movePlayer() {
if (keyUp) {
xpos -= sin(yrot * PI_OVER_180) * 0.5f;
zpos -= cos(yrot * PI_OVER_180) * 0.5f;
if (walkbiasangle >= 360.0f)
walkbiasangle = 0.0f;
else
walkbiasangle += 7.0f;
walkbias = sin(walkbiasangle * PI_OVER_180) / 10.0f;
} else if (keyDown) {
xpos += sin(yrot * PI_OVER_180)*0.5f;
zpos += cos(yrot * PI_OVER_180)*0.5f ;
if (walkbiasangle <= 7.0f)
walkbiasangle = 360.0f;
else
walkbiasangle -= 7.0f;
walkbias = sin(walkbiasangle * PI_OVER_180) / 10.0f;
}
if (keyLeft)
yrot += 0.5f;
else if (keyRight)
yrot -= 0.5f;
if (keyPageUp)
lookupdown -= 0.5;
else if (keyPageDown)
lookupdown += 0.5;
}
void metinalifeyyaz::keyPressEvent(QKeyEvent *event) {
switch (event->key()) {
case Qt::Key_Escape:
close();
break;
case Qt::Key_F1:
setWindowState(windowState() ^ Qt::WindowFullScreen);
break;
default:
QGLWidget::keyPressEvent(event);
case Qt::Key_PageUp:
keyPageUp = true;
break;
case Qt::Key_PageDown:
keyPageDown = true;
break;
case Qt::Key_Left:
keyLeft = true;
break;
case Qt::Key_Right:
keyRight = true;
break;
case Qt::Key_Up:
keyUp = true;
break;
case Qt::Key_Down:
keyDown = true;
break;
}
}
void metinalifeyyaz::changeEvent(QEvent *event) {
switch (event->type()) {
case QEvent::WindowStateChange:
if (windowState() == Qt::WindowFullScreen)
setCursor(Qt::BlankCursor);
else
unsetCursor();
break;
default:
break;
}
} void metinalifeyyaz::keyReleaseEvent(QKeyEvent *event) {
switch (event->key()) {
case Qt::Key_PageUp:
keyPageUp = false;
break;
case Qt::Key_PageDown:
keyPageDown = false;
break;
case Qt::Key_Left:
keyLeft = false;
break;
case Qt::Key_Right:
keyRight = false;
break;
case Qt::Key_Up:
keyUp = false;
break;
case Qt::Key_Down:
keyDown = false;
break;
default:
QGLWidget::keyReleaseEvent(event);
}
} void metinalifeyyaz::drawScene(){
glBegin(GL_QUADS);
glNormal3f(0.0f,0.0f,1.0f);
// glColor3f(0,0,1); //back
glVertex3f(-6,0,-4);
glVertex3f(-6,-0.5,-4);
glVertex3f(6,-0.5,-4);
glVertex3f(6,0,-4);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0.0f,0.0f,-1.0f); //front
glVertex3f(6,0,4);
glVertex3f(6,-0.5,4);
glVertex3f(-6,-0.5,4);
glVertex3f(-6,0,4);
glEnd();
glBegin(GL_QUADS);
glNormal3f(-1.0f,0.0f,0.0f);
// glColor3f(0,0,1); //left
glVertex3f(-6,0,4);
glVertex3f(-6,-0.5,4);
glVertex3f(-6,-0.5,-4);
glVertex3f(-6,0,-4);
glEnd();
glBegin(GL_QUADS);
glNormal3f(1.0f,0.0f,0.0f);
// glColor3f(0,0,1); //right
glVertex3f(6,0,-4);
glVertex3f(6,-0.5,-4);
glVertex3f(6,-0.5,4);
glVertex3f(6,0,4);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glNormal3f(0.0f,1.0f,0.0f);//top
glTexCoord2f(1.0f,0.0f);
glVertex3f(6,0,-4);
glTexCoord2f(1.0f,1.0f);
glVertex3f(6,0,4);
glTexCoord2f(0.0f,1.0f);
glVertex3f(-6,0,4);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-6,0,-4);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0.0f,-1.0f,0.0f);
//glColor3f(0,0,1); //bottom
glVertex3f(6,-0.5,-4);
glVertex3f(6,-0.5,4);
glVertex3f(-6,-0.5,4);
glVertex3f(-6,-0.5,-4);
glEnd();
// glPushMatrix();
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
glNormal3f(1.0f,0.0f,0.0f);
glTexCoord2f(1.0f,0.0f); //right far goal post front face
glVertex3f(5,0.5,-0.95);
glTexCoord2f(1.0f,1.0f);
glVertex3f(5,0,-0.95);
glTexCoord2f(0.0f,1.0f);
glVertex3f(5,0,-1);
glTexCoord2f(0.0f,0.0f);
glVertex3f(5, 0.5, -1);
glColor3f(1,1,1); //right far goal post back face
glVertex3f(5.05,0.5,-0.95);
glVertex3f(5.05,0,-0.95);
glVertex3f(5.05,0,-1);
glVertex3f(5.05, 0.5, -1);
glColor3f(1,1,1); //right far goal post left face
glVertex3f(5,0.5,-1);
glVertex3f(5,0,-1);
glVertex3f(5.05,0,-1);
glVertex3f(5.05, 0.5, -1);
glColor3f(1,1,1); //right far goal post right face
glVertex3f(5.05,0.5,-0.95);
glVertex3f(5.05,0,-0.95);
glVertex3f(5,0,-0.95);
glVertex3f(5, 0.5, -0.95);
glColor3f(1,1,1); //right near goal post front face
glVertex3f(5,0.5,0.95);
glVertex3f(5,0,0.95);
glVertex3f(5,0,1);
glVertex3f(5,0.5, 1);
glColor3f(1,1,1); //right near goal post back face
glVertex3f(5.05,0.5,0.95);
glVertex3f(5.05,0,0.95);
glVertex3f(5.05,0,1);
glVertex3f(5.05,0.5, 1);
glColor3f(1,1,1); //right near goal post left face
glVertex3f(5,0.5,1);
glVertex3f(5,0,1);
glVertex3f(5.05,0,1);
glVertex3f(5.05,0.5, 1);
glColor3f(1,1,1); //right near goal post right face
glVertex3f(5.05,0.5,0.95);
glVertex3f(5.05,0,0.95);
glVertex3f(5,0,0.95);
glVertex3f(5,0.5, 0.95);
glColor3f(1,1,1); //right crossbar front face
glVertex3f(5,0.55,-1);
glVertex3f(5,0.55,1);
glVertex3f(5,0.5,1);
glVertex3f(5,0.5,-1);
glColor3f(1,1,1); //right crossbar back face
glVertex3f(5.05,0.55,-1);
glVertex3f(5.05,0.55,1);
glVertex3f(5.05,0.5,1);
glVertex3f(5.05,0.5,-1);
glColor3f(1,1,1); //right crossbar bottom face
glVertex3f(5.05,0.5,-1);
glVertex3f(5.05,0.5,1);
glVertex3f(5,0.5,1);
glVertex3f(5,0.5,-1);
glColor3f(1,1,1); //right crossbar top face
glVertex3f(5.05,0.55,-1);
glVertex3f(5.05,0.55,1);
glVertex3f(5,0.55,1);
glVertex3f(5,0.55,-1);
glColor3f(1,1,1); //left far goal post front face
glVertex3f(-5,0.5,-0.95);
glVertex3f(-5,0,-0.95);
glVertex3f(-5,0,-1);
glVertex3f(-5, 0.5, -1);
glColor3f(1,1,1); //right far goal post back face
glVertex3f(-5.05,0.5,-0.95);
glVertex3f(-5.05,0,-0.95);
glVertex3f(-5.05,0,-1);
glVertex3f(-5.05, 0.5, -1);
glColor3f(1,1,1); //right far goal post left face
glVertex3f(-5,0.5,-1);
glVertex3f(-5,0,-1);
glVertex3f(-5.05,0,-1);
glVertex3f(-5.05, 0.5, -1);
glColor3f(1,1,1); //right far goal post right face
glVertex3f(-5.05,0.5,-0.95);
glVertex3f(-5.05,0,-0.95);
glVertex3f(-5,0,-0.95);
glVertex3f(-5, 0.5, -0.95);
glColor3f(1,1,1); //left near goal post front face
glVertex3f(-5,0.5,0.95);
glVertex3f(-5,0,0.95);
glVertex3f(-5,0,1);
glVertex3f(-5,0.5, 1);
glColor3f(1,1,1); //right near goal post back face
glVertex3f(-5.05,0.5,0.95);
glVertex3f(-5.05,0,0.95);
glVertex3f(-5.05,0,1);
glVertex3f(-5.05,0.5, 1);
glColor3f(1,1,1); //right near goal post left face
glVertex3f(-5,0.5,1);
glVertex3f(-5,0,1);
glVertex3f(-5.05,0,1);
glVertex3f(-5.05,0.5, 1);
glColor3f(1,1,1); //right near goal post right face
glVertex3f(-5.05,0.5,0.95);
glVertex3f(-5.05,0,0.95);
glVertex3f(-5,0,0.95);
glVertex3f(-5,0.5, 0.95);
glColor3f(1,1,1); //left crossbar front face
glVertex3f(-5,0.55,-1);
glVertex3f(-5,0.55,1);
glVertex3f(-5,0.5,1);
glVertex3f(-5,0.5,-1);
glColor3f(1,1,1); //right crossbar back face
glVertex3f(-5.05,0.55,-1);
glVertex3f(-5.05,0.55,1);
glVertex3f(-5.05,0.5,1);
glVertex3f(-5.05,0.5,-1);
glColor3f(1,1,1); //right crossbar bottom face
glVertex3f(-5.05,0.5,-1);
glVertex3f(-5.05,0.5,1);
glVertex3f(-5,0.5,1);
glVertex3f(-5,0.5,-1);
glColor3f(1,1,1); //right crossbar top face
glVertex3f(-5.05,0.55,-1);
glVertex3f(-5.05,0.55,1);
glVertex3f(-5,0.55,1);
glVertex3f(-5,0.55,-1);
glEnd();
// glPopMatrix();
// glPushMatrix(); // glTranslatef(0,0,0);
// glutSolidSphere(0.10005,500,30); // glPopMatrix();
}
© Game Development or respective owner