【OpenGL学习代码笔记之一】 基本形状绘制及动画初窥

OpenGL的初步学习,具体涉及窗口初始化及创建、基本图形绘制以及简单动画效果

代码如下:

  1 #include<glut.h>
2
3 // initial square position and size
4 GLfloat x1 = 100.0f;
5 GLfloat y1 = 150.0f;
6 GLsizei rsize = 50;
7
8 // step size in x and y directions
9 // (number of pixels to move each time)
10 GLfloat xstep = 1.0f;
11 GLfloat ystep = 1.0f;
12
13 // keep track of windows changing width and height
14 GLfloat windowWidth;
15 GLfloat windowHeight;
16
17 // called to draw scene
18 void RenderScene(void)
19 {
20 // clear the window with current clearing color
21 glClear(GL_COLOR_BUFFER_BIT);
22
23 // set current drawing color to red
24 glColor3f(1.0f, 0.0f, 0.0f);
25
26 // draw a filled rectangle with current color
27 glRectf(x1,y1,x1+rsize,y1+rsize);
28
29 // flush drawing commands and swap
30 glutSwapBuffers();
31
32 }
33
34 // called by GLUT library when idle (window not being)
35 // resized or moved)
36 void TimerFunction(int value)
37 {
38 // reverse direction when you reach left or right edge
39 if(x1 > windowWidth - rsize || x1 < 0)
40 xstep = -xstep;
41
42 // reverse direction when you reach top or bottom edge
43 if(y1 > windowHeight - rsize || y1 < 0)
44 ystep = -ystep;
45
46 // check bounds. This is in case the window is made
47 // smaller and rectangle is outside the new clipping volume
48 if(x1 > windowWidth - rsize)
49 x1 = windowWidth - rsize -1;
50
51 if(y1 > windowHeight - rsize)
52 y1 = windowHeight - rsize -1;
53
54 // actually move the square
55 x1 += xstep;
56 y1 += ystep;
57
58 // redraw the scene with new coordinates
59 glutPostRedisplay(); // 强制刷新画面
60 glutTimerFunc(33,TimerFunction,1); // 以递归方式更新rectangular的坐标,充当线程
61 }
62
63 // setup the rendering state
64 void SetupRC(void)
65 {
66 glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
67 }
68
69 // Called by GLUT library when the window has changed size
70 void ChangeSize(GLsizei w, GLsizei h)
71 {
72 if(h == 0)
73 h = 1;
74
75 glViewport(0,0,w,h); // 设置图形显示在屏幕上的方式(设置视见区)
76 glMatrixMode(GL_PROJECTION); // 正射投影
77 glLoadIdentity(); // 复位坐标系!
78
79 // keep the square square;this time, save calculated
80 // width and height for later use
81 if(w <= h)
82 {
83 windowHeight = 250.0f*h/w;
84 windowWidth = 250.0f;
85 }
86 else
87 {
88 windowWidth = 250.0f*w/h;
89 windowHeight = 250.0f;
90 }
91
92 // set the clipping volume
93 glOrtho(0.0f,windowWidth,0.0f, windowHeight,1.0f,-1.0f);
94 glMatrixMode(GL_MODELVIEW);
95 glLoadIdentity();
96 }
97
98 void main(void)
99 {
100 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
101 glutCreateWindow("Bone !!");
102 glutDisplayFunc(RenderScene);
103 glutReshapeFunc(ChangeSize);
104 glutTimerFunc(33,TimerFunction,1);
105
106 SetupRC();
107 glutMainLoop();
108 }

最终结果如图:

    

 

强制刷新屏幕显示: glutPostRedisplay();

代码中用到了双缓冲技术,即图形的绘制在屏幕外的缓冲区内进行渲染,完成渲染后再显示。这样可以获得更好的显示效果。

双缓冲体现代码中,glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 注意到参数为GLUT_DOUBLE

并在需要重绘屏幕的地方加上 glutSwapBuffers(); 即表示将缓冲区内容换进屏幕显示

 

{

单缓冲的设置:

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB));

    // 对应执行重绘的方法:

    glFlush();

}

 

值得一提的是,glOrtho()用于创建一个正交平行的视景体,即截取物体。glViewport()则规定视见区,将截取的物体显示在设定的屏幕范围中。

可参考这篇博文 http://www.cnblogs.com/yxnchinahlj/archive/2010/10/30/1865298.html



posted @ 2012-03-01 14:34  浮沉之主  阅读(581)  评论(0编辑  收藏  举报