Basically I would like to move in the direction pointed by the cursor, but this doesn’t happen and instead the “forward” direction remains the same it was at the beginning.
For example if I turn 180° and press “w” to walk forward, I walk backwards instead of going where I’m looking at. I’m talking about walking because I’m trying to replicate a first-person camera.
Down here you can see the code I’ve written so far:
// angle of rotation for the camera direction
float angle = 0.0f;
void mouseMove(int x, int y) {
// this will only be true when the left button is down
if (xOrigin >= 0) {
// update deltaAngle
deltaAngle = (x - xOrigin) * 0.001f;
// update camera's direction
lx = sin(angle + deltaAngle)*2;
lz = -cos(angle + deltaAngle)*2;
}
}
void mouseButton(int button, int state, int x, int y) {
// only start motion if the left button is pressed
if (button == GLUT_LEFT_BUTTON) {
// when the button is released
if (state == GLUT_UP) {
angle += deltaAngle;
xOrigin = -1;
}
else {// state = GLUT_DOWN
xOrigin = x;
}
}
}
void handleKeys(unsigned char key, int x, int y){
switch (key)
{
case 'w':
posZ+=4;
cout << "FORWARD
";
break;
case 's':
posZ-=4;
cout << "BACK
";
break;
case 'd':
posX-=4;
cout << "RIGHT
";
break;
case 'a':
posX+=4;
cout << "LEFT
";
break;
case 27:
exit(0);
break;
}
}
void computePos(float deltaMove) {
x += deltaMove * lx * 0.1f;
z += deltaMove * lz * 0.1f;
}
//draws the 3D sccene
void drawScene(){
cout << "ANGLE: "<<angle<<"
";
if (deltaMove){
computePos(deltaMove);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set the camera
gluLookAt(x, 1.0f, z,
x + lx, 1.0f, z + lz,
0.0f, 1.0f, 0.0f);
glTranslatef(posX, 0.0f, posZ);
glColor3f(1.0f, 1.0f, 0.0f); //we set the cube color
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBegin(GL_QUADS);
//Front
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-40.5f, -40.0f, 40.5f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(40.5f, -40.0f, 40.5f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(40.5f, 40.0f, 40.5f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-40.5f, 40.0f, 40.5f);
//Right
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(40.5f, -40.0f, -80.5f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(40.5f, 40.0f, -80.5f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(40.5f, 40.0f, 40.5f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(40.5f, -40.0f, 40.5f);
//Back
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-40.5f, -40.0f, -80.5f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-40.5f, 40.0f, -80.5f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(40.5f, 40.0f, -80.5f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(40.5f, -40.0f, -80.5f);
//Left
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-40.5f, -40.0f, -80.5f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-40.5f, -40.0f, 40.5f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-40.5f, 40.0f, 40.5f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-40.5f, 40.0f, -80.5f);
glEnd();
glutSwapBuffers();
}