kaboom

09-20-2015, 12:25 PM

I need to convert these shaders from #version 400 core to #version 130 successfully but for some reason when I do it my models are not rendering properly when I load more then one type and I cant find any other issues with my code

Version 400 (vertexShader):

#version 400 core

in vec3 position;

in vec2 textureCoords;

in vec3 normal;

out vec2 pass_textureCoords;

out vec3 surfaceNormal;

out vec3 toLightVector;

out vec3 toCameraVector;

out float visibility;

uniform mat4 transformationMatrix;

uniform mat4 projectionMatrix;

uniform mat4 viewMatrix;

uniform vec3 lightPosition;

uniform float useFakeLighting;

uniform vec2 offset;

const float density = 0.004;

const float gradient = 1.5;

void main(void) {

vec4 worldPosition = transformationMatrix * vec4(position, 1.0);

vec4 positionRelativeToCam = viewMatrix * worldPosition;

gl_Position = projectionMatrix * positionRelativeToCam;

pass_textureCoords = textureCoords;

vec3 actualNormal = normal;

if(useFakeLighting > 0.5) {

actualNormal = vec3(0.0, 1.0, 0.0);

}

surfaceNormal = (transformationMatrix * vec4(actualNormal, 0.0)).xyz;

toLightVector = lightPosition - worldPosition.xyz;

toCameraVector = (inverse(viewMatrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz - worldPosition.xyz;

float distance = length(positionRelativeToCam.xyz);

visibility = exp(-pow((distance * density), gradient));

visibility = clamp(visibility, 0.0, 1.0);

}

and heres my version 130 vertexShader:

#version 130

in vec3 position;

in vec2 textureCoords;

in vec3 normal;

out vec2 pass_textureCoords;

out vec3 surfaceNormal;

out vec3 toLightVector;

out vec3 toCameraVector;

out float visibility;

uniform mat4 transformationMatrix;

uniform mat4 projectionMatrix;

uniform mat4 viewMatrix;

uniform vec3 lightPosition;

uniform float useFakeLighting;

uniform vec2 offset;

const float density = 0.004;

const float gradient = 1.5;

void main(void) {

vec4 worldPosition = transformationMatrix * vec4(position, 1.0);

vec4 positionRelativeToCam = viewMatrix * worldPosition;

gl_Position = projectionMatrix * positionRelativeToCam;

pass_textureCoords = textureCoords;

vec3 actualNormal = normal;

if(useFakeLighting > 0.5) {

actualNormal = vec3(0.0, 1.0, 0.0);

}

surfaceNormal = (transformationMatrix * vec4(actualNormal, 0.0)).xyz;

toLightVector = lightPosition - worldPosition.xyz;

toCameraVector = (transpose(viewMatrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz - worldPosition.xyz;

float distance = length(positionRelativeToCam.xyz);

visibility = exp(-pow((distance * density), gradient));

visibility = clamp(visibility, 0.0, 1.0);

}

I swapped out the inverse for transpose as its not supported in glsl 130 apparently. Theres also a small change in the fragment shaders as below:

Core 400 version fragment:

#version 400 core

in vec2 pass_textureCoords;

in vec3 surfaceNormal;

in vec3 toLightVector;

in vec3 toCameraVector;

in float visibility;

out vec4 out_Color;

uniform sampler2D modelTexture;

uniform vec3 lightColour;

uniform float shineDamper;

uniform float reflectivity;

uniform vec3 skyColour;

void main(void) {

vec3 unitNormal = normalize(surfaceNormal);

vec3 unitLightVector = normalize(toLightVector);

float nDot1 = dot(unitNormal, unitLightVector);

float brightness = max(nDot1, 0.2);

vec3 diffuse = brightness * lightColour;

vec3 unitVectorToCamera = normalize(toCameraVector);

vec3 lightDirection = -unitLightVector;

vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);

float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);

specularFactor = max(specularFactor, 0.0);

float dampedFactor = pow(specularFactor, shineDamper);

vec3 finalSpecular = dampedFactor * reflectivity * lightColour;

vec4 textureColour = texture(modelTexture, pass_textureCoords);

if(textureColour.a < 0.5) {

discard;

}

out_Color = vec4(diffuse, 1.0) * textureColour + vec4(finalSpecular, 1.0);

out_Color = mix(vec4(skyColour, 1.0), out_Color, visibility);

}

and the 130 version:

#version 130

in vec2 pass_textureCoords;

in vec3 surfaceNormal;

in vec3 toLightVector;

in vec3 toCameraVector;

in float visibility;

out vec4 out_Color;

uniform sampler2D modelTexture;

uniform vec3 lightColour;

uniform float shineDamper;

uniform float reflectivity;

uniform vec3 skyColour;

void main(void) {

vec3 unitNormal = normalize(surfaceNormal);

vec3 unitLightVector = normalize(toLightVector);

float nDot1 = dot(unitNormal, unitLightVector);

float brightness = max(nDot1, 0.2);

vec3 diffuse = brightness * lightColour;

vec3 unitVectorToCamera = normalize(toCameraVector);

vec3 lightDirection = -unitLightVector;

vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);

float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);

specularFactor = max(specularFactor, 0.0);

float dampedFactor = pow(specularFactor, shineDamper);

vec3 finalSpecular = dampedFactor * reflectivity * lightColour;

vec4 textureColour = texture2D(modelTexture, pass_textureCoords);

if(textureColour.a < 0.5) {

discard;

}

out_Color = vec4(diffuse, 1.0) * textureColour + vec4(finalSpecular, 1.0);

out_Color = mix(vec4(skyColour, 1.0), out_Color, visibility);

}

