OpenGL and GLSL, openglglsl

Source: Internet
Author: User

OpenGL and GLSL, openglglsl

From: https://github.com/mattdesl/lwjgl-basics/wiki/GLSL-Versions

You can use#versionCommand as the first line of your shader to specify GLSL version:

#version 120void main() {    gl_FragColor = vec4(1.0);}

GLSL versions are released alongside GL versions. See the following charts to decide which version you wowould like to target.

GLSL Versions
OpenGL Version GLSL Version
2.0 110
2.1 120
3.0 130
3.1 140
3.2 150
3.3 330
4.0 400
4.1 410
4.2 420
4.3 430
Glsl es Versions (Android, iOS, WebGL)

OpenGL ES has its own Shading Language, and the versioning starts fresh. It is based on OpenGL Shading Language version1.10.

OpenGL ES Version Glsl es Version
2.0 100
3.0 300

So, for example, if a feature is available in GLSL 120, it probably won't be available in glsl es 100 unless the ES compiler specifically allows it.

Differences at a Glance

Differences between (desktop) GLSL versions.

Version 100:

Vertex shader:

uniform mat4 projTrans;attribute vec2 Position;attribute vec2 TexCoord;varying vec2 vTexCoord;void main() {    vTexCoord = TexCoord;    gl_Position = u_projView * vec4(Position, 0.0, 1.0);}

Fragment shader:

uniform sampler2D tex0;varying vec2 vTexCoord;void main() {    vec4 color = texture2D(tex0, vTexCoord);    gl_FragColor = color;}
Version 330:

As of GLSL 130 +,inAndoutAre used insteadattributeAndvarying. GLSL 330 + includes other features like layout qualifiers and changestexture2DTotexture.

Vertex shader:

#version 330uniform mat4 projTrans;layout(location = 0) in vec2 Position;layout(location = 1) in vec2 TexCoord;out vec2 vTexCoord;void main() {    vTexCoord = TexCoord;    gl_Position = u_projView * vec4(Position, 0, 1);}

Fragment shader:

#version 330uniform sampler2D tex0;in vec2 vTexCoord;//use your own output instead of gl_FragColor out vec4 fragColor;void main() {    //'texture' instead of 'texture2D'    fragColor = texture(tex0, vTexCoord);}
Other Significant ChangesGLSL 120 Additions
  • You can initialize arrays within a shader, like so:
float a[5] = float[5](3.4, 4.2, 5.0, 5.2, 1.1);float b[5] = float[](3.4, 4.2, 5.0, 5.2, 1.1);

However, the above is not supported on Mac OSX Snow Leopard, even with GLSL 120. (1)

  • You can initialize uniforms in a shader, and the value will be set at link time:
uniform float val = 1.0;
  • You can use built-ins likesin()When settingconstValue
  • Integers are implicitly converted to floats when necessary, for example:
float f = 1.0; <-- validfloat g = 1; <-- only supported in GLSL 120vec2 v = vec2(1, 2.0); <-- only supported in GLSL 120
  • You can usefTo define a float:float f = 2.5f;
GLSL 130 Additions
  • intAnduintSupport (and bitwise operations with them)
  • switchStatement support
  • New built-ins:trunc(),round(),roundEven(),isnan(),isinf(),modf()
  • Fragment output can be user-defined
  • Input and output is declaredinAndoutSyntax insteadattributeAndvarying
GLSL 150 Additions
  • texture()Shocould now be used insteadtexture2D()
GLSL 330 Additions
  • Layout qualifiers can declare the location of vertex shader inputs and fragment shader outputs, eg:
layout(location = 2) in vec3 values[4];

Formally this was only possibleARB_explicit_attrib_locationExtension

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.