Results 1 to 3 of 3

Thread: redraw loop drawing multiple objects

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2012

    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 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);
    		xPos-=0.00005;	//move in a negative direction along the xaxis	
    		yPos = (startYPos*xPos/startXPos);

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    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
    Senior Member Regular Contributor
    Join Date
    Oct 2009
    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.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 :
    glTranslated(xPos, yPos, 0.0);
    ... draw ...

Similar Threads

  1. Drawing multiple objects.
    By in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 01-01-2016, 03:46 AM
  2. Best Practices for drawing multiple objects
    By mitocondriac in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 12-23-2015, 02:53 PM
  3. Drawing multiple objects using shaders
    By sam_thedancer in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 11-06-2015, 11:24 AM
  4. Drawing multiple circles in a loop
    By Chidori in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-09-2009, 08:53 AM
  5. How to redraw some objects while not redraw others?
    By ding in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 09-28-2002, 09:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean