Page 1 of 1

Shaders, Help!

Posted: Thu Feb 22, 2024 7:52 am
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

Re: Shaders, Help!

Posted: Thu Feb 22, 2024 12:51 pm
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).

Re: Shaders, Help!

Posted: Thu Feb 22, 2024 9:10 pm
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

Re: Shaders, Help!

Posted: Sat Feb 24, 2024 1:18 pm
by Ross
You're welcome. I'm glad that helped.

Re: Shaders, Help!

Posted: Sat Feb 24, 2024 7:52 pm
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 ^^

Re: Shaders, Help!

Posted: Tue Feb 27, 2024 5:09 am
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?