Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Post 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.
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Post 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.
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Post 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
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Post 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".
Last edited by arampl on Sun Dec 07, 2014 1:01 am, edited 1 time in total.
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Post 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.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Post 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?
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Post 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! :)
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Wrappers for glReadPixel, glColor3uiv / glColor4uiv

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 15 guests