Sign in to follow this  

Error with shaders - OpenGL

This topic is 823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys!

When I compile there is an error, which says:

 

Failed to compile shader!
Vertex shader failed to compile with the following errors:
ERROR: 0:1: error(#132) Syntax error: "<" parse error
ERROR: error(#273) 1 compilation errors.  No code generated

 

Here you go my vertex shader:

#version 330 core

layout (location = 0) in vec4 position;

uniform mat4 pr_matrix = mat4(1.0);
uniform mat4 vw_matrix = mat4(1.0);
uniform mat4 ml_matrix = mat4(1.0);

void main()
{
    gl_Position = position;
}

 

Any ideas?

Share this post


Link to post
Share on other sites
I'm thinking, if you're not using std::string to hold your shader string, then you may need to add a null terminator after your load or make sure your string length is correctly set so we're not sending garbage memory.

https://www.opengl.org/sdk/docs/man/html/glShaderSource.xhtml Edited by Goliath Forge

Share this post


Link to post
Share on other sites

Gooey I think yes.

 

The class, which has function to load (read) a file:

    class FileUtils {
    public:
        static std::string read_file(const char* filePath)
        {
            FILE* file = fopen(filePath, "rt");
            fseek(file, 0, SEEK_END);
            unsigned long length = ftell(file);
            char* data = new char[length + 1];
            memset(data, 0, length + 1);
            fseek(file, 0, SEEK_SET);
            fread(data, 1, length, file);
            fclose(file);

            std::string result(data);
            delete[] data;
            return result;
        }
    };

 

 

And my shader.cpp file, which is used for loading the shaders and enabling them:

 

 

    Shader::Shader(const char* vertexFilePath, const char* fragFilePath)
    {
        m_VertPath = vertexFilePath;
        m_FragPath = fragFilePath;

        m_ShaderID = load();
    }

    Shader::~Shader()
    {
        glDeleteProgram(m_ShaderID);
    }

    GLuint Shader::load()
    {
        GLuint program = glCreateProgram();
        GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
        GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);

        const char* vertSource = FileUtils::read_file(m_VertPath).c_str();
        const char* fragSource = FileUtils::read_file(m_FragPath).c_str();

        glShaderSource(vertex, 1, &vertSource, NULL);
        glCompileShader(vertex);

        GLint result;
        glGetShaderiv(vertex, GL_COMPILE_STATUS, &result);
        if(result == GL_FALSE)
        {
            GLint length;
            glGetShaderiv(vertex, GL_INFO_LOG_LENGTH, &length);
            std::vector<char> error(length);

            glGetShaderInfoLog(vertex, length, &length, &error[0]);
            std::cout << "Failed to compile shader!" << std::endl;
            std::cout << &error[0] << std::endl;
            glDeleteShader(vertex);
            return 0;
        }


        glShaderSource(fragment, 1, &fragSource, NULL);
        glCompileShader(fragment);

        glGetShaderiv(fragment, GL_COMPILE_STATUS, &result);
        if(result == GL_FALSE)
        {
            GLint length;
            glGetShaderiv(fragment, GL_INFO_LOG_LENGTH, &length);
            std::vector<char> error(length);

            glGetShaderInfoLog(fragment, length, &length, &error[0]);
            std::cout << "Failed to compile shader!" << std::endl;
            std::cout << &error[0] << std::endl;
            glDeleteShader(fragment);
            return 0;
        }

        glAttachShader(program, vertex);
        glAttachShader(program, fragment);

        glLinkProgram(program);
        glValidateProgram(program);


        glDeleteShader(vertex);
        glDeleteShader(fragment);

        return program;
    }

    void Shader::enable() const
    {

    }

    void Shader::disable() const
    {

    }
 

Share this post


Link to post
Share on other sites


        const char* vertSource = FileUtils::read_file(m_VertPath).c_str();
        const char* fragSource = FileUtils::read_file(m_FragPath).c_str();

 

It seems like strings are created from function, then you getting c_str of them. Now strings are destroyed, but pointers are still in use. Try to do something like:

const std::string vertSource = FileUtils::read_file(m_VertPath);
const std::string fragSource = FileUtils::read_file(m_FragPath); 

// ... 

glShaderSource(vertex, 1, &vertSource.c_str(), NULL); // same for fragment

// ... 

This way, strings are remaninbg for whole function scope and keeps valid c_str pointers.

Share this post


Link to post
Share on other sites

@Sagaceil I tryed this now

        std::string vertSourceString = FileUtils::read_file(m_VertPath);
        std::string fragSourceString = FileUtils::read_file(m_FragPath);

        const char* vertSource = FileUtils::read_file(m_VertPath).c_str();
        const char* fragSource = FileUtils::read_file(m_FragPath).c_str();

        glShaderSource(vertex, 1, vertSourceString.c_str(), NULL);

there are new errors:

Error    1    error C2102: '&' requires l-value
Error    2    error C2198: 'PFNGLSHADERSOURCEPROC' : too few arguments for call

 

EDIT:

I saw, that I forgot to implement two functions (enable and disable)

void Shader::enable() const
    {
        glUseProgram(m_ShaderID);
    }

    void Shader::disable() const
    {
        glUseProgram(0);
    }

 

Again compiled, but there is the same error..

Edited by sn0k3

Share this post


Link to post
Share on other sites

try:


        std::string vertSourceString = FileUtils::read_file(m_VertPath);
        std::string fragSourceString = FileUtils::read_file(m_FragPath);

        const char* vertSource = vertSourceString.c_str();
        const char* fragSource = fragSourceString.c_str();

        glShaderSource(vertex, 1, &vertSource , NULL);

Share this post


Link to post
Share on other sites


@Sagaceil I tryed this now

std::string vertSourceString = FileUtils::read_file(m_VertPath);
std::string fragSourceString = FileUtils::read_file(m_FragPath);

const char* vertSource = FileUtils::read_file(m_VertPath).c_str();
const char* fragSource = FileUtils::read_file(m_FragPath).c_str();

glShaderSource(vertex, 1, vertSourceString.c_str(), NULL);

 

Ok im not sure why you have read both files twice there this isnt needed

const char* vertSource = FileUtils::read_file(m_VertPath).c_str();
const char* fragSource = FileUtils::read_file(m_FragPath).c_str();

 

you also have duplicate code for the shaders which could be put into a function but none of the less i still think it might be what is being read in try after getting your data into your string simply using

std::cout << vertSourceString << "\n";

and make sure it has what you have in the file

Share this post


Link to post
Share on other sites

glShaderSource(vertex, 1, vertSourceString.c_str(), NULL);

 

this bit here is your problem i am suprised this compiles....

make a funtion somthing like

void ShaderSource(const char * data, GLuint shader)
{
    glShaderSource(shader, 1, &data, NULL);
    glCompileShader(shader);
}

Obviously you will need to use this like

ShaderSource(vertSourceString.c_str(), vertex);

or

ShaderSource(vertSourceString.c_str(), fragment);

 

and try that

Edited by Gooey

Share this post


Link to post
Share on other sites

@Gooey My wrong.. :D I've pasted it wrong..

It should be:

    std::string vertSourceString = FileUtils::read_file(m_VertPath);
    const char* vertSource = vertSourceString.c_str();
        glShaderSource(vertex, 1, &vertSource, NULL);

 

But I've posted this:

        std::string vertSourceString = FileUtils::read_file(m_VertPath);
        std::string fragSourceString = FileUtils::read_file(m_FragPath);

        const char* vertSource = FileUtils::read_file(m_VertPath).c_str();
        const char* fragSource = FileUtils::read_file(m_FragPath).c_str();

        glShaderSource(vertex, 1, vertSourceString.c_str(), NULL);

[/code]

 

So other thing, when I put cout to print the fragSource of FragSourceString

       std::cout << "Fragment source file: " << fragSource << std::endl;

this prints the content of the shader..

Share this post


Link to post
Share on other sites

Other error:

Fragment shader failed to compile with the following errors:
ERROR: 0:11: error(#143) Undeclared identifier: gl_Position
WARNING: 0:11: warning(#402) Implicit truncation of vector from size: 4 to size:
 1
ERROR: error(#273) 1 compilation errors.  No code generated

 

Any ideas why?

Share this post


Link to post
Share on other sites
They sound simple enough put your frag shader code here.
A hint though gl_Position cant be used in frag shader only vertex i believe by the sound of it you are trying to pass a float out instead of a vec4

Share this post


Link to post
Share on other sites

Hello guys!

When I compile there is an error, which says:

 

Failed to compile shader!
Vertex shader failed to compile with the following errors:
ERROR: 0:1: error(#132) Syntax error: "<" parse error
ERROR: error(#273) 1 compilation errors.  No code generated

 

Here you go my vertex shader:

#version 330 core

layout (location = 0) in vec4 position;

uniform mat4 pr_matrix = mat4(1.0);
uniform mat4 vw_matrix = mat4(1.0);
uniform mat4 ml_matrix = mat4(1.0);

void main()
{
    gl_Position = position;
}

Any ideas?

It is valid GLSL code to initialize the uniforms within the shader? Never mind. I really need to Google how to use this Google thing...

Edited by MarkS

Share this post


Link to post
Share on other sites

Frag shader:

 

#version 330 core

layout (location = 0) out vec4 color;


void main()
{
    color = vec4(1.0, 1.0, 1.0, 1.0);
}

 

The vert shader is up.

Edited by sn0k3

Share this post


Link to post
Share on other sites

so your shaders look like this?

 

vertex shader


#version 330 core

layout (location = 0) in vec4 position;

uniform mat4 pr_matrix = mat4(1.0);
uniform mat4 vw_matrix = mat4(1.0);
uniform mat4 ml_matrix = mat4(1.0);

void main()
{
gl_Position = position;
}

 

frag shader

 


#version 330 core

out vec4 color;


void main()
{
color = vec4(1.0, 1.0, 1.0, 1.0);
}

Share this post


Link to post
Share on other sites

I found the error!

The error wasn't because of shader, it was in main.cpp file..

 

The error was this:

    Shader shader("shaders/shader.vert", "shaders/shader.vert");

 

It was passed only vertex shader, and actually yes..The second shader was compiled as frag shader..

I fixed it:

    Shader shader("shaders/shader.vert", "shaders/shader.frag");

 

Thank you!

Share this post


Link to post
Share on other sites

This topic is 823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this