Search Tutorials

Friday 14 June 2013

OpenGL code to zoom in and zoom out Triangle using Key

This code is for making a simple triangle which can zoom in and zoom out using keyboard key.

OpenGL program:-

#include<iostream>
#include<stdlib.h>

#ifdef __APPLE__
#include<openGL/openGL.h>
#include<GLUT/glut.h>
#else 
#include<GL/glut.h>
#endif

using namespace std;

double rotate_by_key=0;

double rotate_x=0.5;

void keyPress(int key,int x,int y)
{

    if(key==27)
          exit(0);
    if (key == GLUT_KEY_UP)
            rotate_x += .05;
        if (key == GLUT_KEY_DOWN)
            rotate_x -= .05;

    glutPostRedisplay();
   
}

void initRendering()
{
    glEnable(GL_DEPTH_TEST);
}


//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);
   
    glMatrixMode(GL_MODELVIEW);
   
    glLoadIdentity();

    glScalef( rotate_x,rotate_x,1.0f );
    //glScalef(1.0f,1.0f,rotate_x);
    glRotatef(  rotate_by_key,-1.0f, 1.5f, -5.0f );
    glBegin(GL_TRIANGLES);

        glVertex3f(1.0f, 0.0f, -5.0f);
        glVertex3f(0.0f, 1.0f, -5.0f);
        glVertex3f(-1.0f, 0.0f, -5.0f);

    glEnd();
   
    glutSwapBuffers();
}

int main(int argc,char** argv)
{
    glutInit(&argc,argv);
   
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
   
    glutInitWindowSize(400,400);
   
    glutCreateWindow("My triangle");
   
    initRendering();
   
    glutDisplayFunc(drawScene);
   
    glutSpecialFunc(keyPress);

    glutReshapeFunc(handleResize);
   
    glutMainLoop();
   
    return(0);
}

//Output Of the above program:-

OpenGL code to zoom in and zoom out Triangle using Key

Related Programs:-

Self rotating Triangle

Rotate Triangle using Key

Draw text on the Screen

Sun Rise and Down

Simple Triangles

OpenGl code to make self rotating Triangle

This source code is for making a simple triangle which rotates itself.

OpenGL program:-

#include<iostream>
#include<stdlib.h>

#ifdef __APPLE__
#include<openGL/openGL.h>
#include<GLUT/glut.h>
#else 
#include<GL/glut.h>
#endif

using namespace std;
void keyPress(unsigned char key,int x,int y)
{

    switch(key)
    {
       case 27:
            exit(0);     
    }
}

void initRendering()
{
    glEnable(GL_DEPTH_TEST);
}


//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
}



float _angle = 30.0f;
float _cameraAngle = 0.0f;

void drawScene()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   
    glMatrixMode(GL_MODELVIEW);
   
    glLoadIdentity();

    glPushMatrix(); //Save the transformations performed thus far

    glRotatef(_angle, -1.5f, 0.5f, -5.0f); //Rotate about the z-axis

    glBegin(GL_TRIANGLES);
   
        glVertex3f(-0.5f, 0.5f, -5.0f);
        glVertex3f(-1.0f, 1.5f, -5.0f);
        glVertex3f(-1.5f, 0.5f, -5.0f);

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

    glutSwapBuffers();
}

void update(int value) {
    _angle += 2.0f;
    if (_angle > 360) {
        _angle -= 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)
{
    glutInit(&argc,argv);
   
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
   
    glutInitWindowSize(400,400);
   
    glutCreateWindow("My Triangle");
   
    initRendering();
   
    glutDisplayFunc(drawScene);
   
    glutKeyboardFunc(keyPress);

    glutReshapeFunc(handleResize);
   
    glutTimerFunc(25, update, 0); //Add a timer
   
    glutMainLoop();
   
    return(0);
}

//Output Of the above program:-

OpenGl code to make self rotating Triangle

Related Programs:-

Zoom in and Zoom out Triangle using key

Rotate Triangle using Key

Draw text on the Screen

Sun Rise and Down

Simple Triangles

OpenGL code to rotate Triangle using Key

This source code is for making a simple triangle which can rotate by keyboard key.

OpenGL program:

#include<iostream>
#include<stdlib.h>

#ifdef __APPLE__
#include<openGL/openGL.h>
#include<GLUT/glut.h>
#else 
#include<GL/glut.h>
#endif

using namespace std;

double rotate_by_key=0;

void keyPress(int key,int x,int y)
{

    if(key==27)
            exit(0);
    if(key==GLUT_KEY_RIGHT)
        rotate_by_key+=5;
    if(key==GLUT_KEY_LEFT)
        rotate_by_key-=5;
    glutPostRedisplay();
   
}

void initRendering()
{
    glEnable(GL_DEPTH_TEST);
}


//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);
   
    glMatrixMode(GL_MODELVIEW);
   
    glLoadIdentity();
   
    glRotatef(  rotate_by_key,-1.0f, 1.5f, -5.0f );
    glBegin(GL_TRIANGLES);
   
        glVertex3f(-0.5f, 0.5f, -5.0f);
        glVertex3f(-1.0f, 1.5f, -5.0f);
        glVertex3f(-1.5f, 0.5f, -5.0f);

    glEnd();
   
    glutSwapBuffers();
}

int main(int argc,char** argv)
{
    glutInit(&argc,argv);
   
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
   
    glutInitWindowSize(400,400);
   
    glutCreateWindow("My first window");
   
    initRendering();
   
    glutDisplayFunc(drawScene);
    glutSpecialFunc(keyPress);

    glutReshapeFunc(handleResize);

    glutMainLoop();
   
    return(0);
}

//Output of the above program:-

OpenGL code to rotate Triangle using Key

Related Programs:-

Zoom in and Zoom out Triangle using key

Self rotating Triangle

Draw text on the Screen

Sun Rise and Down

Simple Triangles

OpenGL code to draw text on screen

This code is for displaying simple text on the screen. In this program,"This is my first text " will display on the screen when we run this code.

OpenGL program:-

#include <stdio.h>
#include <string.h>
#include <math.h>

#ifdef WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glut.h>

static int font_index=0;

void print_bitmap_string(/*void* font,*/ char* s)
{

      while (*s) {
         glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, *s);
         s++;
      }
  
}

void my_reshape(int w, int h)
{
   GLdouble size;
   GLdouble aspect;

   /* Use the whole window. */
   glViewport(0, 0, w, h);

   /* We are going to do some 2-D orthographic drawing. */
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   size = (GLdouble)((w >= h) ? w : h) / 2.0;
   if (w <= h) {
      aspect = (GLdouble)h/(GLdouble)w;
      glOrtho(-size, size, -size*aspect, size*aspect, -100000.0, 100000.0);
   }
   else {
      aspect = (GLdouble)w/(GLdouble)h;
      glOrtho(-size*aspect, size*aspect, -size, size, -100000.0, 100000.0);
   }

   /* Make the world and window coordinates coincide so that 1.0 in */
   /* model space equals one pixel in window space.                 */
   glScaled(aspect, aspect, 1.0);

   /* Now determine where to draw things. */
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

}


void draw_stuff()
{
   char* bitmap_font_names[7] = {"This is my firs text"};

   GLfloat x, y, ystep, yild, stroke_scale;

   /* Draw the strings, according to the current mode and font. */
   glTranslatef(0.5,-1.0,0);
   glColor4f(1.0, 1.0, 0.0, 0.0);
   x = -225.0;
   y = 70.0;
   ystep  = 100.0;
   yild   = 20.0;
      glRasterPos2f(-150, y+1.25*yild);
     print_bitmap_string(bitmap_font_names[0]);
}

void my_display(void)
{
   glClear(GL_COLOR_BUFFER_BIT);
   draw_stuff();

    glBegin(GL_POLYGON);
    glColor3f(1.0,0.5,1.0);
   
        glVertex3f(0.5f, 0.0f, -4.0f);
        glVertex3f(1.0f, 0.5f, -4.0f);
        glVertex3f(0.5f, 0.5f, -4.0f);
        
   glEnd();

   glutSwapBuffers();
}

int main(int argc, char **argv)
{

   glutInitWindowSize(500, 250);
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
   glutCreateWindow("GLUT fonts");
   glutDisplayFunc(my_display);
   glutReshapeFunc(my_reshape);
   glutMainLoop();

   return 0;
}

//Output of the above Program:-

OpenGL code to draw text on the screen

Related Programs:-

Sun Rise and Down

Simple Triangles

Simple Triangle and Bouncing Ball

Simple Cloud

Moving Circle

OpenGL code for sun rise and down

This source code is for displaying the moving sun and mountains. The sun will rise from one side and down in other side.

OpenGL program:-

