View Full Version : glShaderSource problem

11-05-2011, 05:26 PM
I'm trying to write a general purpose ShaderProgram manager.

I'm having problems with compiling my shader. The shader is a very simple example out of the OpenGL 4.0 Shading language cookbook.

Shader compilation (during C++ execution) returns the following errors:

(0) : error C0206: invalid token "<null atom>" in version line
(0) : error C0000: syntax error, unexpected $end at token "<EOF>"

Here's my code for the shader:

#version 400

in vec3 VertexPosition;
in vec3 VertexColor;
out vec3 Color;

void main()
Color = VertexColor;
gl_Position = vec4( VertexColor, 1.0 );

And here's my code to read in the shader from a file, line by line, then convert that into an array of char arrays, for use in the glShaderSource() call.

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()) &amp;&amp; (stream.getline(sLineBuffer, nMaxLineSize)) )
if( strlen(sLineBuffer) > 0 )
vsLines.push_back( string(sLineBuffer) );


nNumLines = vsLines.size();
pnSrcLineLen = new GLint[nNumLines];
ppSrc = new GLchar*[nNumLines];

for( int n = 0; n < nNumLines; n ++ )
string &amp; 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;

// 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, &amp;nResult );

Could anyone share insight as to what's going wrong here?

Alfonse Reinheart
11-05-2011, 06:30 PM
I liked your question on Stack Overflow better, when you actually formatted it so that people could read it. Speaking of SO, you should read this answer (http://stackoverflow.com/questions/8024433/glsl-shader-compilation-issue-at-runtime/8024613#8024613).

11-05-2011, 07:30 PM
I'm not familiar with the code formatting tools in this forum's editor. Stack Overflow's is much easier to use.

11-06-2011, 01:26 AM
Come on, it is not as the "code" button was completly hidden ...