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

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.
Post Reply
User avatar
IsawU
Prole
Posts: 16
Joined: Sat May 02, 2015 6:26 pm

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

Post 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 8404 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?
Attachments
code.love
(931 Bytes) Downloaded 170 times
Last edited by IsawU on Sat Jul 29, 2017 12:36 pm, edited 1 time in total.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

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

Post 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.
User avatar
IsawU
Prole
Posts: 16
Joined: Sat May 02, 2015 6:26 pm

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

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

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

Post 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.
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

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

Post 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
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

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

Post 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
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
IsawU
Prole
Posts: 16
Joined: Sat May 02, 2015 6:26 pm

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

Post 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)?
User avatar
IsawU
Prole
Posts: 16
Joined: Sat May 02, 2015 6:26 pm

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

Post 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 :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Ross and 78 guests