Change field of view according to speed

This commit is contained in:
duarteapcoelho 2022-09-05 21:02:25 +01:00
parent 6904a54d5e
commit 91afd62a9c
5 changed files with 41 additions and 3 deletions

View File

@ -46,6 +46,8 @@ namespace Rasterizer {
void reset();
extern fp *depthBuffer;
void setFOV(int fov);
vec3d toDevice(vec3d p);
vec3i toScreen(vec3d p);
void drawLine(vec3d p0, vec3d p1);

View File

@ -25,6 +25,8 @@ fp _fp_sin(fp x);
fp fp_sin(fp x);
fp fp_cos(fp x);
fp fp_tan(fp x);
fp fp_atan(fp x);
fp fp_atan2(fp y, fp x);
float isqrt(float x);

View File

@ -1121,6 +1121,7 @@ int main(){
Model carModel = Model(carMesh);
Rasterizer::init();
Rasterizer::setFOV(70);
while(true){
Rasterizer::reset();
@ -1151,6 +1152,8 @@ int main(){
if(wheelSpeed < 0)
wheelSpeed = 0;
Rasterizer::setFOV(70 + 100.0f / carSpeed.i_length());
carSpeed.x = carSpeed.x + wheelSpeed * float(fp_cos(fp(carAngle))) * Time::delta / 150.0f;
carSpeed.z = carSpeed.z + wheelSpeed * float(fp_sin(fp(0)-fp(carAngle))) * Time::delta / 150.0f;
carSpeed.x = carSpeed.x - (carSpeed.x * 0.01f) * Time::delta;

View File

@ -33,6 +33,8 @@ namespace Rasterizer {
fp *depthBuffer;
fp fov_d = 1;
void init(){
clippingPlanes[0] = {{0, 0, 1}, fp(0)-fp(NEAR_PLANE)}; // near
clippingPlanes[1] = {{fp(I_SQRT_2), 0, fp(I_SQRT_2)}, 0}; // left
@ -47,10 +49,29 @@ namespace Rasterizer {
}
}
void setFOV(int fov){
fp half_fov_rad = fp(fov) * fp(PI) / fp(180) / fp(2);
fov_d = fp_tan(half_fov_rad) * fp(2);
half_fov_rad = half_fov_rad + fp(0.1);
fp a = fp_cos(half_fov_rad - fp(HALF_PI));
fp b = fp_sin(half_fov_rad - fp(HALF_PI));
half_fov_rad = fp_atan(fp_tan(half_fov_rad) * (fp(RENDER_HEIGHT)/fp(RENDER_WIDTH)));
fp c = fp_cos(half_fov_rad - fp(HALF_PI));
fp d = fp_sin(half_fov_rad - fp(HALF_PI));
clippingPlanes[0] = {{0, 0, 1}, fp(0)-fp(NEAR_PLANE)}; // near
clippingPlanes[1] = {{b, 0, a}, 0}; // left
clippingPlanes[2] = {{fp(0)-b, 0, a}, 0}; // right
clippingPlanes[3] = {{0, d, c}, 0}; // bottom
clippingPlanes[4] = {{0, fp(0)-d, c}, 0}; // top
}
inline vec3d toDevice(vec3d p){
return {
p.x / p.z,
p.y / p.z,
p.x / p.z / fov_d,
p.y / p.z / fov_d,
p.z
};
}

View File

@ -68,8 +68,18 @@ fp fp_cos(fp x){
return fp_sin(x+fp(HALF_PI));
}
fp fp_tan(fp x){
return fp_sin(x) / fp_cos(x);
}
fp _fp_atan(fp x){
return fp(PI)/fp(4) * x - x * (fp_abs(x) - fp(1)) * (fp(0.245) + fp(0.066) * fp_abs(x));
}
fp fp_atan(fp x){
return fp(PI)*fp(4) * x - x * (fp_abs(x) - fp(1)) * (fp(0.245) + fp(0.066) * fp_abs(x));
if(x < 1)
return _fp_atan(x);
else
return fp(HALF_PI) - _fp_atan(fp(1)/x);
}
fp fp_atan2(fp y, fp x){
if(x == 0 || y == 0)