#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(25, update, 0);

    glutMainLoop();
   
    return(0);
}

//Output of the above Program:-

OpenGL code for sun rise and down

Related Programs:-

Rotate Triangle using Key

Draw text on the Screen

Simple Triangles

Simple Triangle and Bouncing Ball

Simple Cloud

OpenGL code to make simple Triangles

This source code is for making a simple triangle and rectangle.

OpenGL program:-

#include<iostream>
#include<stdlib.h>

#ifdef __APPLE__
#include<openGL/openGL.h>
#include<GLUT/glut.h>
#else 
#include<GL/glut.h>
#endif

using namespace std;

void keyPress(unsigned char key,int x,int y)
{

    switch(key)
    {
        case 27:
            exit(0);
   
    }
}

void initRendering()
{
    glEnable(GL_DEPTH_TEST);
}

//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);
   
    glMatrixMode(GL_MODELVIEW);
   
    glLoadIdentity();
   
    glBegin(GL_TRIANGLES);
   
        glVertex3f(-0.5f, 0.5f, -5.0f);
        glVertex3f(-1.0f, 1.5f, -5.0f);
        glVertex3f(-1.5f, 0.5f, -5.0f);


        glVertex3f(0.5f, 0.5f, -5.0f);
        glVertex3f(1.0f, 1.5f, -5.0f);
        glVertex3f(1.5f, 0.5f, -5.0f);
   
    glEnd();

    glBegin(GL_QUADS);
        glVertex3f(0.5f, 0.0f, -4.0f);
        glVertex3f(0.5f, -0.5f, -4.0f);
        glVertex3f(1.5f, -0.5f, -4.0f);
        glVertex3f(1.5f, 0.0f, -4.0f);

    glutSwapBuffers();
}

int main(int argc,char** argv)
{
    glutInit(&argc,argv);
   
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
   
    glutInitWindowSize(400,400);
   
    glutCreateWindow("Triangle");
   
    initRendering();
   
    glutDisplayFunc(drawScene);
   
    //glutKeyBoardFunc(keyPress);
    glutKeyboardFunc(keyPress);

    glutReshapeFunc(handleResize);
   
    glutMainLoop();
   
    return(0);
}

//Output of the above Program:-

OpenGL code to make simple Triangles

Related Programs:-

Simple Triangle and Bouncing Ball

Simple Cloud

Moving Circle

Moving Car

Simple Fountain

OpenGL program to make simple Triangle and bouncing Ball

This source code is for making a simple triangle and bouncing ball.

OpenGL program:-

#if __APPLE__
#include<GLUT/glut.h>
#else
#include<GL/glut.h>
#endif

static int flag=0;
float tx=0.0,ty=0.0,tz=0.0;
float ball_x=0.5,ball_y=0.0,ball_z=0.0;
//placing the ball 0.5 unit right to origin

void glutInitRendering()
{
    glEnable(GL_DEPTH_TEST);
   
}

void reshaped(int w,int h)
{
    glViewport(0,0,w,h);
   
    glMatrixMode(GL_PROJECTION);
   
    glLoadIdentity();
   
    gluPerspective(45,0,1,200);
}

void updateBall()
{
    if(!flag)
    {
        ball_y+=0.05;
        if(ball_y>1.0)
            flag=1;
    }
    if(flag)
    {
        ball_y-=0.05;
        if(ball_y<-1)
            flag=0;
    }
}

void display()
{
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        
        glClearColor(0,0,1,0);
        
        glPushMatrix();
        
        glBegin(GL_POLYGON);
            glColor3f(1,0,0);
            
            glVertex3f(-0.25,0,-1);

            glVertex3f(0.25,0,-1);

            glVertex3f(0,0.25,-1);
        glEnd();
        glPopMatrix();
   

        glPushMatrix();
            glColor3f(0,1,1);
            glTranslatef(ball_x,ball_y,ball_z);
            glutSolidSphere(0.1,23,23);
        glPopMatrix();

        //update functin, will be called after every microsecond
        updateBall();


        glutSwapBuffers();
}

void keyPressed(int key,int x,int y){}
     
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
   
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
   
    glutInitWindowSize(400,500);
   
    glutCreateWindow("Bouncing Ball");
   
    glutInitRendering();
   
    glutDisplayFunc(display);
   
    glutIdleFunc(display);
   
    glutSpecialFunc(keyPressed);
   
    glutReshapeFunc(reshaped);
   
    glutMainLoop();
   
}

//Output of the above Program:-

OpenGL program to make simple triangle and bouncing ball

Related Programs:-

Simple Cloud

Moving Circle

Moving Car

Simple Fountain

Collision Detection between two Objects

OpenGL code for making simple Cloud

This source code is for making a simple cloud. The cloud will move from one side to another side.

OpenGL program:-

#include<GL/glut.h>

void initRendering()
{
    glEnable(GL_DEPTH_TEST);
}

void reshaped(int w,int h)
{
        glViewport(0,0,w,h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45,(double)w/(double)h,1,200);
}

void keyPressed(int k,int x,int y)
{
            if(k==GLUT_KEY_LEFT)
            {
            }
}

float x=0.0,y=0.0,x1=5.0;

void update()
{
    x+=0.01;
    x1-=0.02;
    if(x>6)
        {
            x=-6;
            x1=4;
    }
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   
    glMatrixMode(GL_MODELVIEW);
   
    glLoadIdentity();
    glPushMatrix();
    glTranslatef(x1,y,-5.0);
    glBegin(GL_POLYGON);
        
        glColor3f(1.0,0.0,0.5);
        
        glVertex3f(-1.0,1.0,-5.0);
        glVertex3f(0.0,2.0,-5.0);
        glVertex3f(-2.0,2.0,-5.0);
        glVertex3f(1.0,1.0,-5.0);
        
    glEnd();
    glPopMatrix();

    glPushMatrix();

    glTranslatef(x,y,-5.0);

    glBegin(GL_POLYGON);
        
        glColor3f(0.0,0.5,0.5);
        
        glVertex3f(1.0,0.7,-5.0);

        glVertex3f(1.5,1.0,-5.0);

        glVertex3f(0.7,1.5,-5.0);

        glVertex3f(0.0,2.0,-5.0);

        glVertex3f(-0.7,1.5,-5.0);

        glVertex3f(-1.4,1.6,-5.0);

        glVertex3f(-1.7,1.0,-5.0);

        glVertex3f(-1.5,0.7,-5.0);

        glVertex3f(-1.0,0.5,-5.0);
        
    glEnd();

    glPopMatrix();

    glBegin(GL_POLYGON);
        
        glColor3f(1.0,1.0,1.5);
        
        glVertex3f(-2.0,-2.0,-5.0);
        glVertex3f(-1.0,-1.5,-5.0);
        //glVertex3f(0.0,0.0,-5.0);
        glVertex3f(2.0,-2.0,-5.0);
        glVertex3f(1.2,-1.5,-5.0);
        
    glEnd();

    update();
   
    glutSwapBuffers();
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
   
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
   
    glutInitWindowSize(400,400);
   
    glutCreateWindow("Simple Cloud");
   
    initRendering();
   
    glutDisplayFunc(display);
   
    glutIdleFunc(display);
   
    glutReshapeFunc(reshaped);
   
    glutSpecialFunc(keyPressed);
        
    glutMainLoop();

return(0);
}

//Output of the above Program:-

OpenGL code for making simple Cloud

Related Programs:-

Moving Circle

Moving Car

Simple Fountain

Collision Detection between two Objects

All in one OpenGL programs

OpenGl code to make a moving Circle

This source code is for making a circle which can move from one side to another side.

OpenGL program:-

#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.3f;
float ballY = 0.0f;
float ballZ = -1.0f;
/*
float ballX2 = 0.3f;
float ballY2 = 0.0f;
float ballZ2 = -1.0f;
*/
static int flag=1;

void drawBall(void) {

        glColor3f(0.0, 1.0, 0.0); //set ball colour
        glTranslatef(ballX,ballY,ballZ); //moving it toward the screen a bit on creation
        glutSolidSphere (0.1, 10, 10); //create ball.

}

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);
}

//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);
   
    glMatrixMode(GL_MODELVIEW);
   
    glLoadIdentity();
    drawBall();
    //drawBall2();

    glutSwapBuffers();
}

