Jump to content
  • Advertisement
Sign in to follow this  
sn0k3

Error with shaders - OpenGL

This topic is 1191 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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!