Members - Reputation: 104
Posted 05 May 2012 - 07:24 AM
Apologies if this is a somewhat idiotic question, I'm still somewhat new to OpenGL programming. In fact, I'm not even sure if this is the right forum or if it should have been posted in Beginners!
Firstly, some background.
I currently have a basic 2D tiling engine which uses OpenGL via the OpenTK bindings. For drawing textures, the engine is currently using the somewhat obsolete vertex arrays rather than VBO's (one challenge at a time!)
Each map is a simple 2D array of tiles. I define a field size which is the visible viewport on the screen, and set up an array of vertexes which describe each tile position in the viewport. This is defined once. There is also an origin, which is simply the top/left co-ordinate of the map in relation to the player.
Then, each update, the engine cycles through the visible tiles in the current map and updates the vertex array with texture coordinates from a tile sheet texture. If the player moves and the origin changes, this is naturally handled as part of the cycle through visible tiles. It's a very simple system
So far so good.
However, the disadvantage is that it "scrolls" in full tiles. So if the character charges from one side of the screen to the other, it will jump along one block at a time.
What I wanted to do was implement a form a smooth scrolling, so that instead of jumping the entire width of a tile, it will jump a smaller increment until it has reached the desired end position.
I tried implementing smooth scrolling in a test project some months ago, and while it worked, it had one nasty drawback - on one side of the scroll you'd have a gap as technically the subsquent column wasn't visible so it wouldn't be drawn. And on the other side of the scroll you'd have an excess because it was trying to draw the last column beyond the boundaries of the view port. So assuming you had UI elements, they could be overwritten.
If I was using GDI, then I might just set a clip region and then just draw the visible tiles +/- 1 column/row and everything would be displayed without overwriting any element. Of course, that would mean extra rendering being done for no purpose which is a bit daft.
Not really sure I'm explaining this terribly well, so here's a couple of images to partially demonstrate:
In the second screen shot, the player has moved one tile to the right. As the engine currently tries to keep the player character centred in the map, it automatically scrolls it along one tile. Which is fine, as the end result - but I want to smoothly bump it along instead of it just jumping.
This third image demonstrates a test program I did which tried smoothly scrolling the background from right to left and also scrolling the water from left to right. The jagged edges at the top and bottom show how my rendering is currently flawed.
So, my questions would be:
Firstly, does OpenGL support anything like GDI's clip regions so I could draw extra tiles without them going beyond what I define as a view port. [The lazy approach I suppose]
Should I recalculate the first/last column/row and draw only the visible portion of the texture [The hurt-your-brain-with-maths approach]
Is there another technique that other developers use that I haven't thought of?
Again, apologies if this is a really dumb question, but thanks in advance for any advice that could be offered.
Moderators - Reputation: 7928
Posted 05 May 2012 - 07:45 AM
The easy solution isn't really that difficult. I get the feeling that you may be thinking too much about complex solutions to this.
- Define the viewport as the region of the screen where you want the game to be drawn. I see in your first two screen shots that the map covers the whole screen except for the top row. What I mean here is that your viewport should only cover the region of the screen where the map is to be drawn. Make a separate viewport for the score board in a second pass, but I will not consider that here.
- Set the coordinate system (for example with glOrtho) such that the desired part of the map is visible within the viewport. All you need to do to scroll the map is to adjust the coordinate system. No need to touch the viewport itself.
- Draw the visible tiles. You know the bounds of the visible region of the map, so draw only those tiles if you want to do some primitive culling. You can draw the entire map as well if you want, since the viewport is clipping anyway. If you want partial-tile-scrolling, then you just round the visible region's left and bottom borders down to nearest tile-border, and the right and upper borders up to nearest tile-border.
edit: By the way, what the name of the game in the first two images? It was one of my favorite games way back, but I just can't remember the name now.
Edited by Brother Bob, 05 May 2012 - 07:47 AM.
Members - Reputation: 104
Posted 05 May 2012 - 07:54 AM
I do set a co-ordinate system, but I set that once when the window is created (and I reset it if the window is resized). Didn't think about creating a sub co-ordinate system as such.
When I say "viewport", I'm just referring to the region of screen that I'm drawing in, I'm not actually creating view ports with OpenGL - but your reply in point 3 seems to infer that OpenGL can in fact do them. Guess I need to go and look over the API reference to find out how
The screenshots are from my clone of a old arcade game called Boulder Dash that I used to play on an Atari 800XL, using some custom graphics I'm currently trying to draw too.
Thanks again for your reply!
Members - Reputation: 104
Posted 05 May 2012 - 08:07 AM