简体   繁体   English

GLSL Shader(编译错误)

[英]GLSL Shader (compilation error)

I've read all recommended posts, I've tried those solutions, but non of them helped. 我已经阅读了所有推荐的帖子,尝试了这些解决方案,但没有一个提供帮助。

In short problem lies in the third argument of 简而言之,问题在于

glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);

This code work: 此代码的工作方式:

const char *vertexShaderSource = "#version 120 \n"
                         "attribute vec3 pos;\n"
                         "attribute vec2 texCoord;\n"
                         "varying vec2 texCoord0;\n"
                         "uniform mat4 transform;\n"
                         "void main()\n"
                         "{\n"
                         "   gl_Position = transform * vec4(pos, 1.0);\n"
                         "   texCoord0 = texCoord;\n"
                        "}\0";

But I want to read it from a file, following code works 但是我想从文件中读取它,以下代码可以工作

std::string s= "vertex";
std::ifstream file(s.c_str());
std::stringstream buffer;

buffer << file.rdbuf();
std::string str = buffer.str();
std::cout << str; 

And is outputing: 并输出:

#version 120
attribute vec3 pos;
attribute vec2 texCoord;
varying vec2 texCoord0;
uniform mat4 transform;

void main()
{
    gl_Position = transform * vec4(pos, 1.0);
    texCoord0 = texCoord;
}

I know that I cannot just simply convert string with code like this: 我知道我不能简单地用以下代码转换字符串:

const char *vertexShaderSource = str.c_str();

And pass it into: glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); 并将其传递给: glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); So I've used following code to prevent it from ceasing to exist: 因此,我使用以下代码来防止其停止存在:

char * writable = new char[str.size() + 1];
std::copy(str.begin(), str.end(), writable);
writable[str.size()] = '\0';

Passing glShaderSource(vertexShader, 1, &writable, NULL); 传递glShaderSource(vertexShader, 1, &writable, NULL); does not work also. 也无法正常工作。 I'm getting following error all the time, even with another copy&paste code from tutorials 我一直都在跟踪错误,即使使用教程中的另一个复制粘贴代码也是如此

 0:1(4): preprocessor error: syntax error, unexpected HASH_TOKEN

What else I can do? 我还能做什么?

I've read these posts: Reading a shader from a .txt file using a structure 我已阅读以下文章: 使用结构从.txt文件读取着色器

read GLSL shaders from file 从文件读取GLSL着色器

Here is the code I use to load shader sources: 这是我用来加载着色器源的代码:

ifstream iStream(filename);
stringstream buffer;
buffer << iStream.rdbuf();
string source = buffer.str();

const char* sources[] = { source.c_str() };
glShaderSource(handle, 1, sources, 0);

As you can see, the string can be given directly to glShaderSource ; 如您所见,可以将string直接提供给glShaderSource ; there is no need to create a copy of it. 无需创建它的副本。 Have a look at the spec for c_str() if you're not convinced. 如果您不确定,请查看c_str()的规范。

Not sure what the problem is in your case. 不确定您遇到的问题是什么。 I have used both \\r\\n and \\n line endings successfully. 我已经成功使用\\r\\n\\n行尾。 Maybe you have a U+FEFF BOM character at the start of the file? 也许文件开头有一个U+FEFF BOM字符 It could with the the "hash" mentioned in the compilation error. 它可能与编译错误中提到的“哈希”有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM