Game Development Community

Object selection and highlighting

by Rubes · in Torque Game Engine · 09/18/2006 (7:53 pm) · 10 replies

Hopefully John or someone else more knowledgeable than me can help with this one...

I created the Object Selection (with TLK Bonus) resource so I could highlight objects when they are selected with the mouse. Highlighting is achieved by modifying the ShapeBase::renderObject method, such that sgLightManager::sgGlobalBlendColor is set to ColorF(5.0, 5.0, 5.0, 5.0) if the object is the currently selected object. If it's not the selected object, then it's set to ColorF(1.0, 1.0, 1.0, 1.0).

One thing I and others began noticing is that some objects were being highlighted when a different object was highlighted. This seemed to happen primarily to TSStatic objects (whereas all of my "selectable" objects are StaticShape or Item objects).

The fix, as pointed out by John, was to make sure to reset sgLightManager::sgGlobalBlendColor to ColorF(1.0, 1.0, 1.0, 1.0) later on in the method -- I put it right after the call to uninstallLights().

It has been working fine ever since...until now. I'm beginning to notice objects becoming highlighted again when I select a different object. The selectable object is an Item object, and selecting it seems to trigger a whole array of other TSStatic objects to become highlighted -- something like three or four of them, and it's consistently reproduceable with these objects.

I'm stumped as to why this is happening now. Any thoughts would be appreciated.

#1
09/19/2006 (3:30 pm)
Hi Rubes,

Make sure the selection variable is being set properly on the objects (the correct object is being flagged). To do this you could change the code so rendering is skipped if the object is selected, this will show you if the problem is related to the selection or the highlighting.

If any objects are rendered and highlighted then something weird is happening with the highlighting as opposed to the selection. If multiple objects start disappearing then something weird is happening with the selection.

Also make sure all instances of setting sgLightManager::sgGlobalBlendColor is followed by setting it back to normal.

Btw: very cool resource!
#2
09/19/2006 (4:40 pm)
Brilliant! As always, John. Can't believe I didn't think of doing that.

Problem was that I completely forgot that I introduced the same highlighting effect for GuiObjectView, which I am using for my inventory items. I realized that this bug was only happening when I selected inventory items, which made me realize I had another instance of setting sgLightManager::sgGlobalBlendColor which wasn't being reset. D'oh!

Thanks for the help, you rock (and thanks for the compliment!).
#3
09/21/2006 (5:33 am)
Nice fix :)
#4
10/28/2006 (1:53 pm)
Hey John,

Now that 1.5 is out, I see that there have been quite a few changes to the lighting code. It appears that there is no longer an sgLightManager, so this code doesn't work anymore. Before I go digging into the new code all day, do you happen to know what the new call would be to set/reset the lighting on a per-object basis?

Thanks...
#5
10/28/2006 (11:45 pm)
Hey Rubes,

That feature was quietly removed as only a handful of people were using it, but putting it back is a snap and would make a great resource (or in your case a great addition to your resource).

The first change is in sgLightingModel.cc at line 302:

void sgLightingModelGLBase::sgSetState(LightInfo *light)
{
	sgLightingModel::sgSetState(light);

	sgConstantAttenuation = 0;
	sgLinearAttenuation = 0;
	sgQuadraticAttenuation = 0;

    sgLightParamDiffuse = Point4F(light->mColor.red, light->mColor.green, light->mColor.blue, 1.0f);
    sgLightParamAmbient = Point4F(light->mAmbient.red, light->mAmbient.green, light->mAmbient.blue, 1.0f);
    sgLightParamPosition = Point4F(light->mPos.x, light->mPos.y, light->mPos.z, 1.0f);
    sgLightParamDirection = Point4F(-light->mDirection.x, -light->mDirection.y, -light->mDirection.z, 0.0f);
}

Change to this:

void sgLightingModelGLBase::sgSetState(LightInfo *light)
{
	sgLightingModel::sgSetState(light);

	sgConstantAttenuation = 0;
	sgLinearAttenuation = 0;
	sgQuadraticAttenuation = 0;
[b]
	sgLightParamDiffuse = Point4F(light->mColor.red * LightManager::sgGlobalBlendColor.red,
		light->mColor.green * LightManager::sgGlobalBlendColor.green,
		light->mColor.blue * LightManager::sgGlobalBlendColor.blue,
		1.0f * LightManager::sgGlobalBlendColor.alpha);

	sgLightParamAmbient = Point4F(light->mAmbient.red * LightManager::sgGlobalBlendColor.red,
		light->mAmbient.green * LightManager::sgGlobalBlendColor.green,
		light->mAmbient.blue * LightManager::sgGlobalBlendColor.blue,
		1.0f * LightManager::sgGlobalBlendColor.alpha);
[/b]
    sgLightParamPosition = Point4F(light->mPos.x, light->mPos.y, light->mPos.z, 1.0f);
    sgLightParamDirection = Point4F(-light->mDirection.x, -light->mDirection.y, -light->mDirection.z, 0.0f);
}

Then add the public static member to the LightManager:

...
public:
static ColorF sgGlobalBlendColor;
...
And remember to add the static object with the others in sgLightManager.cc:

...
ColorF LightManager::sgGlobalBlendColor = ColorF(1.0, 1.0, 1.0, 1.0);
...
#6
10/28/2006 (11:48 pm)
Because the member is now in LightManager instead of sgLightManager you'll need to change your references to:

LightManager::sgGlobalBlendColor = ...
#7
10/29/2006 (6:58 am)
Wow, fantastic....thanks so much, John! After looking through the code I was getting the impression it was taken out entirely, but I wasn't certain. This is incredibly helpful.
#8
10/29/2006 (9:25 am)
@John: I'm assuming that the references to sgLightManager in the first part of the code changes above should be LightManager, correct?

Also, the new changes to 1.5 also break the GuiObjectView code. That code also uses references to sgLightManager, but I'm not entirely sure why it doesn't work since the sgLightManager is declared in GuiObjectView.h...
#9
10/29/2006 (11:12 am)
Whoops, I just fixed that to avoid confusion.


Regarding GuiObjectView; I just posted some info on that (can't find the thread and the site search isn't working again), but basically there were issues with the core control related to DTS sequence loading, so it was left out of the engine until someone addresses those.

The control should use LightManager, which is the new replacement for TLK's sgLightManager and TGE 1.0-1.4's LightManager (keeping the LightManager name was better than replacing all references to it, which would have made merging TGE 1.5 with an existing project/game *much* more involved).

The new LightManager has methods similar to those used in 1.4, so you'll need to change the calls to addLight, installGLLights, ... (setMaxGLLights can be removed).
#10
10/29/2006 (7:46 pm)
By the way, the fix for Object Highlighting is working great, thanks.

The fix for GuiObjectView, however, is proving a bit more challenging. I saw your other post, but there seems to be an issue with switching some of the calls. I'll keep plugging away at it.