我的glsl shader加载代码有什么问题吗? [英] Is there anything wrong with my glsl shader loading code?

查看:1416
本文介绍了我的glsl shader加载代码有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是,着色器(很简单的,因为我正在学习OpenGL)无法以看似随机的方式编译(并给出随机错误消息*)。
然而,相同的着色器在大约3或4次尝试后编译。
下面是代码:

The problem is that, shaders (pretty simple ones, as I'm learning OpenGL) fail to compile in a seemingly random manner (and gives random error messages * ). The same shaders, however, compile after about 3 or 4 tries. Here is the code:

Shader::Shader(GLenum Type,std::string filename)
{
    shader_type = Type;

    std::ifstream ifs(filename);
    if(!ifs)
        throw(std::runtime_error("File:"+filename+" not opened."));
    std::ostringstream stream;
    stream<<ifs.rdbuf();

    const GLchar* data = stream.str().c_str();

    handle = glCreateShader(shader_type);
    glShaderSource(handle,1,static_cast<const GLchar**>(&data),0);

    glCompileShader(handle);

    int status;
    glGetShaderiv(handle,GL_COMPILE_STATUS,&status);
    if(status == GL_FALSE)
    {
        int loglength;
        glGetShaderiv(handle,GL_INFO_LOG_LENGTH,&loglength);

        auto data = new char[loglength];

        glGetShaderInfoLog(handle,loglength,&loglength,data);

        std::string strdata(data);
        delete [] data;

        throw(std::runtime_error(strdata));
    }
}

请注意,着色器不会丢失换行符end,在最后一个分号后面有一个额外的空格,并使用制表符而不是空格。 (如通过互联网在各种旧帖中建议的那样)。

Note that the shaders aren't missing newlines at the end, has an extra space after the last semicolon and uses tabs instead of spaces. (as suggested in various old posts over the internet!).


  • 这里有两个错误消息同一顶点着色器不在同一时间:


#version 330
in vec2 Position;
uniform mat4 transform;
void main()
{
    gl_Position = transform*vec4(Position,0.0f,1.0f);

}

错误:

0(1) : error C0000: syntax error, unexpected $undefined at token "<undefined>"


0(6) : error C0000: syntax error, unexpected '!', expecting ',' or ')' at token "!"

有时它只是工作!
这是我的驱动程序的问题吗?
(我在Arch Linux 64位上使用最新的302.x稳定的nvidia二进制驱动程序,使用年龄为9600的GSO卡)

And sometimes it just works ! Is it a problem with my drivers ? (I'm using the recent 302.x stable nvidia binary drivers on Arch Linux 64 bit, with an aged 9600 GSO card )

PS:工作原理,只要着色器正确编译,所以我认为它是正确的。
如果无法找到问题,并且有人想要查看,我将很乐意将一个工作(有时!)示例作为zip文件发布。

P.S: The code works as expected ,whenever the shader compiles correctly, so I think it shoud be correct. I'll be happy to post a working(sometimes !) example as a zip file if the problem can't be found from this, and someone wants to take a look.

推荐答案

const GLchar* data = stream.str().c_str();

这很糟糕。如果您想要字符串的数据,您需要存储 str 将返回缓冲区的副本,然后您将获得 c_str 。一旦该临时文件被销毁(在这一行的末尾),那个指针将指向你不再能访问的内存。

This is bad. If you want the string's data, you need to store it. str will return a copy of the buffer, which you then get a pointer to with c_str. Once that temporary is destroyed (at the end of this line), that pointer will point to memory you no longer have access to.

正确的代码是:

std::string dataString = stream.str();
const GLchar *data = reinterpret_cast<GLchar*>(dataString.c_str());

这篇关于我的glsl shader加载代码有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