Blur shadows with OpenB3D

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
angros47
Posts: 2371
Joined: Jun 21, 2005 19:04

Blur shadows with OpenB3D

Post by angros47 »

Time ago, someone (d.j. Peters) complained about the fact that in OpenB3D shadows pop up in unrealistic way.

There is no way to fix that issue (a stencil shadow is all-or-nothing), but there is a work-around: it's possible to cast the shadow more than once, and every time slightly move the light source, so the shadow is not exactly in the same place: then, all the rendered images can be fused in one, that represent the mean of all. In that way, when a shadow is "on limit", in some images is rendered, in other isn't, and it will appear less intense (the shadow intensity on that surface is proportional to the frequency it has been rendered).

With this trick, the shadow will pop up gradually (in the example, I also added entityFx cube,1 to make it full bright, to show that all the shading effect is due to stencil shadow and nothing else: of course, you can remove that line); also, the shadow is not sharp, has a small penumbra region (as with a light source that is small, but not infinitely small; more realistic). The flickering effect could be reduced further by increasing the number of iteration.

The same trick can be used to achieve other nice effects: i.e., if you move camera, and keep it pointed on the same object

The only drawback of this trick is that RenderWorld need to be called many times for each frame; if the scene is complex, frame rate might drop to very low values.

Code: Select all

#include "openb3d.bi"
#include once "GL/gl.bi"




screen 18,  32, , &h10002 

	
Graphics3d 640,480,32,1,1


var camera=createcamera(0)
var cube=createcube()
entityfx cube,1

moveentity cube,0,0,5

var plane=createplane()'cube():scaleentity plane, 100,.1,100
moveentity plane,0,-1.4,15


var light=createlight()
positionentity light,5,5,5
pointentity light,cube
moveentity camera,0,0,-5

createshadow cube

dim key as string
do

	key=inkey
        if key=chr(255)+"H" then turnentity cube,1,0,0,0
        if key=chr(255)+"P" then turnentity cube,-1,0,0,0
        if key=chr(255)+"M" then turnentity cube,0,-1,0,0
        if key=chr(255)+"K" then turnentity cube,0,1,0,0
	updateworld 1

	glClear(GL_ACCUM_BUFFER_BIT)

	for i as integer=1 to 20
		positionentity light,5+rnd()*1-.5,5+rnd()*1-.5,5+rnd()*1-.5
		renderworld
		glAccum(GL_ACCUM, .05)
	next

	glAccum(GL_RETURN, 1.0)
	sleep 1
	flip
flip
loop until key=chr(27)
Post Reply