Alfryd
Jr. Member   Posts: 92
|
 |
«
on:
2009-03-03 10: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?
|
|
|
|
|
|
|
Alfryd
Jr. Member   Posts: 92
|
 |
«
Reply #2 on:
2009-03-03 14: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! Go get 'em!
|
|
Cakey
Full Member   Posts: 127
|
 |
«
Reply #3 on:
2009-03-03 15: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
|
|
|
|
Cakey
Full Member   Posts: 127
|
 |
«
Reply #4 on:
2009-03-03 21: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.
|
|
|
|
Alfryd
Jr. Member   Posts: 92
|
 |
«
Reply #5 on:
2009-03-04 02: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...
|
|
|
|
|
Orangy Tang
JGO Kernel      Posts: 2960 Medals: 37
Monkey for a head
|
 |
«
Reply #6 on:
2009-03-04 04: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.
|
|
|
|
Cakey
Full Member   Posts: 127
|
 |
«
Reply #7 on:
2009-03-04 12: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  , thats a bummer. I guess I'm lucky mine does!
|
|
|
|
Alfryd
Jr. Member   Posts: 92
|
 |
«
Reply #8 on:
2009-03-04 21:18:52 » |
|
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.  And thanks for the shader code too.
|
|
|
|
|
Alfryd
Jr. Member   Posts: 92
|
 |
«
Reply #9 on:
2009-03-07 07: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! Go get 'em!
|
|
Alfryd
Jr. Member   Posts: 92
|
 |
«
Reply #10 on:
2009-03-24 09: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...
|
|
|
|
|
kevglass
« League of Dukes » JGO Kernel      Posts: 5214 Medals: 49
Mentally unstable, best avoided.
|
 |
«
Reply #11 on:
2009-03-24 09: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
|
|
|
|
Riven
« League of Dukes » JGO Kernel      Posts: 5867 Medals: 255
Hand over your head.
|
 |
«
Reply #12 on:
2009-03-24 12: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. 
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings
|
|
|
kevglass
« League of Dukes » JGO Kernel      Posts: 5214 Medals: 49
Mentally unstable, best avoided.
|
 |
«
Reply #13 on:
2009-03-24 12:14:49 » |
|
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
|
|
|
|
Riven
« League of Dukes » JGO Kernel      Posts: 5867 Medals: 255
Hand over your head.
|
 |
«
Reply #14 on:
2009-03-24 13: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
|
|
|
|
|
Orangy Tang
JGO Kernel      Posts: 2960 Medals: 37
Monkey for a head
|
 |
«
Reply #16 on:
2009-03-24 13: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
| GL11.glEnable(GL14.GL_COLOR_SUM);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY); GL11.glEnableClientState(EXTSecondaryColor.GL_SECONDARY_COLOR_ARRAY_EXT);
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.
|
|
|
|
Orangy Tang
JGO Kernel      Posts: 2960 Medals: 37
Monkey for a head
|
 |
«
Reply #17 on:
2009-03-24 13:31:22 » |
|
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.
|
|
|
|
Alfryd
Jr. Member   Posts: 92
|
 |
«
Reply #18 on:
2009-03-25 09:58:52 » |
|
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. 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... 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.  )
|
|
|
|
|
kevglass
« League of Dukes » JGO Kernel      Posts: 5214 Medals: 49
Mentally unstable, best avoided.
|
 |
«
Reply #19 on:
2009-03-25 10:13:35 » |
|
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 ReinersKev
|
|
|
|
Orangy Tang
JGO Kernel      Posts: 2960 Medals: 37
Monkey for a head
|
 |
«
Reply #20 on:
2009-03-25 10:21:58 » |
|
(Mind you, I'd be quite interested to know how DOT3 is used for bumpmapping.  ) 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.
|
|
|
|
|