Create a solar in computer graphics using c++


     



#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>

/*Faisal Porag
*/


//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 _angle = 0.0;
float _cameraAngle = 0.0;
float _ang_tri = 0.0;

//Draws the 3D scene
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glRotatef(-_cameraAngle, 0.0, 1.0, 0.0); //Rotate the camera
glTranslatef(0.0, 0.0, -7.0); //Move forward 5 units



glPushMatrix(); //Save the transformations performed thus far
glTranslatef(1.0, 1.0, .85); //Move to the center of the trapezoid
//glRotatef(_ang_tri, 1.0, 0.0, 0.0); //Rotate about the z-axis
glRotatef(_ang_tri, 0.0, 1.0, 0.0); //Rotate about the z-axis
glScalef(1.3, 1.9, 0.0); //Scale by 0.7 in the x, y, and z directions


glPopMatrix(); //Undo the move to the center of the pentagon

//middle
glPushMatrix(); //Save the current state of transformations

    glTranslatef(0.0, 0.0, 0.5); //Move to the center of the triangle
    glScalef(0.3,0.3,0.56);
//glRotatef(_angle, 0.0, 0.0, 1.0); //Rotate about the the vector (1, 2, 3)



//glTranslatef(1.0, -1.5, 1.5);
glBegin(GL_POLYGON);
//glColor3f(1.0,1.0,1.0);

glColor3f(1.0, 0.86, 0);
for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50;
//float r=0.55;
float r = 1;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}
glEnd();


//second orbit
glPushMatrix(); //Save the current state of transformations
    glTranslatef(0.0, 0.0, 0.5); //Move to the center of the triangle
    glScalef(2.0,2.0,0.56);
//glRotatef(_angle, 0.0, 0.0, 1.0); //Rotate about the the vector (1, 2, 3)

//glTranslatef(1.0, -1.5, 1.5);
//glBegin(GL_LINES);

//glBegin(GL_TRIANGLES);
glBegin(GL_LINE_LOOP);
//glColor3f(1.0,1.0,1.0);

glColor3f(0, 255, 191);
for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50;
//float r=0.55;
float r = 1;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}
glEnd();

//=======================


//third orbit
glPushMatrix(); //Save the current state of transformations
    glTranslatef(0.0, 0.0, 0.5); //Move to the center of the triangle
    glScalef(1.6,1.6,0.0);
//glRotatef(_angle, 0.0, 0.0, 1.0); //Rotate about the the vector (1, 2, 3)


glColor3f(0, 128, 255);
for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50;
//float r=0.55;
float r = 1;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}
glEnd();

//forth orbit
glPushMatrix(); //Save the current state of transformations
    glTranslatef(0.0, 0.0, 0.5); //Move to the center of the triangle
    glScalef(1.6,1.6,0.0);
    ///
//glRotatef(_angle, 0.0, 0.0, 1.0);

//glTranslatef(1.0, -1.5, 1.5);
//glBegin(GL_TRIANGLES);

glBegin(GL_LINE_LOOP);
//glBegin(GL_POINTS);

//glColor3f(1.0,1.0,1.0);

glColor3f(0, 128, 255);
for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50;
//float r=0.55;
float r = 1;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}
glEnd();


glRotatef(_angle, 0.0, 0.0, 1.0);

//planet
//====================
glTranslatef(0.34, 0.5, 0.0); //Move to the center of the triangle
    glScalef(0.08,0.08,0.56);
//glRotatef(_angle, 0.0, 0.0, 1.0); //Rotate about the the vector (1, 2, 3)


//glTranslatef(1.0, -1.5, 1.5);
glBegin(GL_POLYGON);
//glColor3f(1.0,1.0,1.0);

glColor3f(0, 0, 255);
for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50;
//float r=0.55;
float r = 1;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}
glEnd();

//====================
glTranslatef(7.8, -3.5, 0.0); //Move to the center of the triangle
    glScalef(0.95,0.95,0.56);
//glRotatef(_angle, 0.0, 0.0, 1.0); //Rotate about the the vector (1, 2, 3)

glBegin(GL_POLYGON);
//glColor3f(1.0,1.0,1.0);

glColor3f(0.99, 0.76, 0.85);

for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50;
//float r=0.55;
float r = 1;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}
glEnd();


//====================
glTranslatef(-7.8, -3.5, 0.0); //Move to the center of the triangle
    glScalef(0.95,0.95,0.56);
//glRotatef(_angle, 0.0, 0.0, 1.0); //Rotate about the the vector (1, 2, 3)


glBegin(GL_POLYGON);
//glColor3f(1.0,1.0,1.0);

glColor3f(0, 1, 0.35);
for(int i=0;i<200;i++)
{
float pi=3.1416;
float A=(i*2*pi)/50;
//float r=0.55;
float r = 1;
float x = r * cos(A);
float y = r * sin(A);
glVertex2f(x,y );
}
glEnd();




glPopMatrix(); //Undo the move to the center of the triangle

glutSwapBuffers();
}

void update(int value) {
_angle += 2.0f;
if (_angle > 360) {
_angle -= 360;
}
_ang_tri += 2.0f;
if (_ang_tri > 360) {
_ang_tri -= 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, 500);

//Create the window
glutCreateWindow("Transformations");
initRendering();

//Set handler functions
glutDisplayFunc(drawScene);

glutReshapeFunc(handleResize);

glutTimerFunc(25, update, 0); //Add a timer

glutMainLoop();
return 0;
}

output:


//Try this ........................






মন্তব্যসমূহ

Popular Posts

Simple 2D hut design in computer graphics using c++

HashMap in Java