问题描述
大家好,
这些天,我正在阅读OpenGL SuperBible.第2章中有一个有关OpenGL动画的程序.以下是源代码.对于此示例,我认为窗口的大小是800 * 600,视口区域也是800 * 600,裁剪区域可能是(100 * 4/3)* 100.在代码中,windowWidth是剪切宽度,windowHeight是剪切高度.但是,当我们检查矩形是否达到4条边时,我感到困惑,我们只是使用windowWidth或windowHeight来检查而不是实际的窗口宽度和高度.但是从运行的结果来看,我发现矩形在达到实际窗口宽度或高度时会改变方向,而不是达到剪切宽度或高度.
谁能告诉我它是如何工作的?顺便说一句,我是OpenGL的新手.请给我一些有关如何学习它的建议.
提前谢谢!
Hi all,
I am reading OpenGL SuperBible these days. There is a program about Animation with OpenGL in CHAPTER 2. Following is sorce code. For this example, I think the size of window is 800 * 600, viewport region is also 800 * 600, and clipping region could be (100*4/3)*100. In the code, windowWidth is clipping width, windowHeight is clipping height. But I am confused about when we check whether rectangle reach 4 edges, we just use windowWidth or windowHeight to check instead of the real window width and height. But from the result of running, I found when rectangle reach the real window width or height, it change direction rather than reach clipping width or height.
Could anyone tell me how it works? BTW, I am a novice about OpenGL. Plese give me some suggestions about how to learn it.
Thanks in advance!
#include <glut.h>
// Initial square position and size
GLfloat x1 = 0.0f;
GLfloat y1 = 0.0f;
GLfloat rsize = 25;
//Step size in x and y directions
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;
// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;
void RenderScene()
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
// Set current drawing color to red
glColor3f(1.0f, 0.0f, 0.0f);
// Draw a filled rectangle with current color
glRectf(x1, y1, x1 + rsize, y1 - 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 (x1 > windowWidth - rsize || x1 < -windowWidth)
{
xstep = -xstep;
}
// Reverse direction when you reach top or bottom edge
if (y1 > windowHeight || y1 < -windowHeight + rsize)
{
ystep = -ystep;
}
x1 += xstep;
y1 += 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(x1 > (windowWidth -rsize + xstep))
x1 = windowWidth - rsize - 1;
else if(x1 < -(windowWidth + xstep))
x1 = -windowWidth -1;
if(y1 > (windowHeight + ystep))
y1 = windowHeight-1;
else if(y1 < -(windowHeight - rsize + ystep))
y1 = -windowHeight + rsize - 1;
// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(33, TimerFunction, 1);
}
void SetupRC()
{
// This function sets the color used for clearing the window.
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;
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();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Bounce");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutTimerFunc(33, TimerFunction, 1);
SetupRC();
glutMainLoop();
return 0;
}
推荐答案
这篇关于使用OpenGL示例在动画中剪切区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!