Solar System Using OpenGl
#include<GL/glu.h>
#include<GL/glut.h>
#include<GL/gl.h>
#include<math.h>
#include<cstdlib>
#include<iostream>
using namespace std;
float rotAngle=0;
float rotAngle1=10;
float theta=0;
GLfloat randomFloat()
{
return (int)rand() / 1000;
}
void display()
{
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2);
glBegin(GL_POINTS);
glColor3f(1,1,1);
for(int i=0;i<10;i++)
{
// cout<<randomFloat()<<endl;
glVertex3i(randomFloat(),randomFloat(),randomFloat());
glVertex3i(randomFloat(),-randomFloat(),-randomFloat());
glVertex3i(-randomFloat(),randomFloat(),randomFloat());
glVertex3i(-randomFloat(),-randomFloat(),randomFloat());
}
glEnd();
glFlush();
glPushMatrix();
glColor3f(1,0,0);
glTranslatef(8*cos(theta),8*sin(theta),0);
glutSolidSphere(1,50,50);
glPopMatrix();
glPushMatrix();
glColor3f(1,0,1);
glTranslatef(16*sin(theta),14*cos(theta),0);
glutSolidSphere(1,50,50);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,1);
glTranslatef(20*cos(theta),20*sin(theta),0);
glutSolidSphere(1,50,50);
glPopMatrix();
glPushMatrix();
glColor3f(1,1,0);
glRotatef(rotAngle,0,1,0);
glutSolidSphere(1,50,50);
glPopMatrix();
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
void timer(int v)
{
rotAngle+=0.1;
rotAngle1+=1;
theta+=0.01;
glutTimerFunc(10,timer,1);
}
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80,1/1,2,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1,1,30,0,0,0,0,0,1);
}
void keyboard(unsigned char k, int x, int y)
{
switch (k)
{
case ' ':
{
}
break;
case 's':
rotAngle-=0.1;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(10, 10);
glutInitWindowSize(700,700);
glutCreateWindow("Solar System");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
init();
glutTimerFunc(1,timer,1);
glutMainLoop();
}
#include<GL/glut.h>
#include<GL/gl.h>
#include<math.h>
#include<cstdlib>
#include<iostream>
using namespace std;
float rotAngle=0;
float rotAngle1=10;
float theta=0;
GLfloat randomFloat()
{
return (int)rand() / 1000;
}
void display()
{
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2);
glBegin(GL_POINTS);
glColor3f(1,1,1);
for(int i=0;i<10;i++)
{
// cout<<randomFloat()<<endl;
glVertex3i(randomFloat(),randomFloat(),randomFloat());
glVertex3i(randomFloat(),-randomFloat(),-randomFloat());
glVertex3i(-randomFloat(),randomFloat(),randomFloat());
glVertex3i(-randomFloat(),-randomFloat(),randomFloat());
}
glEnd();
glFlush();
glPushMatrix();
glColor3f(1,0,0);
glTranslatef(8*cos(theta),8*sin(theta),0);
glutSolidSphere(1,50,50);
glPopMatrix();
glPushMatrix();
glColor3f(1,0,1);
glTranslatef(16*sin(theta),14*cos(theta),0);
glutSolidSphere(1,50,50);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,1);
glTranslatef(20*cos(theta),20*sin(theta),0);
glutSolidSphere(1,50,50);
glPopMatrix();
glPushMatrix();
glColor3f(1,1,0);
glRotatef(rotAngle,0,1,0);
glutSolidSphere(1,50,50);
glPopMatrix();
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
void timer(int v)
{
rotAngle+=0.1;
rotAngle1+=1;
theta+=0.01;
glutTimerFunc(10,timer,1);
}
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80,1/1,2,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1,1,30,0,0,0,0,0,1);
}
void keyboard(unsigned char k, int x, int y)
{
switch (k)
{
case ' ':
{
}
break;
case 's':
rotAngle-=0.1;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(10, 10);
glutInitWindowSize(700,700);
glutCreateWindow("Solar System");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
init();
glutTimerFunc(1,timer,1);
glutMainLoop();
}