Trouble with if statment in shader and loading array of mat4 to uniform

Started by
6 comments, last by borcha 8 years, 11 months ago

I have with if here, for reasons uknown to me, it dosnt work. When i delete if statment or malualy write shadowMap[0] 1 or 2 it works fine, but with if i just get set of white triangles and squares.

here is part of my frag shader


float shadow(float bias)
{
    float visibility = 0;

    int index=1;
    
    if(gl_FragCoord.z<1){
        index=0;
    }
        
    vec4 shadowCoord=depthPV*vPos;

    if ( texture(shadowMap[index], shadowCoord.xy).z  <  shadowCoord.z+bias){
      visibility = -1;
    }
    return visibility;
}

Other problem i have is with loading array of mat4 into uniform here is code i tried, but it dosnt work, i use lwjgl 3 libery in java


shadowPVs=GL20.glGetUniformLocation(pId, "shadowPVs");
ByteBuffer shadowPVbuff=BufferUtils.createByteBuffer(shadePV.length*16*4);
for(int i=0;i<shadePV.length;i++){
    for(int v=0;v<16;v++){
        shadowPVbuff.putFloat(shadePV[i].val[v]);
    }
}
shadowPVbuff.flip();

GL20.glUniformMatrix4f(shadowPVs, shadePV.length, false, shadowPVbuff);

and in shader


uniform mat4 shadowPVs[3];

Advertisement
Why not just write:
int index = gl_FragCoord.z<1 ? 0 : 1;


Other problem i have is with loading array of mat4 into uniform here is code i tried, but it dosnt work, i use lwjgl 3 libery in java

There could be a million things wrong with the CPU code you posted, as all of those are custom functions. Is the array length correct? Should it be inverted? Is it putting floats into the correct bytes?


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

white pixels sounds like maybe no shader was used, are you validating that the shader compiles and links with no errors?

texture(shadowMap[index]

I'm not sure but are you allowed to do a branching read like that? Since the branch depends on the current fragment, don't you have to read both textures branchless into temporary variables first?
To me, when it comes to the matrix uniform, it looks like you're trying to allocate too many bytes. I would think it should be:
ByteBuffer shadowPVbuff=BufferUtils.createByteBuffer(16*4);

because of the 16 floats for the matrix and then times 4 for each of the bytes

Also, I'm not too sure on this, but if you are calling this code (the byte buffer put code) every frame, which I assume you are. I believe the call to flip might be messing you up. I believe you need to call clear first at the top of that code bit, before the put calls. As clear will set the byte buffer's position back to 0 and flags it for writing.


I'm not sure but are you allowed to do a branching read like that?

Works in OpenGL 4.x on a Kepler GPU, although it can kill performance.

because of the 16 floats for the matrix and then times 4 for each of the bytes

Times shadePV.length for the number of matrices in the array.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

I think i found why my code dont work:

one-does-not-simply.jpg

i use sampler2DArrayShadow uniform and shadow2DArray to sample my shadows and everyting work nice, still didnt get array of mat 4 done, but this is not issue. Here my current frag shader code:


#version 330 core
#extension GL_EXT_texture_array : enable

uniform vec3 light;
uniform sampler2DArray textureSamp;
uniform sampler2DArrayShadow shadowMap;
uniform vec3 Far;
uniform mat4 depthPV1;
uniform mat4 depthPV2;
uniform mat4 depthPV3;
uniform int texIndex;

in vec3 vPos;
in vec3 Normal;
in vec2 TexCord[3];
in vec3 weight;

out vec4 color;

float shade(){
	int index=2;
	vec4 shadePos=vec4(vPos+0.2*Normal,1.0);   // i use normal vec as bias, and it work nice
	if(gl_FragCoord.z<Far.x){
		index=0;
		shadePos=depthPV1*shadePos;		
	}else if(gl_FragCoord.z<Far.y){
		index=1;
		shadePos=depthPV2*shadePos;
	}else{
		shadePos=depthPV3*shadePos;
	}
	shadePos.w=shadePos.z;
	shadePos.z=float(index);			
	return shadow2DArray(shadowMap, shadePos).x ;
}

vec4 texColor(){
	vec4 texColor=	texture2DArray(textureSamp, vec3(TexCord[0],texIndex))*weight.y+
			texture2DArray(textureSamp, vec3(TexCord[1],texIndex))*weight.z+
			texture2DArray(textureSamp, vec3(TexCord[2],texIndex))*weight.x;
return texColor;
}
 
void main(void) {
	float cosTheta=dot(Normal, -light);	
	cosTheta=clamp(cosTheta,0,1);	
	color = texColor()*(cosTheta*shade()+0.15+cosTheta*0.5)/1.5;
}

This topic is closed to new replies.

Advertisement