Alpha map materials

Discuss artwork in all of its forms here, from level design to GUI button themes. New MODs planning, work-in-progress and other samples are welcome in this forum as well.
Post Reply
User avatar
BRabbit
Posts:28
Joined:2010-08-16, 19:26
Location:Moscow, Russia
Alpha map materials

Post by BRabbit » 2010-11-30, 02:25

It is possible to use separate alpha maps for the materials? If so, what is the keyword used in that case? If not, which is the right way to make textures with alpha colors (like magenta, blue, or black). I looked at the Material System documentation but it didn't work. It is also possible to use transparent PNGs instead of alpha maps?

Oh, and also how can I change the red cross cursor in the main menu? (I would like to have the default pointer system cursor)
Last edited by BRabbit on 2010-11-30, 19:05, edited 1 time in total.
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: Alpha map materials

Post by Carsten » 2010-11-30, 12:09

BRabbit wrote:It is possible to use separate alpha maps for the materials? If so, what is the keyboard used in that case? If not, which is the right way to make textures with alpha colors (like magenta, blue, or black). I looked at the Material System documentation but it didn't work. It is also possible to use transparent PNGs instead of alpha maps?
The alpha channel is normally provided by the diffuse-map, which not only stores the RGB channels, but also alpha (only possible with .png or .tga bitmap formats).

For examples where the diffuse-map also stores alpha, see
http://trac.cafu.de/browser/cafu/trunk/ ... lants.cmat

Note that you see nothing special, it's all in a single line, e.g.

Code: Select all

diffusemap  Plants/Ahorn/AhornLeaves.png
For examples for the blue2alpha() function, the "older" technique, see
http://trac.cafu.de/browser/cafu/trunk/ ... s/Kai.cmat

blue2alpha() inserts the alpha values into the specified bitmap as an afterthought.

Note that you can also set an alpha value for the entire texture; for examples see
http://trac.cafu.de/browser/cafu/trunk/ ... quids.cmat


The second important step how the alpha channel is used / activated:
  • You can either use alphaTest, examples for which are in the above mentioned Kai.cmat file.
  • Or use blendFunc src_alpha one_minus_src_alpha, this blends the texture according to alpha channel and is probably what you want.
Oh, and also how can I change the red cross cursor in the main menu? (I would like to have the default pointer system cursor)
Well, yes, the pointer is not particularly fancy, its rendering code is in GuiImplT::Render().

