Rate this:
See more:
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.
```#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);
// 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);
}

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;
}
```
Posted 6-Nov-12 5:27am
Edited 6-Nov-12 5:31am
v2

Rate this:

## Solution 1

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
It explains the relation between 3D space and window region.
wendy&Piano at 7-Nov-12 10:12am

Hi Santhosh G_,
Thanks for your help. Now I can tell the difference between window region and clipping region. But when I tested glOrtho function, I found the bigger size I specify for it, the smaller the rectangle is. Could you explain it?
Thanks!
Santhosh G_ at 7-Nov-12 18:06pm

Suppose you doubled the clipping area using glOrtho(). For 800,600 window size, you will call glOrtho( -266, 266, 200,200 ). The objects drawn in this Clipping area[in x direction -266 to 266, and in Y direction 200 to 200 ] is mapped to window region 800, 600. But the size of object[Square of size 25] is same as that of old. Here viewing area is increased, but the object size is same as that of small area. It creates a smaller size rectangle.
Santhosh G_ at 7-Nov-12 18:08pm

The above example is similar to taking a picture of a ball by placing camera very near to the ball and placing camera very far from the ball. When camera is near to the ball, the viewing area will be small and it create ball as bigger. When camera is far from the ball, the viewing area will be bigger and the ball will become small.
wendy&Piano at 8-Nov-12 6:50am

Many thanks to you!