Results 1 to 2 of 2

Thread: Multiple Key Input BUG

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    28

    Exclamation Multiple Key Input BUG

    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

  2. #2
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    28
    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.

Similar Threads

  1. Replies: 1
    Last Post: 11-21-2015, 07:01 AM
  2. Replies: 5
    Last Post: 04-28-2015, 01:23 AM
  3. Color Input For Multiple Attached Fragment Shaders - OpenGL 1.2
    By Rennie Johnson in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 10-18-2012, 07:46 PM
  4. fragment shader multiple texcoords input
    By Karel in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 04-14-2004, 04:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean