GLSL syntax error

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
Zarty55
Citizen
Posts: 79
Joined: Thu Jul 25, 2013 2:36 am

GLSL syntax error

Post by Zarty55 »

Huh, I'm trying to learn GLSL, I know how it's suposed to work and all, my big problem is with it's syntax. I was trying to do a voronoi-kinda-thing, I did it in Lua (Which was really slow) so I thought about trying to use a shader with all those multiple tasty cores and stuff. Here is what I have:

Code: Select all

shad = lg.newShader([[
extern vec2 points[]; //It's supposed to be a array which holds points in the screen
extern vec4 colors[]; //supposed to be the color of the points
extern int pointsAmount; // amount of points
number di = 100000; //the smallest distance to the pixel
int point = 0.; // the point which is closest to the pixel

vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) // line that the error occurs aparently
for(int i = 0; i<pointsAmount; i++){
  number dista = ((screen_coords[1]-points[i][1])^2 - (screen_coords[2]-points[i][2])^2);
  if dista < di then
    di = dista;
    point = i;
  }
}]])
I get this error:
Cannot compile pixel shader code:
Line 8: error: syntax error, unexpected reserved word "for", expecting ',' or ';' at token "for"
As you can see, the problem is in the newShader function. So there's nothing to do with the externals or anything else really. What's the problem in the code? Again, this is pretty much the first code I'm doing in GLSL, please be patient D:
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: GLSL syntax error

Post by Germanunkol »

You need to start the function body with a {

Code: Select all

vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords)
{    //this line was missing
for(int i = 0; i<pointsAmount; i++){
...
Also, the syntax for an if is:

Code: Select all

if ( dista < di ) // no then!
{
And then you'll probably need to close one more } (the function body again) at the end, before the ]].

GLSL uses something very close to C-Syntax:

Code: Select all


vec4 effect( ... )
{
     if (blah) {
          ...
    }
    while( blooh ) {
         ....
    }
    return foo;
}
After this, the shader still won't compile. You seem to be attempting to use a dynamic array, which you can't use in glsl.
If you want to pass a large amount of data to the shader, passing an image is usually a better way. You can then index the points in that image using texture2D( image, position ).
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
Zarty55
Citizen
Posts: 79
Joined: Thu Jul 25, 2013 2:36 am

Re: GLSL syntax error

Post by Zarty55 »

THANK YOU! I just maded it work and it's just soooo awesome to make something like that work hahaha :D
Attachments
Voronoi.love
(1.96 KiB) Downloaded 65 times
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: GLSL syntax error

Post by Ref »

I get:

Code: Select all

Error:main.lua:24: Sorry, your graphics card does not support shaders.
Traceback
[C]: in function 'newShader'
main.lua:24: in function 'load'
[C]: in function 'xpcall'
Better not tell all my other shaders they aren't supported.
(I'm on Windows 7 )
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: GLSL syntax error

Post by Germanunkol »

Great work, considering you're new to GLSL!

It's looking quite good, well done.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
Zarty55
Citizen
Posts: 79
Joined: Thu Jul 25, 2013 2:36 am

Re: GLSL syntax error

Post by Zarty55 »

@Ref

Hahaha that's weird. I have no idea why that happened :p

@Germanunkol

Thanks! I actually had already put a lot of time thinking in shaders, I'm just not used to the syntax. I kinda know what they are and how they work. I'm impressed with myself to be fair :p
Post Reply

Who is online

Users browsing this forum: slime and 9 guests