0% found this document useful (0 votes)
27 views9 pages

Document

The document contains C++ code for a simple OpenGL program that creates a graphical scene with a moving ball and a polygon representing a mountain. It includes functions for rendering the scene, handling keyboard input, and updating the ball's position and color. The program initializes OpenGL settings and enters a main loop to continuously render the scene and respond to user inputs.

Uploaded by

swamipravi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views9 pages

Document

The document contains C++ code for a simple OpenGL program that creates a graphical scene with a moving ball and a polygon representing a mountain. It includes functions for rendering the scene, handling keyboard input, and updating the ball's position and color. The program initializes OpenGL settings and enters a main loop to continuously render the scene and respond to user inputs.

Uploaded by

swamipravi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Assignment No 5

Roll No: 59

#include<iostream>

#include<stdlib.h>

#ifdef __APPLE__

#include<openGL/openGL.h>

#include<GLUT/glut.h>

#else

#include<GL/glut.h>

#endif

Using namespace std;

Float ballX = -0.8f;

Float ballY = -0.3f;

Float ballZ = -1.2f;

Float colR=3.0;

Float colG=1.5;

Float colB=1.0;

Float bgColR=0.0;

Float bgColG=0.0;

Float bgColB=0.0;
Static int flag=1;

Void drawBall(void) {

glColor3f(colR,colG,colB); //set ball colour

glTranslatef(ballX,ballY,ballZ); //moving it toward the screen a bit on


creation

glutSolidSphere (0.05, 30, 30); //create ball.

Void drawAv(void) {

glBegin(GL_POLYGON);

glColor3f(1.0,1.0,1.0);

glVertex3f(-0.9,-0.7,-1.0);

glVertex3f(-0.5,-0.1,-1.0);

glVertex3f(-0.2,-1.0,-1.0);

glVertex3f(0.5,0.0,-1.0);

glVertex3f(0.6,-0.2,-1.0);

glVertex3f(0.9,-0.7,-1.0);
glEnd();
}

Void drawClouds(){}

Void keyPress(int key, int x, int y)

If(key==GLUT_KEY_RIGHT)

ballX -= 0.05f;

if(key==GLUT_KEY_LEFT)

ballX += 0.05f;

glutPostRedisplay();

Void initRendering() {

glEnable(GL_DEPTH_TEST);

glEnable(GL_COLOR_MATERIAL);

glEnable(GL_LIGHTING); //Enable lighting

glEnable(GL_LIGHT0); //Enable light #0

glEnable(GL_LIGHT1); //Enable light #1

glEnable(GL_NORMALIZE); //Automatically normalize normals

//glShadeModel(GL_SMOOTH); //Enable smooth shading

//Called when the window is resized


Void handleResize(int w, int h) {

//Tell OpenGL how to convert from coordinates to pixel values

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective

//Set the camera perspective

glLoadIdentity(); //Reset the camera

gluPerspective(45.0, //The camera angle

(double)w / (double)h, //The width-to-height ratio

1.0, //The near z clipping coordinate

200.0); //The far z clipping coordinate

Void drawScene()

{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glClearColor(bgColR,bgColG,bgColB,0.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

//Add ambient light

GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color (0.2, 0.2, 0.2)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

//Add positioned light

GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)

GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)

glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);

glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

//Add directed light

GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)

//Coming from the direction (-1, 0.5, 0.5)

GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};

glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);

glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);

//drawing the SUN

glPushMatrix();

drawBall();

glPopMatrix();

//drawing the Mount Avarest

glPushMatrix();

drawAv();

glPopMatrix();
//drawing the Clouds

glPushMatrix();

drawClouds();

glPopMatrix();

glutSwapBuffers();

//float _angle = 30.0f;

Void update(int value) {

If(ballX>0.9f)

ballX = -0.8f;

ballY = -0.3f;

flag=1;

colR=2.0;

colG=1.50;

colB=1.0;

bgColB=0.0;

}
If(flag)

ballX += 0.001f;

ballY +=0.0007f;

colR-=0.001;
//colG+=0.002;

colB+=0.005;

bgColB+=0.001;

if(ballX>0.01)

Flag=0;
}

If (!flag)

ballX += 0.001f;

ballY -=0.0007f;

colR+=0.001;

colB-=0.01;

bgColB-=0.001;

if(ballX<-0.3)
{

Flag=1;
}
}

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)


{

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);

glutInitWindowSize(400,400);

glutCreateWindow(“Sun”);

initRendering();

glutDisplayFunc(drawScene);

glutFullScreen();

glutSpecialFunc(keyPress);

glutReshapeFunc(handleResize);

glutTimerFunc(1, update, 0);

glutMainLoop();

return(0);

Output:

You might also like