Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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  
  'Ghosting' objects using the stencil buffer?  (Read 6668 times)
0 Members and 1 Guest are viewing this topic.
Offline Alfryd

Junior Member





« Posted 2009-03-03 15:09:29 »

I'm trying to put together a relatively simple isometric-perspective game, and I'd like the ability to 'ghost out' various portions of the screen as part of UI feedback.  Specifically, when the player is trying to place a specific building, I'd like to have the building-preview (and possibly underlying terrain) turn red or green depending on whether it's over a legal or illegal location.

As in, for every pixel corresponding to the elements in question, I'd average the RGB components, and then set either R, G or B to that exact value (with 0 in other components.)  How would I go about this?
Offline Cakey

Junior Member





« Reply #1 - Posted 2009-03-03 18:15:31 »

You might want to look into shaders. I just did yesterday and I wrote an Opacity(changes the transparency of textured objects) shader.
http://www.java-gaming.org/topics/solved-opacity-shader-working-code/20006/view.html
If that isn't what you mean then I don't quite understand what your asking?

Offline Alfryd

Junior Member





« Reply #2 - Posted 2009-03-03 19:11:15 »

Well, I mean imagine a standard RTS, where you're placing a new building- the little image of the building will turn different colours depending on where you're trying to put it.  I'm almost certain you wouldn't need a vertex shader for something like this, since I saw this effect back 10-15 years ago.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cakey

Junior Member





« Reply #3 - Posted 2009-03-03 20:01:04 »

I'd imagine you could use different textures for them,
or perhaps blending different textures say(a red texture + original texture)

Shader's aren't as ugly as they sound, I just found that out.

I'm sure you don't NEED a shader, but it is very practical, in my opinion. Just load the shader to change the color balance of the texture, if it is picked/under mouse, or selected, then unload it after.

If there's another way to achieve this effect in JOGL(OpenGL even) I'd be interested as well. However, I've googled for probably 15(collectively) hours to find an alternative to shaders using JOGL for changing the color balance of a textured object, and came up with nothing. 

I'm actually about to write a shader for this for practice and possibly for my own personal project, if you'd like I could post the code here?

And just as a little side-note... If you've seen that effect 15 years ago, I gaurantee you DO NOT want to structure your code/game in a similar method. The complexity of it would be mind numbing and beyond fruitless for the hours put in versus the product received. Maybe I'm biased but I believe using a shader has to be better then down-stepping technology to below OpenGL 1.3(when I believe GLSL was invented/implemented).

http://www.opengl.org/wiki/Texture_Combiners

Offline Cakey

Junior Member





« Reply #4 - Posted 2009-03-04 02:45:03 »

I wrote the code for that shader, if you want it just post back sometime tommarrow. Otherwise I'm not going to use my time posting it.

Offline Alfryd

Junior Member





« Reply #5 - Posted 2009-03-04 07:34:14 »

Well, I'm almost certain there's some way to do this without shaders, but I guess it wouldn't hurt to take a look.  So, sure, please...
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2009-03-04 09:52:40 »

I'm sure you don't NEED a shader, but it is very practical, in my opinion. Just load the shader to change the color balance of the texture, if it is picked/under mouse, or selected, then unload it after.

If by "practical" you mean "won't work on the majority of laptop computers" then yes, using a shader is practical.

For a non-shader approach it depends exactly what effect you're after. If you just need the "can place"/"can't place" feedback then just changing the vertex colours will multiply the texture by the vertex colour so a light green or red will give the sprite a green or red appearence (but with the side effect of making it darker too, since you're multiplying).

Alternatively you can use glTexEnv with DOT3 to make your sprite greyscale, or SECONDARY_COLOR to tint a sprite towards a specific colour. Both of which will work on pretty much any hardware still in use today.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Cakey

Junior Member





« Reply #7 - Posted 2009-03-04 17:53:39 »

