PDA

View Full Version : OpenGL Blur effect...



M109uk
07-05-2007, 12:23 PM
Hi all,

Yet another problem lol

Im trying to get a rubik's cube with a blur effect, how ever i get something else :s Its a nice effect and all, but not what i want...

I looked at the examples from NeHe, sulaco and somewhere else i cant remember..

What im getting is:

http://www.pulse-soft.oneuk.com/images/stories/prj_devilscube/blur_attempt1.jpg
(http://www.pulse-soft.oneuk.com/images/stories/prj_devilscube/blur_attempt1.jpg)

My code is as follows:


procedure Box(const Pos: TVector3f);

procedure renderPoint(Point: TVector3f);
var
p,n: TVector3f;
begin
p := Point;
n := VectorNormalize(p);
glNormal3fv(@n);
glVertex3fv(@p);
end;

begin
If bindEnv Then
Begin
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_BLEND);
_XMaterials.Bind(cubEnv, Nil);
End Else
_XMaterials.Bind(cubTex, Nil);

glBegin(GL_QUADS);
// front
glTexCoord2f(0, 0); renderPoint(Vector3fMake(Pos[0], Pos[1], Pos[2]));
glTexCoord2f(0, 1); renderPoint(Vector3fMake(Pos[0], Pos[1]+Size, Pos[2]));
glTexCoord2f(1, 1); renderPoint(Vector3fMake(Pos[0]+Size, Pos[1]+Size, Pos[2]));
glTexCoord2f(1, 0); renderPoint(Vector3fMake(Pos[0]+Size, Pos[1], Pos[2]));

glTexCoord2f(0, 0); renderPoint(Vector3fMake(Pos[0]+Size, Pos[1], Pos[2]));
glTexCoord2f(0, 1); renderPoint(Vector3fMake(Pos[0]+Size, Pos[1]+Size, Pos[2]));
glTexCoord2f(1, 1); renderPoint(Vector3fMake(Pos[0]+Size, Pos[1]+Size, Pos[2]+Size));
glTexCoord2f(1, 0); renderPoint(Vector3fMake(Pos[0]+Size, Pos[1], Pos[2]+Size));

glTexCoord2f(0, 0); renderPoint(Vector3fMake(Pos[0], Pos[1], Pos[2]+Size));
glTexCoord2f(0, 1); renderPoint(Vector3fMake(Pos[0], Pos[1]+Size, Pos[2]+Size));
glTexCoord2f(1, 1); renderPoint(Vector3fMake(Pos[0]+Size, Pos[1]+Size, Pos[2]+Size));
glTexCoord2f(1, 0); renderPoint(Vector3fMake(Pos[0]+Size, Pos[1], Pos[2]+Size));

glTexCoord2f(0, 0); renderPoint(Vector3fMake(Pos[0] , Pos[1], Pos[2]));
glTexCoord2f(0, 1); renderPoint(Vector3fMake(Pos[0] , Pos[1]+Size, Pos[2]));
glTexCoord2f(1, 1); renderPoint(Vector3fMake(Pos[0] , Pos[1]+Size, Pos[2]+Size));
glTexCoord2f(1, 0); renderPoint(Vector3fMake(Pos[0] , Pos[1], Pos[2]+Size));

glTexCoord2f(0, 0); renderPoint(Vector3fMake(Pos[0]+Size, Pos[1]+Size, Pos[2]));
glTexCoord2f(0, 1); renderPoint(Vector3fMake(Pos[0]+Size, Pos[1]+Size, Pos[2]+Size));
glTexCoord2f(1, 1); renderPoint(Vector3fMake(Pos[0] , Pos[1]+Size, Pos[2]+Size));
glTexCoord2f(1, 0); renderPoint(Vector3fMake(Pos[0] , Pos[1]+Size, Pos[2]));

glTexCoord2f(0, 0); renderPoint(Vector3fMake(Pos[0]+Size, Pos[1] , Pos[2]));
glTexCoord2f(0, 1); renderPoint(Vector3fMake(Pos[0]+Size, Pos[1] , Pos[2]+Size));
glTexCoord2f(1, 1); renderPoint(Vector3fMake(Pos[0] , Pos[1] , Pos[2]+Size));
glTexCoord2f(1, 0); renderPoint(Vector3fMake(Pos[0] , Pos[1] , Pos[2]));
glEnd;

If bindEnv Then
Begin
_XMaterials.UnBind(cubEnv, Nil);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
End Else
_XMaterials.Bind(cubTex, Nil);
end;

