Hello there,
I am to draw a cannon right where ball is. I can shoot but i cant make the cannon appear on the screen. Anyone have have any clue on how to do it? I am been stuck for last 3days. Please help me.
#include <Windows.h>
#include <stdlib.h> // standard definitions
#include <math.h> // math definitions
#include <stdio.h> // standard I/
#include <gl/glut.h>
#include <iostream>
using namespace std;
// Camera position
float x = 0.0, y = -25.0, z = -5.0; // initialyaxis 5 units south of origin
float movespeed = 0.0; // initialyaxis camera doesn't move
float xaxis = 0.0, yaxis = 1.0; // camera points initialyaxis along y-axis
float angle = 0.0; // angle of rotation for the camera direction
float green, red, blue;
struct Sphere {
GLfloat x, y, z;
GLfloat a, b, c;
};
Sphere mySpheres[1];
GLfloat coordX[9], coordY[9], coordZ[9], ratio[9];
void changeSize(int w, int h)
{
float ratio = ((float)w) / ((float)h); // window aspect ratio
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, ratio, 1, 200.0);
gluLookAt(0, 0, 2, 0, 0, 0, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Update with each idle event
void update(void)
{
for (int i = 0; i < 1; i++) {
mySpheres[i].z -= .01;
ratio[i] = mySpheres[i].z / coordZ[i];
mySpheres[i].x = coordX[i] * ratio[i];
mySpheres[i].y = coordY[i] * ratio[i];
}
glutPostRedisplay();
}
void drawcylinder()
{
glTranslatef(0, -2, -1);
GLUquadric* qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj, GL_EYE_LINEAR);
gluCylinder(qobj, 0.5, 0.5, 0.5, 40, 40);
}
void drawsqaure()
{
for (int i = 0; i < 1; i++)
{
glColor3d(0, 0, 1);
glPushMatrix();
glTranslatef(mySpheres[i].x, mySpheres[i].y, mySpheres[i].z);
glTranslatef(0, -2, -1);
glutSolidSphere(0.25, 20, 20);
glPopMatrix();
}
}
void newSphere(int i) {
mySpheres[i].x = 0;
mySpheres[i].y = 0;
mySpheres[i].z = -1;
coordX[i] = mySpheres[i].x;
coordY[i] = mySpheres[i].y;
coordZ[i] = mySpheres[i].z;
}
void firstSpheres() {
for (int i = 0; i < 1; i++) {
mySpheres[i].x = 0;
mySpheres[i].y = 0;
mySpheres[i].z = -1;
coordX[i] = mySpheres[i].x;
coordY[i] = mySpheres[i].y;
coordZ[i] = mySpheres[i].z;
}
}
// Draw the entire scene
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawsqaure();
for (int i = 0; i < 9; i++) {
if (mySpheres[i].z > 0)
newSphere(i);
}
//drawcylinder();
glutSwapBuffers(); // Make it all visible
}
void init()
{
glClearColor(1, 1, 1, 1);
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat lightInt[] = { .9, .9, .9, 1 };
glShadeModel(GL_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightInt);
}
void myKeyboard(unsigned char key, int mouseX, int mouseY)
{
if (key == 81 || key == 113)
{
exit(-1);
}
if (key == 32)
{
update();
firstSpheres();
renderScene();
}
}
//----------------------------------------------------------------------
// Main program - standard GLUT initializations and callbacks
//----------------------------------------------------------------------
int main(int argc, char **argv)
{
// general initializations
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 400);
glutCreateWindow("Flying Sphere");
// register callbacks
glutReshapeFunc(changeSize); // window reshape callback
glutDisplayFunc(renderScene); // (re)display callback
firstSpheres();
glutIdleFunc(update); // incremental update
glutKeyboardFunc(myKeyboard);
init();
glEnable(GL_DEPTH_TEST);
// enter GLUT event processing cycle
glutMainLoop();
return 0;
}