You can't use texture in the 130 core so I used texture2d and it works fine when I am loading only one model type but when I add the other models, they lose vertex positions and I am not sure as to why so i want to make sure these are converted properly. Thanks in advance.

Version 400 (vertexShader):

#version 400 core

in vec3 position;

in vec2 textureCoords;

in vec3 normal;

out vec2 pass_textureCoords;

out vec3 surfaceNormal;

out vec3 toLightVector;

out vec3 toCameraVector;

out float visibility;

uniform mat4 transformationMatrix;

uniform mat4 projectionMatrix;

uniform mat4 viewMatrix;

uniform vec3 lightPosition;

uniform float useFakeLighting;

uniform vec2 offset;

const float density = 0.004;

const float gradient = 1.5;

void main(void) {

vec4 worldPosition = transformationMatrix * vec4(position, 1.0);

vec4 positionRelativeToCam = viewMatrix * worldPosition;

gl_Position = projectionMatrix * positionRelativeToCam;

pass_textureCoords = textureCoords;

vec3 actualNormal = normal;

if(useFakeLighting > 0.5) {

actualNormal = vec3(0.0, 1.0, 0.0);

}

surfaceNormal = (transformationMatrix * vec4(actualNormal, 0.0)).xyz;

toLightVector = lightPosition - worldPosition.xyz;

toCameraVector = (inverse(viewMatrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz - worldPosition.xyz;

float distance = length(positionRelativeToCam.xyz);

visibility = exp(-pow((distance * density), gradient));

visibility = clamp(visibility, 0.0, 1.0);

}

and heres my version 130 vertexShader:

#version 130

in vec3 position;

in vec2 textureCoords;

in vec3 normal;

out vec2 pass_textureCoords;

out vec3 surfaceNormal;

out vec3 toLightVector;

out vec3 toCameraVector;

out float visibility;

uniform mat4 transformationMatrix;

uniform mat4 projectionMatrix;

uniform mat4 viewMatrix;

uniform vec3 lightPosition;

uniform float useFakeLighting;

uniform vec2 offset;

const float density = 0.004;

const float gradient = 1.5;

void main(void) {

vec4 worldPosition = transformationMatrix * vec4(position, 1.0);

vec4 positionRelativeToCam = viewMatrix * worldPosition;

gl_Position = projectionMatrix * positionRelativeToCam;

pass_textureCoords = textureCoords;

vec3 actualNormal = normal;

if(useFakeLighting > 0.5) {

actualNormal = vec3(0.0, 1.0, 0.0);

}

surfaceNormal = (transformationMatrix * vec4(actualNormal, 0.0)).xyz;

toLightVector = lightPosition - worldPosition.xyz;

toCameraVector = (transpose(viewMatrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz - worldPosition.xyz;

float distance = length(positionRelativeToCam.xyz);

visibility = exp(-pow((distance * density), gradient));

visibility = clamp(visibility, 0.0, 1.0);

}

I swapped out the inverse for transpose as its not supported in glsl 130 apparently. Theres also a small change in the fragment shaders as below:

Core 400 version fragment:

#version 400 core

in vec2 pass_textureCoords;

in vec3 surfaceNormal;

in vec3 toLightVector;

in vec3 toCameraVector;

in float visibility;

out vec4 out_Color;

uniform sampler2D modelTexture;

uniform vec3 lightColour;

uniform float shineDamper;

uniform float reflectivity;

uniform vec3 skyColour;

void main(void) {

vec3 unitNormal = normalize(surfaceNormal);

vec3 unitLightVector = normalize(toLightVector);

float nDot1 = dot(unitNormal, unitLightVector);

float brightness = max(nDot1, 0.2);

vec3 diffuse = brightness * lightColour;

vec3 unitVectorToCamera = normalize(toCameraVector);

vec3 lightDirection = -unitLightVector;

vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);

float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);

specularFactor = max(specularFactor, 0.0);

float dampedFactor = pow(specularFactor, shineDamper);

vec3 finalSpecular = dampedFactor * reflectivity * lightColour;

vec4 textureColour = texture(modelTexture, pass_textureCoords);

if(textureColour.a < 0.5) {

discard;

}

out_Color = vec4(diffuse, 1.0) * textureColour + vec4(finalSpecular, 1.0);

out_Color = mix(vec4(skyColour, 1.0), out_Color, visibility);

}

and the 130 version:

#version 130

in vec2 pass_textureCoords;

in vec3 surfaceNormal;

in vec3 toLightVector;

in vec3 toCameraVector;

in float visibility;

out vec4 out_Color;

uniform sampler2D modelTexture;

uniform vec3 lightColour;

uniform float shineDamper;

uniform float reflectivity;

uniform vec3 skyColour;

void main(void) {

vec3 unitNormal = normalize(surfaceNormal);

vec3 unitLightVector = normalize(toLightVector);

float nDot1 = dot(unitNormal, unitLightVector);

float brightness = max(nDot1, 0.2);

vec3 diffuse = brightness * lightColour;

vec3 unitVectorToCamera = normalize(toCameraVector);

vec3 lightDirection = -unitLightVector;

vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);

float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);

specularFactor = max(specularFactor, 0.0);

float dampedFactor = pow(specularFactor, shineDamper);

vec3 finalSpecular = dampedFactor * reflectivity * lightColour;

vec4 textureColour = texture2D(modelTexture, pass_textureCoords);

if(textureColour.a < 0.5) {

discard;

}

out_Color = vec4(diffuse, 1.0) * textureColour + vec4(finalSpecular, 1.0);

out_Color = mix(vec4(skyColour, 1.0), out_Color, visibility);

}

You can't use texture in the 130 core so I used texture2d and it works fine when I am loading only one model type but when I add the other models, they lose vertex positions and I am not sure as to why so i want to make sure these are converted properly. Thanks in advance.