views:

641

answers:

5

I'm trying to run a GLSL example, but I can't.

The error is in the line glGetShaderiv(vShader, GL_COMPILE_STATUS, &status); where the program exits. vShader is the file vPhong.glsl.

The error returned is:

0(18) : error C0000: syntax error, unexpected $undefined at token "<undefined>"
0(18) : error C0501: type name expected at token "<undefined>"

I have 'installed' and referenced GLEW and Glut and their lib-files. I have glewInit'ed my program.

The program is able to find the .glsl-files.

The weird thing is that I am able to run a precompiled GLSL-demo, and also compile and run the project on my pc. The "initializations" in the non-working program and this are the same.

I have tried copying the defective source into the successful project, to see if there was something in the project settings causing the error, but unfortunately, that wasn't the case. So the error seems to be in the C++ code, not the project.


PROGRESS:

On my Windows 7-machine i got "Access violation" when running through Visual Studio. The program just stopped working if I ran it "alone". On a Windows XP-machine I got an uncaught exception.

I received no errors or warnings (apart from a warning that fopen is unsafe) when I compiled the program.

Visual Studio highlighted the line vShader = glCreateShader(GL_VERTEX_SHADER); as the source of the error.

Adding glewInit(); fixed the above errors.

A: 

I'm not sure how you compiled that code. It's using GL API that is not directly available in the windows SDK. All the shader APIs are in that category.

To access them on windows, you generally need to use the extension mechanism provide by wgl, or use an extension wrapper.

[edit to add]

So... with glew init out of the way! Shader compilation provides a build log when failing to build (taken from this tutorial):

GLint blen = 0; 
GLsizei slen = 0;

glGetShaderiv(ShaderObject, GL_INFO_LOG_LENGTH , &blen);       

if (blen > 1)
{
 GLchar* compiler_log = (GLchar*)malloc(blen);

 glGetInfoLogARB(ShaderObject, blen, &slen, compiler_log);
 cout << "compiler_log:\n", compiler_log);
 free (compiler_log);
}
Bahbar
As I've written: I'm using GLEW. The only difference between the linked source file and the one I'm using is that I include <GL/glut.h> and <GL/glew.h> instead of <GLUT/glut.h>.
jmoeller
doh, sorry. Well, glew is likely hiding the issue. Did you init glew ? what is the value of glCreateShader ?
Bahbar
I forgot to init glew -_-. Now the vertex shader apparently won't compile.
jmoeller
A: 

That error message usually means there is some kind of error in your shader program file itself. In my experience, it's been something like an unexpected character at the beginning or end of the file. Try copying and pasting the text from that URL into notepad or gedit and then saving the file.

Eric
+1  A: 

Those errors are coming from the GLSL compiler, not the C++ compiler. At line 18 in some shader, you have something that is confusing the GLSL compiler; probably random garbage or an unprintable character. One easy way to do this is to specify too long a length (or no length at all) for your shader source, and not have a NUL-terminator at the end of the string

Chris Dodd
A: 

There were two errors in the program:

First:

glLinkProgram(program);
glGetShaderiv(program, GL_LINK_STATUS, &status);

The last line should have been glGetProgramiv(program, GL_LINK_STATUS, &status);.

Also, the fopen that read in the shader files should have been used with rb as an argument instead of just r.

Substituting glGetShaderiv with glGetProgramiv and adding glewInit() fixed one of my initial errors.

Changing the argument to fopen fixed the shader error.

jmoeller
A: 

I had the same error. The problem was in the encoding of the shader file, which was Unicode UTF-8 with signature. In Visual Studio 2008, I converted the encoding to "US-ASCII" via the "File" > "Advanced Save Options..." menu item, and it now works perfectly. By the way, for those without Visual Studio 2008, the great "NotePad++" freeware editor also has that encoding-conversion capability.

Good luck!

Mathieu Frenette