1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/small3dlib.git synced 2024-11-23 20:59:58 +01:00

Start adding controls to the test

This commit is contained in:
Miloslav Číž 2019-05-11 20:23:08 +02:00
parent 85cadda3b3
commit 534f4aa2b9
2 changed files with 62 additions and 2 deletions

21
s3l.h
View file

@ -1175,6 +1175,9 @@ void S3L_drawTriangle(S3L_Vec4 point0, S3L_Vec4 point1, S3L_Vec4 point2,
const S3L_DrawConfig *config, const S3L_Camera *camera, const S3L_DrawConfig *config, const S3L_Camera *camera,
S3L_Index triangleID) S3L_Index triangleID)
{ {
if (point0.z <= 0 && point1.z <= 0 && point2.z <= 0)
return; // completely behind the camera
if (config->backfaceCulling != S3L_BACKFACE_CULLING_NONE) if (config->backfaceCulling != S3L_BACKFACE_CULLING_NONE)
{ {
int32_t winding = // determines CW or CCW int32_t winding = // determines CW or CCW
@ -1312,11 +1315,29 @@ void S3L_makeWorldMatrix(S3L_Transform3D worldTransform, S3L_Mat4 *m)
void S3L_makeCameraMatrix(S3L_Transform3D cameraTransform, S3L_Mat4 *m) void S3L_makeCameraMatrix(S3L_Transform3D cameraTransform, S3L_Mat4 *m)
{ {
/*
S3L_makeTranslationMat( S3L_makeTranslationMat(
-1 * cameraTransform.translation.x, -1 * cameraTransform.translation.x,
-1 * cameraTransform.translation.y, -1 * cameraTransform.translation.y,
-1 * cameraTransform.translation.z, -1 * cameraTransform.translation.z,
m); m);
*/
S3L_makeTranslationMat(
-1 * cameraTransform.translation.x,
-1 * cameraTransform.translation.y,
-1 * cameraTransform.translation.z,
m);
S3L_Mat4 r;
S3L_makeRotationMatrix(
cameraTransform.rotation.x,
cameraTransform.rotation.y,
cameraTransform.rotation.z,
&r);
S3L_mat4Xmat4(m,&r);
} }
static inline void S3L_zDivide(S3L_Vec4 *vector) static inline void S3L_zDivide(S3L_Vec4 *vector)

View file

@ -34,6 +34,8 @@ const S3L_Unit ver[] = { S3L_CUBE_VERTICES };
const S3L_Index tri[] = { S3L_CUBE_TRIANGLES }; const S3L_Index tri[] = { S3L_CUBE_TRIANGLES };
const S3L_Unit tex_coords[] = { S3L_CUBE_TEXCOORDS }; const S3L_Unit tex_coords[] = { S3L_CUBE_TEXCOORDS };
int8_t keys[256];
const uint8_t testTexture[] = const uint8_t testTexture[] =
{ {
2,2,2,0,0,0,2,2,2,2,0,0,0,2,2,2, 2,2,2,0,0,0,2,2,2,2,0,0,0,2,2,2,
@ -134,8 +136,8 @@ void draw()
modelTransform.rotation.z = f * 0.1; modelTransform.rotation.z = f * 0.1;
modelTransform.rotation.x = f * 0.3; modelTransform.rotation.x = f * 0.3;
modelTransform.translation.x = sin(f >> 7) * 700; // modelTransform.translation.x = sin(f >> 7) * 700;
modelTransform.translation.y = sin(f >> 8) * 600; // modelTransform.translation.y = sin(f >> 8) * 600;
S3L_drawModelIndexed(ver,tri,12,modelTransform,&camera,&conf); S3L_drawModelIndexed(ver,tri,12,modelTransform,&camera,&conf);
@ -203,6 +205,9 @@ int main()
int running = 1; int running = 1;
for (int i = 0; i < 256; ++i)
keys[i] = 0;
while (running) while (running)
{ {
draw(); draw();
@ -216,11 +221,45 @@ int main()
running = 0; running = 0;
break; break;
case SDL_KEYDOWN:
keys['a' + event.key.keysym.scancode - SDL_SCANCODE_A] = 1;
break;
case SDL_KEYUP:
keys['a' + event.key.keysym.scancode - SDL_SCANCODE_A] = 0;
break;
default: default:
break; break;
} }
} }
int step = 10;
if (keys['w'])
camera.transform.translation.z += step;
if (keys['s'])
camera.transform.translation.z -= step;
if (keys['a'])
camera.transform.translation.x -= step;
if (keys['d'])
camera.transform.translation.x += step;
if (keys['x'])
camera.transform.translation.y += step;
if (keys['c'])
camera.transform.translation.y -= step;
if (keys['q'])
camera.transform.rotation.y -= 1;
if (keys['e'])
camera.transform.rotation.y += 1;
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture,NULL,NULL); SDL_RenderCopy(renderer,texture,NULL,NULL);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);