简体   繁体   English

GLSL VertexShader可与Qt一起使用,但不能与普通OpenGL(SL)一起使用

[英]GLSL VertexShader works with Qt but not plain OpenGL(SL)

I'm currently developing an OpenGL-Widget in Qt, based on the QOpenGLWidget. 我目前正在基于QOpenGLWidget在Qt中开发OpenGL-Widget。 I followed some examples and used the GLSL-Wrapper for Demo purposes. 我遵循了一些示例,并将GLSL包装器用于演示目的。 The application itself should be as independent as possible for compatibility purposes, like changing the GUI framework. 为了兼容起见,应用程序本身应尽可能独立,例如更改GUI框架。

When the Qt code takes care of the shader, the app works fine: 当Qt代码负责着色器时,该应用程序可以正常工作:

QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
const char *vsrc =
    "uniform mediump mat4 matrix;\n"
    "void main(void)\n"
    "{\n"
    "    gl_Position = matrix * gl_Vertex;\n"
    "    gl_FrontColor = gl_Color;\n"
    "}\n";

bool success = vshader->compileSourceCode(vsrc);

program = new QOpenGLShaderProgram();
program->addShader(vshader);
program->link();

Next, I upload and compile the shader on my own: 接下来,我自己上传并编译着色器:

const char *vsrc =
    "uniform mediump mat4 matrix;\n"
    "void main(void)\n"
    "{\n"
    "    gl_Position = matrix * gl_Vertex;\n"
    "    gl_FrontColor = gl_Color;\n"
    "}\n";
GLuint programmID = glCreateProgram();
GLuint shaderID = glCreateShader(GL_VERTEX_SHADER);
int length =(int) std::char_traits<char>::length(vsrc);
glShaderSource(shaderID, 1, &vsrc, &length);
glCompileShader(shaderID);
char *error = new char[1000];
int* messagelength = new int;
glGetShaderInfoLog(shaderID, (GLsizei)1000, messagelength, error);
string str = string(error, *messagelength);
std::cout << str << std::endl << std::flush;
delete error;
delete messagelength;
glAttachShader(programmID, shaderID);
glDeleteShader(shaderID);
glLinkProgram(programmID);
glUseProgram(programmID);

However, this results in the following errors: 但是,这导致以下错误:

0(1) : error C0000: syntax error, unexpected type identifier, expecting '{' at token "mat4"
0(4) : warning C7506: OpenGL does not define the global type matrix
0(4) : warning C7531: pointers requires "#extension GL_NV_shader_buffer_load : enable" before use
0(4) : error C0000: syntax error, unexpected identifier, expecting '(' at token "gl_Vertex"

How do I make this work? 我该如何工作?

Well, your code is invalid in desktop GL. 好吧,您的代码在桌面GL中无效。 Since your shader does not contain a #version directive, it is to be interpreted as GLSL 1.10. 由于您的着色器不包含#version指令,因此#version其解释为GLSL 1.10。 And that version does not know of the precision qualifiers like mediump . 该版本不知道诸如mediump之类的精度限定mediump (Later just accept that keywords, for improved compatibility with GLSL ES). (以后只接受该关键字,以提高与GLSL ES的兼容性)。

Note that Qt might very well use GL ES 2.0 as default (and not desktop GL), depending on your local configuration and also on how the qt libs were built. 请注意,根据您的本地配置以及qt库的构建方式,Qt很可能会默认使用GL ES 2.0(而不是桌面GL)作为默认值。

Also note that your shader is totally invalid in a modern core profile of desktop GL. 还要注意,在现代桌面GL核心配置文件中,着色器完全无效。

The only recommendation I can give you is to first decide which version (or versions) of OpenGL you want/have to target. 我能给您的唯一建议是,首先确定您要/必须定位的OpenGL版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM