[SOLVED]Convert glsl 4.0 to 1.2 for equirectEnvMap

Hi, Now I’m converting version 400 to 120 to put Equirect enviroment map technique into my Work.
i think i’ll have to do this.

  • in->attribute
  • out->varying
  • delete attribute valiable in fragment shader
  • change some functions and valiables
  • ready inverse()

I did above these.
And build was successed, no error. but screen was all-white…
i guess it causes vertex shader, inverse matrix…however i don’t familiar for Matrix and glsl…

Anyone can tell me causes and solutions.
I would like to appreciate your help:)

Advanced.

This is minimal GLSL 4.0 code. it works well.

ofApp.cpp

void ofApp::draw(){
envTex = envImage.getTexture();

cam.begin();
    envMapShader.begin();
    envMapShader.setUniformTexture("envMap", envTex, 0);
    envMapShader.setUniformMatrix4f("viewTranspose",ofMatrix4x4::getTransposedOf(cam.getModelViewMatrix()));
    sphere.draw();
    envMapShader.end();
cam.end();
}
    

equirectEnvMap.vert

#version 400
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 textureMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform vec4 globalColor;

uniform mat4 viewTranspose;

in vec4  position;
in vec4  color;
in vec3  normal;
in vec2  texcoord;

out vec4 colorVarying;
out vec2 texCoordVarying;

out vec3 normalVarying;
out vec4 positionVarying;

out vec3 v_normalVarying;
out vec4 v_positionVarying;

out vec3 reflectVec;

void main() {
    normalVarying = normal;
    positionVarying = position;
    
    v_normalVarying = normalize(vec3(transpose(inverse(modelViewMatrix)) * vec4(normal, 1.0)));
    v_positionVarying = modelViewMatrix * vec4(position.xyz, 1.0);
    
    texCoordVarying = texcoord;
    
    vec3 relfect0 = reflect(-normalize(v_positionVarying.xyz), v_normalVarying);
    reflectVec = vec3(viewTranspose * vec4(relfect0, 0.0));
    
    colorVarying = globalColor;
    
    gl_Position = modelViewProjectionMatrix * position;
    
}

equirectEnvMap.frag

#version 400

#define PI 3.14159265358979
#define TwoPI 6.28318530718

uniform sampler2D envMap;
uniform sampler2D tex;

in vec4 colorVarying;
in vec2 texCoordVarying;

in vec3 normalVarying;
in vec4 positionVarying;
in vec3 v_normalVarying;
in vec4 v_positionVarying;

in vec3 reflectVec;

out vec4 fragColor;





vec2 envMapEquirect(vec3 dir) {
    float phi = acos(-dir.y);
    float theta = atan(-1.0 * dir.x, dir.z) + PI;
    return vec2(theta / TwoPI, phi / PI);
}

void main (void) {
    fragColor = vec4(texture(envMap, envMapEquirect(reflectVec)).rgb, 1.0);
}

This is a 120 code

ofApp.cpp

void ofApp::draw(){
    envTex = envImage.getTexture();
    cam.begin();
    
    envMapShader.begin();
    envMapShader.setUniformTexture("envMap", envTex, 0);
    envMapShader.setUniformMatrix4f("viewTranspose", ofMatrix4x4::getTransposedOf(cam.getModelViewMatrix()));
    envMapShader.setUniformMatrix4f("normalMatrix", ofMatrix4x4::getTransposedOf(cam.getModelViewMatrix().getInverse()));
    sphere.draw();
    envMapShader.end();
    cam.end();
}

equirectEnvMap.vert

#version 120

uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 textureMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform vec4 globalColor;

uniform mat4 viewTranspose;
uniform mat4 normalMatrix;

attribute vec4  position;
attribute vec4  color;
attribute vec3  normal;
attribute vec2  texcoord;


varying vec4 colorVarying;
varying vec2 texCoordVarying;

varying vec3 normalVarying;
varying vec4 positionVarying;

varying vec3 v_normalVarying;
varying vec4 v_positionVarying;

varying vec3 reflectVec;

void main() {
    normalVarying = normal;
    positionVarying = position;
    
    v_normalVarying = normalize(vec3(transpose(inverse(modelViewMatrix)) * vec4(normal, 1.0)));
    v_positionVarying = modelViewMatrix * vec4(position.xyz, 1.0);
    
    texCoordVarying = texcoord;
    
    vec3 relfect0 = reflect(-normalize(v_positionVarying.xyz), v_normalVarying);
    reflectVec = vec3(viewTranspose * vec4(relfect0, 0.0));
    
    colorVarying = globalColor;
    
    gl_Position = modelViewProjectionMatrix * position;
    
}

equirectEnvMap.frag

#version 120

#define PI 3.14159265358979
#define TwoPI 6.28318530718

uniform sampler2D envMap;
uniform sampler2D tex;

varying vec4 colorVarying;
varying vec2 texCoordVarying;

varying vec3 normalVarying;
varying vec4 positionVarying;
varying vec3 v_normalVarying;
varying vec4 v_positionVarying;

varying vec3 reflectVec;


vec2 envMapEquirect(vec3 dir) {
    float phi = acos(-dir.y);
    float theta = atan(-1.0 * dir.x, dir.z) + PI;
    return vec2(theta / TwoPI, phi / PI);
}

void main () {
    gl_FragColor = vec4(texture2D(envMap, envMapEquirect(reflectVec)).rgb, 1.0);
}
  1. The first thing to do, and verify the use of ofDisableArbTex (); in setup ().
  2. the conversion GLSL 120 does not seem to be complete, the function inverse()
    if wrong you can not use with GLSL 120.

