Page 1 of 1

[Solved] Can a shader variable have different value for each vertex?

Posted: Fri Jul 28, 2017 5:00 pm
by IsawU
The way I understand it, if I have 3-vertex polygon and I draw it, the VERTEX shader runs 3 times (once for each vertex).

We can send variable values to the shader just before we call love.graphics.polygon(...). How do I ensure that for each vertex I get a different variable value.

As an example I have this code:
straightforward define vertex position, X offsets and the shader:

Code: Select all

poly = {100, 200, 600, 300, 300, 500}	--polygon coordinates
xoffsets = {100, -100, 20}		--the offsets (as a variable mostly because I need to access them from multiple places)
me = love.graphics.newShader(--[[code below]])
love.draw() function showing what I get VS what I want:

Code: Select all

love.graphics.setColor(255, 255, 255)
--no shader
love.graphics.polygon('fill', poly)
	
love.graphics.setColor(255, 0, 0)
love.graphics.setShader(me)
	m:send("xoffsets", xoffsets[1], xoffsets[2], xoffsets[3])
	me:send("maxp", 2)
	
	--what I get
	love.graphics.polygon('line', poly)
love.graphics.setShader()
	
love.graphics.setColor(0, 255, 0)
--what I want, not a correct solution though
love.graphics.polygon('line', poly[1]+xoffsets[1], poly[2], poly[3]+xoffsets[2], poly[4], poly[5]+xoffsets[3], poly[6])	
and the shader code:

Code: Select all

extern float xoffsets[3];		//array for X offsets
int pointer;				//(pseudo)pointer to xoffsets array
extern int maxp;			//maximum value of pointer (we have this many vertices on this specific shape)
	  
#ifdef VERTEX
	vec4 position( mat4 transform_projection, vec4 vertex_position )
	{
		float xoffset = xoffsets[pointer];
				
		pointer++;
		if (maxp < pointer) {
			pointer = 0;
		}
				
		vertex_position = vec4(vertex_position[0]+xoffset, vertex_position[1], vertex_position[2], vertex_position[3]);
				
		return transform_projection * vertex_position;
	}
#endif
		 
#ifdef PIXEL
	vec4 effect( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords )
	{
			//always TRUE, need to have this, otherwise maxp doesn't exist (defined but not used) HELP??
		if (maxp < 1000) {
			vec4 texcolor = Texel(texture, texture_coords);
			return texcolor * color;
		}
	}
#endif
Visual interpretation of RAW(white) VS what I want(green) VS what I get(red) You will get this sceen if you run the code.love.
screenshot-2017-07-28-18-13-40.png
screenshot-2017-07-28-18-13-40.png (13.26 KiB) Viewed 8860 times
I do understand that this cannot work, it's just the closest I got.

Can this be done in shaders, and if so, how can I deliver theese additional attributes?

Re: Can a shader variable have different value for each vertex?

Posted: Fri Jul 28, 2017 5:50 pm
by raidho36
What you're looking for is vertex attributes. However I'm pretty sure what you really need is a transformation matrix, it's just you don't see how to use it so you instead resort to inventing some square wheels.

Re: Can a shader variable have different value for each vertex?

Posted: Sat Jul 29, 2017 2:07 am
by IsawU
You're spot on, yet wouldn't a transformation matrix be a mathematical description of how to move all the vertices, resulting in a triangle that has a shape of white polygon (such as red triangle)?

Square wheelely talking, how can I implement vertex attributes in LÖVE, because from reading the GLSLangSpec I got the idea that I need them too.

Re: Can a shader variable have different value for each vertex?

Posted: Sat Jul 29, 2017 5:37 am
by raidho36
I think matrix is what you need because it appears that you simply want the shape distorted, not directly manipulate each individual vertex.

You create a mesh and attach additional attributes using standard functionality, then you draw that mesh.

Re: Can a shader variable have different value for each vertex?

Posted: Sat Jul 29, 2017 6:37 am
by MasterLee
raidho36 wrote: Fri Jul 28, 2017 5:50 pm What you're looking for is vertex attributes. However I'm pretty sure what you really need is a transformation matrix, it's just you don't see how to use it so you instead resort to inventing some square wheels.
What is wrong with square wheels?
Image

Re: Can a shader variable have different value for each vertex?

Posted: Sat Jul 29, 2017 8:43 am
by zorg
MasterLee wrote: Sat Jul 29, 2017 6:37 am What is wrong with square wheels?
Tehnically those are the curved triangles present in wankel engines... and are in fact not squares at all.

Re: Can a shader variable have different value for each vertex?

Posted: Sat Jul 29, 2017 11:34 am
by MasterLee
zorg wrote: Sat Jul 29, 2017 8:43 am
MasterLee wrote: Sat Jul 29, 2017 6:37 am What is wrong with square wheels?
Tehnically those are the curved triangles present in wankel engines... and are in fact not squares at all.
Well ok they are triangles but triangles are the opposite of a circle at least by the number of coners
real squares are possible too:
Image
or when driven on special floor
https://www.youtube.com/watch?v=LgbWu8zJubo

Re: Can a shader variable have different value for each vertex?

Posted: Sat Jul 29, 2017 11:43 am
by zorg
Technically, the opposite of an infinity-gon should be one with zero points... not that it could exist.
Image
2 sides is the lowest i believe.

Re: Can a shader variable have different value for each vertex?

Posted: Sat Jul 29, 2017 12:15 pm
by IsawU
raidho36 wrote: Sat Jul 29, 2017 5:37 am You create a mesh and attach additional attributes using standard functionality, then you draw that mesh.
Oh, right. I was wondering what those difficult ways to create a new Mesh meant. Thanks very much. Can this be done with lines and points too (and love.graphics.polygon as such)?

Re: Can a shader variable have different value for each vertex?

Posted: Sat Jul 29, 2017 12:18 pm
by IsawU
zorg wrote: Sat Jul 29, 2017 11:43 am Image
2 sides is the lowest i believe.
Would work if your daily commute to work was about half a meter :)