PDA

View Full Version : glutKeyboardFunc problem

147147
07-19-2012, 07:33 AM
Hello. I'm making a simple 3D shooting game. The problem is that I want to shoot the bullet whenever the "x" button is pressed, but I can't even make it to appear on screen. I'm using this code (http://www.lighthouse3d.com/tutorials/glut-tutorial/the-code-so-far/) as a structure. Here is the code:

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

// angle of rotation for the camera direction
float angle = 0.0f;
// actual vector representing the camera's direction
float lx=0.0f,lz=-1.0f;
// XZ position of the camera
float x=0.0f, z=5.0f;
// the key states. These variables will be zero
//when no key is being presses
float deltaAngle = 0.0f;
float deltaMove = 0;

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

// 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 drawBullet(){
glPushMatrix();
glColor3f(0.0f,1.0f,0.4f);
glTranslatef(0,1,1);
glutSolidSphere(0.05f,10,10);
glPopMatrix();
}

void drawSnowMan() {
// Draw Body
glPushMatrix();
glColor3f(0.6f, 0.6f, 0.6f);
glScalef(1, 1.3, 1);
glTranslatef(0.0f ,0.75f, 0.0f);
glutSolidCube(0.8);
glPopMatrix();
glPushMatrix();
glColor3f(1.0f, 1.0f, 0.1f);
glTranslatef(0.0f, 1.75f, 0.0f);
glutSolidSphere(0.4,25,25);
glPopMatrix();
}

void computePos(float deltaMove) {

x += deltaMove * lx * 0.1f;
z += deltaMove * lz * 0.1f;
}

void computeDir(float deltaAngle) {

angle += deltaAngle;
lx = sin(angle);
lz = -cos(angle);
}

void renderScene(void) {

if (deltaMove)
computePos(deltaMove);
if (deltaAngle)
computeDir(deltaAngle);

// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Reset transformations
// Set the camera
gluLookAt( x, 1.0f, z,
x+lx, 1.0f, z+lz,
0.0f, 1.0f, 0.0f);

// Draw ground

glColor3f(0.9f, 0.9f, 0.9f);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, -100.0f);
glEnd();

// Draw 36 SnowMen

for(int i = -3; i < 3; i++)
for(int j=-3; j < 3; j++) {
glPushMatrix();
glTranslatef(i*10.0,0,j * 10.0);
drawSnowMan();
glPopMatrix();
}

glutSwapBuffers();
}

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

switch (key) {
case GLUT_KEY_LEFT : deltaAngle = -0.01f; break;
case GLUT_KEY_RIGHT : deltaAngle = 0.01f; break;
case GLUT_KEY_UP : deltaMove = 0.5f; break;
case GLUT_KEY_DOWN : deltaMove = -0.5f; break;

}

}

void shoot(unsigned char key, int xmouse, int ymouse){

if(key=='x'){
drawBullet();
}

glutPostRedisplay();
}
void releaseKey(int key, int x, int y) {

switch (key) {
case GLUT_KEY_LEFT :
case GLUT_KEY_RIGHT : deltaAngle = 0.0f;break;
case GLUT_KEY_UP :
case GLUT_KEY_DOWN : deltaMove = 0;break;
}
}

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

// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("Bullet");

// register callbacks
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);

glutSpecialFunc(pressKey);

glutKeyboardFunc(shoot);

// here are the new entries
glutIgnoreKeyRepeat(1);
glutSpecialUpFunc(releaseKey);

// OpenGL init
glEnable(GL_DEPTH_TEST);

// enter GLUT event processing cycle
glutMainLoop();

return 1;
}

ashraful
07-28-2012, 01:29 AM
//just update this:

float ang,lp=0,lq=-1.0,p=0,q=5;

void renderScene(void) {
if (deltaMove)
computePos(deltaMove);
if (deltaAngle)
computeDir(deltaAngle);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Reset transformations
// Set the camera
gluLookAt( x, 1.0f, z,
x+lx, 1.0f, z+lz,
0.0f, 1.0f, 0.0f);

// Draw ground

glColor3f(0.9f, 0.9f, 0.9f);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, -100.0f);
glEnd();

// Draw 36 SnowMen

for(int i = -3; i < 3; i++)
for(int j=-3; j < 3; j++) {
glPushMatrix();
glTranslatef(i*10.0,0,j * 10.0);
drawSnowMan();
glPopMatrix();
}
drawBullet();

glutSwapBuffers();
}

void idle(void)
{
p += 0.5 * lp * 0.1f;
q += 0.5 * lq * 0.1f;
glutPostRedisplay();
}

void shoot(unsigned char key, int xmouse, int ymouse){
if(key=='x'){
p=x,q=z,lp=lx,lq=lz;
glutIdleFunc(idle);
}
glutPostRedisplay();
}