Convert glsl from 150 to 120

I have some examples that I want to run on my PC. The problem is that they are written using glsl target 150, and my computer only supports version 120. I am sure that the program itself is simple enough to not require any advanced features of OpenGL 3.1. I found some information about what steps should be taken to convert glsl (for example, changing to an attribute, regardless of its change), but it still does not compile (is it really possible to somehow get a meaningful error message from this? )

original .vert

#version 150 in vec2 in_Position; in vec3 in_Color; out vec3 ex_Color; void main(void) { gl_Position = vec4(in_Position.x, in_Position.y, 0.0, 1.0); ex_Color = in_Color; } 

original .frag

 #version 150 precision highp float; in vec3 ex_Color; out vec4 gl_FragColor; void main(void) { gl_FragColor = vec4(ex_Color,1.0); } 

changed .vert

 #version 120 attribute vec2 in_Position; attribute vec3 in_Color; varying vec3 ex_Color; void main(void) { gl_Position = vec4(in_Position.x, in_Position.y, 0.0, 1.0); ex_Color = in_Color; } 

changed .frag

 #version 120 precision highp float; attribute vec3 ex_Color; void main(void) { gl_FragColor = vec4(ex_Color,1.0); } 

So can anyone spot the problem here?

+4
source share
3 answers

To get compilation / link error messages, you need to use the glGetShaderInfoLog for shaders and glGetProgramInfoLog for programs.

They will tell you what your specific mistakes are.

Just by taking a hit by mistake, you declare the attribute input in the fragment shader, which I think should be varying . Attributes are for data-> vertex shader, and variables for shader shader -> fragment shader.

The glsl 120 specification also mentions that the precision determinant is “reserved for future use,” so it cannot be applied to version 120. Perhaps you can refuse it.

But you should still be familiar with the functions of infolog, regardless of whether you will definitely need them in the end.

+4
source

You can get compilation errors by extracting the "information log":

 GLuint nVertexShader, nPixelShader; // handles to objects GLint vertCompiled, fragCompiled; // status values GLint linked; glCompileShader(nVertexShader); glGetShaderiv(nVertexShader, GL_COMPILE_STATUS, &vertCompiled); 

if vertCompiled (or fragCompile) == 0, do this to understand why:

 int infologLength = 0; int charsWritten = 0; glGetShaderiv(nVertexShader, GL_INFO_LOG_LENGTH, &infologLength); if (infologLength > 0) { GLchar* infoLog = (GLchar *)malloc(infologLength); if (infoLog == NULL) { printf( "ERROR: Could not allocate InfoLog buffer"); exit(1); } glGetShaderInfoLog(nVertexShader, infologLength, &charsWritten, infoLog); printf( "Shader InfoLog:\n%s", infoLog ); free(infoLog); } 

You can do the same with the link, just set == 0 for the link and get the log as above:

 glLinkProgram(m_nProgram); glGetProgramiv(m_nProgram, GL_LINK_STATUS, &linked); 
+3
source
 precision highp float; 

This is not legal in GLSL 1.20. I don’t even know why it was placed in shader 1.50, since precision specifiers are only useful in GLSL ES , and you cannot share 1.50 shaders with GLSL ES.

+1
source

Source: https://habr.com/ru/post/1441658/


All Articles