Code:
#include <iostream>
#include <GL/glut.h>
#include <windows.h>
#include <math.h>
using namespace std;
HDC hdc;
int slices = 40;
int stacks = 40;
float radius = 0.1;
float angle[2] = {0.0f, 0.0f};
GLfloat mat_ambient[] = {0.5, 0.5, 0.5, 0.01};
GLfloat mat_specular[] = {0.1, 0.1, 0.1, 0.01};
GLfloat mat_shininess[] = {50.0};
GLfloat light_position[] = {10.0, 10.0, 20.0, 0.0};
GLfloat model_ambient[] = {0.5, 0.5, 0.5, 0.0};
void setupMaterials() {
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
}
void changeColour(GLfloat r, GLfloat g, GLfloat b, GLfloat A) {
model_ambient[0] = r;
model_ambient[1] = g;
model_ambient[2] = b;
model_ambient[3] = A;
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
}
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.01);
setupMaterials();
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glShadeModel(GL_SMOOTH);
}
void reshape(int w, int h) {
GLfloat fAspect;
if(h==0) h=1;
glViewport(0,0,w,h);
fAspect = (GLfloat)w / (GLfloat)h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, fAspect, 0.5, 1000.0);
glTranslatef(0,0,-1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void DrawSphere(){
glPushMatrix();
changeColour(0.3, 0.3, 0.35, 0.0);
glTranslatef(0.0f, 0.5f,0.0f);
glBegin(GL_LINES);
glVertex2f(0.0, 0.0);
glVertex2f(0.0, -0.5);
glEnd();
glTranslatef(0.0, -0.5, 0.0);
glutSolidSphere(radius, slices, stacks);
glPopMatrix();
}
void DrawSphere2(){
glPushMatrix();
changeColour(0.3, 0.3, 0.35, 0.0);
glTranslatef(0.0f,0.5f,0.0f);
glBegin(GL_LINES);
glVertex2f(0.2, 0.0);
glVertex2f(0.2, -0.5);
glEnd();
glTranslatef(0.2, -0.5, 0.0);
glutSolidSphere(radius, slices, stacks);
glPopMatrix();
}
void display(void) {
bool static sphere= false;
bool static sphere2=true;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (sphere){
angle[0] = angle[0] +1.0f;
}
else {
angle[0] = angle[0] -1.0f;
}
if (sphere2){
angle[1] = angle[1] +1.0f;
}
else {
angle[1] = angle[1] -1.0f;
}
if(angle[0]<=-20.0f){
sphere = true;
}
if(angle[0]>=0.0f){
sphere = false;
}
glPushMatrix();
glTranslatef(0.0, 0.5,0.0);
glRotatef(angle[0],0.0,0.0,1.0);
glTranslatef(-0.0, -0.5, 0.0);
DrawSphere();
glPopMatrix();
if(angle[1]<=0.0f){
sphere2 = true;
}
if(angle[1]>=20.0f){
sphere2 = false;
}
glPushMatrix();
glTranslatef(0.2, 0.5,0.0);
glRotatef(angle[1],0.0,0.0,1.0);
glTranslatef(-0.2,-0.5,0.0);
DrawSphere2();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 27:
exit(0); // Exit the application if 'Esc' key is pressed
}
}
void animate() {
Sleep(100);
glutPostRedisplay();
}
void processMouse(int button, int state, int x, int y){
if (state==GLUT_DOWN){
if((button==GLUT_LEFT_BUTTON) && x<350 && x>450){
glTranslatef(0.0, 0.5,0.0);
glRotatef(x, 0.0 ,0.0, 1.0);
glTranslatef(0.0, -0.5,0.0);
angle[0] = angle[0]+x;
}
}
if (state==GLUT_UP){
if (button==GLUT_LEFT_BUTTON && angle[0]!=0){
animate();
}
}
}
int main(int argc, char * argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (800, 600);
glutCreateWindow (argv[0]); init();
glutKeyboardFunc (keyboard);
PaintDesktop(hdc);
glCullFace(GL_BACK);
glutDisplayFunc (display);
glutReshapeFunc (reshape);
glutMouseFunc(processMouse);
glutIdleFunc(animate);
glutMainLoop();
return 0;
}