Umair Ayub
Umair Ayub

Reputation: 21271

Drawing a ball and two cones in OpenGL C++

I have a code where I want to draw a bowl and two cones at a time.

But, it is showing only those cones, not showing the ball.

#include <GL/glut.h>
#include <stdlib.h>
#include <Math.h>     // Needed for sin, cos

#define PI 3.14159265f

GLfloat ballRadius = 0.5f;   // Radius of the bouncing ball
GLfloat ballX = 0.0f;        // Ball's center (x, y) position
GLfloat ballY = 0.0f;
GLfloat ballXMax, ballXMin, ballYMax, ballYMin; // Ball's center (x, y) bounds
GLfloat xSpeed = 0.02f;      // Ball's speed in x and y directions
GLfloat ySpeed = 0.007f;
int refreshMillis = 30;      // Refresh period in milliseconds



static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);


}

static void display(void)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// This is ball's code that is not being drawn.

    ***glTranslatef(ballX, ballY, 0.0f);  // Translate to (xPos, yPos)
     // Use triangular segments to form a circle
     glBegin(GL_TRIANGLE_FAN);
        glColor3f(1.0f, 0.0f, 0.0f);  // Blue
        glVertex2f(0.0f, 0.0f);       // Center of circle
        int numSegments = 100;
        GLfloat angle;
        for (int i = 0; i <= numSegments; i++) { // Last vertex same as first vertex
           angle = i * 2.0f * PI / numSegments;  // 360 deg for all segments
           glVertex2f(cos(angle) * ballRadius, sin(angle) * ballRadius);
        }
     glEnd();***

//End of ball code

     glColor3d(0,1,0);
    glPushMatrix();
        glTranslated(-1.0,0.5,-6);
        glRotated(65, -1.0, 0.0, 0.0);
        glutSolidCone(1, 2, 70, 50);
    glPopMatrix();

    glPushMatrix();
        glTranslated(0.0,-1.5,-6);
        glRotated(65, -1.0, 3.0, 0.0);
        glutWireCone(1,2, 16, 16);
    glPopMatrix();

    glutSwapBuffers();
}


/* Program entry point */

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(740,580);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("Programming Techniques - 3D Cones");

    glutReshapeFunc(resize);
    glutDisplayFunc(display);

    glClearColor(1,1,1,1);
 glutMainLoop();

    return EXIT_SUCCESS;
}

Upvotes: 2

Views: 3012

Answers (1)

Andreas Haferburg
Andreas Haferburg

Reputation: 5520

The reason you don't see the circle is that it's clipped against the near plane. With glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0); you specify that the near plane is at z = -2, and the far plane at z = -100. Anything outside these values are clipped. But by using glVertex2, your z values for the circle vertices are 0, so all of them are clipped. You can fix it by calling glTranslatef(ballX, ballY, -10.0f); instead.

A couple more pointers:

  1. Always reset the matrix mode to GL_MODELVIEW (e.g. in your resize() function). You don't have to, but it's a good convention.
  2. Always glPush/PopMatrix() before modifying the matrix stack (e.g. when translating the circle).
  3. glColor3f(1.0f, 0.0f, 0.0f); // Blue? ;)

Upvotes: 3

Related Questions