Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filling a circle with alpha blending will create visible stripes #71

Open
joshinils opened this issue Feb 19, 2020 · 0 comments
Open

Filling a circle with alpha blending will create visible stripes #71

joshinils opened this issue Feb 19, 2020 · 0 comments

Comments

@joshinils
Copy link

joshinils commented Feb 19, 2020

the problem seems to stem from the fact that in the FillCircle method the individual lines sometimes get draw multiple times:
grafik

here is my modified example code to generate the above image:

#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"

class Example : public olc::PixelGameEngine
{
public:
	Example()
	{
		sAppName = "Example";
	}

public:
	bool OnUserCreate() override
	{
		// Called once at the start, so create things here
		olc::PixelGameEngine::SetPixelMode(olc::Pixel::ALPHA);
		this->Clear(olc::BLACK);
		FillCircle(100, 100, 60, olc::Pixel(255,0,0,200));
		return true;
	}

	bool OnUserUpdate(float fElapsedTime) override
	{
		// called once per frame
		return true;
	}
};


int main()
{
	Example demo;
	if (demo.Construct(256, 256, 2, 2))
		demo.Start();

	return 0;
}

and here the modified FillCircle to show that is has overdrawn:

	void PixelGameEngine::FillCircle(int32_t x, int32_t y, int32_t radius, Pixel p)
	{
		// Taken from wikipedia
		int x0 = 0;
		int y0 = radius;
		int d = 3 - 2 * radius;
		if (!radius) return;

		static std::vector<std::vector<int>> hits(260, std::vector<int>(260,0));
		auto drawline = [&](int sx, int ex, int ny)
		{
			for (int i = sx; i <= ex; i++)
			{
				hits[i][ny]++;
				Draw(i, ny, p);
			}
		};

		while (y0 >= x0)
		{
			// Modified to draw scan-lines instead of edges
			drawline(x - x0, x + x0, y - y0);
			drawline(x - y0, x + y0, y - x0);
			drawline(x - x0, x + x0, y + y0);
			drawline(x - y0, x + y0, y + x0);
			if (d < 0) d += 4 * x0++ + 6;
			else d += 4 * (x0++ - y0--) + 10;
		}

		for(auto v : hits)
		{
			for(int i : v)
				std::cout << i;
			std::cout << std::endl;
		}
	}

on the console (background in the image) one can clearly see multiple calls to draw for the same position, because alphablending is enabled this leads to brighter reds than at other positions, hence the stripes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant