问题描述
我正在开发一个使用OpenGL 4.0着色器的项目.
I'm working on a project that uses OpenGL 4.0 shaders.
我必须提供对 glShaderSource()的调用带有一个char数组的数组,该数组代表着色器的源.
I have to supply the call to glShaderSource() with an array of char arrays, which represents the source of the shader.
着色器编译失败,出现以下错误:
The shader compilation is failing, with the following errors:
(0) : error C0206: invalid token "<null atom>" in version line
(0) : error C0000: syntax error, unexpected $end at token "<EOF>"
这是我的(hello world)着色器-直接来自OpenGL 4.0着色语言食谱
Here's my (hello world) shader - straight from OpenGL 4.0 shading language cookbook
#version 400
in vec3 VertexPosition;
in vec3 VertexColor;
out vec3 Color;
void main()
{
Color = VertexColor;
gl_Position = vec4( VertexColor, 1.0 );
}
这是我的代码,用于将着色器文件读入C ++代码,并在运行时编译着色器:
And here's my code to read the shader file into my C++ code, and compile the shader at runtime:
const int nMaxLineSize = 1024;
char sLineBuffer[nMaxLineSize];
ifstream stream;
vector<string> vsLines;
GLchar** ppSrc;
GLint* pnSrcLineLen;
int nNumLines;
stream.open( m_sShaderFile.c_str(), std::ios::in );
while( (stream.good()) && (stream.getline(sLineBuffer, nMaxLineSize)) )
{
if( strlen(sLineBuffer) > 0 )
vsLines.push_back( string(sLineBuffer) );
}
stream.close();
nNumLines = vsLines.size();
pnSrcLineLen = new GLint[nNumLines];
ppSrc = new GLchar*[nNumLines];
for( int n = 0; n < nNumLines; n ++ )
{
string & sLine = vsLines.at(n);
int nLineLen = sLine.length();
char * pNext = new char[nLineLen+1];
memcpy( (void*)pNext, sLine.c_str(), nLineLen );
pNext[nLineLen] = '\0';
ppSrc[n] = pNext;
pnSrcLineLen[n] = nLineLen+1;
}
vsLines.clear();
// just for debugging purposes (lines print out just fine..)
for( int n = 0; n < nNumLines; n ++ )
ATLTRACE( "line %d: %s\r\n", n, ppSrc[n] );
// Create the shader
m_nShaderId = glCreateShader( m_nShaderType );
// Compile the shader
glShaderSource( m_nShaderId, nNumLines, (const GLchar**)ppSrc, (GLint*) pnSrcLineLen );
glCompileShader( m_nShaderId );
// Determine compile status
GLint nResult = GL_FALSE;
glGetShaderiv( m_nShaderId, GL_COMPILE_STATUS, &nResult );
C ++代码按预期执行,但是着色器编译失败.谁能发现我可能做错了什么?
The C++ code executes as expected, but the shader compilation fails. Can anyone spot what I might be doing wrong?
我感觉这可能与行尾字符有关,但是由于这是我第一次进行着色器编译,所以我被卡住了!
I have a feeling that this may be to do with end of line characters somehow, but as this is my first attempt at shader compilation, I'm stuck!
我已经阅读了有关着色器编译的其他SO答案,但是它们似乎特定于Java/其他语言,而不是C ++.如果有帮助,我就在win32平台上.
I've read other SO answers on shader compilation, but they seem specific to Java / other languages, not C++. If it helps, I'm on the win32 platform.
推荐答案
只是预感:
您是否尝试使用长度参数NULL调用glShaderSource?在这种情况下,OpenGL将假定您的代码以null结尾.
Have you tried calling glShaderSource with NULL as length parameter? In that case OpenGL will assume your code to be null-terminated.
(由于愚蠢而编辑)
这篇关于glsl着色器在运行时的编译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!