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);
}