PDA

View Full Version : KeyboardFunc not working as desired



10-14-2005, 12:49 AM
Dear All,

I have basic doubt about key board. I have written program for displaying cube and manipulating(moving forward and backward) it with keyboard. When I press key for moving forward it is happening, but when I press key for backward it is not moving backward.

Moreover, I have one more doubt. When I press c it is giving view as if it is moving in the z direction(becoming smaller) rather than x direction ( right of the screen)

How to solve these two problems: -

/* A simple program displaying a Wired Cube and manipulation with keyboard */

#include <glut.h>
#include <stdio.h>

int width = 1000;
int height =1000;
static GLdouble Objx = 0.0f;
static GLdouble Objy = 0.0f;
static GLdouble Objz = 0.0f;

void translate(GLdouble,GLdouble,GLdouble);

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0f,1.0f,1.0f);
gluLookAt(0.0f,0.0f,5.0f,Objx,Objy,Objz,0.0f,1.0f, 0.0f);
glScalef(2.0f,2.0f,2.0f);
glutWireCube(2.0f);
//glutSolidSphere(1.0f,20,20);
//translate(Objx,Objy,Objz);
glutSwapBuffers();
}

void init()
{
glShadeModel(GL_FLAT);
glClearColor(0.0f,0.0f,0.0f,1.0f);
}

void reshape(int width,int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5.0,5.0,-5.0,5.0,1.5,20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

void translate(GLdouble Objx,GLdouble Objy,GLdouble Objz)
{
Objx += 0.0f;
Objy += 0.0f;
Objz += 0.2f;
glutPostRedisplay();
}

void keyboard(unsigned char key, int x,int y)
{
switch(key)
{
// moveforward
case 'c':
Objx += 0.001f;
glutPostRedisplay();
break;
// movebackward
case 'd':
Objx -= 0.002f;
glutPostRedisplay();
break;
default:
break;
}
}

void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitWindowSize(width,height);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Simple Program");
init();
glutDisplayFunc(display); // display()
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();

}

Rajesh.R
IRIS,
Bangalore

10-14-2005, 04:04 AM
#include <glut.h>
#include <stdio.h>

int width = 1000;
int height =1000;
static GLdouble Objx = 0.0f;
static GLdouble Objy = 0.0f;
static GLdouble Objz = 0.0f;

void translate(GLdouble,GLdouble,GLdouble);

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0f,1.0f,1.0f);
gluLookAt(0.0f,0.0f,5.0f,Objx,Objy,Objz,0.0f,1.0f, 0.0f);
glScalef(2.0f,2.0f,2.0f);
gltranslatef(Objx,Objy,Objz);
glutWireCube(2.0f);
glutSwapBuffers();
}

void init()
{
glShadeModel(GL_FLAT);
glClearColor(0.0f,0.0f,0.0f,1.0f);
}

void reshape(int width,int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5.0,5.0,-5.0,5.0,1.5,20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}

void keyboard(unsigned char key, int x,int y)
{
switch(key)
{
// moveforward
case 'c':
Objx += 0.001f;
break;
// movebackward
case 'd':
Objx -= 0.002f;
break;
default:
break;
}
}

void main(int argc,char** argv)
{
glutInit(&amp;argc,argv);
glutInitWindowSize(width,height);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Simple Program");
init();
glutDisplayFunc(display); // display()
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();

}should work.

And avoid to post twice.