使用OpenGL示例在动画中剪切区域 [英] Clipping Region in Animation with OpenGL Example

查看:71
本文介绍了使用OpenGL示例在动画中剪切区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
这些天,我正在阅读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;
}

推荐答案


我发现当矩形达到实际窗口的宽度或高度时,它会改变方向,而不是达到剪切的宽度或高度.


简单来说,在3D空间中绘制的对象将被映射到窗口区域(视口).因此,对象绘制不取决于窗口大小.裁剪区域(glOrtho)确定要显示的3D空间的区域.在此裁剪区域中绘制的所有对象都将根据视口映射到窗口.

假设Width为800,Height为600,则glOrtho将像这样
glOrtho(-133,133,100,100).在此Clipping区域中绘制的对象(在x方向-133到133上,在Y方向100到100上)被映射到窗口区域800、600.因此,对象绘制不依赖于窗口区域.

http://www.glprogramming.com/red/chapter03.html
请在上面的链接中检查正交投影和视口变换.
解释了3D空间与窗口区域之间的关系.

I found when rectangle reach the real window width or height, it change direction rather than reach clipping width or height.


In simple, the objects drawn in 3D space will be mapped to window area( viewport ). Therefore object drawing is not depend on the window size. The clipping region( glOrtho) determines the region of 3D space to be displayed. All objects drawn in this clipping region will be mapped to window, based on the viewport.

Suppose Width is 800 and Height is 600, then glOrtho will be called like this
glOrtho( -133, 133, 100, 100 ). The objects drawn in this Clipping area[in x direction -133 to 133, and in Y direction 100 to 100 ] is mapped to window region 800, 600. Therefore object drawing is not depend on the window region.

http://www.glprogramming.com/red/chapter03.html
Please check Orthographic Projection and Viewport Transformation in the above link.
It explains the relation between 3D space and window region.


这篇关于使用OpenGL示例在动画中剪切区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