procedure DCCube;
begin
glPushMatrix;
Box(Vector3fMake(-11,-11,-11)); Box(Vector3fMake( 0,-11,-11)); Box(Vector3fMake( 11,-11,-11));
Box(Vector3fMake(-11,-11, 0)); Box(Vector3fMake( 0,-11, 0)); Box(Vector3fMake( 11,-11, 0));
Box(Vector3fMake(-11,-11, 11)); Box(Vector3fMake( 0,-11, 11)); Box(Vector3fMake( 11,-11, 11));

Box(Vector3fMake(-11, 0,-11)); Box(Vector3fMake( 0, 0,-11)); Box(Vector3fMake( 11, 0,-11));
Box(Vector3fMake(-11, 0, 0)); Box(Vector3fMake( 0, 0, 0)); Box(Vector3fMake( 11, 0, 0));
Box(Vector3fMake(-11, 0, 11)); Box(Vector3fMake( 0, 0, 11)); Box(Vector3fMake( 11, 0, 11));

Box(Vector3fMake(-11, 11,-11)); Box(Vector3fMake( 0, 11,-11)); Box(Vector3fMake( 11, 11,-11));
Box(Vector3fMake(-11, 11, 0)); Box(Vector3fMake( 0, 11, 0)); Box(Vector3fMake( 11, 11, 0));
Box(Vector3fMake(-11, 11, 11)); Box(Vector3fMake( 0, 11, 11)); Box(Vector3fMake( 11, 11, 11));
glPopMatrix;
end;

procedure RenderDCCube_ENV;
begin
bindEnv := False;
DCCube;

glDisable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);

bindEnv := True;
DCCube;
end;

procedure createTexture;
var
rX,rY,rZ: Integer;
begin
glBlendFunc(GL_SRC_ALPHA, GL_ONE);

glPushAttrib(GL_ALL_ATTRIB_BITS);
glViewport(0, 0, 128, 128);
glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT);

_XCameras[_XEngine.Camera].Render;

glTranslatef(0, 0, Z);
glRotatef(rot[0], 1, 0, 0);
glRotatef(rot[1], 0, 1, 0);
glRotatef(rot[2], 0, 0, 1);

RenderDCCube_ENV;

glMatrixMode(GL_PROJECTION);
glPopMatrix;

glMatrixMode(GL_MODELVIEW);
If blrTex > 0 Then
Begin
glBindTexture(GL_TEXTURE_2D, blrTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 128, 128);
End Else
Begin
glGenTextures(1, @blrTex);
glBindTexture(GL_TEXTURE_2D, blrTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 128, 128, 0);
End;
glPopAttrib;
end;

procedure InitDCubes;
begin
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
cubTex := _XMaterials.Add(BASE_DIR+'data.src\cube.png');
cubEnv := _XMaterials.Add(BASE_DIR+'data.src\Envmap.bmp');
end;

procedure PreRenderDCubes;
begin
glEnable(GL_DEPTH_TEST);
createTexture;
end;

procedure RenderDCubes;
var
a1,a2: Single;
i,rX,rY,rZ: Integer;
aspect: Double;
begin
glEnable(GL_DEPTH_TEST);
glTranslatef(0, 0, Z);
glRotatef(rot[0], 1, 0, 0);
glRotatef(rot[1], 0, 1, 0);
glRotatef(rot[2], 0, 0, 1);

RenderDCCube_ENV;

glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);

glBindTexture(GL_TEXTURE_2D, blrTex);
glColor4f(1, 1, 1, 1/numSamples);

_XEngine.Ortho(True);

a1 := 1;
a2 := 1/(numSamples-1)*blrDistance;

For i := 0 To numSamples Do
Begin
a1 := a1+a2;
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(-a1,-a1);
glTexCoord2f(1, 0); glVertex2f( a1,-a1);
glTexCoord2f(1, 1); glVertex2f( a1, a1);
glTexCoord2f(0, 1); glVertex2f(-a1, a1);
glEnd;
End;

_XEngine.Ortho(False);

rot[0] := rot[0]+Sin(50/(_XEngine.Delta/200));
rot[1] := rot[1]+Sin(60/(_XEngine.Delta/200));
rot[2] := rot[2]+Sin(70/(_XEngine.Delta/200));

If rot[0] > 360 Then rot[0] := 0;
If rot[1] > 360 Then rot[1] := 0;
If rot[2] > 360 Then rot[2] := 0;

If rot&#91;0&#93; < 0 Then rot&#91;0&#93; &#58;= 360;
If rot&#91;1&#93; < 0 Then rot&#91;1&#93; &#58;= 360;
If rot&#91;2&#93; < 0 Then rot&#91;2&#93; &#58;= 360;
end;


