Program starting to become laggy...

This is a discussion on Program starting to become laggy... within the Game Programming forums, part of the General Programming Boards category; In Direct3D you only call BeginScene() before you draw and EndScene() after you are done. You don't call it every ...

  1. #31
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    In Direct3D you only call BeginScene() before you draw and EndScene() after you are done.

    You don't call it every time during the scene in which you want to draw. Call them both ONCE per frame.

    I'm sure OGL is the same.

    One thing I see is that you have "glBegin() and glEnd()" after each polygon. This causes a major slow down (several orders of magnitude on one program I was working on).
    But I think that has already been said in this thread and you still have not corrected your code.

  2. #32
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I see, soooo, even though I'm making a display list for my quad, i'm still looping the creation process, instead of gl beginning and ending in every single loop.. i'll just translatef inside the loop instead of doing that externally...

    switch glbegin/end's with translatef's

  3. #33
    ---
    Join Date
    May 2004
    Posts
    1,379
    switch glbegin/end's with translatef's
    What? They have nothing to do with each other.
    It might be impossible to eliminate all but one set of glBegin/glEnd but the idea is to have as little as possible. Putting that into a display list (or better yet, a vertex array) and fixing that web you call a render loop, should increase performance.

  4. #34
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    To give you an idea of a render loop this is one of mine. It is from my terrain engine that I posted screenshots of.

    Code:
    bool Display(float timeDelta)
    {
      if (g_pDevice)
      {
        g_pDevice->Clear(0,0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,SkyColor,1.0f,0);
        g_pDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
    
    
        D3DXMATRIX View;
    
        TheCamera.getViewMatrix(&View);
        g_pDevice->SetTransform(D3DTS_VIEW,&View);
        GetKeyboardInput();    
      
        g_pDevice->BeginScene();
        
        
        TheTerrain.Render(timeDelta);
        
        g_pDevice->EndScene();
        
        g_pDevice->Present(0,0,0,0);
      }
    
    
      return true;
    }
    That's it. Now a game would have significantly more, but most of the processing and core code is outside of the main render loop. Main render loops should be small and concise.

    And translating one quad to create a grid is well...um...ludicrous.
    Have you actually read any of these responses? If you have you would know about 95% of this already since it has already been posted. Read.

    My patience grows thin because:

    • Nearly everyone that has responded has said the same thing and yet you insist that your way is working. If it was working right then well you wouldn't be here asking us now would you? I don't like it when people tell someone a solution and they respond with arguments. Just admit you have no idea what you are doing and let us help you.
    • It's obvious from your recent replies that you have not read or heeded any of the advice given in any of the responses because your code still has the for loops, still has the glBegin() and glEnd() functions all over - all of these have been addressed. Fix it.
    • You need to probably read a book on how OpenGL works. We cannot explain the API in one post. It also appears that you don't understand some fundamental graphics concepts in 2D, let alone use an API that was really designed for 3D. Buy a book:
      www.amazon.com has many.
    • All of us started out in graphics not having a clue as to what we are doing. Don't be a poser. There is nothing wrong with not knowing how to do something in code, there is something wrong with acting like you do, when we all know you don't. Be open and honest and have an open mind when people respond and you will get a much better attitude from all of us.


    When you show that you are interested in fixing your code rather than arguing with us when we tell you it's wrong, then we, or at least I, will be a lot nicer.
    Last edited by VirtualAce; 10-13-2005 at 11:06 PM.

  5. #35
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I am reading all the posts that are being sent my way but, I'm just not understanding, not everyone is saying the exact same thing.... I guess there is no excuse for ignorance when you've been told your problem, but you still don't understand it....

    You keep saying I'm using the API incorrectly, I learned all I know from NeHe, I guess theres nothing on there about writing efficient drawing methods.... (it is understandable that drawing 392 quads for a grid is unreasonable, just felt like the most logical way to do it... (one object for each seat)), guess not, guess it would be better if I were to write just a couple functions that used lines to create a border and cells..... and then use mouse detection to seperate the cells... that would definately be more efficient....

    You make it seem that I am purposely not taking your information, but I am, I'm just not understanding it...

    About the GLBegin()/end()'s..... what do I do to avoid them?
    Do display lists provide a way around them? whenever you draw a new object don't you need to GLBegin???

    I'll just go read the RedBook, I suppose, maybe i'll be able to teach myself something about proper drawing methods...

    I believe you when you say loops and glbegins/ends are my problem, but I just want to know all the logic behind it as well, forgive me if I can't take your word as law, logic is king in my world, I need to know WHY :d..... (especially If I'm about to rewrite my entire program)....

    I will take your word for truth, but all this time i've just been bugging for an answer why...
    Last edited by Shamino; 10-14-2005 at 08:13 AM.

  6. #36
    ---
    Join Date
    May 2004
    Posts
    1,379
    You can draw multiple quads within the same glBegin/glEnd block. After four calls to glVertex* a new quad is started on the fifth call. Same goes with triangles except you only need three verts not four.

  7. #37
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    ohhh, well, thats cool, so i can write a giant display list for everything that isnt my grid, or even do it for my grid too (seeing as how I'm rewriting that into just lines)....

    Then I can do another display list for the menu.....

    That will solve my glbegin/end problem...

    Now I just have to rewrite my grid drawing function without all those nasty nested loops, that would be -392 glbegins and ends each.....

    So, code optimization 4tw

  8. #38
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    OpenGL unfortunately has no drawsquare function, only QUADS, which draws a filled in square that ultimately is no use in a grid pattern... what to do what to doo.....

  9. #39
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Ahah, I'll make my OWN drawsquare function!

  10. #40
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    OMG, OMG, fixed. 4tw....

    Congratulations to me, it runs UBER FAST... :d

    Code:
    void DrawSquare(int left, int top, int right, int bottom)
    {
    	glBegin(GL_LINES);
    	glVertex2d(left, top);
    	glVertex2d(right, top);
    
    	glVertex2d(right, top);
    	glVertex2d(right, bottom);
    
    	glVertex2d(right, bottom);
    	glVertex2d(left, bottom);
    
    	glVertex2d(left, bottom);
    	glVertex2d(left, top);
    	glEnd();
    }
    
    
    void DrawGrid(int left, int top, int right, int bottom, int w, int h)
    {
    	for(int i=left; i<right; i+= w)
    	{
    		for(int o=top; o<bottom; o+= h)
    		{
    			DrawSquare(i,o,i+w,o+h );
    		}
    	}
    }
    Bubba, you roxxors 4tw, , i still don't understand WHY it is faster, but it is :d

  11. #41
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Hm, one thing nice about that big ugly loop I made was that I had a quad that I could turn green on command, to show that it was available or not, and mouse click detection was done in a big 1-2-3 punch, draw, detect, and change on demand all at the same time, but it was laggy....

    now I gotta do mouse click detection for a 14x28 grid, thats not that difficult....

    but turning one cell green? how would I go about doing that?, theres no quad there, unless wait, are the cells a single line strip.... they might be!

  12. #42
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Calculate what grid cell the mouse cursor is in.
    Find out where in your vertex buffer it lies.
    Set those vertex colors to green.

  13. #43
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Alright, sounds simple, gotta figure out how to manipulate the vertex buffer though, more learning to do!


    Doh, stupid post, this might work, if I make a function that makes click detection for each cell, if you click the cell, it does something along these lines:

    Code:
    glColor3f (1.0, 0.0, 0.0);  /* the current RGB color is red: */
                                /* full red, no green, no blue. */
    glBegin (GL_POINTS);
        glVertex3fv (point_array);
    glEnd ();
    I'd have to do that for 10x10 pixels though (and then 392 times), thats a bunch...
    Must be a better way......

    Vertex Buffer, Vertex Buffer..... hmmmmm

    "reads about the framebuffer"....... Oh...

    so theres a color buffer, a depth buffer, stencil buffer... etc etc, all of these hold information for a single pixel, which buffer it is determines what kind of information they hold, okay, got it....

    But to manipulate the color buffer to color in a specific set of pixels... (instead of drawing a square for each area I want to color!)...

    Still researching....

    I believe I've pinpointed the exact thing I need to learn, Redbook says, and I quote

    Up to four optional, nondisplayable auxiliary color buffers can also be supported. OpenGL doesn't specify any particular uses for these buffers, so you can define and use them however you please. For example, you might use them for saving an image that you use repeatedly; rather than redrawing the image, you can just copy it into the usual color buffers. See the description of glCopyPixels() in "Reading, Writing, and Copying Pixel Data" for more information about how to do this.
    Now I just gotta find out where the actual description is

    Ahah!

    You can read the contents of the color, depth, and stencil buffers with the glReadPixels() command. Likewise, glDrawPixels() and glCopyPixels() are available for sending images to and BLTing images around in the OpenGL buffers.
    Info can be found here I believe

    Learning is cool
    Last edited by Shamino; 10-18-2005 at 08:17 AM.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting C++;First Program
    By Caliban in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2009, 01:41 PM
  2. Starting a program
    By mcgeady in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2006, 11:52 AM
  3. starting program
    By Ideswa in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2006, 01:36 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21