4)右击项目名下的源文件,选择添加(ADD),再选择新建项 (New Item),在弹出来的对话框中选择C++ File(.cpp),输入文件名test,然后点击添加(ADD),即新建源文件。
5)在源文件test.cpp中输入如下代码:
#include
float x1 = -0.25,y1 = -0.25;// float width = 0.5,height = 0.5; float xMin = -1,xMax = 1;
float yMin = -1,yMax = 1;
float widthWindows = 400,heightWindows=400; float colorR = 0.5,colorG = 0.5,colorB = 0.5; void display(void) {
glClearColor(0.0f,0.0f,0.0f,0.0f); glClearDepth(1.0); glClear (GL_COLOR_BUFFER_BIT); //绘制一个矩形,z轴位置为-1.0f glColor3f (colorR, colorG, colorB); glRectf(x1,y1,x1+width,y1+height); glFlush (); }
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity ();
/* 规定二维视景区域,参数分别为left,right,bottom,top */ glOrtho(xMin, xMax, yMin, yMax,0.0, 10.0); }
void MyMouse(int button,int state,int x,int y) { if(state == GLUT_DOWN) { switch(button) { case GLUT_LEFT_BUTTON: colorR += 0.1; if(colorR > 1.0) colorR = 0.0; glutPostRedisplay(); break; case GLUT_MIDDLE_BUTTON: colorG += 0.1; if(colorG > 1.0) colorG = 0.0; glutPostRedisplay(); break; case GLUT_RIGHT_BUTTON: colorB += 0.1; if(colorB > 1.0) colorB = 0.0; glutPostRedisplay(); break; } } }
void MyMotion(int x, int y) { x1 = (float)x / widthWindows *(xMax - xMin) + xMin - width / 2; y1 = (float)(heightWindows - y) / heightWindows *(yMax - yMin) + yMin - width / 2; glutPostRedisplay(); }
void MyKeyboard(unsigned char key, int x, int y) {
switch(key) { case 'W': case 'w':// 矩形坐标变量修改使得矩形上移 y1 += 0.1; if(y1 >= yMax - height) y1 = yMax - height; glutPostRedisplay(); break; case 'S': case 's': // 矩形坐标变量修改使得矩形下移 y1-= 0.1; if(y1 <= yMin) y1 = yMin; glutPostRedisplay(); break; case 'A': case 'a': // 矩形坐标变量修改使得矩形左移 x1 -= 0.1; if(x1 <= xMin) x1 = xMin; glutPostRedisplay(); break; case 'D': case 'd': // 矩形坐标变量修改使得矩形右移 x1+= 0.1; if(x1 >= xMax - width) x1 = xMax - width; glutPostRedisplay(); break; } }
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE); glutInitWindowSize (400, 400); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]);
glutDisplayFunc(display); glutReshapeFunc(reshape);
glutKeyboardFunc(MyKeyboard); glutMouseFunc(MyMouse); glutMotionFunc(MyMotion); glutMainLoop(); return 0; }
2.实现一个使用双缓存绘制的旋转正方形。
实验步骤1)-4)同前一个程序的设置一样。 5)在源文件test.cpp中输入如下代码:
#include
static GLfloat spin = 0.0;
void display(void) {
glClear(GL_COLOR_BUFFER_BIT); glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0); glColor3f(1.0, 1.0, 1.0);
glRectf(-25.0, -25.0, 25.0, 25.0); glPopMatrix();
glutSwapBuffers(); }
void spinDisplay(void) {
spin = spin + 2.0; if (spin > 360.0)
spin = spin - 360.0; glutPostRedisplay(); }
void init(void) {
glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); }
void reshape(int w, int h) {
glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }
void mouse(int button, int state, int x, int y) {
switch (button) {
case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay); break;
case GLUT_MIDDLE_BUTTON: case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) glutIdleFunc(NULL); break; default: break; } }
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init ();
glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMainLoop(); return 0; }
3.实现一个反弹方块动画。
实验步骤1)-4)同前一个程序的设置一样。 5)在源文件test.cpp中输入如下代码:
#include
// Initial square position and size GLfloat x = 0.0f; GLfloat y = 0.0f; GLfloat rsize = 25;
// Step size in x and y directions
// (number of pixels to move each time) GLfloat xstep = 1.0f; GLfloat ystep = 1.0f;
// Keep track of windows changing width and height GLfloat windowWidth; GLfloat windowHeight;
/////////////////////////////////////////////////////////// // Called to draw scene void RenderScene(void) { // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT); // Set current drawing color to red // R G B glColor3f(1.0f, 0.0f, 0.0f); // Draw a filled rectangle with current color glRectf(x, y, x + rsize, y - rsize); // Flush drawing commands and swap glutSwapBuffers(); }
/////////////////////////////////////////////////////////// // Called by GLUT library when idle (window not being // resized or moved)
void TimerFunction(int value) { // Reverse direction when you reach left or right edge