Orangy Tang knows what hes talking about. So I guess you might not wanna use a shader. But if you do for whatever reason, here's the code:(Fragment shader)
1  
2  
3  
4  
5  
6  
7  
8  
9  
uniform sampler2D BaseImage;
uniform float ColorR;
uniform float ColorG;
uniform float ColorB;
uniform float ColorA;
void main (void) {
vec4 base = texture2D(BaseImage, gl_TexCoord[0].xy);
gl_FragColor = vec4(base.r * ColorR, base.g * ColorB, base.b * ColorB, base.a * ColorA);
}

VertexShader
1  
2  
3  
void main (void) {
gl_TexCoord[0] = gl_MultiTexCoord0;
}

I didn't know most labtops didn't support GLSL Sad, thats a bummer. I guess I'm lucky mine does!

Offline Alfryd

Junior Member





« Reply #8 - Posted 2009-03-05 02:18:52 »

Quote
Alternatively you can use glTexEnv with DOT3 to make your sprite greyscale, or SECONDARY_COLOR to tint a sprite towards a specific colour. Both of which will work on pretty much any hardware still in use today.
Thanks a lot.  That sounds exactly like what I need.  Smiley

And thanks for the shader code too.
Offline Alfryd

Junior Member





« Reply #9 - Posted 2009-03-07 12:52:33 »

Alternatively you can use glTexEnv with DOT3 to make your sprite greyscale, or SECONDARY_COLOR to tint a sprite towards a specific colour. Both of which will work on pretty much any hardware still in use today.
How, exactly, would I use glTexEnv with DOT3 again?  I mean, what's the precise syntax?  Are you talking about generating a secondary texture?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Alfryd

Junior Member





« Reply #10 - Posted 2009-03-24 13:13:41 »



I'm afraid the technique you suggested doesn't work...  after some experimentation, I managed to get the following results:



The problem is that the GL_DOT3_RGB formula goes like this:

4 × (((A0 r - 0.5) × (A1 r - 0.5)) + ((A0 g - 0.5) × (A1 g - 0.5)) + ((A0 b - 0.5) × (A1 b - 0.5)))

It's the "-0.5f" all over the place that ruins it. Basically, any RGB values less than 50% just disappear.  What I need is:

(A0 r x A1 r) + (A0 g x A1 g) + (A0 b x A1 b)

...also, GL_COMBINE seemed to eliminate normal lighting calculations, which is not what I'm looking for...
Offline kevglass

JGO Kernel


Medals: 153
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #11 - Posted 2009-03-24 13:46:24 »

I might be a bit off mark here, but is this the sort of effect you're looking for:



The green house in the upper left is in a valid location so could be placed by the user. If so, arn't vertex colours set with the colour and non-1 alpha enough?

Kev

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2009-03-24 16:09:04 »

If your object has a blue-ish texture, and you set vertex-colors to green, you end up with black...

You can look into HSL and change the texture (...slowww...) and set the Hue to green for every pixel.

 persecutioncomplex

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline kevglass

JGO Kernel


Medals: 153
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #13 - Posted 2009-03-24 16:14:49 »

Quote
If your object has a blue-ish texture, and you set vertex-colors to green, you end up with black...

Yep, but that doesn't actually look bad - see the corner dark wood on the house above. It's down to what they're trying to achieve in game (rather than in the head).

Kev

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #14 - Posted 2009-03-24 17:04:52 »

Wouldn't it be easiest to:

1. render the item to the stencil buffer
2. render it for real
3. render a semi-transparant red/green quad using the 'shape' in the stencil buffer

No shaders, and it looks perfect, as there is a colored haze over the item, independant of the item's texture - even a totally black item would end up red-ish or green-ish.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Cakey

Junior Member





« Reply #15 - Posted 2009-03-24 17:09:32 »

This might sound dumb but what if you multi-textured it and made a screen-texture for the second texture
something like this but instead of black the colors you wanted,
<img src="http://www.istockphoto.com/file_thumbview_approve/2838173/2/istockphoto_2838173-speaker-screen-texture.jpg">

Just trying to think outside of the box.

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #16 - Posted 2009-03-24 17:25:22 »

Wouldn't it be easiest to:

1. render the item to the stencil buffer
2. render it for real
3. render a semi-transparant red/green quad using the 'shape' in the stencil buffer

No shaders, and it looks perfect, as there is a colored haze over the item, independant of the item's texture - even a totally black item would end up red-ish or green-ish.
If you're just looking to tint a texture then SECONDARY_COLOR is a better option IMHO. Whereas vertex colours multiply the texture colour with an additional colour, secondary colour can be added on top - so you can either brighten a texture or shift it's hue towards a specific colour (including rendering a sprite as pure white, which can be very useful for masking effects).

IIRC you need something like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
// General state
GL11.glEnable(GL14.GL_COLOR_SUM);

// Vertex array state
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
GL11.glEnableClientState(EXTSecondaryColor.GL_SECONDARY_COLOR_ARRAY_EXT);

// Vertex data
GL11.glColorPointer(4, stride, data);
EXTSecondaryColor.glSecondaryColorPointerEXT(3, stride, data);


The result is
fragment colour = (texture colour * vertex colour) + secondary colour

Now if you wanted to tint towards purple at 80% then you set that as your secondary colour  (1, 0, 1), and set the primary vertex colour as (1 - tintAlpha, 1 - tintAlpha, 1 - tintAlpha, 1), in this case (0.2, 0.2, 0.2, 1).

This only requires a single draw call, and you don't need a pesky stencil buffer either.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #17 - Posted 2009-03-24 17:31:22 »

I'd forgotten about the pesky -0.5 in the DOT3 extension, stupid bumpmapping hacks. Tongue However if you combine that with SECONDARY_COLOR above you could use that to add 0.5 onto your texture colour and hopefully eliminate the clamping.

However I'm not entirely sure what order those operations are done in, so you might find that the colours have already been clamped before the +0.5 gets applied.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Alfryd

Junior Member





« Reply #18 - Posted 2009-03-25 13:58:52 »

Quote
I might be a bit off mark here, but is this the sort of effect you're looking for:
Very similar, yes.  (The screen looks nice, by the way- where'd you get it from?)  I'll use vertex colouring if push comes to shove, but I'm reasonably sure there must be some simple method for getting true greyscale, and then tinting toward a specific colour.
Quote
This might sound dumb but what if you multi-textured it and made a screen-texture for the second texture...
Oh, sure- you can manufacture luminance textures easily enough... I dunno.  It just seems sort of like a waste of memory...
Quote
I'd forgotten about the pesky -0.5 in the DOT3 extension, stupid bumpmapping hacks.  However if you combine that with SECONDARY_COLOR above you could use that to add 0.5 onto your texture colour and hopefully eliminate the clamping.

However I'm not entirely sure what order those operations are done in, so you might find that the colours have already been clamped before the +0.5 gets applied.
I'll try and give that a shot and see what the results are, thanks.  (Mind you, I'd be quite interested to know how DOT3 is used for bumpmapping. Smiley )
Offline kevglass

JGO Kernel


Medals: 153
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #19 - Posted 2009-03-25 14:13:35 »

Quote
The screen looks nice, by the way- where'd you get it from?

It's one of my previous projects, The Village, more screenies here http://www.cokeandcode.com/demos/thevillage/

The art mostly came from Reiners

Kev

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #20 - Posted 2009-03-25 14:21:58 »

(Mind you, I'd be quite interested to know how DOT3 is used for bumpmapping. Smiley )

This is getting somewhat before my time, but IIRC the idea is to put the current light direction and strength into the vertex colour, then DOT3 that with a normal map texture. The output of that gives you a bumpmapped light intensity, which you then need further texenv shenanigans to multiply by an additional base texture to give your final lit output.

Things get even fruitier when you're dealing with proper 3d and have to deal with object space or tangent space normal maps, or if you want to do specular as well, but for the basic 2d effect that should be all you need.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (15 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!