PDA

View Full Version : Multiple Key Input BUG



Marc Colomé
03-23-2014, 10:49 AM
Hi at all, i am testing my "simulator" key system by rotating the car (later when this would be working i will use the others keys)

There's a bug in it, because it should only be rotating the car when I press 'a' and it is rotating the car every update.

I'm updating the function keyoperations() because it should be updating the scene with the changes made by TRUE key board input reasults.

//---------------------------------

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <glut.h>
#define RADIANS(a) (a * 3.14f / 180.0f)

using namespace std;

float steer_angle = 0.0f;
float posicio_x = 0.0f;
float posicio_y = 0.0f;
float camera_height = -40.0f;

bool* keyStates = new bool[256];

void keyOperations (void) {
if (keyStates['a']) { // If the 'a' key has been pressed
steer_angle += 20.0f;
}
}


void initRendering() {
keyOperations();
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0); //Enable light #0
glEnable(GL_LIGHT1); //Enable light #1
glEnable(GL_COLOR_MATERIAL);
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
}


void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 0.01, 1000.0);
}


void drawScene() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, camera_height);
glRotatef(-70.0f, 1.0f, 0.0f, 0.0f);
glRotatef(90.0f, 0.0f, 0.0f, 1.0f);

///////////////////////////////////
//LIGHTING
//SUN
GLfloat ambientColor[] = {0.75f, 0.75f, 0.75f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

//POSITIONED
GLfloat lightColor0[] = {0.75f, 0.75f, 0.75f, 1.0f};
GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

//DIRIGITED
GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f};
GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
///////////////////////////////////////////////

///////////////////////////////////////////////
//ACTION
glPushMatrix();
glTranslatef(-12.0f,0.0f , 0.0f);
glTranslatef(posicio_x, posicio_y, -0.0f);
glRotatef(steer_angle, 0.0f, 0.0f, 1.0f);

//CAR
//Floor
glBegin(GL_QUADS);
glColor3f(0.25f, 0.25f, 0.25f);
glNormal3f(0.0f, 0.0f, -1.0f);
glVertex3f(-2.25f, 1.0f, 0.0f);
glVertex3f(2.25f, 1.0f, 0.0f);
glVertex3f(2.25f, -1.0f, 0.0f);
glVertex3f(-2.25f, -1.0f, 0.0f);
glEnd();
//Roof
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glVertex3f(-2.25f, 1.0f, 1.0f);
glVertex3f(2.25f, 1.0f, 1.0f);
glVertex3f(2.25f, -1.0f, 1.0f);
glVertex3f(-2.25f, -1.0f, 1.0f);
glEnd();
//Right
glBegin(GL_QUADS);
glNormal3f(0.0f, 1.0f, 0.0f);
glVertex3f(-2.25f, 1.0f, 1.0f);
glVertex3f(2.25f, 1.0f, 1.0f);
glVertex3f(2.25f, 1.0f, 0.0f);
glVertex3f(-2.25f, 1.0f, 0.0f);
glEnd();
//Left panel
glBegin(GL_QUADS);
glNormal3f(0.0f, -1.0f, 0.0f);
glVertex3f(-2.25f, -1.0f, 1.0f);
glVertex3f(2.25f, -1.0f, 1.0f);
glVertex3f(2.25f, -1.0f, 0.0f);
glVertex3f(-2.25f, -1.0f, 0.0f);
glEnd();
glPopMatrix();
////////////////////////////////////////

////////////////////////////////////////
//STATIC
//TERRAIN
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(1.0f, 0.5f, 0.0f);
glVertex3f(-205.0f, 100.0f, -0.1f);
glColor3f(0.5f, 1.0f, 0.5f);
glVertex3f(205.0f, 100.0f, -0.1f);
glColor3f(0.0f, 0.5f, 1.0f);
glVertex3f(205.0f, -100.0f, -0.1f);
glColor3f(0.5f, 0.0f, 0.5f);
glVertex3f(-205.0f, -100.0f, -0.1f);
glEnd();
glPopMatrix();

glutSwapBuffers();
}

void update(int value) {
if (steer_angle > 360) {
steer_angle -= 360;
}

keyOperations();
glutPostRedisplay();
glutTimerFunc(16, update, 0);
}

void keyPressed (unsigned char key, int x, int y) {
keyStates['a'] = true; // Set the state of the current key to pressed
}

void keyUp (unsigned char key, int x, int y) {
keyStates['a'] = false; // Set the state of the current key to not pressed
}

int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(320, 240);
glutCreateWindow("3D Car Simulator");
initRendering();
glutDisplayFunc(drawScene);

glutKeyboardUpFunc(keyUp);
glutReshapeFunc(handleResize);
glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses


glutTimerFunc(16, update, 0);
glutMainLoop();
return 0;
}

//-------------------------

Anyone has any advice on making it work?

Thank you,


Marc

