Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: redraw loop drawing multiple objects

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2012
    Posts
    9

    Unhappy redraw loop drawing multiple objects

    Hello! I am so confused...I have a very simple loop that I would really like to be a while loop..however when I do use while, I get nothing...so I started working with a for loop and I do get a picture! But I am not getting the behavior I want.

    Ideally I would like:

    while (xPos != 0.0){
    draw a cube at xPos, yPos
    decrement x
    decrement y as a function of x
    }

    //i am starting out the cube at (.75, .75)

    and here is my code that will draw a cube--but something really strange is happening, as the cube gets closer to the origin, more and more cubes start appearing then they all kind of meet in the middle and then escape at opposite diagonal ends of the screen?

    I have no idea what's going on :-(

    Code :
    	int i = 0;
     
    	//while (xPos != 0.0) {
     
    	for (i; i < 200; i++) {
    		glTranslated(xPos, yPos, 0.0);
    		glutWireCube(.25);
    		xPos-=0.00005;	//move in a negative direction along the xaxis	
    		yPos = (startYPos*xPos/startXPos);
    		glutSwapBuffers();
    		glutPostRedisplay();
    	}

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,104
    When using real numbers never do exact tests like xPos != 0.0 because precision might result in a number like 0.00000001 or -0.00000001 instead of 0.0. It is good programming practice do use inequality tests rather than exact tests when the results would have the same meaning.
    So try
    Code :
      while ( xPos > 0)

    If you need to do tests against an exact number like 0.0 with reals do something like
    Code :
     while  (! absf(xPos  < 0.00001))

  3. #3
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    433
    Are you intending them to be spaced evenly along the x-axis every 0.00005 units, because that's not what will happen since you are translating each time with glTranslate + adjusting the amount you are translating by too.

    The first few values of x that the cube will be drawn at will be:
    0.75
    0.75 + (0.75-0.00005) =
    0.75 +
    (0.75-0.00005) + (0.75-2*0.00005)
    0.75 + (0.75-0.00005) + (0.75-2*0.00005) + (0.75-3*0.00005)
    ...

    x won't start to decrease till 15000 iterations of the loop, when the amount being added each time will finally become negative (since 15000*0.00005 = 0.75).

    You either want to translate by a fixed amount:
    Code :
    glTranslate(-0.00005, delta_y, 0)
    but this would require calculating the change in y at each step, or make sure the modelview matrix is reset each time instead of each translation being added, with glLoadIdentity() or glPushMatrix()/glPopMatrix() :

    Code :
     
    Code :
    glPushMatrix();
    glTranslated(xPos, yPos, 0.0);
    ... draw ...
    glPopMatrix();


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •