PDA

View Full Version : adding sphere to my code



muhrnb
11-17-2017, 04:34 AM
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 :

#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.

Carmine
11-19-2017, 10:02 PM
See glusphere for an easy way to draw a sphere.