Marc Colomé
03-23-2014, 11:02 AM
Just tried it with codeblocks (mingw) and it worked with some changes:

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <GL\gl.h>
#include <GL\glu.h>
//#include <gl\glaux.h>
#include <GL/glut.h>
#define RADIANS(a) (a * 3.14f / 180.0f)

using namespace std;

float steer_angle = 0.0f;
float posicio_x = 0.0f;
float posicio_y = 0.0f;
float camera_height = -40.0f;

bool* keyStates = new bool[256];

void keyOperations (void) {
if (keyStates['a']) { // If the 'a' key has been pressed
steer_angle += 10.0f;
}
if (keyStates['d']) { // If the 'a' key has been pressed
steer_angle -= 10.0f;
}
if (keyStates['w']) { // If the 'a' key has been pressed
posicio_x += cos(RADIANS(steer_angle)) * 1.0f;
posicio_y += sin(RADIANS(steer_angle)) * 1.0f;
}
if (keyStates['s']) { // If the 'a' key has been pressed
posicio_x -= cos(RADIANS(steer_angle)) * 1.0f;
posicio_y -= sin(RADIANS(steer_angle)) * 1.0f;
}
}


void initRendering() {
keyOperations();
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0); //Enable light #0
glEnable(GL_LIGHT1); //Enable light #1
glEnable(GL_COLOR_MATERIAL);
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
}


void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 0.01, 1000.0);
}


void drawScene() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, camera_height);
glRotatef(-70.0f, 1.0f, 0.0f, 0.0f);
glRotatef(90.0f, 0.0f, 0.0f, 1.0f);

///////////////////////////////////
//LIGHTING
//SUN
GLfloat ambientColor[] = {0.75f, 0.75f, 0.75f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

//POSITIONED
GLfloat lightColor0[] = {0.75f, 0.75f, 0.75f, 1.0f};
GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

//DIRIGITED
GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f};
GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
///////////////////////////////////////////////

///////////////////////////////////////////////
//ACTION
glPushMatrix();
glTranslatef(-12.0f,0.0f , 0.0f);
glTranslatef(posicio_x, posicio_y, -0.0f);
glRotatef(steer_angle, 0.0f, 0.0f, 1.0f);

//CAR
//Floor
glBegin(GL_QUADS);
glColor3f(0.25f, 0.25f, 0.25f);
glNormal3f(0.0f, 0.0f, -1.0f);
glVertex3f(-2.25f, 1.0f, 0.0f);
glVertex3f(2.25f, 1.0f, 0.0f);
glVertex3f(2.25f, -1.0f, 0.0f);
glVertex3f(-2.25f, -1.0f, 0.0f);
glEnd();
//Roof
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glVertex3f(-2.25f, 1.0f, 1.0f);
glVertex3f(2.25f, 1.0f, 1.0f);
glVertex3f(2.25f, -1.0f, 1.0f);
glVertex3f(-2.25f, -1.0f, 1.0f);
glEnd();
//Right
glBegin(GL_QUADS);
glNormal3f(0.0f, 1.0f, 0.0f);
glVertex3f(-2.25f, 1.0f, 1.0f);
glVertex3f(2.25f, 1.0f, 1.0f);
glVertex3f(2.25f, 1.0f, 0.0f);
glVertex3f(-2.25f, 1.0f, 0.0f);
glEnd();
//Left panel
glBegin(GL_QUADS);
glNormal3f(0.0f, -1.0f, 0.0f);
glVertex3f(-2.25f, -1.0f, 1.0f);
glVertex3f(2.25f, -1.0f, 1.0f);
glVertex3f(2.25f, -1.0f, 0.0f);
glVertex3f(-2.25f, -1.0f, 0.0f);
glEnd();
glPopMatrix();
////////////////////////////////////////

////////////////////////////////////////
//STATIC
//TERRAIN
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(1.0f, 0.5f, 0.0f);
glVertex3f(-205.0f, 100.0f, -0.1f);
glColor3f(0.5f, 1.0f, 0.5f);
glVertex3f(205.0f, 100.0f, -0.1f);
glColor3f(0.0f, 0.5f, 1.0f);
glVertex3f(205.0f, -100.0f, -0.1f);
glColor3f(0.5f, 0.0f, 0.5f);
glVertex3f(-205.0f, -100.0f, -0.1f);
glEnd();
glPopMatrix();

glutSwapBuffers();
}

void update(int value) {
if (steer_angle > 360) {
steer_angle -= 360;
}

keyOperations();
glutPostRedisplay();
glutTimerFunc(16, update, 0);
}

void keyPressed (unsigned char key, int x, int y) {
keyStates[key] = true; // Set the state of the current key to pressed
}

void keyUp (unsigned char key, int x, int y) {
keyStates[key] = false; // Set the state of the current key to not pressed
}

int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(320, 240);
glutCreateWindow("3D Car Simulator");
initRendering();
glutDisplayFunc(drawScene);

glutKeyboardUpFunc(keyUp);
glutReshapeFunc(handleResize);
glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses


glutTimerFunc(16, update, 0);
glutMainLoop();
return 0;
}

But in visual studio it doesn't work.