Shadows and PerPixel Lighting with Alpha Textures

BlitzMax Forums/MiniB3D Module/Shadows and PerPixel Lighting with Alpha Textures

RustyKristi(Posted 2016) [#1]
I would like to use perpixel lighting with shadows on meshes with alpha textures. It seems when I try to apply CreateShadow using the perpixel lighting example, the alpha texture is not affected with the effect and still cast shadows.

https://github.com/markcwm/openb3d.mod/blob/master/examples/sl_pixellight.bmx


markcw(Posted 2016) [#2]
Can you provide a sample so I don't have to? :)


RustyKristi(Posted 2016) [#3]
Hey munch, thanks! ok here it is.. :-)

that's a tree model in replacement of the teapot with the source and textures. I forgot to mention that I still have problems correctly applying textures with the per pixel lighting.


markcw(Posted 2016) [#4]
Ok, I have an example for pixellight with alphamap now here. This doesn't include your media so for now it needs that supplied.

I added alphamap on it's own to compare with the P key. Also I found a bug where HideEntity crashes if it is a shadow caster, need to fix that as it should work.

So note I needed to load a brush and set BrushFX surf.brush to have the leaves alpha but the trunk opaque. I also use ShadeSurface so the trunk is not shadered. In the pixellight2 shader all I did was add the alphamap code and then I had to remove the light alpha value from the final color as the leaves were flickering "gl_FragColor = vec4(color.rgb * pixel.rgb, pixel.a);"

And of course, stencil shadows don't work with alpha maps.


RustyKristi(Posted 2016) [#5]
I added alphamap on it's own to compare with the P key. Also I found a bug where HideEntity crashes if it is a shadow caster, need to fix that as it should work.


This is awesome thanks! Since we're still on topic, I have a small issue in creating my own basic shaders, I tried a simple example and it works fine in a standalone glsl program where I got it from, but I when I port it to OpenB3D, the gl_Fragcolor image is always inverted.

Any idea why?


markcw(Posted 2016) [#6]
Maybe you're not setting a uniform. It only takes one mistake to ruin a shader.


RustyKristi(Posted 2016) [#7]
I have tried this

gl_Position = ftransform();


and this..

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;


in the vertex shader

any suggestions?


markcw(Posted 2016) [#8]
Both of those do the same thing, ftransform is older. I'd need to see the full source.


RustyKristi(Posted 2016) [#9]
Ok it's just a simple greyscale shader that I'm currently trying out here:

https://github.com/chudooder/entanglement/blob/master/shaders/greyscale.frag


markcw(Posted 2016) [#10]
I'm not having any problems with it, it could be the image but I don't think so. Are you using the default.vert with it?
' sl_greyscale.bmx

Strict

Framework Openb3d.B3dglgraphics

Graphics3D 800,600,0,2


Local camera:TCamera=CreateCamera()
CameraClsColor camera,0,125,250

Local light:TLight=CreateLight()

Local cube:TMesh=CreateCube()
PositionEntity cube,-1.5,0,4

Local cube2:TMesh=CreateCube()
PositionEntity cube2,1.5,0,4

Local cone:TMesh=CreateCone()
PositionEntity cone,0,0,10
ScaleEntity cone,4,4,4

Local plane:TMesh=CreateCube()
ScaleEntity plane,10,0.1,10
MoveEntity plane,0,-1.5,0

Local shader:TShader=LoadShader("","shaders/default.vert.glsl","shaders/greyscale.frag.glsl")
ShaderTexture(shader,LoadTexture("media/colorkey.jpg"),"texture0",0)
ShadeEntity(cube,shader)
EntityFX(cube,32)

Local shader2:TShader=LoadShader("","shaders/default.vert.glsl","shaders/default.frag.glsl")
ShaderTexture(shader2,LoadTexture("media/colorkey.jpg"),"texture0",0)
ShadeEntity(cube2,shader2)
EntityFX(cube2,32)

Local efx%=1


While Not KeyDown(KEY_ESCAPE)

	' turn cubes
	If KeyDown(KEY_LEFT)
		TurnEntity cube,0,-0.5,0.1
		TurnEntity cube2,0,0.5,-0.1
	EndIf
	If KeyDown(KEY_RIGHT)
		TurnEntity cube,0,0.5,-0.1
		TurnEntity cube2,0,-0.5,0.1
	EndIf
	
	' alpha blending: alpha / nothing
	If KeyHit(KEY_B)
		efx=Not efx
		If efx
			EntityFX(cube,32) ; EntityFX(cube2,32)
		Else
			EntityFX(cube,0) ; EntityFX(cube2,0)
		EndIf
	EndIf
	
	RenderWorld
	
	Text 0,0,"Left/Right: turn cubes"+", B: alpha blending = "+efx
	
	Flip

Wend
End



markcw(Posted 2016) [#11]
the gl_Fragcolor image is always inverted.

I know what you're doing here now. You're rendering a texture to a sprite and it's upsidedown. You have to invert the y in the shader like this
gl_TexCoord[0].st
// change to
vec2(gl_TexCoord[0].s, -gl_TexCoord[0].t)



RustyKristi(Posted 2016) [#12]
Thanks! What if I want to y-flip the resulting gl_FragColor itself in preparing for another operation or effect, is it possible?

It's not important atm but just trying to learn some glsl stuff.


markcw(Posted 2016) [#13]
I found a nice way to avoid inverting the y coords in the shader now. You use ScaleTexture shadertex,1,-1 before you call LoadShader. Edit: also you must multiply texcoords in the vert by gl_TextureMatrix[].

I think if you're doing a 2 pass shader (like in sl_2pass.bmx) the texture gets re-inverted so you don't need ScaleTexture in that case. Edit: wrong, this was due to the shader not multiplying by TextureMatrix.


RustyKristi(Posted 2016) [#14]
Awesome! :)