Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: adding sphere to my code

  1. #1
    Newbie Newbie
    Join Date
    Nov 2017
    Posts
    1

    Exclamation adding sphere to my code

    hey there , I am trying to add a moving sphere to my code , i have created a simple stairway using cubes , now I want to add a sphere that moves from up to down, i don't need special physics or etc,so it's ok if it is simple, here is my code :
    Code :
    #include <GL/glut.h>
     
    void init()
    {
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_COLOR_MATERIAL);
    }
     
    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        GLint viewport[4];
        glGetIntegerv(GL_VIEWPORT, viewport);
        double aspect = (double)viewport[2] / (double)viewport[3];
        gluPerspective(60, aspect, 1, 100);
     
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
     
        // move back a bit
        glTranslatef( 0, 0, -35 );
     
        static float angle = 0;
        angle += 1.0f;
     
        glPushMatrix();
            glTranslatef(0,0,0);
            glRotatef(angle, 0.1, 0.2, 0.5);
            glColor3ub(255,0,255);
            glutSolidCube(5);
        glPopMatrix();
     
        glPushMatrix();
            glTranslatef(3,3,3);
            glRotatef(angle, 0.1, 0.2, 0.5);
            glColor3ub(255,0,0);
            glutSolidCube(5);
        glPopMatrix();
     
        glPushMatrix();
            glTranslatef(6,6,6);
            glRotatef(angle, 0.1, 0.2, 0.5);
            glColor3ub(0,255,0);
            glutSolidCube(5);
        glPopMatrix();
     
        glPushMatrix();
            glTranslatef(-3,-3,-3);
            glRotatef(angle, 0.1, 0.2, 0.5);
            glColor3ub(0,0,255);
            glutSolidCube(5);
        glPopMatrix();
     
        glPushMatrix();
            glTranslatef(-6,-6,-6);
            glRotatef(angle, 0.1, 0.2, 0.5);
            glColor3ub(255,255,0);
            glutSolidCube(5);
        glPopMatrix();
     
        glutSwapBuffers();
    }
     
    void reshape(int w, int h)
    {
        glViewport(0, 0, w, h);
    }
     
    void timer(int extra)
    {
        glutPostRedisplay();
        glutTimerFunc(16, timer, 0);
    }
     
    int main(int argc, char **argv)
    {
        glutInit(&argc, argv);
        glutInitWindowSize(640,480);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
        glutCreateWindow("CUBES");
     
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutTimerFunc(0, timer, 0);
     
        init();
     
        glutMainLoop();
        return 0;
    }

    thanks a lot.

  2. #2
    Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    388
    See glusphere for an easy way to draw a sphere.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •