Hafte201 0 Newbie Poster
#include<GL/glut.h>
#include<Math.h>
#include<iostream>
using namespace std;
int direction=0;
void drawRectangle(GLfloat x1,GLfloat y1,GLfloat x2,GLfloat y2)
{
    glBegin(GL_LINES);
    glVertex2f(x1,y1);
    glVertex2f(x1,y2);
    glVertex2f(x1,y1);
    glVertex2f(x2,y1);
    glVertex2f(x2,y1);
    glVertex2f(x2,y2);
    glVertex2f(x1,y2);
    glVertex2f(x2,y2);
    glEnd();
}
void drawQuadrants()
{
    glLoadIdentity();
    glBegin(GL_LINES);
    glVertex2f(0.0,-1.0);
    glVertex2f(0.0,1.0);
    glVertex2f(-1.0,0.0);
    glVertex2f(1.0,0.0);
    glEnd();
}
void display()
{
    glPushMatrix();
    glLoadIdentity();
    glClearColor(0.0f,0.0f,0.0f,0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glDisable(GL_DEPTH_TEST);
    glColor3f(1.0f,1.0f,1.0f);
    drawQuadrants();
    glPopMatrix();
    if(direction==1)
    {
    glTranslatef(0.2*(1.0-cos(-1.0))+0.3*sin(-1.0),0.3*(1.0-cos(-1.0))-0.2*sin(-1.0),0.0f);
    glRotatef(-1,0.2,0.3,1);
    cout<<0.3*(1.0-cos(-1.0))-0.2*sin(-1.0)<<"\n";
    }
    else if(direction==2)
    {
    glTranslatef(0.2*(1.0-cos(1.0))+0.3*sin(1.0),0.3*(1.0-cos(1))-0.2*sin(1.0),0.0f);
    glRotatef(1,0.2,0.3,1);
    cout<<0.3*(1.0-cos(1))-0.2*sin(1.0)<<"\n";
    }
    drawRectangle(0.2,0.3,0.3,0.4);
    direction=0;
    glFlush();
}
void nkeyboard(unsigned char key,int x,int y)
{
    if(key==27)
        exit(0);
    else if(key=='l'|| key=='L')
        direction=1;
    else if(key=='r' || key=='R')
        direction=2;
    else
        direction=0;
    glutPostRedisplay();
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(900,900);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Rotation animation");
    glutDisplayFunc(display);
    glutKeyboardFunc(nkeyboard);
    glutMainLoop();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.