How to set shader uniform?

For people working on the C++ code.
Post Reply
Aeodar
Member
Posts: 18
Joined: Sun Feb 19, 2023 22:11

How to set shader uniform?

by Aeodar » Post

In opengl_fragment.glsl I added an extra uniform:

Code: Select all

uniform vec3 dayLight;
uniform vec4 skyBgColor;
uniform float fogDistance;
uniform vec3 eyePosition;
// my uniform:
uniform int test_uniform;
Then in game.cpp I made a CashedPixelShaderSetting:

Code: Select all

CachedPixelShaderSetting<s32> m_test_uniform_pixel;
s32 m_test_uniform;
Then inside onSetConstants I added:

Code: Select all

s32 test_uniform = 10;
m_slice_height_pixel.set(&test_uniform, services);
For some reason its not passing the value 10 to the shader.
Weirdly when I debug and try to step into the .set function it wont go in, but I am able to step into

Code: Select all

m_fog_distance.set(&fog_distance, services);
Im not the best at c++ and dont fully understand the argument type

Code: Select all

const T value[count]
Is it possible Im passing the wrong type?

Any help would be great thanks!

User avatar
Hybrid Dog
Member
Posts: 2828
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

Re: How to set shader uniform?

by Hybrid Dog » Post

Perhaps you forgot to initialise the CachedPixelShaderSetting m_test_uniform_pixel in the GameGlobalShaderConstantSetter constructor (to be more precise: the constructor's initialiser list) with the name of the uniform in opengl_fragment.glsl.

I could add a uniform in the following way:
  • Add the uniform in client/shaders/nodes_shader/opengl_fragment.glsl
  • In src/client/shader.cpp:
    • Add a CachedPixelShaderSetting member variable for the uniform in MainShaderConstantSetter
    • In the MainShaderConstantSetter constructor, initialise this member variable with the uniform name in the shader
    • In the MainShaderConstantSetter onSetConstants method, assign a value to the uniform using the member variable's set method

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests