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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值