Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Strange struct behavior

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Intern Contributor
    Join Date
    Apr 2015

    Question Strange struct behavior

    Hey, i have a strange behavior with my Light Structures. The problem is the following: I have one in/out struct LightComponents (which had initially 2 components - color and to light vector) and another structure LightTransformed .The lighting was working find until i decided to add another component attenuation to the LightComponents struct. For some unknown reason adding this somehow corrupts the other structure(LightTransformed) which is no way connected to the LightComponents. I managed to pin point the location where the data gets corrupted.

    Basically: If i keep the attenuation it gets f*^$%d up the line that's causing the problem is : "vec3 unitToCamera = (normalize(lightTransf.ToCamera)).xyz;"(if i replace unitToCamera with hard coded value it works). if i remove the attenuation completely from the LightComponents the lighting calculations pass and work fine without any adjustments.

    Bits from the Fragment shader
    Code :
    in struct LightTransformed {
    	vec4 Normal;
    	vec4 ToCamera;
    } lightTransf;
    in struct LightComponents {
    	vec4 ToLight;
    	vec4 DirLightColor;
    	vec4 Attenuation;
    } lightData[10];
    float calculateDirSpecularFactor(vec3 unitNormal,vec3 unitToLight,vec3 unitToCamera) {
    	vec3 invToLight = -unitToLight;
    	vec3 reflected = reflect(invToLight,unitNormal);
    	float specFactor = dot(unitToCamera,reflected);
    	float brightness = pow(max(specFactor,0.0),90.0);
    	return brightness;
    LightColor calculateDirLightColor(LightTransformed lightTransf, LightComponents lightComponents) {
    	LightColor finalColor;
    	//float distance = length(lightTransf.ToCamera);
    	//vec4 attFactor = lightComponents.Attenuation;
    	vec3 unitNormal = (normalize(lightTransf.Normal)).xyz;
    	vec3 unitToCamera = (normalize(lightTransf.ToCamera)).xyz;
    	vec3 unitToLight = (normalize(lightComponents.ToLight)).xyz;
    	finalColor.diffuseColor = lightComponents.DirLightColor * calculateDirDiffuseFactor(unitNormal,unitToLight);
    	finalColor.specularColor = lightComponents.DirLightColor * calculateDirSpecularFactor(unitNormal,unitToLight,unitToCamera);
    	finalColor.ambientColor = vec4(0.2);
    	return finalColor;

    Now if that is not weird enough here is another very strange thing if i use the first part of the code to generate the final color of the light(s), the code works even with the attenuation and the problems from above are not present. If i use the for loop function to generate the final color of the light(s) the same problem arises. Note here that the for loop works when the attenuation is not present in the structure

    Code :
    LightColor light;
    LightColor light1 = calculateDirLightColor(lightTransf,lightData[0]);
    	LightColor light2 = calculateDirLightColor(lightTransf,lightData[1]);
    	light.diffuseColor = light1.diffuseColor + light2.diffuseColor;
    	light.specularColor = light2.specularColor + light2.specularColor;
    LightColor calculateAllLights(LightTransformed lightTransf,LightComponents lightComponents[10]) {
    	LightColor finalLight;
    	for(int i=0; i<10; ++i) {
    		LightColor curLight = calculateDirLightColor(lightTransf,lightComponents[i]);
    		finalLight.diffuseColor = finalLight.diffuseColor + curLight.diffuseColor;
    		finalLight.specularColor = finalLight.specularColor + curLight.specularColor;
    	finalLight.ambientColor = vec4(0.2);
    	return finalLight;

    Bottom Line: No attenuation - no problems, every bit of code gets compiled and works just fine, with attenuation in the struct i have to remove the for loop to make it work (which is not very desired) . For me this makes exactly 0 sense !!!!

    Vertex Shader, the way i fill up those two structures
    Code :
    LightTransformed calculateLightTransformed(mat4 modelMatrix,mat4 viewMatrix,vec4 pos, vec3 normal) {
    	LightTransformed lightTransf;
    	lightTransf.ToCamera = inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0) - pos;
    	lightTransf.Normal = modelMatrix * vec4(normal,0);
    	return lightTransf;
    LightComponents calculateDirLightComponents(DirLightData dirLight,vec4 pos) {
    	LightComponents lightComponent;
    	lightComponent.DirLightColor = dirLight.color;
    	lightComponent.Attenuation = dirLight.attenuation;
    	lightComponent.ToLight = dirLight.lightpos - pos;
    	return lightComponent;
    Last edited by Asmodeus; 06-15-2016 at 11:11 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts