windows 编译和运行 GLSL 程序需要什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1922656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:36:47  来源:igfitidea点击:

What is required to compile and run GLSL programs?

windowsopenglglsl

提问by jumoel

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

我正在尝试运行GLSL 示例,但我不能。

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

错误在glGetShaderiv(vShader, GL_COMPILE_STATUS, &status);程序退出的那一行。vShader是文件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.

我已经“安装”并引用了 GLEW 和 Glut 及其库文件。我已经完成了glewInit我的程序。

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

该程序能够找到 .glsl 文件。

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

奇怪的是,我能够运行预编译的 GLSL-demo,并且还可以在我的电脑上编译和运行该项目。非工作程序中的“初始化”与此相同。

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.

我曾尝试将有缺陷的源复制到成功的项目中,以查看项目设置中是否存在导致错误的内容,但不幸的是,事实并非如此。所以错误似乎在 C++ 代码中,而不是在项目中。



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.

在我的 Windows 7 机器上,通过 Visual Studio 运行时出现“访问冲突”。如果我“单独”运行该程序,它就会停止工作。在 Windows XP 机器上,我遇到了一个未捕获的异常。

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

fopen编译程序时,我没有收到任何错误或警告(除了不安全的警告)。

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

Visual Studio 突出显示该行vShader = glCreateShader(GL_VERTEX_SHADER);作为错误来源。

Adding glewInit();fixed the above errors.

添加glewInit();修复了上述错误。

回答by jumoel

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);.

最后一行应该是glGetProgramiv(program, GL_LINK_STATUS, &status);.

Also, the fopenthat read in the shader files should have been used with rbas an argument instead of just r.

此外,fopen在着色器文件中读取的 应该已用作rb参数,而不仅仅是r.

Substituting glGetShaderivwith glGetProgramivand adding glewInit()fixed one of my initial errors.

glGetShaderivglGetProgramiv和加入glewInit()我的初始误差的固定之一。

Changing the argument to fopenfixed the shader error.

更改参数以fopen修复着色器错误。

回答by Chris Dodd

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

这些错误来自 GLSL 编译器,而不是 C++ 编译器。在某些着色器的第 18 行,您有一些令 GLSL 编译器感到困惑的东西;可能是随机垃圾或无法打印的字符。一种简单的方法是为着色器源指定太长的长度(或根本没有长度),并且在字符串末尾没有 NUL 终止符

回答by Bahbar

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.

我不确定您是如何编译该代码的。它使用的是在 Windows SDK 中不直接可用的 GL API。所有着色器 API 都在该类别中。

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

要在 windows 上访问它们,您通常需要使用 wgl 提供的扩展机制,或者使用扩展包装器。

[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):

所以......用glew init挡住!着色器编译在构建失败时提供构建日志(取自本教程):

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);
}

回答by Eric

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.

该错误消息通常意味着您的着色器程序文件本身存在某种错误。根据我的经验,它在文件的开头或结尾有点像意外的字符。尝试将该 URL 中的文本复制并粘贴到记事本或 gedit 中,然后保存该文件。