coder

#include <iostream>
#include <GL/glut.h>
using std::cout;
using std::endl;
float windowWidth=0;
float windowHeight=0;
float curX=0.0;
float curY=0.0;
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,1.0,1.0);

    glBegin(GL_POLYGON);
    glVertex3f(curX,curY,0.0);
    glVertex3f(curX+50,curY,0.0);
    glVertex3f(curX+50,curY+50,0.0);
    glVertex3f(curX,curY+50,0.0);
    glEnd();

//    glutSwapBuffers();
    glFlush();
}
void reshape(int w,int h)
{
    windowWidth=(float)w;
    windowHeight=(float)h;
    cout<<"cur width is :"<<w<<endl;
    cout<<"cur height is :"<<h<<endl;
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,(float)w,0.0,(float)h,-1.0,1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
    cout<<"you pressed the key is :"<<key<<endl;
    switch(key)
    {
    case 'w':
        curY+=20;
        break;
    case 's':
        curY-=20;
        break;
    case 'a':
        curX-=20;
        break;
    case 'd':
        curX+=20;
        break;
    default:
        break;
    }
    if(key=='q'||key=='Q')
        exit(0);
    glutPostRedisplay();
}
void motion(int x, int y)
{
    cout<<"mouse position :"<< "("<<x<<","<<y<<")"<<endl;
    curX=(float)x;
    curY=(float)(windowHeight-y);
    glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
    cout<<"come here yet"<<endl;
    switch(button)
    {
    case GLUT_LEFT_BUTTON:
        if(state==GLUT_DOWN)
            glutFullScreen();
        break;
    case GLUT_RIGHT_BUTTON:
        if(state==GLUT_DOWN)
        {
            glutInitWindowSize(800,600);
            glutInitWindowPosition(0.0,0.0);
        }
        break;
    default:
        break;
    }
    glutPostRedisplay();
}
void idler()
{
    cout<<"i am a idle"<<endl;
}
void init()
{
    glClearColor(0.0,0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-300.0,300.0,-300.0,300.0,-1.0,1.0);
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(250,250);
    glutInitWindowPosition(0.0,0.0);
    glutCreateWindow("hello,gl Master");
//    glutFullScreen();
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    //  glutIdleFunc(idler);
    glutMainLoop();
    return 0;
}

posted @ 2015-07-26 20:51  Arikes  阅读(552)  评论(0编辑  收藏  举报