#Help with shaders

63 messages · Page 1 of 1 (latest)

normal jay
#

Hello guys so im completely new to shaders, im creating a gameengine in C++, and i want to implement lightiing. (Im using SFML as my graphics API) However i need to write my own light shader and i just asked chat gpt and it gave me a littel light shader that kind of works. It does have problems tho:

  1. It doesn't work dynamicly with the SFML camera.
  2. I dont know how to positions are getting set? Like i see the gl_FragColor thing which sets the FragColor but the positions are not being set. However for some reason i can modify the position array and the light moves
    I can hop in a call with you if you'd like to talk
// light_shader.frag

#define MAX_LIGHTS 255

uniform sampler2D texture; 
uniform vec2 lightPositions[MAX_LIGHTS]; 
uniform float lightRadii[MAX_LIGHTS]; 
uniform float lightIntensities[MAX_LIGHTS];
uniform vec3 lightColors[MAX_LIGHTS];
uniform int lightAmount;

void main()
{
    vec4 finalColor = vec4(0.0, 0.0, 0.0, 1.0);

    for (int i = 0; i < lightAmount; i++) {

        vec2 fragmentPosition = gl_FragCoord.xy;

        vec2 lightPosition = lightPositions[i];
        float lightRadius = lightRadii[i];
        float lightIntensity = lightIntensities[i];
        vec3 lightColor = lightColors[i];

        float distanceToLight = length(fragmentPosition - lightPosition);
        float intensity = 1.0 - smoothstep(0.0, lightRadius, distanceToLight);
        intensity *= lightIntensity;
        intensity = clamp(intensity, 0.0, 1.0);

        vec4 texColor = texture2D(texture, gl_TexCoord[0].xy);
        finalColor += texColor * vec4(lightColor, 1.0) * intensity;

        gl_FragColor = finalColor;
    }
}


#

Just ping : D

gilded marsh
#

Ur knowledge has way too many holes in it

#

They cover alot of the stuff u asked chat gpt write for u

#

Also in general
If u wanna learn something, don't take shortcuts

normal jay
#

Well tbh i dont really want to learn it (ýeah that sounds dumb) but i jsut want to get throught with this and i wont touch OpenGL in a long time still thanks for your advice tho

#

I thought my question was ok, but it seems it's some garbage

gilded marsh
normal jay
#

Yeah maybe i should really just actually like invest time 💀

gilded marsh
#

Also u said ur creating a game
Engine

Graphics is a huge part of a game engine

normal jay
#

Yeah its a 2D engine

#

So i dont really did mess around with rendering

#

I just let SFMl do the stuff

gilded marsh
#

2d or 3d
Doesn't matter

normal jay
#

It's a good library and i dont think i would write more efficient code

gilded marsh
#

If u wanna take a easier route
Look into pygame

normal jay
#

My engine is already finished

#

I just wanted to add some lights

#

As like entites

gilded marsh
#

How did u complete ur engine without lights

normal jay
#

Wanna hop in a call

#

I cant explain it im slow in writing

gilded marsh
#

And learning how graphic work

normal jay
#

I mean i can -

gilded marsh
#

But really curious about ur progress

normal jay
#

so in SFML when you render a sprite it doesnt need any lighting and it just renders the sprite

#

uhm

normal jay
gilded marsh
#

Something like this

#

And just blending the background with it

#

So it appears to be bright and dim

normal jay
#

So when i use this can i still like change colors and radius and stuff?

gilded marsh
#

U can tint it manually or programmatically

#

For color

#

There is this guy named dafluffypotato
He mostly uses pygame
And for a long time didn't use opengl
So he show cased alot of these alternative methods of dealing with these problems

#

Even tho ur using c++
His older videos is worth a watch I would say

normal jay
#

Okay, just 1 last question is the thing where im storing the lights in a single shader a okay approach or did i like literally misunderstood everything

gilded marsh
#

U don't store lights in a shader

#

A shader has no memory
It's just a program that the gpu runs

normal jay
#

So im storing the lightPosition and lightColor etc in the .frag file and i update the light positions for example with uniforms

gilded marsh
#

That's correct

normal jay
#

Ok

#

well ty

#

Ill look into OpenGl and actually invest like time to maybe learn it

grave lichen
#

FYI it's against the rules to ask questions about ChatGPT generated code on this server

normal jay
#

That code was modified alot and not just pure Chat GPT. It just kind of gave me the structure and i figureed it out on my own

grave lichen
#

Doesn't really matter, if it comes from chatgpt it's your responsibility to deal with it, don't shoulder others with that

normal jay
#

that argument is kinda trash ngl. When we dont talk about this code right here, and i understand like acutally the code ChatGPT wrote but i just dont understand what to add/what is missing why shouldnt i use this code in my question then

grave lichen
#

Just don't ask others to look at AI-generated code period, it's a lot of work to read code as it is, let alone to scour it in the detail required to make sure the AI didn't generate nonsense

normal jay
#

You really dont understand that AI and humans can literally write the same code so it isnt any difference and i modified the code alot so dont be mad fr

#

Also i tested it so it didnt generate nonsense

#

Do you think my ass was like
Write me a light shader?
AI: Shader
Me: Copy paste, didnt work
Me: Ask question

#

Nah it wasnt like this, i spent like 3hours fixing it and geting the camera shit to work

#

And now im here

gilded marsh
#

That's annoying. If u don't know what's happening, u really shouldn't be asking how to make changes or additions