Hello, I am trying to draw cylinder and put it a center. But it show a black background and the ball is showing can someone help me
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<GL/glut.h>
GLUquadric* qobj;
#define PI_OVER180 3.14159265359 / 180
//player 1 variables
float angl = 0;
float tankx = 90, tanky = 100;
float bulletx, bullety;
bool btrue = false;
float bxv = 0, byv = 0, vx = 0, vy = 0;
float vdp = 0, vdpd = 1; //velocity decider pointer (vdp) and velocity decider pointer direction (vdpd)
bool vdtrue = false;
float gravity = 0.025; //gravity of the game change the value to increase or decrease
bool keystates[256];
voida init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 0, 480, -1, 1);
glMatrixMode(GL_MODELVIEW);
}
void drawcannon()
{
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
qobj = gluNewQuadric();
gluQuadricNormals(qobj, GLU_SMOOTH);
gluCylinder(qobj, 1.0, 1.0, 0.4, 10, 16);
}
void drawbullet()
{
glLoadIdentity();
glTranslatef(bulletx, bullety, 0);
glColor3f(0, 1, 0);
GLUquadricObj *quadric;
quadric = gluNewQuadric();
gluQuadricDrawStyle(quadric, GLU_FILL);
gluSphere(quadric, 1, 20, 18);
if (btrue)
{
if (bulletx > 640 || bulletx < 0 || bullety > 480 || bullety - 2 < 86)
{
btrue = false;
}
}
}
void KeyUpPress(unsigned char key, int x, int y){
switch (key){
case 'a':
keystates['a'] = false;
break;
case 'd':
keystates['d'] = false;
break;
case ' ':
keystates[' '] = false;
break;
case 27:
exit(0);
}
}
void KeyDownPress(unsigned char key, int x, int y){
switch (key){
case 'a':
keystates['a'] = true;
break;
case 'd':
keystates['d'] = true;
break;
case ' ':
keystates[' '] = true;
break;
case 27:
exit(0);
}
}
void updategame(int value)
{
glutTimerFunc(0, updategame, 0);
if (keystates['a'] == true)
{
if (vdtrue == false)
{
angl = angl + 1;
}
}
if (keystates['d'] == true)
{
if (vdtrue == false)
{
angl = angl - 1;
}
}
if (keystates[' '] == true)
{
if (btrue == false)
{
btrue = true;
vdtrue = true;
}
if (vdtrue)
{
vdtrue = false;
bulletx = tankx + cos(angl * PI_OVER180) * 20; bullety = tanky + sin(angl * PI_OVER180) * 20;
bxv = cos(angl * PI_OVER180) * vx; byv = sin(angl * PI_OVER180) * vy;
}
}
if (vdp >= 17 || vdp <= -17) { vdpd = -vdpd; }
vdp = vdp + vdpd;
vx = cos(vdp / 17 * 90 * PI_OVER180) * 3.5;
vy = cos(vdp / 17 * 90 * PI_OVER180) * 3.5;
bulletx = bulletx + bxv;
bullety = bullety + byv;
byv = byv - gravity;
if (angl < 0){ angl = 0; }
if (angl > 180){ angl = 180; }
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
drawcannon();
if (btrue && vdtrue == false)
{
drawbullet();
}
glutSwapBuffers();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("cannnon");
init();
glutDisplayFunc(display);
glutKeyboardFunc(KeyDownPress);
glutKeyboardUpFunc(KeyUpPress);
glutIdleFunc(display);
glutTimerFunc(10, updategame, 0);
glutMainLoop();
}
Reply With Quote Reply With Quote Multi-Quote This Message