Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transforming glsl 150 to 120

Tags:

opengl

glsl

I have a couple of examples that I want to run on my PC. The problem is that they're written with glsl target 150 and my PC only supports version 120. I'm pretty sure that the program itself is simple enough not to require any extended functionality of OpenGL 3.1. I have found some information on what steps should be taken to transform glsl(f.e. changing in to attribute, out to varying) but it's still not compiling(is it actually possible to somehow get a meaningful error message out of 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 a problem here?

like image 269
Homulvas Avatar asked Feb 18 '23 17:02

Homulvas


2 Answers

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

These will tell you what your particular errors are.

Just taking a stab at what the error might be, you're declaring an attribute input in the fragment shader, which I believe should be a varying. Attributes are for data->vertex shader, and varyings are for vertex shader -> fragment shader.

The glsl 120 spec also mentions that precision qualifier is "reserved for future use", so it may not be applicable to version 120. You can probably leave it out.

But you should still get familiar with the infolog functions regardless, you'll definitely need them eventually.

like image 127
Tim Avatar answered Feb 28 '23 12:02

Tim


You can get the compile errors by retrieving the "info 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 see 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 linking, just check for linked == 0 and retrieve the log as above:

glLinkProgram(m_nProgram);
glGetProgramiv(m_nProgram, GL_LINK_STATUS, &linked);
like image 44
Mark Stevens Avatar answered Feb 28 '23 11:02

Mark Stevens