Sign in to follow this  

Environment Mapping Issues [Solved]

This topic is 1768 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am working on environment mapping my objects from a skybox and I am getting an unexpected result, the final output is nothing, yet all the components that are used in created the final output are correct.



This is my reflective factor



this is my base colour



Here is the environment mapped to the object



This is the final output from a mix() function, but I also tried just using the standard lerp (I wrote myself), which is all mix is.



As you can see, all of the inputs are visible, but the output color is blank.


This is the combination line.


finalColor = vec4(mix(baseColour.rgb,cubeColor,reflectiveFactor),1.0);


I did the math by hand as well, and I do not get this result. Any help would be great.



Edit Attaching Shaders:


Vertex Shader

#version 330
layout(location = 0) in vec3 vertex;
layout(location = 1) in vec2 uv;
layout(location = 2) in vec3 normal;
layout(location = 3) in vec3 tangent;
layout(location = 4) in vec3 bitangent;
out vec2 uv0;
out vec3 uv1;
out vec3 normal0;
out vec3 tangent0;
out vec3 bitangent0;

out vec3 worldPos0;
uniform mat4 mvp;
uniform mat4 world;

uniform vec3 cameraEyeWorldPosition;

void main()
	gl_Position =  mvp * vec4(vertex,1.0);
	uv0 = uv;
	normal0 = (world * vec4(normal,1.0)).xyz;
	tangent0 = (world * vec4(tangent,1.0)).xyz;
	worldPos0 = (world * vec4(vertex,1.0)).xyz;
	bitangent0 = (world * vec4(bitangent,1.0)).xyz;

	vec3 E = normalize(worldPos0 - cameraEyeWorldPosition);
	uv1 = reflect(E,normal0); 

Fragment Shader

#version 330
struct Attenuation
    float constant;
    float linear;
    float exponential;
struct BaseLight
	vec4 colour;
    float ambientIntensity;
    float diffuseIntensity;  
struct DirectionalLight 
    BaseLight base;
    vec3 direction;        
struct PointLight
	BaseLight base;  
    vec3 position;
	Attenuation attenuation;

struct SpotLight
	PointLight base;
	vec3 direction;
	float cutoff;

vec3 CalcBumpedNormal();
vec4 CalcLightInternal(BaseLight _light, vec3 _lightDirection, vec3 _normal);
vec4 CalcDirectionalLight(vec3 _normal);
vec4 CalcPointLight(PointLight _light, vec3 _normal);
vec4 CalcSpotLight(SpotLight _light, vec3 _normal);
const int MAX_POINT_LIGHTS = 10;
const int MAX_SPOT_LIGHTS = 5;

in vec2 uv0;
in vec3 uv1;
in vec3 normal0;
in vec3 tangent0;
in vec3 bitangent0;

in vec3 worldPos0;
out vec4 finalColor;
uniform DirectionalLight directionalLight;
uniform PointLight pointLights[MAX_POINT_LIGHTS];
uniform SpotLight spotLights[MAX_SPOT_LIGHTS];
uniform int numPointLights = 0;
uniform int numSpotLights = 0;
uniform vec3 cameraEyeWorldPosition = vec3(0.0,0.0,0.0);
uniform sampler2D diffuseTexture;
uniform sampler2D ambientTexture;
uniform sampler2D normalTexture;
uniform sampler2D specularTexture;
uniform sampler2D glossTexture;
uniform sampler2D reflectiveTexture;

