Keyboard Input - C++ OpenGL

Nibbie
Posts: 2
Joined: 2009.04
Post: #1
Hello there,

I've just started learning how to program in C++ and OpenGL on XCODE 2.5. I've been having trouble figuring out how to get multiple simultaneous inputs to move objects on the screen. I tried to develop a keydown type program, by using the glKeyboardfunc and glKeyboardUpfunc.

Basically the logic I used was, to set a variable to true/false (using boolean), or to a certain value (using either int, float, or glFloat). I'd used glKeyboardfunc to set the value to true, and then glKeyboardUpfunc to set it to false.

Then in the main loop, I set up:

if (a = true)
{
move = move + 1;
}

For some reason the block automatically moves , even if i set the value of 'a' to false right before it.

a = false;

if (a = true )
{move = move +1;
}

I'm not sure what is up. If anyone could help that would be great. Here's the actual code that I have so far.

Code:
(C++, GLUT, XCODE 2.5)
/***************Headers*********************/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <GLUT/glut.h>
#include <OpenGL/glext.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
using namespace std;

/*******************Variables********************/

GLfloat angle = 0.0;

GLfloat tX = 0, tY = 0, tZ = 0;




bool ignoreRepeats = false;

/******************Prototypes********************/

void display (void);
void reshape (int w, int h);
void cube ();
void keyboard (unsigned char key, int x, int y);

void keyboardUp (unsigned char key, int x, int y);
//void moveOnOff (void);


/********************Main**************************/

int main (int argc, char**argv)
{

glutInit (&argc, argv); //initialize the program.
glutInitDisplayMode (GLUT_SINGLE); //set up a basic display buffer (only singular for now)
glutInitWindowSize (600, 500); //set whe width and height of the window
glutInitWindowPosition (100, 100); //set the position of the window
glutCreateWindow ("A basic OpenGL Window"); //set the caption for the window

glutIgnoreKeyRepeat(ignoreRepeats);
glutKeyboardUpFunc (keyboardUp);

glutKeyboardFunc (keyboard);

//moveOnOff ();

glutIdleFunc (display); //change any idle values
glutDisplayFunc (display); //call the display function to draw our world



glutReshapeFunc (reshape);
glutMainLoop (); //initialize the OpenGL loop cycle




return 0;
}

/**********************Functions******************/

/*************Display Function****************/

void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

//moveOnOff ();

cube ();



bool j = false;  // The keydown variable
if (j = true){
angle = angle + .1;
}

glFlush();

}//End Function

/*****************Reshape*****************/


void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION); //set it so we can play with the 'camera'
glLoadIdentity (); //replace the current matrix with the Identity Matrix
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0); //set the angle of view,
//the ratio of sight, the near and far factors
glMatrixMode (GL_MODELVIEW); //switch back the the model editing mode.
}

void cube ()
{
glTranslatef (tX, tY, tZ);
glRotatef (angle, 1.0, 0.0, 0.0); //rotate on x axis
glRotatef (angle, 0.0, 1.0, 0.0); //rotate on y axis
glRotatef (angle, 0.0, 0.0, 1.0); //rotate on z axis
glColor3f (1.0, 0.0, 0.0);
glutWireCube (2);
}

/**************Keyboard Down Functions******************/

void keyboard (unsigned char key, int x, int y)
{

//rotate

if (key == 'a')
{
angle = angle + 2;
}

else if (key == 'u')
{
j = true;
}

else if (key == 'j')
{
tZ = tZ + 1;
}






//Exit Program
if (key == 27)
{
exit (0);
}


}//end of keyboard funtion

/******************Keyboard up*****************/

void keyboardUp (unsigned char key, int x, int y)
{


}//End of Function
Quote this message in a reply
Member
Posts: 283
Joined: 2006.05
Post: #2
In C and C++ you use two equals signs to test for equality. So it should be:

Code:
if (a==true) { // etc.

At the moment, you're setting a to true and testing the result of that operation.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2009.04
Post: #3
Thanks a Bunch. Its been driving me crazy, but that definitely helped. Thanks again
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Keyboard Handling skyhawk 10 6,771 Oct 26, 2010 08:47 AM
Last Post: skyhawk
  keyboard limitations daveh84 8 4,674 Feb 13, 2009 02:48 PM
Last Post: ferum
  Raw key even handler hogging the keyboard input monteboyd 4 3,269 Nov 2, 2005 06:29 PM
Last Post: monteboyd
  Cocoa Keyboard help Iceman 5 4,166 Aug 1, 2005 06:06 PM
Last Post: nabobnick
  Keyboard Question rogue 6 3,899 Jul 10, 2003 06:36 AM
Last Post: rogue