#include "stdafx.h"
#include <glut.h>
void display(void);
void myreshape(GLsizei w, GLsizei h);//窗口改变函数
void mykeyboard(unsigned char key, int x, int y);
float x1=100,y1=100,x2=300,y2=300;
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
char *argv[] = {"hello ", " "};
int argc = 2; // must/should match the number of strings in argv
glutInit(&argc, argv); //初始化GLUT库;
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //设置显示模式;(缓冲,颜色类型)
glutInitWindowSize(800, 800);
glutInitWindowPosition(1024 / 2 - 250, 768 / 2 - 250);
glutCreateWindow("Hello"); //创建窗口,标题为“hello”;
glutDisplayFunc(display); //用于绘制当前窗口;
glutKeyboardFunc(mykeyboard);
glutReshapeFunc(myreshape);
glutMainLoop(); //表示开始运行程序,用于程序的结尾;
return 0;
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT); //刷新颜色缓冲区
glColor3f(1,1,1);
glRectf(x1,y1,x2,y2);
glColor3f(0,0,0);
glRectf(x1+10,y1+10,x2-10,y2-10);
glColor3f(1,1,1);//眼睛
glRectf(x1+25,y1+125,x2-125,y2-25);
glColor3f(0,0,0);
glRectf(x1+30,y1+130,x2-130,y2-30);
glColor3f(1,1,1);
glRectf(x1+125,y1+125,x2-25,y2-25);
glColor3f(0,0,0);
glRectf(x1+130,y1+130,x2-30,y2-30);
glColor3f(1,1,1);//鼻子
glRectf(x1+90,y1+90,x2-90,y2-90);
glColor3f(0,0,0);
glRectf(x1+95,y1+95,x2-95,y2-95);
glColor3f(1,1,1);//嘴巴
glRectf(x1+50,y1+25,x2-50,y2-150);
glColor3f(0,0,0);
glRectf(x1+55,y1+30,x2-55,y2-155);
glColor3f(1,0,0);
glRasterPos2i(x1+40,y1+150); //定位当前光标
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'L'); //写字符"l"
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'i'); //写字符"i"
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'a'); //写字符"a"
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'n'); //写字符"n"
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'g');
glRasterPos2i(x1+150,y1+150);
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'S');
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'e');
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'n');
glRasterPos2i(x1+100,y1+35);
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'Z');
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'h');
glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'i');
glFlush(); //用于刷新命令队列和缓冲区,使所有尚未被执行的OpenGL命令得到执行
}
void mykeyboard(unsigned char key, int x, int y)
{
switch(key)
{ case 'W':
case 'w':// 矩形对角坐标变量修改使得矩形上移
y1+=10; y2+=10;
break;
case 'S':
case 's'://矩形对角坐标变量修改使得矩形下移
y1-=10;y2-=10;
break;
case 'A':
case 'a'://矩形对角坐标变量修改使得矩形左移
x1-=10; x2-=10;
break;
case 'D':
case 'd'://矩形对角坐标变量修改使得矩形右移
x1+=10; x2+=10;
break;
}
glutPostRedisplay();//重画函数
}
void myreshape(GLsizei w, GLsizei h)
{
glViewport(0,0,w,h); //设置视区位置
glMatrixMode(GL_PROJECTION);//设置投影变换模式
glLoadIdentity(); //调单位矩阵,清空当前矩阵堆栈
gluOrtho2D(0,400,0,400); //左右下上
}
评论0