uniform samplerCube cubeMapTexture;
void main()
	finalColor = vec4(1.0,1.0,1.0,1.0);

	vec4 MaterialDiffuseColor = texture2D(diffuseTexture, uv0);
	vec3 Normal = CalcBumpedNormal();

	vec4 TotalLight = vec4(0.0,0.0,0.0,1.0);
	TotalLight = CalcDirectionalLight(Normal);
	for (int i = 0; i < numPointLights; i++)
		TotalLight = TotalLight + CalcPointLight(pointLights[i], Normal);

	for (int i = 0 ; i < numSpotLights ; i++)
		TotalLight += CalcSpotLight(spotLights[i], Normal);

	float reflectiveFactor = texture2D(reflectiveTexture, uv0).r;
	vec3 cubeColor = texture(cubeMapTexture,;
	finalColor = MaterialDiffuseColor * TotalLight;
	finalColor = vec4(mix(finalColor.rgb,cubeColor,reflectiveFactor),1.0);

vec3 CalcBumpedNormal()
    vec3 Normal = normalize(normal0);
    vec3 Tangent = normalize(tangent0);
	vec3 Bitangent = normalize(bitangent0);
    Tangent = normalize(Tangent - dot(Tangent, Normal) * Normal);
    vec3 BumpMapNormal = texture(normalTexture, uv0).xyz;
    BumpMapNormal = 2.0 * BumpMapNormal - vec3(1.0, 1.0, 1.0);
    vec3 NewNormal;
    mat3 TBN = mat3(Tangent, Bitangent, Normal);
    NewNormal = TBN * BumpMapNormal;
    NewNormal = normalize(NewNormal);
    return NewNormal;
vec4 CalcLightInternal(BaseLight _light, vec3 _lightDirection, vec3 _normal)
	vec3 VertexToEye = vec3(0.0, 0.0, 0.0);
	VertexToEye = normalize(cameraEyeWorldPosition - worldPos0);

	vec4 AmbientColor = vec4(0.0, 0.0, 0.0, 0.0);

	AmbientColor = _light.colour * (texture2D(ambientTexture, uv0));
	float DiffuseFactor = 1.0;
	DiffuseFactor = dot(normalize(_normal), _lightDirection);

	DiffuseFactor = max(0.0,DiffuseFactor);
	vec4 DiffuseColor = vec4(0.0, 0.0, 0.0, 0.0);
	vec4 SpecularColor  = vec4(0.0, 0.0, 0.0, 0.0);
    DiffuseColor = _light.colour * _light.diffuseIntensity * DiffuseFactor;
	vec3 LightReflect = vec3(0.0, 0.0, 0.0);
	float SpecularFactor = 0.0;

	LightReflect = 2.0 * dot(_lightDirection, normal0) * normal0 - _lightDirection;

	SpecularFactor = dot(VertexToEye, LightReflect);;
	SpecularFactor = max(0.0,SpecularFactor);

	float specularPower = (texture2D(glossTexture, uv0).r);
	float specularIntensity = texture2D(specularTexture, uv0).r;

	SpecularFactor = pow(SpecularFactor, specularPower);
	SpecularColor = _light.colour * specularIntensity * SpecularFactor;
	return (AmbientColor /*+ RimColor*/ + DiffuseColor + SpecularColor);
vec4 CalcDirectionalLight(vec3 _normal)
    return CalcLightInternal(directionalLight.base, directionalLight.direction, _normal);
vec4 CalcPointLight(PointLight _light, vec3 _normal)
	vec3 LightDirection = vec3(0.0, 0.0, 0.0);
	float Distance = 0.0;

    LightDirection = worldPos0 - _light.position;
    Distance = length(LightDirection);
    LightDirection = normalize(LightDirection);
	vec4 Color = vec4(0.0, 0.0, 0.0, 0.0);
	float Attenuation = 1.0;

    Color = CalcLightInternal(_light.base, LightDirection, _normal);

	Attenuation =  _light.attenuation.constant +
							_light.attenuation.linear * Distance +
							_light.attenuation.exponential * Distance * Distance; 
	return Color/Attenuation;

vec4 CalcSpotLight(SpotLight _light, vec3 _normal)
	vec3 LightToPixel = vec3(0.0,0.0,0.0);
	float SpotFactor = 0.0;
    LightToPixel = normalize(worldPos0 - _light.base.position);
    SpotFactor = dot(LightToPixel, _light.direction);

    if (SpotFactor > _light.cutoff) 
        vec4 Color = CalcPointLight(_light.base, _normal);
        return Color * (1.0 - (1.0 - SpotFactor) * 1.0/(1.0 - _light.cutoff));
        return vec4(0.0, 0.0, 0.0, 0.0);


This issue is now solved, I had two textures using the same texture unit.

Edited by harshman_chris

Share this post

Link to post
Share on other sites

Is "reflectiveFactor" a sensible value? Try hardcoding it to 0.2 or something. Also, It looks like the fragment wasn't just blank, it didn't exist--this maybe points to a problem in your shader's compilation. Maybe also try expanding out the mix function (i.e. "(1.0-0.2)*baseColour.rgb+0.2*cubeColor").


Share this post

Link to post
Share on other sites

Is "reflectiveFactor" a sensible value? Try hardcoding it to 0.2 or something. Also, It looks like the fragment wasn't just blank, it didn't exist--this maybe points to a problem in your shader's compilation. Maybe also try expanding out the mix function (i.e. "(1.0-0.2)*baseColour.rgb+0.2*cubeColor").



Already done, same result.




So I set the outColour to vec4(1.0,1.0,1.0,1.0);


and then I run GEDebugger, and it doesnt break when I break on error, but if I ad one line, such as this.


Edit Again:


So the error I get is on the glDrawElements - GL_INVALID_OPERATION.


Which is one of two things:


GL_INVALID_OPERATION is generated if a geometry shader is active and mode is incompatible with the input primitive type of the geometry shader in the currently installed program object.

GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to an enabled array or the element array and the buffer object's data store is currently mapped.


Neither of these seem to apply though to this type of issue.



finalColor.r = (baseColour.r * (1.0 - 0.2)) + (cubeColor.r * 0.2);


It will break on an error, so there must be an error I will have to get and output. It is not a compile or linking error though because those are output.


Edit Again:


So I have narrowed down the issue to adding the the result of the first and second parts of the lerp.


Here is the first lerp part



The second lerp part



So these when added causes an OpenGL error. What would cause two vec3's being added together to cause this error? Considering both are valid on there own, but added them breaks it?

Edited by harshman_chris

Share this post

Link to post
Share on other sites
Sign in to follow this