//float _angle = 30.0f;
void update(int value) {
    if(flag)
    {
    ballX += 0.001f;
       if(ballX>0.3)
       {
           flag=0;

       }
    }
    if (!flag)
    {
        ballX -= 0.001f;
        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("Moving Circle");
   
    initRendering();
   
    glutDisplayFunc(drawScene);

    glutSpecialFunc(keyPress);
    glutReshapeFunc(handleResize);
    //autorot();

    glutTimerFunc(25, update, 0);

    glutMainLoop();
   
    return(0);
}

//Output of the above Program:-

OpenGl code to make a moving Circle

Related Programs:-

Simple Cloud

Moving Car

Simple Fountain

Collision Detection between two Objects

All in one OpenGL programs

OpenGL code for making a moving Car

This source code is for making a simple car which can move from one side to another side.

OpenGL program:-

#include <GL/glut.h>    // Header File For The GLUT Library 
#include <GL/gl.h>    // Header File For The OpenGL32 Library
#include <GL/glu.h>    // Header File For The GLu32 Library
//#include <unistd.h>     // Header File For sleeping.

/* ASCII code for the escape key. */
#define ESCAPE 27

/* The number of our GLUT window */
int window; 

/* rotation angle for the triangle. */
float rtri = 0.0f;

/* rotation angle for the quadrilateral. */
float rquad = 0.0f;

/* A general OpenGL initialization function.  Sets all of the initial parameters. */
// We call this right after our OpenGL window is created.
void InitGL(int Width, int Height)         
{
  // This Will Clear The Background Color To Black
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);     
  glClearDepth(1.0);                // Enables Clearing Of The Depth Buffer
  glDepthFunc(GL_LESS);                // The Type Of Depth Test To Do
  glEnable(GL_DEPTH_TEST);            // Enables Depth Testing
  glShadeModel(GL_SMOOTH);            // Enables Smooth Color Shading

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();                // Reset The Projection Matrix

  gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);  

  glMatrixMode(GL_MODELVIEW);
}

/* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */
void ReSizeGLScene(int Width, int Height)
{
  if (Height==0)                // Prevent A Divide By Zero If The Window Is Too Small
    Height=1;

  glViewport(0, 0, Width, Height);        // Reset The Current Viewport And Perspective Transformation

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
  glMatrixMode(GL_MODELVIEW);
}

float ballX = -0.5f;
float ballY = 0.0f;
float ballZ = 0.0f;

void drawBall(void) {
        glColor3f(0.0, 1.0, 0.0); //set ball colour
        glTranslatef(ballX,ballY,ballZ); //moving it toward the screen a bit on creation
        //glRotatef(ballX,ballX,ballY,ballZ);
        glutSolidSphere (0.3, 20, 20); //create ball.
        glTranslatef(ballX+1.5,ballY,ballZ); //moving it toward the screen a bit on creation
        glutSolidSphere (0.3, 20, 20); //
        }


/* The main drawing function. */
void DrawGLScene()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        // Clear The Screen And The Depth Buffer
  glLoadIdentity();                // Reset The View

  glTranslatef(rtri,0.0f,-6.0f);        // Move Left 1.5 Units And Into The Screen 6.0
   
  //glRotatef(rtri,1.0f,0.0f,0.0f);        // Rotate The Triangle On The Y axis
  // draw a triangle (in smooth coloring mode)
  glBegin(GL_POLYGON);                // start drawing a polygon
  glColor3f(1.0f,0.0f,0.0f);            // Set The Color To Red
  glVertex3f(-1.0f, 1.0f, 0.0f);        // Top left
  glVertex3f(0.4f, 1.0f, 0.0f);
  
  glVertex3f(1.0f, 0.4f, 0.0f);
  
  glColor3f(0.0f,1.0f,0.0f);            // Set The Color To Green
  glVertex3f( 1.0f,0.0f, 0.0f);        // Bottom Right
  glColor3f(0.0f,0.0f,1.0f);            // Set The Color To Blue
  glVertex3f(-1.0f,0.0f, 0.0f);// Bottom Left    

  //glVertex3f();
  glEnd();                    // we're done with the polygon (smooth color interpolation)
  drawBall();
 
  rtri+=0.005f;                    // Increase The Rotation Variable For The Triangle
  if(rtri>2)
      rtri=-2.0f;
  rquad-=15.0f;                    // Decrease The Rotation Variable For The Quad

  // swap the buffers to display, since double buffering is used.
  glutSwapBuffers();
}

/* The function called whenever a key is pressed. */
void keyPressed(unsigned char key, int x, int y) 
{
    /* sleep to avoid thrashing this procedure */
   // usleep(100);

    /* If escape is pressed, kill everything. */
    if (key == ESCAPE) 
    { 
    /* shut down our window */
    glutDestroyWindow(window);
   
    /* exit the program...normal termination. */
    exit(0);                  
    }
}

int main(int argc, char **argv) 
{  
  glutInit(&argc, argv);  

  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);  

  /* get a 640 x 480 window */
  glutInitWindowSize(640, 480);  

  /* the window starts at the upper left corner of the screen */
  glutInitWindowPosition(0, 0);  

  /* Open a window */  
  window = glutCreateWindow("Moving Car");  

  /* Register the function to do all our OpenGL drawing. */
  glutDisplayFunc(&DrawGLScene);  

  /* Go fullscreen.  This is as soon as possible. */
  //glutFullScreen();

  /* Even if there are no events, redraw our gl scene. */
  glutIdleFunc(&DrawGLScene);

  /* Register the function called when our window is resized. */
  glutReshapeFunc(&ReSizeGLScene);

  /* Register the function called when the keyboard is pressed. */
  glutKeyboardFunc(&keyPressed);

  /* Initialize our window. */
  InitGL(640, 480);
  
  /* Start Event Processing Engine */  
  glutMainLoop();  

  return 1;
}

//Output of the above program:-

OpenGL code for making moving Car

Related Programs:-

Simple Cloud

Moving Circle

Simple Fountain

Collision Detection between two Objects

All in one OpenGL programs

OpenGL code for making a simple Fountain

This source code is for making a simple fountain. In this program, we made three balls and they disperse from one point after every few time.

OpenGL program:-

#ifdef __APPLE__
#include<GLUT/glut.h>
#include<openGL/opengl.h>

#else
#include<GL/glut.h>
#endif

float x=1.0,y=1.0;
float x1=0.0,y1=0.0;
float x2=0.03,y2=0.0;
float x3=-0.03,y3=0.0;

void initRendering()
{
    glEnable(GL_DEPTH_TEST);
}

void reshape(int w,int h)
{
    glViewport(0,0,w,h);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluPerspective(45,w/h,1,200);
}

void keyPressed(int k,int xX,int yY)
{
    if(k==GLUT_KEY_UP)
    {
        x+=0.05;
        y+=0.05;
    }
    if(k==GLUT_KEY_DOWN)
    {
        x-=0.05;
        y-=0.05;
    }
    glutPostRedisplay();
}

void update()
{
    x1+=0.005;
    y1+=0.005;
    if(x1>3.5)
    {
        x1=0.0;
        y1=0.0;
    }

    x2-=0.005;
    y2+=0.010;
    if(x2<-2.5)
    {
        x2=0.0;
        y2=0.0;
    }

    //x3+=0.005;
    y3+=0.005;
    if(y3>3.0)
    {
        //x1=0.0;
        y3=0.0;
    }
}

void makeBubbles()
{
    glColor3f(0.3,0.3,1.3);

    glPushMatrix();

    glTranslatef(x1,y1,-5.0);

    glutSolidSphere(0.1,20,20);

    glPopMatrix();

    glColor3f(1.,0.3,1.3);

    glPushMatrix();

    glTranslatef(x2+0.03,y2,-5.0);

    glutSolidSphere(0.1,20,20);

    glPopMatrix();

    glColor3f(1.0,1.0,0.0);

    glPushMatrix();

    glTranslatef(x3-0.03,y3,-5.0);

    glutSolidSphere(0.1,20,20);

    glPopMatrix();

    update();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    glPushMatrix();
   
    glTranslatef(1.1,1.1,-5.0);
    glScalef(x,y,1.0);
   
    glBegin(GL_TRIANGLES);
        
        glColor3f(0.5,0.5,0.5);

        glVertex3f(-1.0,0.0,0.0);
        glVertex3f(1.0,0.0,0.0);
        glVertex3f(0.0,1.0,0.0);

    glEnd();

    glPopMatrix();

    makeBubbles();

    glutSwapBuffers();
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);

    glutInitWindowSize(400,400);

    glutCreateWindow("Fountain Using OpenGL");

    initRendering();

    glutDisplayFunc(display);

    glutIdleFunc(display);
   
    glutReshapeFunc(reshape);

    glutSpecialFunc(keyPressed);

    glutMainLoop();

    return(0);
}

// Output of the above program:-

OpenGL code For making simple Fountain

Related Programs:-

Zoom in and Zoom out Triangle using key

Self rotating Triangle

Rotate Triangle using Key

Draw text on the Screen

Sun Rise and Down
Back to Top