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

Add extra camera control

This commit is contained in:
Miloslav Číž 2019-05-19 20:14:06 +02:00
parent ff56c9da7f
commit 465c338628
2 changed files with 87 additions and 10 deletions

65
s3l.h
View file

@ -238,6 +238,9 @@ typedef struct
static inline void S3L_initVec4(S3L_Vec4 *v);
static inline void S3L_vec3Add(S3L_Vec4 *result, S3L_Vec4 added);
static inline void S3L_vec3Sub(S3L_Vec4 *result, S3L_Vec4 substracted);
#define S3L_writeVec4(v)\
printf("Vec4: %d %d %d %d\n",((v).x),((v).y),((v).z),((v).w))
@ -253,6 +256,16 @@ typedef struct
static inline void S3L_initTransoform3D(S3L_Transform3D *t);
/** Converts rotation transformation to three direction vectors of given length
(any one can be NULL, in which case it won't be computed).
*/
void S3L_rotationToDirections(
S3L_Vec4 rotation,
S3L_Unit length,
S3L_Vec4 *forw,
S3L_Vec4 *right,
S3L_Vec4 *up);
typedef S3L_Unit S3L_Mat4[4][4]; /**< 4x4 matrix, used mostly for 3D
transforms. The indexing is this:
matrix[column][row]. */
@ -495,6 +508,20 @@ void S3L_initVec4(S3L_Vec4 *v)
v->x = 0; v->y = 0; v->z = 0; v->w = S3L_FRACTIONS_PER_UNIT;
}
void S3L_vec3Add(S3L_Vec4 *result, S3L_Vec4 added)
{
result->x += added.x;
result->y += added.y;
result->z += added.z;
}
void S3L_vec3Sub(S3L_Vec4 *result, S3L_Vec4 substracted)
{
result->x -= substracted.x;
result->y -= substracted.y;
result->z -= substracted.z;
}
void S3L_initMat4(S3L_Mat4 *m)
{
#define M(x,y) (*m)[x][y]
@ -738,8 +765,44 @@ void S3L_initCamera(S3L_Camera *c)
S3L_initTransoform3D(&(c->transform));
}
void S3L_initPixelInfo(S3L_PixelInfo *p)
void S3L_rotationToDirections(
S3L_Vec4 rotation,
S3L_Unit length,
S3L_Vec4 *forw,
S3L_Vec4 *right,
S3L_Vec4 *up)
{
S3L_Mat4 m;
S3L_makeRotationMatrix(-1 * rotation.x,-1 * rotation.y,-1 * rotation.z,&m);
if (forw != 0)
{
forw->x = 0;
forw->y = 0;
forw->z = length;
S3L_vec3Xmat4(forw,*m);
}
if (right != 0)
{
right->x = length;
right->y = 0;
right->z = 0;
S3L_vec3Xmat4(right,*m);
}
if (up != 0)
{
up->x = 0;
up->y = length;
up->z = 0;
S3L_vec3Xmat4(up,*m);
}
}
void S3L_initPixelInfo(S3L_PixelInfo *p) // TODO: maybe non-pointer for p
{ // could be faster?
p->x = 0;
p->y = 0;
p->barycentric0 = S3L_FRACTIONS_PER_UNIT;

View file

@ -12,7 +12,7 @@
#define S3L_RESOLUTION_X 640
#define S3L_RESOLUTION_Y 480
#define S3L_PERSPECTIVE_CORRECTION 0
#define S3L_PERSPECTIVE_CORRECTION 1
#include "s3l.h"
@ -248,25 +248,33 @@ int main()
}
}
S3L_Vec4 camF, camR, camU;
int step = 10;
S3L_rotationToDirections(
camera.transform.rotation,
step,
&camF,
&camR,
&camU);
if (keys['w'])
camera.transform.translation.z += step;
S3L_vec3Add(&camera.transform.translation,camF);
if (keys['s'])
camera.transform.translation.z -= step;
if (keys['a'])
camera.transform.translation.x -= step;
S3L_vec3Sub(&camera.transform.translation,camF);
if (keys['d'])
camera.transform.translation.x += step;
S3L_vec3Add(&camera.transform.translation,camR);
if (keys['a'])
S3L_vec3Sub(&camera.transform.translation,camR);
if (keys['x'])
camera.transform.translation.y += step;
camera.transform.translation.y += 10;
if (keys['c'])
camera.transform.translation.y -= step;
camera.transform.translation.y -= 10;
if (keys['q'])
camera.transform.rotation.y -= 1;
@ -274,6 +282,12 @@ int main()
if (keys['e'])
camera.transform.rotation.y += 1;
if (keys['r'])
camera.transform.rotation.x -= 1;
if (keys['t'])
camera.transform.rotation.x += 1;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture,NULL,NULL);
SDL_RenderPresent(renderer);