Results 1 to 4 of 4

Thread: OT: C++ loop Question?

  1. #1
    Senior Member Regular Contributor
    Join Date
    Sep 2001
    Location
    macedon, ny
    Posts
    288

    OT: C++ loop Question?

    I know this is off topic, but it has to do with my Rendering Loop. Currenlty I am checking each of my Items/NPC to see if there in a zone i can see. I do this with a for loop.

    for(a=0; a<NumItems; a++)
    {
    if(Item[a].InViewableRoom = true)
    {
    DrawItem(a);
    }
    }

    My question is this, how long does it take a compiler to check to see if an if is valid?? I do this for quite a few objects each time the scene is drawn. I want to draw 60ish frames per second. I thought it might be better to create a linked list, knowing what objects were in what rooms, but they can change rooms at any time, if they move. So a linked list is not realy feasible, without rewriting my current setup. So mainly i want to know, are using large if chains slow? or are they run through pretty quickly??

  2. #2
    Member Contributor
    Join Date
    Jan 2001
    Posts
    60

    Re: OT: C++ loop Question?

    dabeav,

    "if" statements are usualy not the problem when considering performance.
    You should be probably more worried about how many "for" loops you use, specially if there one inside others.
    The performance will also depend on the complexity of the item you draw with you DrawItem() function.

    HTH,

    Noah

  3. #3
    Senior Member Regular Contributor
    Join Date
    May 2000
    Location
    Oxford, England
    Posts
    472

    Re: OT: C++ loop Question?

    for loops and if statements aren't necessarily a bad thing, however in the context of what you are using a tree of some sort would be better (hint: search for BSP trees, quad-trees, oct-trees and/or portals)

    With a tree you can hierarchically group objects into rooms and test to see if you can see a room, if you can then only test the objects in that room. If not don't bother.

    You may want to have a look at http://www.gametutorials.com/Tutoria...OpenGL_Pg5.htm for their octree tutorials and http://nate.scuzzy.net/programming/ for the BSP demo. I'm sure there are a million and one other tutorials and references you can find with google....

    If you decide to use one of these methods, research them all to find out which one is most suitable for your game.

    [This message has been edited by Rob The Bloke (edited 08-13-2002).]

  4. #4
    Senior Member Regular Contributor
    Join Date
    Sep 2001
    Location
    macedon, ny
    Posts
    288

    Re: OT: C++ loop Question?

    Well, i am doing something similar to that now. I am however NOT using BSP. I have simply broke my world up into zones, and each zone has a PVS. I simply check which zone im currently in (few sqrts each time i move, so no biggie) then i simply check out of the PVS, what is visable (with a box in frustum check, once again a few sqrts, so no biggie) Then i run through my items, and NPC, (because, like i said earlier, they can move at any time). I simply check to see if there listed in a room that is curretnly drawn, and if they are I run them through a bounding sphere in zone frustum check, to once again see if there visable.

    THEN if there visable after ALL that, i draw them. I think this is a very very agressive method of dumping objects that arent drawn anyways. BUT, i have to run through them 'all' because i have to move them anyways.

    Take for example, a box, if I shoot a box off a shelf, and turn around before it hits the floor. If is was using a tree, and determined that the box was not visable, it would never update the box while NOT visable, so the box would freeze in mid air. So i 'have'(maybe not have to, but i think i should) to run through all the objects anyhow to test for movement. So i think this present setup is ok. I was just curious about if the IFS, and FORS were going to cause some issues.

    Thanks for the help, let me know what you think about my ideas for culling objects, and rooms. I think its a pretty good algorithm. But i ALWAYS like people to tell me where they might see a fault. Thanks

Similar Threads

  1. Updating VAO using for loop
    By hiroki in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 12-10-2017, 09:22 PM
  2. Replies: 0
    Last Post: 01-01-2016, 09:34 PM
  3. while loop
    By jofoxy in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 03-07-2005, 08:12 AM
  4. loop problem
    By mk in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 12-21-2004, 08:53 PM
  5. Loop message and the eternal C vs. C++ question.
    By Anonymous Coward in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 08-31-2000, 09:38 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