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

Author Topic: error: 'uniform' does not name a type  (Read 3253 times)

0 Members and 1 Guest are viewing this topic.

Kain5056

  • Newbie
  • *
  • Posts: 26
    • View Profile
error: 'uniform' does not name a type
« on: July 21, 2013, 08:53:05 am »
Hello, everybody.
I'm trying to compile this code found HERE, but I can't seem to get it right.

Code: [Select]
#include <SFML/Graphics.hpp>

uniform sampler2D texture;
uniform vec4 colorkey;

void main()
{
    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
    if (pixel == colorkey)
        pixel.a = 0;
    gl_FragColor = pixel;
}

But the compiler cannot seem to be able to find anything about shaders, giving me these errors:

Code: [Select]
error: 'uniform' does not name a type
error: 'uniform' does not name a type
error: '::main' must return 'int'
error: 'vec4' was not declared in this scope
error: expected ';' before 'pixel'
error: 'pixel' was not declared in this scope
error: 'colorkey' was not declared in this scope
error: 'gl_FragColor' was not declared in this scope
error: 'pixel' was not declared in this scope

I have checked, though, and "Shaders.hpp" is in the "Graphics" folder, so I don't know why it can't read it.

I don't really know what I'm doing here, since the Tutorials only cover how to read a .frag file and not how to create one, so I just assumed that if I compile this code it will create a .frag file. This is the very first time I'm trying to utilize shaders, so I might be doing a lot of things wrong.

Everything that does not include shaders works great so far, so I don't really know where the problem actually is.

Any help would be appreciated.
Thank you in advance.  :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: error: 'uniform' does not name a type
« Reply #1 on: July 21, 2013, 09:11:57 am »
Your C++ compiler knows nothing about the GLSL shader language. You can't mix them like this.

If you want to define your shader in the code rather than in a separate file, you must put it in a string:

std::string shaderSource =
    "uniform sampler2D texture;"
    "uniform vec4 colorkey;"
    ""
    "void main()"
    "{"
    "    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);"
    "    if (pixel == colorkey)"
    "        pixel.a = 0;"
    "    gl_FragColor = pixel;"
    "}"
    ;

sf::Shader shader;
shader.loadFromMemory(shaderSource, sf::Shader::Fragment);

To create a .frag file, just create a simple text file and copy your shader code in it. There's no magic, shaders are really just text files, like C++ or any other language sources.
« Last Edit: July 21, 2013, 09:13:29 am by Laurent »
Laurent Gomila - SFML developer

Kain5056

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: error: 'uniform' does not name a type
« Reply #2 on: July 21, 2013, 09:19:03 am »
So simple, it didn't even cross my mind!   :o
Thanks for the help!  :)