An question about using different version of GLSL in QtOpenGL
-
Recently I am using shadering language of OpenGL and I try to use QtOpenGL ,however,the default version of GLSL in QtOpenGL is ES2.0 (QOpenGLShaderProgram) , its grammar is a little different from other versions .Could I just use GLSL in other version like 3.30 becauese I couldn't compile the following shader:
static const char *vertexShaderSource2= "#version 330\n" "layout (location = 0) in vec3 a_pos;\n" "layout (location = 1) in vec3 a_normal;\n" "layout (location = 2) in vec2 a_uv;\n" "uniform float u_linewidth;\n" "uniform mat4 modelViewMatrix;\n" "uniform mat4 projectionMatrix;\n" "out vec2 uv;\n" "void main() {\n" "vec4 delta = vec4(a_normal * u_linewidth/2, 0);\n" "vec4 pos = modelViewMartrix * vec4(a_pos ,1);\n" "gl_Position = projectionMatrix * (pos +delta) ;\n" "uv = a_uv;\n" "}\n"; static const char *fragmentShaderSource2 = "#version 330\n" "uniform vec4 u_color;\n" "uniform sampler2D u_sampler;\n" "in vec2 uv;\n" "void main() {\n" "gl_FragColor = texture(u_sampler, uv) + vec4(u_color.rgb, 0);\n" "}\n";
But the compiler shows
So how can I use other version GLSL correctly?
Here is the shader example which is compiled correctly:
static const char *vertexShaderSource = "attribute highp vec4 posAttr;\n" "attribute lowp vec4 colAttr;\n" "varying lowp vec4 col;\n" "uniform highp mat4 matrix;\n" "void main() {\n" " col = colAttr;\n" " gl_Position = matrix * posAttr;\n" "}\n"; static const char *fragmentShaderSource = "varying lowp vec4 col;\n" "void main() {\n" " gl_FragColor = col;\n" "}\n";
-
Recently I am using shadering language of OpenGL and I try to use QtOpenGL ,however,the default version of GLSL in QtOpenGL is ES2.0 (QOpenGLShaderProgram) , its grammar is a little different from other versions .Could I just use GLSL in other version like 3.30 becauese I couldn't compile the following shader:
static const char *vertexShaderSource2= "#version 330\n" "layout (location = 0) in vec3 a_pos;\n" "layout (location = 1) in vec3 a_normal;\n" "layout (location = 2) in vec2 a_uv;\n" "uniform float u_linewidth;\n" "uniform mat4 modelViewMatrix;\n" "uniform mat4 projectionMatrix;\n" "out vec2 uv;\n" "void main() {\n" "vec4 delta = vec4(a_normal * u_linewidth/2, 0);\n" "vec4 pos = modelViewMartrix * vec4(a_pos ,1);\n" "gl_Position = projectionMatrix * (pos +delta) ;\n" "uv = a_uv;\n" "}\n"; static const char *fragmentShaderSource2 = "#version 330\n" "uniform vec4 u_color;\n" "uniform sampler2D u_sampler;\n" "in vec2 uv;\n" "void main() {\n" "gl_FragColor = texture(u_sampler, uv) + vec4(u_color.rgb, 0);\n" "}\n";
But the compiler shows
So how can I use other version GLSL correctly?
Here is the shader example which is compiled correctly:
static const char *vertexShaderSource = "attribute highp vec4 posAttr;\n" "attribute lowp vec4 colAttr;\n" "varying lowp vec4 col;\n" "uniform highp mat4 matrix;\n" "void main() {\n" " col = colAttr;\n" " gl_Position = matrix * posAttr;\n" "}\n"; static const char *fragmentShaderSource = "varying lowp vec4 col;\n" "void main() {\n" " gl_FragColor = col;\n" "}\n";
Assuming you derive from QOpenGlWidget, you can use QSurfaceFormat to set the OpenGL version and so indirectly the shader langauge in your class's constructor. There are functions like
setMajorVersion
,setMinorVersion
, etc. You are right, without this the default is OpenGL 2.0 and its corresponding shader language.QSurfaceFormat format; format.setMajorVersion(3); format.setMinorVersion(0); format.setDepthBufferSize(24); format.setSamples(4); setFormat(format);
-Michael.
-
Assuming you derive from QOpenGlWidget, you can use QSurfaceFormat to set the OpenGL version and so indirectly the shader langauge in your class's constructor. There are functions like
setMajorVersion
,setMinorVersion
, etc. You are right, without this the default is OpenGL 2.0 and its corresponding shader language.QSurfaceFormat format; format.setMajorVersion(3); format.setMinorVersion(0); format.setDepthBufferSize(24); format.setSamples(4); setFormat(format);
-Michael.
@m.sue Thx !Now I can compile ~