Jump to content

Buffer Alpha


SpiderPig
 Share

Recommended Posts

I'm using a buffer to draw some 2D graphics too but can't seam to clear the buffer with transparency.  I looked at the forum here for the info regarding buffers and 2D drawing; 

I tried clearing the buffer with this code but only get a white screen;

//App Start
Buffer* topLevelBuffer = Buffer::Create(1024, 768, 1, 0);

//Draw Loop
topLevelBuffer->SetColor(1, 1, 1, 0);
topLevelBuffer->Clear();
topLevelBuffer->Enable();

DrawUI(topLevelBuffer);

_context->Enable();
_context->DrawImage(topLevelBuffer->GetColorTexture(), 0, 0);

 

 

Anyone had any luck with clearing to a transparent buffer?

Link to comment
Share on other sites

You are setting the buffer color to white when you clear it which results in a white background. Same would happen if you set the color to red and you cleared it would result in a red background. Try setting the color to (0,0,0,0). Also, you are using 5 parameters in your setcolor() - not sure the mode parameter is a part of the buffer's SetColor()? 

But it looks like you are also missing setting the blendmode to alpha prior to clearing? Maybe have to set the alpha to 1 prior to clearing for your needs... hard to say since you don't give us enough information to understand what you are trying to draw. Look at my example post from the post you linked to:

    Buffer:SetCurrent(mybuffer)
	context:SetBlendMode(Blend.Alpha)
	context:SetColor(0,0,0,0)
	mybuffer:Clear()
	context:SetColor(1,0,0,1)
	context:DrawText(os.date("%I:%M:%S"),25,45)
	tex4 = mybuffer:GetColorTexture(0)
	mat1:SetTexture(tex4,4)
	Buffer:SetCurrent(buffer)
	context:Sync(true)

 

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

I accidentally added an extra 1 in SetColor() when typing the post, sorry.  Besides that, all the info is there.  I've created a buffer and am drawing it to the context (not a materiel).

The reason is so that any UI that may overlap other UI can be drawn to this buffer and then drawn last with context->DrawImage() on top of the whole screen.

The drawing to the buffer works fine, but the buffer has to be cleared every frame so that previous drawings are gone.

Which brings me to asking, how to clear the buffer with transparency because SetColor(1,1,1,0) (I think) should set it to complete transparency regardless of the RGB values.

But it's not.

I tried your suggestion with setting it as the current buffer first then calling context->SetBlendMode() but it still is just the RGB values, no alpha.

If I draw the newly made buffer without clearing first, the image is totally transparent and only shows the drawings like it should, but it needs to be cleared ready for the next frame.

Link to comment
Share on other sites

You need to post an example of your problem because what you are describing is exactly what my code does. If you notice from the post pic the text is being drawn onto the buffer and then the color texture is being set to the material. If there was no transparency on that "text buffer" then you would not see the concrete texture on the box.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

I had a GUI class before the in built system was available and I'd rather not go through and replace everything if I can help it.

Run this code and you'll see the issue.  Your code macklebee draws the text with transparency on a solid background.  I want the entire image to be completely transparent.  Then I will draw what I need to on it.  Then it will be drawn over the top of previously drawn items.

Buffer* buffer;

bool App::Start()
{
	window = Window::Create("Test");
	window->Show();

	context = Context::Create(window);

	world = World::Create();

	Camera* camera = Camera::Create();
	buffer = Buffer::Create(1024, 768, 1, 0);
	

	return true;
}

bool App::Loop()
{
	if (window->KeyHit(Key::Escape) == true) {
		return false;
	}

	world->Update();
	world->Render();

	context->SetBlendMode(Blend::Alpha);

	buffer->Enable();
	buffer->SetColor(0, 0, 0, 0);
	buffer->Clear();						//buffer alpha is not being cleared

	context->SetColor(1, 0, 0, 1);
	context->DrawRect(80, 50, 100, 100);

	context->Enable();
	context->SetColor(1, 1, 1, 1);
	context->DrawRect(10, 10, 100, 100);	//white rectangle not showing.

	context->DrawImage(buffer->GetColorTexture(), 0, 0);

	context->SetBlendMode(Blend::Solid);
	context->Sync();

	return true;
}

If I set the buffers alpha to 0, shouldn't it be transparent except for the red rectangle?

Link to comment
Share on other sites

It seems the SetColor-Overloads that take Integers as arguments are broken and always pass the same alpha value that was passed as the last. Using either a float-overload or the Vec4-overload yields the expected results:

#include "App.h"

using namespace Leadwerks;

App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {}

App::~App() { delete world; delete window; }

Buffer* buffer;

bool App::Start()
{
	window = Window::Create("Test");
	window->Show();

	context = Context::Create(window);

	world = World::Create();

	camera = Camera::Create();
	buffer = Buffer::Create(1024, 768, 1, 0);

	return true;
}

bool App::Loop()
{
	if (window->KeyHit(Key::Escape) == true) {
		return false;
	}

	world->Update();
	world->Render();

	
	context->SetBlendMode(Blend::Solid);
	buffer->Enable();
	//buffer->SetColor(Vec4(10, 1, 0, 0));
	buffer->SetColor(10.0f, 1.0f, 0.0f, 0.0f);
	buffer->Clear(Buffer::Color);
	//context->SetColor(Vec4(1, 0, 0, 1));
	context->SetColor(1.0f, 0.0f, 0.0f, 1.0f);
	static int x = 0;
	context->DrawRect(x++ % 200, 50, 100, 100);

	context->SetBlendMode(Blend::Alpha);
	context->Enable();
	context->SetColor(1.0f, 1.0f, 1.0f, 1.0f);
	context->DrawRect(10, 10, 100, 100);

	context->DrawImage(buffer->GetColorTexture(), 0, 0);

	context->SetBlendMode(Blend::Solid);
	context->Sync();

	return true;
}

(I made the red rectangle move by using the static x-variable, so you can see that the buffer is indeed cleared).

 

@Josh Should I file a bug-report for this?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...