Pages

Friday, 14 June 2013

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

No comments:

Post a Comment

Back to Top