Shaders, Help!

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
vicit
Prole
Posts: 3
Joined: Thu Feb 22, 2024 7:40 am

Shaders, Help!

Post by vicit »

Hello I'm new to this forum and I want to know if anyone has an idea on what is wrong with the shader code i provided,(comment lines will point to location of code 'not part of the code) for I can't seem to draw a triangle on the screen, It worked well for other tests i've done like an rgb altering effect, but it doesn't seem to be working for me :(, the error given when trying to run|bad argument #1 to 'newShader' (missing 'position' or 'effect' function?))|

function love.load()
-- Define vertices of the triangle
vertices = {
{-0.5, -0.5},
{0.5, -0.5},
{0.0, 0.5}
}
------------------------------------------------------------
-- Load the shader
shaderCode = [[
// Vertex shader
attribute vec4 position;
void main() {
gl_Position = position;
}

// Fragment shader
precision mediump float;
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); // Red color
}
]]
shader = love.graphics.newShader(shaderCode)
-----------------------------------------------------------
-- Create a Mesh with position attribute
mesh = love.graphics.newMesh(vertices, "triangles")
mesh:setVertexAttribute("position", vertices, "float", 2) -- Set vertex attribute
end

function love.draw()
-- Set the shader
love.graphics.setShader(shader)

-- Draw the mesh
love.graphics.draw(mesh)

-- Reset shader
love.graphics.setShader()
end
Ross
Party member
Posts: 100
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: Shaders, Help!

Post by Ross »

Take a look at the documentation for love.graphics.newShader again.

As the error message says, you need to provide either a "position" function or an "effect" function (or both). You are instead defining two "main" functions in the same string.

Additionally, if you want to set both shader programs you must either pass in two strings (one for the vertex shader and one for the fragment shader), or use "#ifdef" to separate them within the same string (there are multiple examples on the wiki page).
vicit
Prole
Posts: 3
Joined: Thu Feb 22, 2024 7:40 am

Re: Shaders, Help!

Post by vicit »

Ross wrote: Thu Feb 22, 2024 12:51 pm Take a look at the documentation for love.graphics.newShader again.

As the error message says, you need to provide either a "position" function or an "effect" function (or both). You are instead defining two "main" functions in the same string.

Additionally, if you want to set both shader programs you must either pass in two strings (one for the vertex shader and one for the fragment shader), or use "#ifdef" to separate them within the same string (there are multiple examples on the wiki page).
Wow, thank you so much, I didn't even notice that, it's my first time learning shaders here and struggled to find resources on this topic , Had started glsl a few weeks ago, and was at a lost with how different programming glsl can be in C vs any other language/platform
Ross
Party member
Posts: 100
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: Shaders, Help!

Post by Ross »

You're welcome. I'm glad that helped.
User avatar
thestarknight
Prole
Posts: 9
Joined: Sun Nov 12, 2023 1:19 pm
Contact:

Re: Shaders, Help!

Post by thestarknight »

Shhaders!

I had the same problem when I started with shaders too, after reading " A Beginner's Guide to Shaders " I managed to code them right in Love.

Here is the link https://blogs.love2d.org/content/beginn ... de-shaders

The reason is Love code for shaders differs a little bit from the GLSL one, so to implement a shader you you must apply some changes.
You use effect instead of main and for example:
sampler2D is called Image
texture2D(tex, uv) is Texel(tex, uv)
and so on..

But you find the basic in that guide that is specific for Love.

Hope it helps ^^
vicit
Prole
Posts: 3
Joined: Thu Feb 22, 2024 7:40 am

Re: Shaders, Help!

Post by vicit »

Thank you all for the help, took a bit of a while to get the hang of it, here's a '3d' oscillating ball that I made with a flat circle 'orbiting around it'

Is there a forum where i can showcase?
Attachments
shader.love.zip
love2d file
(1.44 KiB) Downloaded 173 times
Image of shader in action
Image of shader in action
Screen Shot 2024-02-27 at 12.03.25 AM.png (129.32 KiB) Viewed 3402 times
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests