PDA

View Full Version : Shot not visible



iris_raj
05-04-2005, 10:12 PM
Dear All,

Here I am trying to simulate a gun shooting a moving target. gun
is rendered as a cylinder, moving target rendered as sphere and
shot is represented as Line. When I press key, shot should come out
of gun. But I am unable to see shot (line) emerging out of the gun
(cylinder). Code is given below and give necessary suggestions

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <iostream.h>
#include <sys/time.h>
#include "Scene.h"
#include "vec3.h"

vec3 sphere_loc(0.0f,0.0f,1.95);
vec3 sphere_dir(0.0f,0.0f,1.0f);
vec3 shot_loc(0.0f,0.0f,3.9f);
vec3 shot_dir(0.0f,0.0f, -1.0f);
bool shot = false;

static float elap_time = 0.0f;
const double clock_frequency = 0.000000001;

Scene *scene;

void init() // Any GL Init Code & User Initialiazation Goes Here
{
glClearColor (0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth (1.0f); // Depth Buffer Setup
glShadeModel (GL_SMOOTH); // Select Smooth Shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

}


void Draw (void)
{


glMatrixMode(GL_MODELVIEW);
glLoadIdentity (); // Reset The Modelview Matrix
gluLookAt(0, 1.5, 7.0f, 0, 0, 0, 0, 1, 0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glColor3f(1.0f,0.0f,1.0f);
// Ground
glPushMatrix();
glBegin(GL_QUADS);
glColor3ub(255, 255, 255);
glVertex3f(20, -1.5, 20);
glVertex3f(-20, -1.5, 20);
glVertex3f(-20, -1.5, -20);
glVertex3f(20, -1.5, -20);
glEnd();


glPopMatrix();
// Gun represented by cylinder
glPushMatrix();
GLUquadricObj *cone;
cone = gluNewQuadric();
glColor3f(0.0f,1.0f,1.0f);
/* glRotatef(90.0f,1.0f,0.0f,0.0f);
gluQuadricNormals(cone,GLU_FLAT);
glRotatef(90.0f,1.0f,0.0f,0.0f); */
glColor3f(1.0f,0.0f,0.0f);
glTranslatef(0.0f,0.0f,4.0f);
gluCylinder(cone,0.2f,0.2,1.0,10,5);

glPopMatrix();
//When I press key s, shot should come out of the cylinder
if(shot)
{
glPushMatrix();
// Shot
glTranslatef(shot_loc.x,shot_loc.y,shot_loc.z);
glBegin(GL_LINE);
glColor3f(0.0f,1.0f,0.0f);
glLineWidth(5.0);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,10.2f);
glEnd();
glPopMatrix();
}
//target which is moving towards the gun

glPushMatrix();
glTranslatef(sphere_loc.x,sphere_loc.y,sphere_loc. z);
glBegin(GL_LINE);
glColor3f(0.0f,1.0f,0.0f);
glLineWidth(5.0);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,10.2f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}

void reshape(int width,int height)
{
glViewport(0, 0, (GLsizei)(width), (GLsizei)(height));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (45.0f, (GLfloat)(width)/(GLfloat)(height),
0.001, 100.0f);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}

void idle()
{
sphere_loc.x += elap_time * sphere_dir.x;
sphere_loc.y += elap_time * sphere_dir.y;
sphere_loc.z += elap_time * sphere_dir.z;

shot_loc.x += elap_time * shot_dir.x;
shot_loc.y += elap_time * shot_dir.y;
shot_loc.z += elap_time * shot_dir.z;

elap_time += 0.0001;

printf("Value of Shot = %f\n", shot);

glutPostRedisplay();
}

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

switch(key)
{
case 's':
shot = true;
break;
default:
break;
}
glutPostRedisplay();

}

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

glutInit(&argc, argv);
glutInitWindowSize(600,600);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(Draw);
//glutTimerFunc(500,TimerFunc,400);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
glutMainLoop();


}