Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C Program to draw Sun rise and fall in OpenGL : Computer Graphics

Below is the computer graphics program to display sun rise and fall. First try to run the program. Below are the details for terminologies and functions used in program. Try to understand importance of each function.

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

void glTranslatef(GLfloat  x,  GLfloat  y,  GLfloat  z); 
// moving it toward the screen a bit on creation

void glutSolidSphere(GLdouble radius,GLint slices, GLint stacks); 
// render sphere objects

void glClearColor(GLclampf red,  GLclampf green,  GLclampf blue,  GLclampf alpha); 
// It specifies value to clear color buffer.

void glEnable(GLenum  cap); 
// enable / disable server side GL capabilities

void glViewport(GLint x,  GLint y,  GLsizei width,  GLsizei height); 
// initialize the viewport

void glMatrixMode(GLenum mode); 
// initialize Current matrix

void glLoadIdentity( void); 
// transforms current matrix to identity matrix
void gluPerspective(GLdouble fovy,  GLdouble aspect,  GLdouble zNear,  GLdouble zFar); 
// set up value for perspective projection matrix

void glClear(GLbitfield  mask); 
// clear preset values of buffer

void glLightModelf(GLenum pname,  GLfloat param); 
// set the lighting model parameters

void glLightf(GLenum light,  GLenum pname,  GLfloat param);  
// initialize light source parameters

void glPushMatrix( void); 
// it does pop and push current matrix stack.

void glutPostRedisplay(void); 
// it marks current window to re-display.

void glutInit(int *argcp, char **argv); 
// it initialize GLUT library.

void glutInitDisplayMode(unsigned int mode); 
// Used to set initial display mode.

void glutInitWindowSize(int width, int height); 
//it sets the size of window and position of window.

int glutCreateWindow(char *name); 
// It creates Top Level window.

void glutDisplayFunc(void (*func)(void)); 
// it sets display callback for current window

void glutFullScreen(void); 
// call the current window be made full screen.

void glutSpecialFunc(void (*func)(int key, int x, int y)); 
// it sets the keyboard callback for current window.

void glutTimerFunc(unsigned int msecs,void (*func)(int value), value); 
// it registers timer callback to get triggered in a specified milliseconds.

void glutMainLoop(void);  
// enters to GLUT event processing loop


Program:
#include
#include

#ifdef __APPLE__
#include
#include
#else
#include
#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       {
           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(25, update, 0);

    glutMainLoop();
  
    return(0);
}


This post first appeared on Ask For Program, please read the originial post: here

Share the post

C Program to draw Sun rise and fall in OpenGL : Computer Graphics

×

Subscribe to Ask For Program

Get updates delivered right to your inbox!

Thank you for your subscription

×