简体   繁体   中英

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

I'm currently developing an OpenGL-Widget in Qt, based on the QOpenGLWidget. I followed some examples and used the GLSL-Wrapper for Demo purposes. The application itself should be as independent as possible for compatibility purposes, like changing the GUI framework.

When the Qt code takes care of the shader, the app works fine:

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. Since your shader does not contain a #version directive, it is to be interpreted as GLSL 1.10. And that version does not know of the precision qualifiers like mediump . (Later just accept that keywords, for improved compatibility with 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.

Also note that your shader is totally invalid in a modern core profile of desktop GL.

The only recommendation I can give you is to first decide which version (or versions) of OpenGL you want/have to target.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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