Im guessing that its just rendering the blur as the textures of the cube instead, but i cant for the life of me work out how and why :s im sure i've missed something really dumb..

Thanks

Robert Kosek
07-05-2007, 01:12 PM
I haven't touched OpenGL in quite awhile so I'm a bit rusty, but if I recall the most simple way to do a motion blur is to set the clear-scene color (the one you draw before each frame renders) to have an alpha value of about 0.8.

M109uk
07-05-2007, 02:43 PM
Thanks for the top :)

I have managed to get it working after a re-write... and i changed the alpha of the clear colour and all seems to work good :)

heres a screeny:

http://www.pulse-soft.oneuk.com/images/stories/prj_devilscube/blur_attempt2.jpg

Thanks again

Robert Kosek
07-05-2007, 03:12 PM
Awesome looking effect. :D I like it!

You're welcome, I'm just glad I remembered how to do it.

M109uk
07-05-2007, 03:34 PM
Thanks :)

I've updated it so that it looks like it is glowing on the inside of the cube, all i have to do now is think of a way that i can have it rotate throught the colours :?

http://www.pulse-soft.oneuk.com/images/stories/prj_devilscube/blur_attempt3.jpg

Traveler
07-05-2007, 04:19 PM
Stunning!

WILL
07-05-2007, 04:29 PM
Very nice indeed. ;)

Robert Kosek
07-05-2007, 05:00 PM
Ooh! I know!

Draw the whole thing in greyscale and then draw an additive color overlay! Voila! Rotating, colorized, blurred cubes! :D

Though, if you don't want something else beneath/within the cube to be the same color that'd be a mess.

M109uk
07-05-2007, 05:05 PM
Thanks for all the comments :)

heres a few more, i now have it bluring from white to red with a blue flash from with in :)

http://www.pulse-soft.oneuk.com/images/stories/prj_devilscube/blur_attempt4_1.jpg
http://www.pulse-soft.oneuk.com/images/stories/prj_devilscube/blur_attempt4_2.jpg

It sort of looks like theres lightning with in the cube :D

Robert Kosek
07-05-2007, 05:25 PM
Awesome! :D If you wanted to get psychedelic with it, you could stretch a dynamic plasma texture over it... but that could make some folks sick. Heh.

M109uk
07-05-2007, 05:30 PM
haha yeah, but would look cool ;)

Although at the moment i hope no one would get an epileptic fit :p

Im looking in to rendering it on to a texture, and adding water drops, and possibly a tunnel behind it.. depending on it does'nt look to crappy.

M109uk
07-05-2007, 07:16 PM
Heres another one,

I've added a tunnel in the background (although you can't really tell in the screenshot)... It does'nt look so empty now :)

http://www.pulse-soft.oneuk.com/images/stories/prj_devilscube/blur_attempt5.jpg

Robert Kosek
07-05-2007, 11:51 PM
That looks cool, I think it'd make an interesting screensaver. ;)

M109uk
08-05-2007, 06:26 PM
Thats such an great idea, i have done just that :)

it can be downloaded here:
http://www.pulse-soft.oneuk.com/downloads/devilscube/DCIntro.zip
ZIP File, 1.05Mb

I will probably add music at some point too, if i can get it working that is :p

PS. at the moment moving the mouse or keyboard input doesnt come out of the screensaver, but the esc key does.

Robert Kosek
08-05-2007, 06:34 PM
Heh, coolness! Looks really great in real-time. This'll stay as my screensaver for awhile. ;)

Traveler
08-05-2007, 09:35 PM
The sceenshots really aren't doing it any justice.

Nice work!

JSoftware
08-05-2007, 10:33 PM
Runs rather laggy on my machine. Barton 3200+, 9600 pro, 1,2gb ram

Is it just too old or have I forgot to set some settings?

Edit: But it looks nice!

M109uk
09-05-2007, 07:04 AM
Thanks for all the comments :)



Runs rather laggy on my machine. Barton 3200+, 9600 pro, 1,2gb ram

Is it just too old or have I forgot to set some settings?


Its probably the screensaver, by default it uses multisample with 1 buffer and 4 samples also it renders the blur 30+ times each render.

I have updated it, so now you can turn off multisample in the config panel, and i have also added music :) (to turn off music just delete/rename the ogg file.

I will add some options to change the amount of renders tonight when i get back to help speed things up :)

http://www.pulse-soft.oneuk.com/downloads/devilscube/DCIntro.zip
ZIP File, 5.21Mb