add projection matrix to AscWindow

Wed, 08 Nov 2023 21:46:29 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 08 Nov 2023 21:46:29 +0100
changeset 12
d89e0ebc76d2
parent 11
d83af80eb09b
child 13
f04a49b2aeee

add projection matrix to AscWindow

src/ascension/datatypes.h file | annotate | diff | comparison | revisions
src/ascension/window.h file | annotate | diff | comparison | revisions
src/window.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/ascension/datatypes.h	Tue Nov 07 21:24:06 2023 +0100
     1.2 +++ b/src/ascension/datatypes.h	Wed Nov 08 21:46:29 2023 +0100
     1.3 @@ -35,6 +35,10 @@
     1.4  extern "C" {
     1.5  #endif
     1.6  
     1.7 +// --------------------------------------------------------------------------
     1.8 +//    Datatype Definitions
     1.9 +// --------------------------------------------------------------------------
    1.10 +
    1.11  typedef unsigned char asc_ubyte;
    1.12  typedef signed char asc_sbyte;
    1.13  
    1.14 @@ -62,6 +66,12 @@
    1.15  #endif
    1.16  } asc_col4f;
    1.17  
    1.18 +typedef float asc_mat4f[4][4];
    1.19 +
    1.20 +// --------------------------------------------------------------------------
    1.21 +//    General Utility Functions
    1.22 +// --------------------------------------------------------------------------
    1.23 +
    1.24  static inline int asc_clamp_i(int v, int min, int max) {
    1.25      if (v < min) return min;
    1.26      if (v > max) return max;
    1.27 @@ -90,6 +100,25 @@
    1.28      return r;
    1.29  }
    1.30  
    1.31 +// --------------------------------------------------------------------------
    1.32 +//   Matrix Functions
    1.33 +// --------------------------------------------------------------------------
    1.34 +
    1.35 +static inline void asc_mat4f_ortho(
    1.36 +        asc_mat4f mat,
    1.37 +        float left,
    1.38 +        float right,
    1.39 +        float bottom,
    1.40 +        float top
    1.41 +) {
    1.42 +    memset(mat, 0, sizeof(float) * 16);
    1.43 +    mat[0][0] = 2.f / (right - left);
    1.44 +    mat[1][1] = 2.f / (top - bottom);
    1.45 +    mat[2][2] = -1;
    1.46 +    mat[3][0] = -(right + left) / (right - left);
    1.47 +    mat[3][1] = -(top + bottom) / (top - bottom);
    1.48 +}
    1.49 +
    1.50  #ifdef __cplusplus
    1.51  } // extern "C"
    1.52  #endif
     2.1 --- a/src/ascension/window.h	Tue Nov 07 21:24:06 2023 +0100
     2.2 +++ b/src/ascension/window.h	Wed Nov 08 21:46:29 2023 +0100
     2.3 @@ -57,6 +57,7 @@
     2.4      SDL_GLContext glctx;
     2.5      Uint32 id;
     2.6      asc_vec2i dimensions;
     2.7 +    asc_mat4f projection;
     2.8  } AscWindow;
     2.9  
    2.10  
     3.1 --- a/src/window.c	Tue Nov 07 21:24:06 2023 +0100
     3.2 +++ b/src/window.c	Wed Nov 08 21:46:29 2023 +0100
     3.3 @@ -57,6 +57,7 @@
     3.4          if (asc_context.windows[i].id == id) {
     3.5              asc_context.windows[i].dimensions.width = width;
     3.6              asc_context.windows[i].dimensions.height = height;
     3.7 +            asc_mat4f_ortho(asc_context.windows[i].projection, 0, (float) width, (float) height, 0);
     3.8              return;
     3.9          }
    3.10      }
    3.11 @@ -144,6 +145,13 @@
    3.12              &window->dimensions.width,
    3.13              &window->dimensions.height
    3.14      );
    3.15 +    asc_mat4f_ortho(
    3.16 +            window->projection,
    3.17 +            0,
    3.18 +            (float) window->dimensions.width,
    3.19 +            (float) window->dimensions.height,
    3.20 +            0
    3.21 +    );
    3.22  
    3.23      SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    3.24      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version);

mercurial