Continous shot generation

Dear All,

Every time When i click the left mouse,a shot should generate from an object positioned near to the camera. My aim is to render the scene with shots as much times I click.
How should I proceed.I am able generate one shot, but when I click next time no shot is generated. Scene is rendered with old shot moving.

With regards
IRIS_RAJ,
Graphics and Virtual Reality Research Group,
Banglaore (rajeshr@cair.res.in)

Maybe make every shot into a separate object with a lifespan (or removed when it hits a wall)?

In other words: add an object when the mouse button is pressed, and delete objects when they’ve hit a wall or are too old.

{EDIT) And if that is already what you’re trying to do, you’ll need to post some code: render loop (so we can check if you render a variable number of objects), mouse button handling code (so we can check whether or not it will repeatedly create new objects).

Here is the code, Instead of mouse I am using keyboard

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()
{
/* SHOT IS MOVING */
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
", shot);

glutPostRedisplay();
}

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

switch(key)
{
/* Here I am pressing the key s for generating shots */
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();

}

Sorry to have to ask this, but: have you ever heard of arrays and lists?

Currently you support three objects in your code: the ground, the gun and the shot. And that’s hard-coded.

As an aside: you’re calling gluNewQuadric in your draw code. That allocates a Quadrics object every time you render the scene.
And you only need one - for all objects that use the same shape. You only need to render that same object a number of times in your draw loop, at different positions.

Yep…T101 is right. You’re always drawing the same bullet so naturally you only see one.

You need to define a data structure that you can dynamically add and remove objects (in this case bullets) to and render all objects in the structure.

That way, when you press ‘s’ you add a new bullet structure to the list and when you go to render you render all bullets in the list. When a bullet leaves the view area or hits something (whatever) you remove it from the list. Also, each bullet in the list should maintain its current position or distance traveled, which the rendering routine can use to properly position the bullet on the screen.

something like that…

Thanks Aeluned, How to go about, Some Few lines. I am new to Graphics Programming.