Sign in to follow this  
static_roller

Scaling object very small makes it all distorted (solved)

Recommended Posts

Hey everyone,

 

I'm developing a driving game and for sense of speed I use variable FOV according to speed. But I do not apply the FOV to the driven car but instead scale it down to keep it properly sized in relation to the surroundings. Problem is that with enough speed the cockpit starts to go all distorted, see this picture:

 

distorted_cockpit.jpg

 

Here you can see the problem in action: https://www.youtube.com/watch?v=TU7AEnqfZM8&feature=youtu.be&t=1m0s

 

Initially I assumed this would be a depth buffer issue, but it happens even with DepthEnable=false.

 

I'm rendering the car last, clear the depth buffer and try to set much lower near plane and far planes using D3DXMatrixPerspectiveFovLH, but it would appear it does absolutely nothing, no matter the values, is maybe something else required to change the near & far planes mid frame? Or is the problem related to something else?

 

The scale values given for D3DXMatrixScaling in the particular image above (simulated speed of 300km/h) are quite low:

scaleX: 0.048116, scaleY: 0.048116, scaleZ: 0.048116

 

The car's original size is something like X: 20, Z: 50, one meter is 10 units (not sure what to call them) in the engine.

 

I can give more details if needed.

 

Hopefully somebody can help me, I think I may have some other questions in the future too, this is I think my first ever DX related question on a forum.

 

Thanks,

 

static_roller

Edited by static_roller

Share this post


Link to post
Share on other sites
I currently have these (commented out though as neither does nothing):
 
//g.farPlane=200;
//g.nearPlane=0.1;
 
//g.farPlane=2;
//g.nearPlane=0.03;
 
Defaults for the engine are:
nearPlane=0.1f;
farPlane=35000.0f; 
 
Edit: I tested some small values for the engine defaults with no changes to the values midframe, couldn't see much of the world anymore but cockpit was still distorted. I also tried some further nearplane value for the vehicle rendering midframe which made just some front parts of the vehicle visible, so I guess changing the values mid frame kinda works, but it would seem this problem is related maybe to something else, I don't know..
Edited by static_roller

Share this post


Link to post
Share on other sites

I started thinking, is this a float precision problem? I mean let's suppose a vertice at 1.396 after scaling to 0.048116 according to calculator is 0.067169936, is it just too small number for float to handle? I do know floats have some precision problems and limited amount of digits but don't know much more than that..

Share this post


Link to post
Share on other sites

Are you perhaps transforming the vertices by world and view matrices separately? The further you get from the origin (0,0,0), the more risk is there that scaling will produce a small value that translation (addition) by large coordinates will then force to be represented out of the precision range. Since this model is so close to the view, total translation from model*view should be small, thus transforming a vertex by the combined model*view matrix should increase precision.

 

Floating point numbers can easily handle both large and small values, but not at the same time.

Share this post


Link to post
Share on other sites

Thanks for the reply, I think I am and I just tested jumping to the center of the map and set the 300km/h emulation on and no distortion whatsoever!

So I should somehow render the vehicle at position 0, 0, 0? I will get back to this tomorrow with a more clear head.. Thanks again!

Share this post


Link to post
Share on other sites

3:30am can't get no sleep thinking about this, but can't get my drunken ass head quite wrapped around this.

I do know that some implementations have the character in the center and just move the world around (how common is that btw)?

But that would be quite a hassle at this time, I'm thinking render the world in the edge of the hell where it is and then go with the camera to "the center" and render the vehicle there, is that possible? I just don't know, never done such thing, I currently just render things where they are, camera in single position whole frame. There is some world stage system which moves all things back after going further another 10km but that's another story..

 

Edit: re-read your post, uh I don't know, here's some shader code, view, projection and world matrices are separate..

cbuffer cbView {
matrix View;
matrix Projection;
};
 
cbuffer cbSubset {
matrix World;
float4 diffuseColor;
float enableTexture[2];
};

...
  
PS_INPUT vsBase( VS_INPUT input ) {
 
...
 
output.pos=input.pos;
output.tex = input.tex;
output.worldPos = mul( output.pos, World ); 
 
output=vsCommon(output);
output=vertexSFX(output, input.instanceId);
 
output.pos = mul( output.worldPos, View ); //transform position to clip space - keep worldspace position
output.pos = mul( output.pos, Projection ); 
Edited by static_roller

Share this post


Link to post
Share on other sites

uh having some trouble editing the code..

edit: post, lol

 

But are you saying I should provide identity view matrix for the shader and include (multiply) the view matrix to the world matrix at cpp side? I'm not quite following.. But I will take another look tomorrow..

 

Btw looks like world matrix is in a wrong cbuffer, optimization todo right there I guess..

Edited by static_roller

Share this post


Link to post
Share on other sites

So I did a quick test rendering the vehicle with just identity matrix and

D3DXMatrixLookAtLH(&g.viewMatrix, &D3DXVECTOR3(0, 4, -5), &D3DXVECTOR3(0, 4, 1), &D3DXVECTOR3(0, 1, 0));

after normal world render, so I guess this approach could work.. But I'm intrested to hear what you mean exactly with the combined model*view matrix.

I don't understand matrices too well, I just use them like Jack Hoxley told me to in his DirectX4VB tutorial back in the day :D

identity_render.jpg

Share this post


Link to post
Share on other sites

But I'm intrested to hear what you mean exactly with the combined model*view matrix.

 

I meant that instead of doing calculations in world space, you'd do calculations in view space.

 

So, instead of this - "output.worldPos = mul( output.pos, World ); output.pos = mul( output.worldPos, View );" - you'd pass the world*view matrix to the shader and do this - "output.viewPos = mul( output.pos, WorldView );".

 

It will mean that most (if not all) calculations done will have to be done in view space (all light positions, directions - transformed by view matrix).

Edited by snake5

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this