GLSL Shader(编译错误) [英] GLSL Shader (compilation error)
问题描述
我已经阅读了所有推荐的帖子,尝试了这些解决方案,但没有一个提供帮助.
I've read all recommended posts, I've tried those solutions, but non of them helped.
简而言之,问题在于
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
此代码有效:
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;
并输出:
#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();
并将其传递给:glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
因此,我使用了以下代码来防止其停止存在:
And pass it into: 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';
通过glShaderSource(vertexShader, 1, &writable, NULL);
也不起作用.
我一直都在跟踪错误,即使使用教程
Passing 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
我还能做什么?
我已阅读以下文章: 从中读取着色器使用结构的.txt文件
I've read these posts: Reading a shader from a .txt file using a structure
推荐答案
这是我用来加载着色器源代码:
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);
如您所见,string
可以直接分配给glShaderSource
;无需创建它的副本.看看 c_str()
的规范不相信.
As you can see, the string
can be given directly to 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.
不确定您所遇到的问题是什么.我已经成功使用了\r\n
和\n
行尾.也许在文件的开头有 U+FEFF
BOM字符?可能与编译错误中提到的哈希"有关.
Not sure what the problem is in your case. I have used both \r\n
and \n
line endings successfully. Maybe you have a U+FEFF
BOM character at the start of the file? It could with the the "hash" mentioned in the compilation error.
这篇关于GLSL Shader(编译错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!