Page 2 of 5

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Posted: Sat Dec 06, 2014 6:02 pm
by s-ol
arampl wrote:glReadPixels & glColor(single value) will solve so many problems...
This functions is so "base" functions of OpenGL...

Of course I can live without them...

P.S. :(
Dude, you can do

Code: Select all

col = { 255, 0, 0 }
love.graphics.setColor( col )
And if that wasnt possible you could still do

Code: Select all

love.graphics.setColor( unpack(col) )
Please learn lua and how to read the documentation (this stuff is written down in the wiki) before you complain about missing features.

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Posted: Sat Dec 06, 2014 11:51 pm
by arampl
Sigh.
I'm not talking about how to do it. There are thousand ways in Lua to convert something to something.
Not sure, but glColor3uiv / glColor4uiv can even be hardware accelerated.
It's about speed optimization (plus code simplicity integrated as a side effect).
Don't you agree that object picking is not so rare operation in most applications including games of course?
And games need to test mouse over object many times per second.

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Posted: Sun Dec 07, 2014 12:07 am
by arampl
S0lll0s wrote:
arampl wrote:glReadPixels & glColor(single value) will solve so many problems...
This functions is so "base" functions of OpenGL...

Of course I can live without them...

P.S. :(
Dude, you can do

Code: Select all

col = { 255, 0, 0 }
love.graphics.setColor( col )
And if that wasnt possible you could still do

Code: Select all

love.graphics.setColor( unpack(col) )
Please learn lua and how to read the documentation (this stuff is written down in the wiki) before you complain about missing features.
Bro, please have a look at the code I posted first, may be you just don't know what I'm talking about here.

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Posted: Sun Dec 07, 2014 12:21 am
by arampl

Code: Select all

o = {} -- table with our buttons

-- fragment shader, only non-transparent pixels are allowed in the stencil
mask_effect = love.graphics.newShader [[ vec4 effect (vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {if(Texel(texture, texture_coords).a == vec3(0.0)) discard; return vec4(1.0);} ]]

function love.load()

	button = love.graphics.newImage("button.png") -- button image with transparency

	-- creating several buttons, random coords, width & height equal to button image size
	for i = 1, 5 do
		o[i] = {}
		o[i].x = love.math.random(100, 800)
		o[i].y = love.math.random(100, 600)
		o[i].image = button
		o[i].w = o[i].image:getWidth()
		o[i].h = o[i].image:getHeight()
	end

	-- preparing canvas (to getPixel(x,y) later)
	offw, offh = love.window.getDesktopDimensions()
	pickbuf = love.graphics.newCanvas(offw, offh)

end

-- stencil function, we must draw all our buttons in the loop
function stencil()
	love.graphics.setShader(mask_effect)
	love.graphics.setColor(255, 255, 255, 255)
	love.graphics.setScissor(mx - 4, my - 4, 8, 8)
	for k, v in ipairs(o) do
		love.graphics.draw(v.image, v.x, v.y)
	end
	love.graphics.setScissor(0, 0, offw, offh)
	love.graphics.setShader()
end

-- now we draw our buttons as a simple rectangles, setting next color in each iteration
-- from color(0,0,1) to (255,255,255)
function pickrender()
	love.graphics.clear()
	love.graphics.setStencil(stencil)
	love.graphics.setScissor(mx - 4, my - 4, 8, 8)
	for k, v in ipairs(o) do
		love.graphics.setColor(math.floor(k / 2 ^ 16), math.floor(k / 2 ^ 8) % 2 ^ 8, k % 2 ^ 8, 255)
		love.graphics.rectangle("fill", v.x, v.y, v.w, v.h)
	end
	love.graphics.setScissor(0, 0, offw, offh)
end

-- here we get pixel color under cursor and convert it into object's id
function pick()
	mx, my = love.mouse.getPosition()
	love.graphics.setScissor(mx - 4, my - 4, 8, 8)
	pickbuf:renderTo(pickrender)
	mr, mg, mb = pickbuf:getPixel(mx, my)
	curr = mb + mg * 255 + mr * 65535
	love.graphics.setScissor(0, 0, offw, offh)
end

function love.draw()

	pick() -- do we have button under cursor?

	love.graphics.setColor(255, 255, 255, 255)

	-- draw buttons normally
	for _, v in ipairs(o) do
		love.graphics.draw(v.image, v.x, v.y)
	end

	-- here goes something if we have button under cursor
	if curr ~= 0 then
		love.graphics.setColor(128, 0, 0, 128)
		love.graphics.draw(button, o[curr].x, o[curr].y)
	end
end

function love.keypressed(key, isRepeat)
	if key == "escape" then
		love.event.quit()
	end
end

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Posted: Sun Dec 07, 2014 12:48 am
by s-ol
arampl wrote:glReadPixels & glColor(single value) will solve so many problems...
This functions is so "base" functions of OpenGL...

Of course I can live without them...

P.S. :(
I don't see why you would need this at all.

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Posted: Sun Dec 07, 2014 12:56 am
by arampl
If we have setColor(single value) then instead of writing

Code: Select all

for k, v in ipairs(o) do
      love.graphics.setColor(math.floor(k / 2 ^ 16), math.floor(k / 2 ^ 8) % 2 ^ 8, k % 2 ^ 8, 255)
...
we can just write

Code: Select all

for k, v in ipairs(o) do
      love.graphics.setColor(k)
...
EDIT: Now imagine that we have thousand objects in table "o".

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Posted: Sun Dec 07, 2014 12:59 am
by arampl
glReadPixels - because without canvas or getting screenshot we can't get pixel color

Getting screenshot 60 times per frame to read single pixel color is...

And canvas is not supported on every platform.

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Posted: Sun Dec 07, 2014 1:15 am
by Azhukar
arampl wrote:And canvas is not supported on every platform.
I see this said a lot. Has anyone actually looked up how old hardware someone must be using for it to not support canvas? How about spriteBatches? Is it mostly crappy mobile devices?

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Posted: Sun Dec 07, 2014 1:30 am
by arampl
S0lll0s wrote:
arampl wrote:glReadPixels & glColor(single value) will solve so many problems...
This functions is so "base" functions of OpenGL...

Of course I can live without them...

P.S. :(
I don't see why you would need this at all.
You mean I can place all this stuff onto shader side?
You just Genius! I feel enlightened now! :)

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Posted: Sun Dec 07, 2014 2:52 am
by s-ol
arampl wrote:If we have setColor(single value) then instead of writing

Code: Select all

for k, v in ipairs(o) do
      love.graphics.setColor(math.floor(k / 2 ^ 16), math.floor(k / 2 ^ 8) % 2 ^ 8, k % 2 ^ 8, 255)
...
we can just write

Code: Select all

for k, v in ipairs(o) do
      love.graphics.setColor(k)
...
EDIT: Now imagine that we have thousand objects in table "o".
So just store the values in a table!

Code: Select all

local o = { {255,0,0}, {255,255,0} }
for i,c in ipairs(o) do
  love.graphics.setColor( c )
  love.graphics.circle( "fill", i*100, 100, 40 )
end
And why would you need to get the pixel color? If its a colorpicker you have the formula anyway, if its for drawing operations shaders are a better choice. And they are mostly supported, except on chipsets in netbooks maybe.