Note however that the GUI cursors do not only work in the main menu, but they are used for 3D world GUIs as well. Thus you cannot simply remove the mouse cursor rendering code in the function above and "un-hide" the true system pointer.
What you can do though is replace the above code with rendering a rectangle that shows a nice mouse pointer image (ideally with alpha channel :cheesy:).
(To make it perfect, it's size might e.g. depend on GuiImplT::EntityName=="", so that the pointer is larger for 3D world GUIs.)
:up:
Best regards,
Carsten
User avatar
BRabbit
Posts:28
Joined:2010-08-16, 19:26
Location:Moscow, Russia

Re: Alpha map materials

Post by BRabbit » 2010-12-01, 04:52

I have done it! :groupwave2: .
Here's it, you Carsten could put this in Cafu or maybe some other user wants to change the cursor too:

Code: (GuiImpl.cpp)

Code: Select all

void GuiImplT::Render()
{
    RootWindow->Render();

    if (MouseIsShown)
    {
        // TODO !!!!!!!!!!!
        // All meshes should be setup ONCE in the constructor!!
        // (Should they? Even if we plan to add scripting? Yes, I think they should, Render() is called far more often than anything else.)

        // Render the background.
     // MatSys::Renderer->SetCurrentAmbientLightColor(BackColor);
        MatSys::Renderer->SetCurrentMaterial(Renderer->RegisterMaterial(MaterialManager->GetMaterial("Gui/Cursor")));

		static MeshT CrossMesh(MatSys::MeshT::Polygon);
        CrossMesh.Vertices.Overwrite();
        CrossMesh.Vertices.PushBackEmpty(4);     // Just a single quad for the background rectangle.

        /*for (unsigned long VertexNr=0; VertexNr<CrossMesh.Vertices.Size(); VertexNr++)
        {
            CrossMesh.Vertices[VertexNr].Color[0]=0.0f;
            CrossMesh.Vertices[VertexNr].Color[1]=0.0f;
            CrossMesh.Vertices[VertexNr].Color[2]=0.0f;
            CrossMesh.Vertices[VertexNr].Color[3]=0.0f; // This is the alpha I guess
        }*/ 

		float b; // The cursor size.

		if(this->EntityName != "") // Carsten's suggestion
		{
			b = 18.0f; // This an in-game GUI 
		}
		else
		{
			b = 9.0f;    
		}

        // The tex-coord currently don't make much sense... Now it does!
        CrossMesh.Vertices[0].SetOrigin(MousePosX, MousePosY); CrossMesh.Vertices[0].SetTextureCoord(0.0f, 0.0f);
        CrossMesh.Vertices[1].SetOrigin(MousePosX+b, MousePosY); CrossMesh.Vertices[1].SetTextureCoord(1.0f, 0.0f);
        CrossMesh.Vertices[2].SetOrigin(MousePosX+b, MousePosY+b); CrossMesh.Vertices[2].SetTextureCoord(1.0f, 1.0f);
        CrossMesh.Vertices[3].SetOrigin(MousePosX, MousePosY+b); CrossMesh.Vertices[3].SetTextureCoord(0.0f, 1.0f);

       MatSys:: Renderer->RenderMesh(CrossMesh);
    }
}
Then also add a definition in Games/DeathMatch/Materials/gui.cmat (it could be in any cmat too) at the end for the Gui/Cursor material:

Code: Select all

Gui/Cursor
{
	diffusemap GUIs/MainMenu/cursor.png, noCompression, wrapS clampToEdge, wrapT clampToEdge, noScaleDown
	blendFunc src_alpha one_minus_src_alpha
}
And finally copy this cursor.png file in Games/DeathMatch/GUIs/MainMenu/.
cursor.png
cursor.png (2.92KiB)Viewed 14024 times
That's it! :up: (Should this be moved to a new different topic?)
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: Alpha map materials

Post by Carsten » 2010-12-01, 22:07

Hi,
BRabbit wrote:Here's it, you Carsten could put this in Cafu or maybe some other user wants to change the cursor too:
Many thanks, the Cafu GUIs certainly look a lot better this way! :-D

I've just implemented this in Cafu as well (with some variations to the code and pointer image though), see http://trac.cafu.de/changeset/195 for the details.

I'd recommend that you use the Cafu implementation, too, because your line

Code: Select all

MatSys::Renderer->SetCurrentMaterial(Renderer->RegisterMaterial(...));
causes a major resource leak.
That's it! :up: (Should this be moved to a new different topic?)
I guess it's ok, as it still has a strong relation to (as a use-case of) making use of the alpha channel.
Best regards,
Carsten
User avatar
BRabbit
Posts:28
Joined:2010-08-16, 19:26
Location:Moscow, Russia

Re: Alpha map materials

Post by BRabbit » 2010-12-01, 22:54

Your implementation is certainly better! :wink:
Carsten wrote:I'd recommend that you use the Cafu implementation, too, because your line causes a major resource leak.
Oh, I thought that the MaterialManager automatically removed the registered materials...
Anyway, thanks for giving it a shot!
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: Alpha map materials

Post by Carsten » 2010-12-02, 00:43

BRabbit wrote:Oh, I thought that the MaterialManager automatically removed the registered materials...
Not the MatSys::RenderMaterialT's, which require that each call to

Code: Select all

X = MatSys::Renderer->RegisterMaterial(...)
is paired with a call to

Code: Select all

MatSys::Renderer->FreeMaterial(X)
just as with new and delete.

You could try to keep the RenderMaterialT pointers in boost::shared_ptr<T>'s though. For example:

Code: Select all

boost::shared_ptr<MatSys::RenderMaterialT> RM(MatSys::Renderer->RegisterMaterial(...), MatSys::Renderer->FreeMaterial);
where the second parameter is our custom deleter.

I think that this should work, but I've not tried - Cafu does currently not employ boost.
Best regards,
Carsten
Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests