Skip to content

Instantly share code, notes, and snippets.

@SohanChy
Created March 25, 2018 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SohanChy/ae0202acce3eaa689ab7deba2b092fd3 to your computer and use it in GitHub Desktop.
Save SohanChy/ae0202acce3eaa689ab7deba2b092fd3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stdlib.h>
#include<GL/gl.h>
#include <GL/glut.h>
#include<math.h>
#include<cstring>
using namespace std;
//Print
void Sprint( float x, float y, char *st)
{
int l,i;
l=strlen( st ); // see how many characters are in text string.
//glColor3f(1.0,0.0,0.0);
//glDisable(GL_LIGHTING);
glRasterPos2f( x, y); // location to start printing text
for( i=0; i < l; i++) // loop until i is greater then l
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, st[i]);
}
}
//Initializes 3D rendering
void initRendering() {
glEnable(GL_DEPTH_TEST);
}
//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
float _rotate1 = 25.0f;
float _rotate2 = 20.0f;
float _rotate3 = 5.0f;
float _rotate4 = 10.0f;
//float _move = 0.0f;
float mouse_x = 0;
float mouse_y = 0;
void mouseMotion(int x, int y)
{
int windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
y = (windowHeight-y);
x = x - (windowHeight/2);
y = y - (windowHeight/2);
mouse_x = (float) x / (windowHeight/2);
mouse_y = (float) y / (windowHeight/2);
cout<<"x "<<mouse_x<<endl;
cout<<"y "<<mouse_y<<endl;
}
void keyboard(unsigned char key, int x, int y) {
//find key codes: https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
switch (key) {
case 'w':
_rotate4 = _rotate4 + 15;
break;
case 's':
_rotate4 = _rotate4 - 15;
break;
}
}
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glLoadIdentity(); //Reset the drawing perspective
glMatrixMode(GL_MODELVIEW);
// glRotatef(_rotate, 1.0f, 0.0f, 0.0f); //Rotate the camera
// Earth-1 ////////
glPushMatrix(); //Save the current state of transformations
//Move to the center of the triangle
glRotatef(_rotate1, 0.0, 0.0, 1.0);
glTranslatef(0.9, 0.0, 0.0);//Rotate about the the vector (1, 2, 3)
glColor3ub(200,05,15);
char text[] = "Saturn";
Sprint(0.07,0,text);
glutSolidSphere(.07,50,50);
glPopMatrix();
// Earth-2 ////////
glPushMatrix(); //Save the current state of transformations
//Move to the center of the triangle
glRotatef(_rotate2, 0.0, 0.0, 1.0);
glTranslatef(0.6, 0.0, 0.0);//Rotate about the the vector (1, 2, 3)
glColor3ub(0,30,190);
char text3[] = "Earth";
Sprint(0.09,0,text3);
glutSolidSphere(.05,50,50);
// MOON ////////
glPushMatrix(); //Save the current state of transformations
//Move to the center of the triangle
glRotatef(_rotate4, 0.0, 0.0, 1.0);
glTranslatef(0.1, 0.0, 0.0);//Rotate about the the vector (1, 2, 3)
glColor3ub(240,240,240);
glutSolidSphere(.02,50,50);
glPopMatrix();
glPopMatrix();
//earth
glPushMatrix(); //Save the current state of transformations
//Move to the center of the triangle
glRotatef(-_rotate3, 0.0, 0.0, 1.0);
glTranslatef(0.3, 0.0, 0.0);//Rotate about the the vector (1, 2, 3)
glColor3ub(240,240,200);
glutSolidSphere(.03,50,50);
char text232[] = "Venus";
Sprint(0.07,0,text232);
glPopMatrix();
//Orbit-1/////////
glPushMatrix();
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);
for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50 ;
float r=0.9;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}
glEnd();
glPopMatrix();
// Orbit-2 ////////
glPushMatrix();
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);
for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50 ;
float r=0.6;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}
glEnd();
glPopMatrix();
//Orbit-3///////
glPushMatrix();
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);
for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50 ;
float r=0.3;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}
glEnd();
glPopMatrix();
//Orbit-4///////
glPushMatrix();
glRotatef(_rotate2, 0.0, 0.0, 1.0);
glTranslatef(0.6, 0.0, 0.0);
glColor3f(1.0,1.0,1.0);
glBegin(GL_LINES);
for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50 ;
float r=0.1;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(mouse_x, mouse_y, 0.0);
glColor3f(1.0,0.0,1.0);
glutSolidSphere(.05,50,50);
glPopMatrix();
// SUN ////////
glPushMatrix();
char text2[] = "Sohan Chowdhury 15-30269-2";
Sprint(-0.9,-0.9,text2);
glColor3f(1.0,0.0,0.0);
glutSolidSphere(.15,50,50);
glPopMatrix();
glutSwapBuffers();
}
void update(int value) {
_rotate1 += 0.5f;
if (_rotate1 > 360) {
_rotate1 -= 360;
}
_rotate2 += 1.0f;
if (_rotate2 > 360) {
_rotate2 -= 360;
}
_rotate3 += 1.5f;
if (_rotate3 > 360) {
_rotate3 -= 360;
}
_rotate4 += 0.8f;
if (_rotate4 > 360) {
_rotate4 -= 360;
}
glutPostRedisplay(); //Tell GLUT that the display has changed
//Tell GLUT to call update again in 25 milliseconds
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 800);
//Create the window
glutCreateWindow("Transformation");
initRendering();
//Set handler functions
glutDisplayFunc(drawScene);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0); //Add a timer
glutKeyboardFunc(keyboard); //Basic keyboard key handler
glutPassiveMotionFunc(mouseMotion);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment