Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (808)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (872)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Motion Blur Woes  (Read 6052 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2014-06-15 09:05:33 »

After re-finding this article ( I decided I would try and tackle motion blur. It's really not that complicated of an effect, and I thought I could implement it rather easily. Of course, I was wrong.
I'm sending in the inverse of the model view matrix as well as the previous model view projection matrix correctly. I'm multiplying the view matrix of the camera (rotation matrix * translation vector) by the transformation matrix (world/model matrix).

I'm inverting it using this code, taken from the jMonkeyEngine but converted to work with my matrices:

I believe this works just fine. If you have a better/simpler way to invert a matrix, do tell.
I also have a linearized depth texture.

After that, I get the position of the current pixel in view space and then do all the other fancy stuff that gets me my blur vector. Then I blur.. along that vector. Surprising, right?
GLSL Code:

Well, everything runs fine however when I move, nothing happens. Nada.
But check this out; if I multiple blurVec by 100, I get a constant blur at a vector that seems to be about 1, 1. It looks like this:

I am not moving in this at all, just standing still. Therefore, when I'm not even moving, the blur vector is a very small value around 0.001 on the x and y. It stays this way when I am moving too though.

Is there anything you can think of off the top of your head why this may be happening?

P.S. This is what the position looks like, used as color:
Offline theagentd
« Reply #1 - Posted 2014-06-15 12:42:02 »

You're calculating the world position wrongly. OpenGL also expects your depth value to go from -1 to +1, not 0 to 1. I am also pretty sure that you should not invert Y when doing that. Finally, you can do a small optimization by not dividing by W for the current position.

float z = texture2D(R_depthBuffer, texCoord0).r;

vec3 currentPosition = vec3(texCoord0, z) * 2 - 1;
vec4 previous = T_previousMVP * (T_MVInverse * vec4(currentPosition, 1.0));
vec3 previousPosition = / previous.w;

vec2 blurVec = currentPosition.xy - previousPosition.xy;


If you still have a problem with this code, then you most likely have a problem with your matrices. Also note that this motion blur technique only takes into consideration camera movement and rotation, not object movement.

Offline Slyth2727
« Reply #2 - Posted 2014-06-15 21:40:07 »

I plugged that code in and still no dice. Seems that I'm sending the matrices in wrong Emo
I'm aware that this only takes into account the camera motion, I'll be moving on to the next article of his about motion blur after I figure this out.

To get the view matrix, I'm doing this:


cameraRotation is the conjugated rotation quaternion turned into a rotation matrix.

Then I do view.mul(worldMatrix).invert().
Worldmatrix is the translation matrix of the post process quad.
I have no clue what could be going wrong, other than my invert method. However, I think it's working fine. I'm going to just go on to his next article on motion blur and see what happens from there...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2014-06-15 22:17:14 »

Start by figuring out which matrix isn't working. Stop when you find a problem, fix it and continue to the next step once you've solved it.

1. Make the fragment shader output to gl_FragColor. This is a bit hard to read, with most of the screen having negative X and/or Y (which displays as 0 of course), to ensure that you're basing the calculation on the correct coordinates. The depth should be somewhat easy to read in the blue channel. Now that I think about it, if your texture coordinates have (0, 0) in the top left corner, you DO need to invert Y.
vec3 currentPosition = vec3(texCoord0, z) * 2 - 1;
currentPosition.y = -currentPosition.y;
If you find an error here: The error occurs before the matrices are even used.

2. Make it output the following to check if the inverted matrix is correct:
vec4 worldPos = T_MVInverse * vec4(currentPosition, 1.0);
gl_FragColor = worldPos / worldPos.w;

This will output the WORLD position of each pixel. This will most likely be a value far over 1.0, so you may want to divide it by 10 or 100 or something to get reasonable values. Make sure that the world position values are stable under camera motion and rotation for static objects.
If you find an error here: The problem lies in the T_MVInverse matrix.

3. Make it output previousPosition, which should look the same as currentPosition when the camera is not moving.
If you find an error here: The problem lies in the T_previousMVP matrix.

If you feel like going all out and implement something much more complicated, you can go for this motion blur algorithm:
I've implemented it myself, and it works great. It relies on per pixel motion vectors so it can handle any kind of motion. I calculate accurate motion vectors which take into account camera movement, object movement and skeleton animation movement. The cool thing about this algorithm is that it can actually blur over edges. It relies on a second low-resolution motion vector texture which keeps track of the dominant motion vector of the pixels that it covers to achieve this, although it does have trouble when motion in different directions causes the motion blur vectors to "overlap"... I get a feeling that this is a bit too advanced, but it's a cool paper nonetheless.

Offline Slyth2727
« Reply #4 - Posted 2014-06-15 23:16:40 »

Great explanation, thank you very much for the reply.
I've used the positions to debug the matrices before, but I didn't really know what to look for so I missed what was going wrong.
The problem lies within the inverted model view matrix, because the stuff changes as I move when using worldPos/worldPos.w as the color, like you suggested.

There's not much you guys can do to help me without knowing the whole damned engine so I'm by myself on this one. Thanks for the help, I'm taking a break for now because I'd rather not break my computer in anger. This hasn't been working for days now so a small break will be nice.

Ah bugs.  Roll Eyes
Offline theagentd
« Reply #5 - Posted 2014-06-16 01:10:15 »

Did you try inverting the Y channel? It may just be that the depth is wrong, in which case it would look very wrong.

Since GLSL shaders are so hard to debug, it's very important to learn how to output and read information as colors. You need to take into consideration the scale of the value, if it's negative or not and how it's supposed to behave when you move the camera.

Pages: [1]
  ignore  |  Print  

mercenarius (12 views)
2020-06-04 19:26:01

mercenarius (18 views)
2020-06-04 19:13:43

Riven (853 views)
2019-09-04 15:33:17

hadezbladez (5843 views)
2018-11-16 13:46:03

hadezbladez (2653 views)
2018-11-16 13:41:33

hadezbladez (6269 views)
2018-11-16 13:35:35

hadezbladez (1506 views)
2018-11-16 13:32:03

EgonOlsen (4742 views)
2018-06-10 19:43:48

EgonOlsen (5801 views)
2018-06-10 19:43:44

EgonOlsen (3292 views)
2018-06-10 19:43:20
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!