第三个例子是第二章的最后一个例子--bounce
代码如下:
// 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
if(x >
windowWidth-rsize ||
x < -windowWidth)
xstep = -xstep;
// Reverse direction when you reach top or bottom edge
if(y >
windowHeight || y < -windowHeight +
rsize)
ystep = -ystep;
// Actually move the square
x += xstep;
y += ystep;
// Check bounds. This is in case the window is made
// smaller while the rectangle is bouncing and the
// rectangle suddenly finds itself outside the new
// clipping volume
if(x > (windowWidth-rsize +
xstep))
x = windowWidth-rsize-1;
else
if(x < -(windowWidth +
xstep))
x = -windowWidth -1;
if(y > (windowHeight +
ystep))
y = windowHeight-1;
else
if(y < -(windowHeight -
rsize + ystep))
y = -windowHeight +
rsize - 1;
// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(33,TimerFunction, 1);
}
///////////////////////////////////////////////////////////
// Setup the rendering state
void SetupRC(void)
{
// Set clear color to blue
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
///////////////////////////////////////////////////////////
// Called by GLUT library when the window has chanaged size
void ChangeSize(int w,
int h)
{
GLfloat aspectRatio;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
aspectRatio = (GLfloat)w / (GLfloat)h;
if (w <= h)
{
windowWidth = 100;
windowHeight = 100 / aspectRatio;
glOrtho (-100.0, 100.0, -windowHeight,
windowHeight, 1.0, -1.0);
}
else
{
windowWidth = 100 * aspectRatio;
windowHeight = 100;
glOrtho (-windowWidth,
windowWidth, -100.0, 100.0, 1.0, -1.0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
///////////////////////////////////////////////////////////
// Main program entry point
int main(int argc,
char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE
| GLUT_RGBA);
glutInitWindowSize(800,600);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(33,
TimerFunction, 1);
SetupRC();
glutMainLoop();
return 0;
}
我们不再分析在前两个例子中出现的函数。
第一个地方是如何使用timer,使用的函数glutTimerFunc(33,TimerFunction, 1);,这里第一个参数是毫秒,第二个是timer触发的函数,第三个是传入的参数,注意这个timer每次都只触发一次。
第二个地方是glutPostRedisplay();,它所作的就是强制刷新界面。
第三个地方就是在对显示模式初始化的时候的参数GLUT_DOUBLE,它表示使用双缓存模式,和GLUT_SINGLE不同的是,双缓存方式调用glutSwapBuffers来把还没有刷新的命令效果刷新到,后者使用
glutSwapBuffers
做这个事情。
分享到:
相关推荐
opengl 超级宝典 第五版 源代码 opengl 超级宝典 第五版 源代码 opengl 超级宝典 第五版 源代码
"OpenGL超级宝典"是一本经典的教程书籍,旨在帮助开发者深入理解和掌握OpenGL技术。这本书的示例代码是你学习OpenGL的基础,通过实际操作和运行代码,你可以更好地理解各种图形渲染原理和技巧。 在压缩包中,我们...
《OpenGL超级宝典(第4版)》开篇详细讲解OpenGL图形编程的核心技术,覆盖了从空间中进行绘制到几何变换,从光照到纹理贴图等内容。书中讲解了新的OpenGL功能,包括OpenGL 2.1的强大可编程管线、定点和片段着色和...
openGL超级宝典(第四版)及代码 电子书
opengl 超级宝典 第二版的 源代码
OpenGL超级宝典第五版的源码。没错是源码啊啊啊啊啊啊啊。
经典OpenGL图形学入门书籍《OpenGL超级宝典英文第五版》及所附完整源码。物有所指!
### OpenGL超级宝典第六版(英文版)OpenGL_SuperBible_6th_Edition #### 知识点一:OpenGL的定义与作用 - **定义**:OpenGL是一种用于渲染二维、三维矢量图形的跨语言、跨平台的应用程序编程接口(API)。它是...
在“OpenGL超级宝典 笔记21 相关代码”中,我们可能看到以下步骤: 1. **创建缓冲区对象**:首先,使用glGenBuffers生成一个或多个缓冲区对象的ID。 2. **绑定缓冲区**:然后,选择GL_UNIFORM_BUFFER作为目标,并...
根据提供的文件信息,“OpenGL超级宝典,英文第五版”是一本深入介绍OpenGL技术的书籍,由Richard S. Wright Jr.、Nicholas Haemel、Graham Sellers和Benjamin Lipchak等人编写。这本书旨在为读者提供一个全面的教程...
这个资源在超级宝典源码的的基础上添加了一些中文注释,方便理解每一步的目的与作用
本资源是以压缩包的形式的, 里面是一个 “TXT”的文档, 文档中 有“百度云” 分享的链接, 这本书太大,...原作名: OpenGL SuperBible: Comprehensive Tutorial and Reference (5th Edition) 译者: 付飞 / 李艳辉
《openGL超级宝典(第四版)及代码》(OpenGL SuperBible (4th Edition))
《openGL超级宝典(第四版)及代码》 Comprehensive Tutorial and Reference Richard S. Wright, Jr. Benjamin Lipchak Nicholas Haemel
OpenGL超级宝典第四版(中文版)是一本深入学习OpenGL编程技术的重要参考资料,尤其适合对图形编程感兴趣的初学者和专业人士。OpenGL是一个开放源代码的跨平台图形库,用于在各种操作系统上创建2D和3D图像。随着技术...
OpenGL超级宝典第五版源代码及开发库是一个珍贵的学习资源,包含了OpenGL编程的详细示例和作者自编的开发库,适用于Linux和Windows操作系统。这个压缩包特别关注了在Windows环境下使用Visual Studio 2013进行配置,...
总之,OpenGL超级宝典第四版的源代码和库文件是学习OpenGL不可或缺的资源,它们为理论学习提供了实践基础,让你能够亲手构建出令人惊叹的图形应用程序。在学习过程中,结合书中的解释和示例代码,一步步深入理解并...
openGL超级宝典代码.rar OpenGL™ 是行业领域中最为广泛接纳的 2D/3D 图形 API, 其自诞生至今已催生了各种计算机平台及设备上的数千优秀应用程序。OpenGL™ 是独立于视窗操作系统或其它操作系统的,亦是网络透明的。...
OpenGL超级宝典的随书光盘,里面所有例子的代码