@@ -149,40 +149,6 @@ static void upload_vertex_buffer(const RenderDevice *renderDevice, SDL_GPUBuffer
149149 $ (renderDevice , releaseTransferBuffer , transferBuffer );
150150}
151151
152- static void rotate_matrix (float angle , float x , float y , float z , float * r ) {
153- float radians = angle * SDL_PI_F / 180.0f ;
154- float c = SDL_cosf (radians ), s = SDL_sinf (radians ), c1 = 1.0f - c ;
155- float length = SDL_sqrtf (x * x + y * y + z * z );
156- float u [3 ] = { x /length , y /length , z /length };
157- for (int i = 0 ; i < 16 ; i ++ ) r [i ] = 0.0f ;
158- r [15 ] = 1.0f ;
159- for (int i = 0 ; i < 3 ; i ++ ) {
160- r [i * 4 + (i + 1 )%3 ] = u [(i + 2 )%3 ] * s ;
161- r [i * 4 + (i + 2 )%3 ] = - u [(i + 1 )%3 ] * s ;
162- }
163- for (int i = 0 ; i < 3 ; i ++ )
164- for (int j = 0 ; j < 3 ; j ++ )
165- r [i * 4 + j ] += c1 * u [i ] * u [j ] + (i == j ? c : 0.0f );
166- }
167-
168- static void perspective_matrix (float fovy , float aspect , float znear , float zfar , float * r ) {
169- float f = 1.0f / SDL_tanf ((fovy / 180.0f ) * SDL_PI_F * 0.5f );
170- for (int i = 0 ; i < 16 ; i ++ ) r [i ] = 0.0f ;
171- r [0 ] = f / aspect ; r [5 ] = f ;
172- r [10 ] = (znear + zfar ) / (znear - zfar ); r [11 ] = -1.0f ;
173- r [14 ] = (2.0f * znear * zfar ) / (znear - zfar );
174- }
175-
176- static void multiply_matrix (const float * lhs , const float * rhs , float * r ) {
177- float tmp [16 ];
178- for (int i = 0 ; i < 4 ; i ++ )
179- for (int j = 0 ; j < 4 ; j ++ ) {
180- tmp [j * 4 + i ] = 0.0f ;
181- for (int k = 0 ; k < 4 ; k ++ )
182- tmp [j * 4 + i ] += lhs [k * 4 + i ] * rhs [j * 4 + k ];
183- }
184- for (int i = 0 ; i < 16 ; i ++ ) r [i ] = tmp [i ];
185- }
186152
187153int main (int argc , char * * argv ) {
188154 (void ) argc ;
@@ -343,17 +309,11 @@ int main(int argc, char **argv) {
343309 depthTexture = create_depth_texture (renderDevice , depthSize );
344310 }
345311
346- float matrixModelView [16 ];
347- float matrixRotate [16 ];
348- float matrixPerspective [16 ];
349- float matrixFinal [16 ];
350-
351- rotate_matrix (angleX , 1.0f , 0.0f , 0.0f , matrixModelView );
352- rotate_matrix (angleY , 0.0f , 1.0f , 0.0f , matrixRotate );
353- multiply_matrix (matrixRotate , matrixModelView , matrixModelView );
354- matrixModelView [14 ] -= 2.5f ;
355- perspective_matrix (45.0f , (float ) swapchain .size .w / (float ) swapchain .size .h , 0.01f , 100.0f , matrixPerspective );
356- multiply_matrix (matrixPerspective , matrixModelView , matrixFinal );
312+ float4x4 mv = float4x4_rotation (angleX , float3_new (1.f , 0.f , 0.f ));
313+ mv = float4x4_mul (float4x4_rotation (angleY , float3_new (0.f , 1.f , 0.f )), mv );
314+ mv = float4x4_mul (float4x4_translation (float3_new (0.f , 0.f , -2.5f )), mv );
315+ const float4x4 proj = float4x4_perspective (45.f , (float ) swapchain .size .w / (float ) swapchain .size .h , 0.01f , 100.f );
316+ const float4x4 matrixFinal = float4x4_mul (proj , mv );
357317
358318 SDL_GPUColorTargetInfo colorTarget = {
359319 .texture = swapchain .texture ,
@@ -376,7 +336,7 @@ int main(int argc, char **argv) {
376336 .buffer = vertexBuffer ,
377337 .offset = 0 ,
378338 }, 1 );
379- $ (cmd , pushVertexUniformData , 0 , matrixFinal , sizeof (matrixFinal ));
339+ $ (cmd , pushVertexUniformData , 0 , matrixFinal . f , sizeof (matrixFinal ));
380340 $ (renderPass , drawPrimitives , (Uint32 ) SDL_arraysize (vertex_data ), 1 , 0 , 0 );
381341 release (renderPass );
382342
0 commit comments