openGL background scrolling

This is a discussion on openGL background scrolling within the C++ Programming forums, part of the General Programming Boards category; I want to make the background to scroll from left to right, right now my background image is in the ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    135

    openGL background scrolling

    I want to make the background to scroll from left to right, right now my background image is in the openGL file, but how should I make it to move. My code for the background image.
    Code:
    /*
    	glPushMatrix();
    		glEnable(GL_BLEND);
    		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    		glBindTexture(GL_TEXTURE_2D, BackgroundTexture[imageNumber].texID );
    		glPushMatrix();
    		
    		
    			glBegin(GL_QUADS);
    				int height = 100 * 1.333/1.5;
    				glTexCoord2f(0,0); glVertex2f(0,600);
    				glTexCoord2f(1,0); glVertex2f(1600,600);
    				glTexCoord2f(1,1); glVertex2f(1600,0);
    				glTexCoord2f(0,1); glVertex2f(0,0);				
    			glEnd();
    		
    		glPopMatrix();
    		glDisable(GL_BLEND);
    	glPopMatrix();
    */
    The size of the background image is 800 by 600. I make it to 1600 so that only the left part of the image would be shown. How should I code such that the background image moved when the character was moving towards the right. All my character and others has been done expect the background scrolling.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well, the simplest way would be to do a glTranslate just before you draw the background. You're already pushing the matrix to save it, which means the translate won't affect the rest of your code. You only need to push/pop once. All pushing and popping does is save the current matrix for you; flags like GL_BLEND are handled separately. So, suggestion:
    Code:
        glPushMatrix();
            glEnable(GL_BLEND);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);  // blending on a background texture? is that really necessary?
            glBindTexture(GL_TEXTURE_2D, BackgroundTexture[imageNumber].texID );
            glTranslatef(someYOffset, 0.0f, 0.0f);
             
            glBegin(GL_QUADS);
                int height = 100 * 1.333/1.5;  // you're not using this variable anywhere...
                glTexCoord2i(0,0); glVertex2i(0,600);  // you can use the i variants if you're passing integers
                glTexCoord2i(1,0); glVertex2i(1600,600);
                glTexCoord2i(1,1); glVertex2i(1600,0);
                glTexCoord2i(0,1); glVertex2i(0,0);
            glEnd();
            
            glDisable(GL_BLEND);
        glPopMatrix();
    Just change the someYOffset every frame, left or right, and you'll have a scrolling background.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    How should I set the value for someYOffset?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well if you want a scrolling background, change it a little bit each frame. Hackish example:
    Code:
    static double someYOffset = 0.0;
    someYOffset += 0.1;
    if(someYOffset > 1000.0) someYOffset -= 1000.0;
    If you want it to depend on the character's movement, then change the variable based on the character's position. Example:
    Code:
    double someYOffset = character.getXPosition() % 1000;
    If you need to do floating-point modulus, check out fmod().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Your code for the background moved for right to left, how should I move from left to right. And how should I put a stopped for the image when it reach a certain limit so the background map would stopped moving when the image has reached the maximum limit. Thank you for your help.
    Last edited by evildotaing; 08-29-2012 at 11:32 AM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well if you want left-to-right instead of right-to-left, just put a negative someYOffset instead of a positive one! i.e. subtract a little bit from someYOffset each frame until it hits -1000 or something, then wrap back to 0.

    I was under the impression that you wanted the background map to wrap. If instead you want it to stop moving, as you "reach the edge of the map" as it were, then just don't let the YOffset value go less than (or greater than) some particular value. Example:
    Code:
    if(someYOffset < -1000) someYOffset = -1000;
    What exactly are you using the background map for? I thought it was just decoration but if it's actually the main background for a 2D game or something, you should use a different method to display it... this method is okay for small images but if you have a big map it's going to be inefficient, and maybe hard to control...

    What do you have so far, exactly? This is pretty straightforward stuff. Maybe you should read the first bit of some opengl tutorials. A really good one is NeHe Productions - Everything OpenGL
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scrolling background??
    By blackCat in forum Game Programming
    Replies: 2
    Last Post: 04-19-2009, 04:08 PM
  2. How to load background pic/texture in OpenGl?
    By salman86 in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2005, 07:37 PM
  3. Scrolling background?
    By Deo in forum Game Programming
    Replies: 6
    Last Post: 06-09-2005, 06:40 PM
  4. 2-D background bitmap behind a 3D wold using OpenGL
    By Laeeqhamid in forum Game Programming
    Replies: 6
    Last Post: 12-28-2002, 04:42 AM
  5. Scrolling Background
    By Traveller in forum Windows Programming
    Replies: 1
    Last Post: 07-21-2002, 04:50 PM

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