PDA

View Full Version : Problem with keyboard function



avpai1992
10-12-2015, 01:13 AM
Hi,
I have written a program where I have separated the keyboard function from the main function. I have used kes.cpp and keys.h to define and declare the normal process keys and special process keys in the "main.cpp"( where the main body is). The program runs but it doesnt detect any key press or act on it. kindly help.

keys.cpp




#include <math.h>
#include <glut.h>
#include "Keys.h"


static float angle=0.0;
// actual vector representing the camera's direction
static float lx=0.0f,lz=-1.0f,ly=0.0;
// XZ position of the camera
static float x=0.0f,z=5.0f,y=1.0;
static float deltaAngleX = 0.0,deltaAngleY = 0.0; // additional angle change when dragging



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

switch(key)
{
case 27 :
exit(0);
break;
case 'w' :
rotate_x -= 5;
break;
case 's' :
rotate_x += 5;
break;
case 'a' :
rotate_y -= 5;
break;
case 'd' :
rotate_y += 5;
break;
case 'z' :
rotate_z += 5;
break;
case 'x' :
rotate_z -= 5;
break;
}
}


void processSpecialKeys(int key, int xx, int yy)
{

float fraction = 0.1f;

switch (key)
{
case GLUT_KEY_LEFT :
angle -= 0.01f;
lx = sin(angle);
lz = -cos(angle);
break;
case GLUT_KEY_RIGHT :
angle += 0.01f;
lx = sin(angle);
lz = -cos(angle);
break;
case GLUT_KEY_UP :
x += lx * fraction;
z += lz * fraction;
break;
case GLUT_KEY_DOWN :
x -= lx * fraction;
z -= lz * fraction;
break;

case GLUT_KEY_PAGE_UP :
r_z += 1;
break;
case GLUT_KEY_PAGE_DOWN :
r_z -= 1;
break;
case GLUT_KEY_HOME :
r_x += 1;
break;
case GLUT_KEY_END :
r_x -= 1;
break;

}
}


keys.h


void processNormalKeys(unsigned char key, int x, int y);
void processSpecialKeys(int key, int xx, int yy);



mains.cpp


#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#include <glut.h>
#include "zpr.h"
#include "Keys.h"
#include "Shapes.h"






// angle of rotation for the camera direction
float angle=0.0;
// actual vector representing the camera's direction
float lx=0.0f,lz=-1.0f,ly=0.0;
// XZ position of the camera
float x=0.0f,z=5.0f,y=1.0;
float deltaAngleX = 0.0,deltaAngleY = 0.0; // additional angle change when dragging

// Mouse drag control
int isDragging = 0; // true when dragging
int xDragStart = 0,yDragStart = 0; // records the x-coordinate when dragging starts


void changeSize(int w, int h)
{

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == 0)
h = 1;
float ratio = w * 1.0 / h;

// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);

// Reset Matrix
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the correct perspective.
gluPerspective(45.0f, ratio, 0.1f, 100.0f);

// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
}


void renderScene(void)
{

// Clear Color and Depth Buffers

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Reset transformations
glLoadIdentity();
// Set the camera
glRotatef( r_x, 1.0, 0.0, 0.0 );
glRotatef( r_y, 0.0, 1.0, 0.0 );
glRotatef( r_z, 0.0, 0.0, 1.0 );

gluLookAt( x, y, z,
x+lx, y+ly, z+lz,
0.0f, 1.0f, 0.0f);

// Draw ground
glColor3f(0.9f, 0.9f, 0.9f);
DrawNet( 10,10 , 10);

prism();

glutSwapBuffers();
}

void mouseMove(int x, int y)
{
if (isDragging) { // only when dragging
// update the change in angle
deltaAngleX = (x - xDragStart) * 0.005;
deltaAngleY = (y - yDragStart) * 0.005;

// camera's direction is set to angle + deltaAngle
lx = sin(angle + deltaAngleX);
lz = -cos(angle + deltaAngleX);
ly= -sin(angle + deltaAngleY);
}
}

void mouseButton(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) { // left mouse button pressed
isDragging = 1; // start dragging
xDragStart = x;// save x where button first pressed
yDragStart = y;// save y where button first pressed
}
else { /* (state = GLUT_UP) */
angle += deltaAngleX; // update camera turning angle
isDragging = 0; // no longer dragging
}
}
}


void pick(GLint name)
{
printf("Pick: %d\n",name);
fflush(stdout);
}
int main(int argc, char **argv)
{

// init GLUT and create window

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(200,200);
glutInitWindowSize(600,650);
glutCreateWindow("Prism 3D");

// register callbacks
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(processSpecialKeys);
glutMouseFunc(mouseButton); // process mouse button push/release
glutMotionFunc(mouseMove); // process mouse dragging motion
//zprInit();
// zprSelectionFunc(renderScene); /* Selection mode draw function */
//zprPickFunc(pick);

// OpenGL init
glEnable(GL_DEPTH_TEST);

// enter GLUT event processing cycle
glutMainLoop();

return 1;
}