Accuracy problem during coordinate transformation
Asked Answered
G

9

0

I calculated the screen coordinates of the vertices in the vertex shader, and inversely calculated the screen coordinates back to space coordinates. Then when I adjust the viewing direction, the vertices of the mesh appear very noticeably jittery. Is this caused by the loss of precision when transforming the coordinates? How to avoid it? The relevant code is as follows

void vertex(){
	vec2 view_size = vec2(1024, 600);
	vec4 p = transform_screen_pos(PROJECTION_MATRIX, INV_CAMERA_MATRIX, VERTEX, view_size);
	VERTEX = (inverse(MODELVIEW_MATRIX) * INV_PROJECTION_MATRIX * unproject(p.xy, view_size, p.z, p.w)).xyz;
}

vec4 unproject(vec2 screen, vec2 screen_size, float z, float w) {
  	vec2 clip_pos = vec2(screen.x / screen_size.x, screen.y / screen_size.y);
	vec3 clip_pos_3d = vec3(clip_pos,z);
	vec3 device_normal = clip_pos_3d * 2.0 - 1.0;
   	vec4 res = vec4(device_normal * w, w);
	return res;
}

vec4 transform_screen_pos(mat4 project, mat4 MODELVIEW, vec3 coord, vec2 screen_size){
	vec4 device = project * MODELVIEW * vec4(coord.xyz, 1.0);
	vec3 device_normal = device.xyz / device.w;
	vec3 clip_pos_3d = (device_normal * 0.5 + 0.5);
	float z = clip_pos_3d.z;
	float w = device.w;
	vec2 screen_pos = vec2(clip_pos_3d.x * screen_size.x, clip_pos_3d.y * screen_size.y); 
	vec4 res = vec4(screen_pos, z, w);
	return res;
}
Gaunt answered 25/1, 2022 at 8:44 Comment(0)
L
0

What are you trying to calculate? That seems like a lot of unnecessary code. For example, you can get the world position in a fragment shader using the SCREEN_UV and depth value, and it would only be like 3 lines of code.

Lilywhite answered 25/1, 2022 at 8:55 Comment(0)
G
0

I want to offset the vertices in screen space and then convert back to space coordinates, however I find that even if I don't offset and just do two opposite coordinate transformations, the space position of the vertices changes.

Gaunt answered 25/1, 2022 at 9:5 Comment(0)
K
0

Why are you using inverse camera matrix instead of modelview matrix when projecting?

Koheleth answered 25/1, 2022 at 11:33 Comment(0)
G
0

@xyz said: Why are you using inverse camera matrix instead of modelview matrix when projecting?

It was an oversight when I copied the code, but it didn't affect the final result. Even changing to a modelview matrix will still cause the coordinates to change slightly when converted back to world coordinates.

Gaunt answered 25/1, 2022 at 12:39 Comment(0)
K
0

Nothing happens when I run the shader. No jitter. Have you tried running the shader on different objects?

Koheleth answered 25/1, 2022 at 13:1 Comment(0)
G
0

Whats the scale of your objects, or where are they positioned? Floating point precision gets worse the further away from 0.0 your values get.

Also what are your z-depth values on the camera?

Goins answered 25/1, 2022 at 15:1 Comment(0)
G
0

@Megalomaniak said: Whats the scale of your objects, or where are they positioned? Floating point precision gets worse the further away from 0.0 your values get.

Also what are your z-depth values on the camera?

You're right, it's related to the size of the object. When I reduce the size of the object, the jitter phenomenon is not very obvious. This is the size of the object I originally tested with. I'm using such a large size because I'm using it to draw some rivers on the earth, using the real earth radius. Can I just reduce the radius of the earth to avoid this problem? Is there any other way to avoid this problem?

Gaunt answered 26/1, 2022 at 1:42 Comment(0)
L
0

20,000 units?!? LOL. Yeah, that's way too big. Why do you need the real Earth radius? A sphere with a 1 meter radius would look Earth sized if the camera was close enough.

Lilywhite answered 26/1, 2022 at 2:8 Comment(0)
G
0

There is a reason we call these floating point values 'scalars', yes you can and should just scale down your earth. Depending on whether you need to 'zoom in' to the planets surface up close you might just do a model swap and recenter your camera/world as need be, have some global variable for keeping track of the 'zoom' scalar.

Goins answered 26/1, 2022 at 4:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.