1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/small3dlib.git synced 2024-11-21 20:39:57 +01:00

Fix direction calculation

This commit is contained in:
Miloslav Číž 2019-06-18 22:52:12 +02:00
parent 7b5e75bebb
commit 7358b8034d
3 changed files with 25 additions and 29 deletions

View file

@ -74,7 +74,7 @@ void sampleTexture(uint8_t *texture, int32_t u, int32_t v, uint8_t *r, uint8_t *
*b = texture[index]; *b = texture[index];
} }
int16_t previousTriangle = -1; uint32_t previousTriangle = -1;
S3L_Unit uv0[2], uv1[2], uv2[2]; S3L_Unit uv0[2], uv1[2], uv2[2];
void drawPixel(S3L_PixelInfo *p) void drawPixel(S3L_PixelInfo *p)
@ -168,6 +168,10 @@ int main()
nextPrintT = clock(); nextPrintT = clock();
S3L_Vec4 carDirection;
S3L_initVec4(&carDirection);
while (running) while (running)
{ {
clock_t frameStartT = clock(); clock_t frameStartT = clock();
@ -198,37 +202,29 @@ int main()
uint8_t *state = SDL_GetKeyboardState(NULL); uint8_t *state = SDL_GetKeyboardState(NULL);
int16_t rotationStep = S3L_max(1,300 * frameDiff); int16_t step = S3L_max(1,3000 * frameDiff);
int16_t zoomStep = S3L_max(1,3000 * frameDiff); int16_t stepRotation = S3L_max(1,300 * frameDiff);
int16_t fovStep = S3L_max(1,1000 * frameDiff);
if (!state[SDL_SCANCODE_LCTRL]) if (state[SDL_SCANCODE_LEFT])
{ {
if (state[SDL_SCANCODE_LEFT]) models[1].transform.rotation.y += stepRotation;
models[0].transform.rotation.y += rotationStep; S3L_rotationToDirections(models[1].transform.rotation,S3L_FRACTIONS_PER_UNIT,&carDirection,0,0);
else if (state[SDL_SCANCODE_RIGHT])
models[0].transform.rotation.y -= rotationStep;
if (state[SDL_SCANCODE_DOWN])
models[0].transform.rotation.x += rotationStep;
else if (state[SDL_SCANCODE_UP])
models[0].transform.rotation.x -= rotationStep;
} }
else else if (state[SDL_SCANCODE_RIGHT])
{ {
if (state[SDL_SCANCODE_LEFT]) models[1].transform.rotation.y -= stepRotation;
scene.camera.focalLength = S3L_rotationToDirections(models[1].transform.rotation,S3L_FRACTIONS_PER_UNIT,&carDirection,0,0);
S3L_min(S3L_FRACTIONS_PER_UNIT * 5,scene.camera.focalLength + fovStep); }
else if (state[SDL_SCANCODE_RIGHT])
scene.camera.focalLength =
S3L_max(S3L_FRACTIONS_PER_UNIT / 2,scene.camera.focalLength - fovStep);
if (state[SDL_SCANCODE_UP]) if (state[SDL_SCANCODE_UP])
scene.camera.transform.translation.z = {
S3L_min(S3L_FRACTIONS_PER_UNIT, scene.camera.transform.translation.z + zoomStep); models[1].transform.translation.x += (carDirection.x * step) / S3L_FRACTIONS_PER_UNIT;
else if (state[SDL_SCANCODE_DOWN]) models[1].transform.translation.z += (carDirection.z * step) / S3L_FRACTIONS_PER_UNIT;
scene.camera.transform.translation.z = }
S3L_max(-S3L_FRACTIONS_PER_UNIT * 16, scene.camera.transform.translation.z - zoomStep); else if (state[SDL_SCANCODE_DOWN])
{
models[1].transform.translation.x -= (carDirection.x * step) / S3L_FRACTIONS_PER_UNIT;
models[1].transform.translation.z -= (carDirection.z * step) / S3L_FRACTIONS_PER_UNIT;
} }
SDL_RenderClear(renderer); SDL_RenderClear(renderer);

View file

@ -144,7 +144,7 @@ void animate(double time)
} }
} }
int16_t previousTriangle = -1; uint32_t previousTriangle = -1;
S3L_Unit uv0[2], uv1[2], uv2[2]; S3L_Unit uv0[2], uv1[2], uv2[2];
uint16_t l0, l1, l2; uint16_t l0, l1, l2;
S3L_Vec4 toLight; S3L_Vec4 toLight;

View file

@ -1420,7 +1420,7 @@ void S3L_rotationToDirections(
{ {
S3L_Mat4 m; S3L_Mat4 m;
S3L_makeRotationMatrixZXY(-1 * rotation.x,-1 * rotation.y,-1 * rotation.z,&m); S3L_makeRotationMatrixZXY(rotation.x,rotation.y,rotation.z,&m);
if (forw != 0) if (forw != 0)
{ {