PDA

View Full Version : Draw Cylinder



noobatopengl
11-10-2015, 04:56 PM
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();
}

GClements
11-11-2015, 01:16 AM
You're setting up a coordinate system where object coordinates are effectively pixel coordinates.

Then you're drawing a cylinder ...
- with a radius of 1 pixel and a length of 0.4 pixels
- at the origin (i.e. the lower-left corner of the window)
- with its axis parallel to the view direction (gluCylinder only draws the sides, it doesn't draw end caps).

To make the position, orientation and size more reasonable, you could add


glTranslatef(320, 240, 0); // centred
glRotatef(90, 1, 0, 0); // vertical
glScalef(20,20,20); // 20 times larger

before calling gluCylinder(). But then you run into another problem, i.e. that your near and far planes are at -1 and 1 respectively, which will result in an enlarged cylinder being mostly clipped away. Given that you aren't using a depth buffer, there's no reason not to make those much larger, e.g.


glOrtho(0, 640, 0, 480, -1e6, 1e6);