Tuesday, July 5, 2011

Creating Movement Using Keyboard Function

// glut.h include codes goes here

float angle=0.0, ratio;
float eyeY=1.5f;
float r=1,g=1,b=1;
bool movetoggle=false, buttondown=false;
int w, h;

//define function declaration
void display();
void viewobject();
void reshape(int,int);
void keypress(unsigned char,int,int);

void display()
{
    glClearColor(0,0,0,1); //set output to alpha color
    glClear(GL_COLOR_BUFFER_BIT); //set type of display color
    viewobject(); // call object gluLookAt thru viewobject() function
    glTranslatef(0,0,-2);
    glColor3f(r,g,b);   
    glutWireCube(2);

    glutSwapBuffers();
    glLoadIdentity();

}

void viewobject()
{
    gluLookAt(0,eyeY,5,0,0,0,0,1,0);
}

void reshape(int w1, int h1)
{
    if(h1 == 0) h1=1;

    w=w1; h=h1;
    ratio=1.0f * w/h ;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0,0,w,h);
    gluPerspective(45,ratio,0.1,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void keypress(unsigned char key, int x, int y)
{
    int mod;

    mod=glutGetModifiers();
    if (key == 27) {exit(0); }
    if (key == 13)
    {
        if((mod==GLUT_ACTIVE_CTRL) && !movetoggle)
            movetoggle=true;
        else
            movetoggle=false;
    }
    glutPostRedisplay();
}

void keyspecial(int key, int x, int y)
{
    if(key==GLUT_KEY_UP)
    {
        if(movetoggle)
        {
            eyeY+=0.1;
        }
    }
    if(key==GLUT_KEY_DOWN)
    {
        if(movetoggle)
        {
            eyeY-=0.1;
        }
    }
    if(key==GLUT_KEY_F1)
    {
        r=1.0f,g=0.0f,b=0.0f;
    }
    if(key==GLUT_KEY_F2)
    {
        r=01.0f,g=1.0f,b=0.0f;
    }
    if(key==GLUT_KEY_F3)
    {
        r=0.0f,g=0.0f,b=1.0f;
    }
    glutPostRedisplay();
    /* switch(key){
    case GLUT_KEY_F1 : red = 1.0; green = 0.0; blue = 0.0; break;
    case GLUT_KEY_F2 : red = 0.0; green = 1.0; blue = 0.0; break;
    case GLUT_KEY_F3 : red = 0.0; green = 0.0; blue = 1.0; break;
    }*/
}

void mouseclick(int button, int state, int x, int y)
{
    if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN && movetoggle)
        buttondown=true;
    else
        buttondown=false;
}

void mousemove(int x, int y)
{
    int mid=h/2;
    if(buttondown)
    {
        if(y>mid){
            eyeY-=0.1f;}
        else{
            eyeY+=0.1f;
        }
    }
    glutPostRedisplay();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("Exercise 2");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keypress);
    glutSpecialFunc(keyspecial);
    glutMouseFunc(mouseclick);
    glutMotionFunc(mousemove);

    glutMainLoop();
    return 0;
}

No comments:

Post a Comment

Question 1: Introduction to Variable

Based on code below, create a dynamic program that solve problem below: Source Code: #include <iostream> using na...