Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (763)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  change texture on the fly  (Read 1870 times)
0 Members and 1 Guest are viewing this topic.
Offline teletubo
Global Moderator

JGO Wizard

Medals: 75
Projects: 4
Exp: 8 years

« Posted 2011-06-15 21:55:55 »

What I'm trying to achieve actually is creating a "fog of war" for my minimap . That is, areas that were not explored are not shown.

I first thought about rendering a small square on each spot which was not yet explored, but that would result very unefficient when a map (which can be 300x300) was mostly unexplored .

My second idea is to change the minimap image (i.e. the texture) on demand : when player newly explores an area, I change the minimap to reflect that .
Would that performance well ? If not, you guys have any idea to solve my problem ?

Offline krasse
« Reply #1 - Posted 2011-06-16 07:45:20 »

You could create a very coarse grid (10 x 10 or 30 x 30) and just render a big "unexplored" square/image for those cells that haven't yet been explored at all and small squares for those cells that contains at least one explored small square.

If you have a growing fog you can just go back to a coarse grid image if all the subcells are "fogged".

You can make a hierarchy of grid sizes as well but that might be to take it to far unless you increase the level size.

Offline theagentd
« Reply #2 - Posted 2011-06-25 20:36:07 »

To avoid the bottleneck of the very expensive CPU-GPU transfer, it's better to render the fog of war on the GPU using a framebuffer (FBO) (but PBO might work too) and a 2D texture with a circle on it. If the line-of-sight is simple then the fog-of-war is very easy to do:

1. At the start of the game create an FBO and attach a 2D GL_ALPHA8 texture to it with the size of the map (in your example 300x300 pixels).
2. Also load or generate a 2D GL_ALPHA8 texture with a circle, maybe 64x64 or 128x128. Preferably also generate mipmaps for this texture.
When rendering the minimap:
2. Bind the FBO. IMPORTANT: Remember to set the viewport and the matrix settings (glOrtho)!!!!! This is VERY easy to screw up when using FBOs.
3. Enable blending and set glBlendFunc to (GL_ONE, GL_ONE).
4. Set glColor4f to (1, 1, 1, 1) (The alpha value is the only important one, all others are discarded as we are rendering to an alpha texture)
5. Bind the circle texture.
6. Draw a GL_QUAD for each unit with vision with the circle texture to create "holes" in the fog-of-war texture. The blendfunc ensures that the circles do not overwrite what's already drawn and "stacks" properly.
7. You now have an alpha texture where visible areas have alpha=1.0f and "fogged" areas have alpha=0.0f!!!

From here on just use this texture either with an overlay quad, multitexturing (and maybe a shader) to put it on top of the minimap, depending on how you store the minimap terrain.
 - If you have the terrain as a texture, just bind the terrain texture to active texture 0 and the fog of war to texture 1. Then use either the multitexturing settings or a shader to output (terrainColor * fogAlpha) in some way.
 - If you render the terrain as geometry or for some reason don't want to use the kinda complicated above method, it's easier to just draw the terrain "unfogged" and then drawing the fog-of-war texture on top of it. Basically just bind the fog texture, set glColor4f to (0, 0, 0, 1), enable blending with blendfunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA) and draw a GL_QUAD over the minimap.

When you grasp the concept it's actually pretty easy. If you're already experienced with FBOs, this'll be a cakewalk. If not, you really should learn to use them, because they are a very powerful tool in OpenGL (combined with shaders)!  Wink
If you ARE experienced with FBOs and want to have vision obstacles (e.g. vision "shadows), feel free to look at this great article: I have implemented the concept but with hard shadows myself and I'm going to use it in a game I'm making for, guess what? Fog of war! Grin
Also, feel free to ask for more details if you don't understand something. I can also post code examples. =)
Good luck!

Pages: [1]
  ignore  |  Print  

EgonOlsen (490 views)
2018-06-10 19:43:48

EgonOlsen (570 views)
2018-06-10 19:43:44

EgonOlsen (406 views)
2018-06-10 19:43:20

DesertCoockie (718 views)
2018-05-13 18:23:11

nelsongames (1015 views)
2018-04-24 18:15:36

nelsongames (981 views)
2018-04-24 18:14:32

ivj94 (1504 views)
2018-03-24 14:47:39

ivj94 (557 views)
2018-03-24 14:46:31

ivj94 (1261 views)
2018-03-24 14:43:53

Solater (560 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!