unfortunately I can not try this code, because at the moment I do not have a configured openFrameworks, try to make these changes:

vertex shader:

#version 120
uniform vec4 globalColor;
uniform mat4 viewTranspose;
uniform mat4 normalMatrix;

attribute vec4  position;
attribute vec4  color;
attribute vec3  normal;
attribute vec2  texcoord;


varying vec4 colorVarying;
varying vec2 texCoordVarying;

varying vec3 normalVarying;
varying vec4 positionVarying;

varying vec3 v_normalVarying;
varying vec4 v_positionVarying;

varying vec3 reflectVec;

void main() {
    normalVarying = normal;
    positionVarying = position;

    //v_normalVarying = normalize(vec3(transpose(inverse(gl_ModelViewMatrix)) * vec4(normal, 1.0)));
    v_normalVarying = normalize(vec3(transpose(gl_ModelViewMatrix) * vec4(normal, 1.0)));

    v_positionVarying = gl_ModelViewMatrix * vec4(gl_Vertex.xyz, 1.0);

    texCoordVarying = gl_TexCoord[0].st;

    vec3 relfect0 = reflect(-normalize(v_positionVarying.xyz), v_normalVarying);
    reflectVec = vec3(viewTranspose * vec4(relfect0, 0.0));

    colorVarying = globalColor;
    gl_Position =  gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
}

fragment shader:

#version 120

#define PI 3.14159265358979
#define TwoPI 6.28318530718

uniform sampler2D envMap;
uniform sampler2D tex;

varying vec4 colorVarying;
varying vec2 texCoordVarying;

varying vec3 normalVarying;
varying vec4 positionVarying;
varying vec3 v_normalVarying;
varying vec4 v_positionVarying;

varying vec3 reflectVec;


vec2 envMapEquirect(vec3 dir) {
    float phi = acos(-dir.y);
    float theta = atan(-1.0 * dir.x, dir.z) + PI;
    return vec2(theta / TwoPI, phi / PI);
}

void main () {
    gl_FragColor = vec4(texture2D(envMap, envMapEquirect(reflectVec)).rgb, 1.0);
}

verify setup ofApp:

 ofDisableArbTex();
 shader.load("env");
 envImage.load("image.jpg");

verify draw ofApp:

ofEnableDepthTest();
scene
ofDisableDepthTest();

you should clean up the GLSL code and remove all unused variables, and to dig more deeply for a inverse() function…

https://www.opengl.org/sdk/docs/man/html/inverse.xhtml

Thanks for reply, @kashim
i tryed your code, sphere was displayed, but not mapped.

My GLSL code is not so good. i have no time but, first i’m gonna clean up my code and study about matrix, envmap.

perhaps it is the problem of texture coord that you can fix in more ways than one.

Here a small example:

fragment:

#version 120

uniform sampler2D tex;

void main()
{
    vec2 coord = gl_TexCoord[0].st;
    gl_FragColor = texture2D(tex,coord);

}

vertex:

#version 120

void main() {
    gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
    gl_Position = ftransform();
}

ofApp:

class ofApp : public ofBaseApp{
        public:
                ofShader shader;
                ofSpherePrimitive sphere;
                ofEasyCam cam;
                ofImage image;

                void setup(){
                        ofDisableArbTex();
                        shader.load("env");
                        image.load("image.jpg");
                }

                void update(){

                }

                void draw(){
                        ofEnableDepthTest();
                        cam.begin();
                            shader.begin();
                            shader.setUniformTexture("tex", image.getTexture(), 0);
                            sphere.draw();
                            shader.end();
                        cam.end();
                        ofDisableDepthTest();
                }
};

int main(){
        ofSetupOpenGL(1024,768, OF_WINDOW);
        ofRunApp( new ofApp());
}
1 Like

i wonder if it needs viewTransporse and this is best way.
Anyway i made it

Thanks for your help!!! @kashim

vertex
#version 120

#pragma include "inverse.vert"

uniform mat4 viewTranspose;

attribute vec3  normal;
varying vec3 reflectVec;


void main() {    
    vec3 v_normal = normalize(gl_NormalMatrix * gl_Normal);
    vec4 position = gl_ModelViewMatrix * gl_Vertex;
    
    vec3 relfect0 = reflect(-normalize(position.xyz), v_normal);
    reflectVec = vec3(viewTranspose * vec4(relfect0, 1.0));
    
    gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
    gl_Position = ftransform();
}

fragment

#version 120

#define PI 3.14159265358979
#define TwoPI 6.28318530718

    uniform sampler2D envMap;
    varying vec3 reflectVec;

    vec2 envMapEquirect(vec3 dir) {
        float phi = acos(-dir.y);
        float theta = atan(1.0 * dir.x, dir.z) + PI;
        return vec2(theta / TwoPI, phi / PI);
    }


    void main () {
        gl_FragColor = texture2D(envMap, envMapEquirect(reflectVec));
    }

at this time I can not test this code to give advice…

but if you want to use the transposition of the Modelview or ProjectionMatrix matrix.
You can also do this in openFrameworks using ofMatrix4x4 methods:

ofMatrix4x4 ofMatrix4x4::getTransposedOf(const ofMatrix4x4 &matrix)

for inverse:

ofMatrix4x4 ofMatrix4x4::getInverseOf(const ofMatrix4x4 &matrix)

pass the result of these to a uniform matrix4f.