Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with shader variable  (Read 1624 times)

0 Members and 1 Guest are viewing this topic.

AntePost

  • Newbie
  • *
  • Posts: 2
    • View Profile
Problem with shader variable
« on: March 20, 2020, 12:24:46 pm »
Quick summary

Hello. I'm learning C++ through this book. In the last game project the author shows how to use simple shaders to make visual effects. I, however, cannot replicate it in my code.

I load .vert & .frag into a variable with sf::Shader::loadFromFile, however fragment shader can't compile with this error:
ERROR: 0:11: 'assign' : l-value required "vTexCoord" (cannot modify a varying)

Full Explanation

Here is the code of vertShader.vert:
#version 110

//varying "out" variables to be used in the fragment shader
varying vec4 vColor;
varying vec2 vTexCoord;
 
void main() {
    vColor = gl_Color;
    vTexCoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
 

Here is the code of rippleShader.frag:
// attributes from vertShader.vert
varying vec4 vColor;
varying vec2 vTexCoord;

// uniforms
uniform sampler2D uTexture;
uniform float uTime;

void main() {
        float coef = sin(gl_FragCoord.y * 0.1 + 1 * uTime);
        vTexCoord.y +=  coef * 0.03;
        gl_FragColor = vColor * texture2D(uTexture, vTexCoord);
}
 

Here is what I do in my program:
1. Declare a shader variable in my game engine class header:
Shader m_RippleShader;

2. Load files into shader variable in the Engine constructor:
if (!sf::Shader::isAvailable())
{
        m_Window.close();
}
else
{
        m_RippleShader.loadFromFile("shaders/vertShader.vert",
                "shaders/rippleShader.frag");
}
 

3. In my "draw" function I update shader variable each frame:
m_RippleShader.setUniform("uTime", m_GameTimeTotal.asSeconds());

4. Finally in the same function I draw the background with shader (I do it three times because the game supports splitscreen):
m_Window.draw(m_BackgroundSprite, &m_RippleShader);

When I try to launch game the rippling effect isn't there & the aforementioned error is output to console. I understand that the problem is with this line:
vTexCoord.y +=  coef * 0.03;
Something is wrong with vTexCoord.

What is curious is that m_RippleShader.loadFromFile doesn't output any errors to console, however it returns false, so I assume there is some problem with loading files. Even more curious is that it returns false after the error from the shader compilation.

I frankly don't have enough knowledge to figure it out by myself so any help will be appreciated.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problem with shader variable
« Reply #1 on: March 20, 2020, 01:37:43 pm »
Quote
What is curious is that m_RippleShader.loadFromFile doesn't output any errors to console
Quote
I load .vert & .frag into a variable with sf::Shader::loadFromFile, however fragment shader can't compile with this error:
ERROR: 0:11: 'assign' : l-value required "vTexCoord" (cannot modify a varying)
??

And I'm not an expert in shaders, but I think the error is pretty clear: in the fragment shader, your vTextCoord varying is an input so you cannot modify it. Copy if first to another variable if it needs to change.
Laurent Gomila - SFML developer

AntePost

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problem with shader variable
« Reply #2 on: March 20, 2020, 02:02:26 pm »
Quote
Copy if first to another variable if it needs to change.

Thank you, that was indeed the problem. Copying it into intermediary variable solved the issue.

 

anything