Click here to Skip to main content
12,698,476 members (23,379 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ 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
   // 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

// 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
   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
   // 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);
        windowWidth = 100 * aspectRatio;
        windowHeight = 100;
        glOrtho (-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0);

int main(int argc, char* argv[])
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
   glutInitWindowSize(800, 600);
   glutTimerFunc(33, TimerFunction, 1);
   return 0;
Posted 6-Nov-12 5:27am
Updated 6-Nov-12 5:31am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

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.
Please check Orthographic Projection and Viewport Transformation in the above link.
It explains the relation between 3D space and window region.
wendy&Piano 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?
Santhosh G_ 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_ 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 8-Nov-12 6:50am
Many thanks to you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100