diff options
author | talha <sarcxd@gmail.com> | 2024-12-14 11:03:37 +0500 |
---|---|---|
committer | talha <sarcxd@gmail.com> | 2024-12-14 11:03:37 +0500 |
commit | 05f82730e6d74ab604b02504cc928e764542a8c2 (patch) | |
tree | 836153cc89ed80b20a4183986907e1ab48becc72 | |
parent | 9022d6c1fa64689ab9203d967a8cd31b406155da (diff) |
Reworked batch renderer, Updated math.hmaster
-rwxr-xr-x | source/main.cpp | 220 | ||||
-rwxr-xr-x | source/math.h | 232 | ||||
-rw-r--r-- | source/shaders/cq_batched.vs.glsl | 4 |
3 files changed, 213 insertions, 243 deletions
diff --git a/source/main.cpp b/source/main.cpp index c72ad53..49f16fa 100755 --- a/source/main.cpp +++ b/source/main.cpp @@ -88,8 +88,7 @@ struct TextState { TextChar* char_map; }; -#define BATCH_SIZE 500 -#define MAT4_ELE 4*4 +#define BATCH_SIZE 2000 struct r32_array { r32* buffer; @@ -165,46 +164,6 @@ struct Rect { Vec3 position; }; -class FrameTimerV2 { -public: - FrameTimerV2(); - void update(); - void enforceFramerate(u32 framerate); -public: - r64 m_tCurr; - r64 m_tPrev; - r64 m_tDeltaMS; - r64 m_tDelta; -}; - -FrameTimerV2::FrameTimerV2() { - m_tCurr = SDL_GetTicks64(); - m_tPrev = m_tCurr; - m_tDeltaMS = m_tCurr - m_tPrev; - m_tDelta = m_tDeltaMS / 1000.0f; -} - -void FrameTimerV2::update() { - m_tPrev = m_tCurr; - m_tCurr = SDL_GetTicks64(); - m_tDeltaMS = m_tCurr - m_tPrev; - m_tDelta = m_tDeltaMS / 1000.0f; -} - -void FrameTimerV2::enforceFramerate(u32 target) { - r64 target_frametime = SDL_floor( - 1000.0f/(r64)target - ); - while(m_tDeltaMS < target_frametime) { - m_tCurr = SDL_GetTicks64(); - m_tDeltaMS = m_tCurr - m_tPrev; - m_tDelta = m_tDeltaMS / 1000.0f; - - // pass time - continue; - } -} - struct FrameTimer { r64 tCurr; r64 tPrev; @@ -391,22 +350,35 @@ void gl_setup_colored_quad_optimized( void gl_cq_flush(GLRenderer* renderer) { glUseProgram(renderer->cq_batch_sp); + glEnable(GL_DEPTH_TEST); + + glUniformMatrix4fv( + glGetUniformLocation(renderer->cq_batch_sp, "View"), + 1, GL_FALSE, (renderer->cam_view).buffer + ); + + glUniformMatrix4fv( + glGetUniformLocation(renderer->cq_batch_sp, "Projection"), + 1, GL_FALSE, (renderer->cam_proj).buffer + ); glBindBuffer(GL_ARRAY_BUFFER, renderer->cq_batch_vbo); + // fill batch data // position batch glBufferSubData( GL_ARRAY_BUFFER, 0, - renderer->cq_pos_batch.size*sizeof(r32), + renderer->cq_pos_batch.capacity*sizeof(r32), renderer->cq_pos_batch.buffer ); + // color batch glBufferSubData( GL_ARRAY_BUFFER, - renderer->cq_pos_batch.size*sizeof(r32), - renderer->cq_color_batch.size*sizeof(r32), - renderer->cq_color_batch.buffer + renderer->cq_pos_batch.capacity*sizeof(r32), + renderer->cq_color_batch.capacity*sizeof(r32), + (void*)renderer->cq_color_batch.buffer ); glBindVertexArray(renderer->cq_batch_vao); @@ -424,38 +396,36 @@ void gl_draw_colored_quad_optimized( Vec3 color ) { Vec4 vertices[6] = { - init4v(-1.0f, -1.0f, 0.0f, 1.0f),// bottom-left - init4v( 1.0f, -1.0f, 0.0f, 1.0f),// bottom-right - init4v( 1.0f, 1.0f, 0.0f, 1.0f),// top-right - init4v( 1.0f, 1.0f, 0.0f, 1.0f),// top-right - init4v(-1.0f, 1.0f, 0.0f, 1.0f),// top-left - init4v(-1.0f, -1.0f, 0.0f, 1.0f) // bottom-left + Vec4{-1.0f, -1.0f, 0.0f, 1.0f},// bottom-left + Vec4{ 1.0f, -1.0f, 0.0f, 1.0f},// bottom-right + Vec4{ 1.0f, 1.0f, 0.0f, 1.0f},// top-right + Vec4{ 1.0f, 1.0f, 0.0f, 1.0f},// top-right + Vec4{-1.0f, 1.0f, 0.0f, 1.0f},// top-left + Vec4{-1.0f, -1.0f, 0.0f, 1.0f} // bottom-left }; // setting quad size - Mat4 model = init_value4m(1.0); + Mat4 model = diag4m(1.0); Mat4 scale = scaling_matrix4m(size.x, size.y, 0.0f); model = multiply4m(scale, model); // setting quad position Mat4 translation = translation_matrix4m(position.x, position.y, position.z); model = multiply4m(translation, model); - - Mat4 mvp = calculate_mvp4m(model, renderer->cam_view, renderer->cam_proj); - - Vec4 mvp_pos; - mvp_pos = multiply4mv(mvp, vertices[0]); - vertices[0] = mvp_pos; - mvp_pos = multiply4mv(mvp, vertices[1]); - vertices[1] = mvp_pos; - mvp_pos = multiply4mv(mvp, vertices[2]); - vertices[2] = mvp_pos; - mvp_pos = multiply4mv(mvp, vertices[3]); - vertices[3] = mvp_pos; - mvp_pos = multiply4mv(mvp, vertices[4]); - vertices[4] = mvp_pos; - mvp_pos = multiply4mv(mvp, vertices[5]); - vertices[5] = mvp_pos; + Vec4 model_pos; + model_pos = multiply4mv(model, vertices[0]); + vertices[0] = model_pos; + model_pos = multiply4mv(model, vertices[1]); + vertices[1] = model_pos; + model_pos = multiply4mv(model, vertices[2]); + vertices[2] = model_pos; + model_pos = multiply4mv(model, vertices[3]); + vertices[3] = model_pos; + model_pos = multiply4mv(model, vertices[4]); + vertices[4] = model_pos; + model_pos = multiply4mv(model, vertices[5]); + vertices[5] = model_pos; + array_insert(&renderer->cq_pos_batch, vertices[0].data, 4); array_insert(&renderer->cq_pos_batch, vertices[1].data, 4); array_insert(&renderer->cq_pos_batch, vertices[2].data, 4); @@ -464,7 +434,6 @@ void gl_draw_colored_quad_optimized( array_insert(&renderer->cq_pos_batch, vertices[5].data, 4); // initialise color to be per vertex to allow batching - // @todo: really need to optimise this array_insert(&renderer->cq_color_batch, color.data, 3); array_insert(&renderer->cq_color_batch, color.data, 3); array_insert(&renderer->cq_color_batch, color.data, 3); @@ -496,7 +465,7 @@ void gl_draw_colored_quad( renderer->cq_init = 1; } // setting quad size - Mat4 model = init_value4m(1.0); + Mat4 model = diag4m(1.0); Mat4 scale = scaling_matrix4m(size.x, size.y, 0.0f); model = multiply4m(scale, model); // setting quad position @@ -509,6 +478,7 @@ void gl_draw_colored_quad( glGetUniformLocation(renderer->cq_sp, "Model"), 1, GL_FALSE, model.buffer ); + glUniformMatrix4fv( glGetUniformLocation(renderer->cq_sp, "View"), 1, GL_FALSE, (renderer->cam_view).buffer @@ -811,9 +781,10 @@ int main(int argc, char* argv[]) // 3 columns, 6 rows u32 pos_ele_count = BATCH_SIZE * 4*6; u32 color_ele_count = BATCH_SIZE * 3*6; - u32 mem_size = pos_ele_count + color_ele_count; + // 1GB <= (((1b*1024)kb*1024)mb*1024)mb + u32 mem_size = (1024*1024*1024); void* batch_memory = calloc(mem_size, sizeof(r32)); - Arena batch_arena = {0}; + Arena batch_arena; arena_init(&batch_arena, (unsigned char*)batch_memory, mem_size*sizeof(r32)); array_init(&batch_arena, &(renderer->cq_pos_batch), pos_ele_count); array_init(&batch_arena, &(renderer->cq_color_batch), color_ele_count); @@ -913,11 +884,11 @@ int main(int argc, char* argv[]) Vec2 p_motion_dir = Vec2{0.0f, 0.0f}; GameState state = {0}; - state.world_size = v2(scr_width, scr_height); - state.screen_size = v2(scr_width, scr_height); - state.render_scale = v2(render_scale); + state.world_size = Vec2{(r32)scr_width, (r32)scr_height}; + state.screen_size = Vec2{(r32)scr_width, (r32)scr_height}; + state.render_scale = vec2(render_scale); Vec3 player_position = Vec3{0.0f, 70.0f, -1.0f}; - Vec2 player_size = Vec2{40.0f, 40.0f}; + Vec2 player_size = vec2(40.0f); state.player = rect(player_position, player_size); // @thinking: level object handling @@ -948,7 +919,6 @@ int main(int argc, char* argv[]) state, Vec2{80.0f, 20.0f} ); - Controller controller = {0}; r32 key_down_time[5] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f}; b8 is_key_down_x = false; @@ -1190,7 +1160,7 @@ int main(int argc, char* argv[]) else { Vec2 dir = get_move_dir(controller); - pd_1 = dir * 1.0f; + pd_1 = dir * render_scale; if (pd_1.x < 0.0f) { p_motion_dir.x = -1.0f; } else if (pd_1.x > 0.0f) { @@ -1337,21 +1307,40 @@ int main(int argc, char* argv[]) state.wall.size, Vec3{1.0f, 0.0f, 0.0f}); - for (int i=0;i<4000;i++) { - u32 max_row_ele = 100; - Vec2 based_size = div2vf(state.render_scale*state.screen_size, max_row_ele); + // benchmark code + u32 max_cq_count = 1000; + u32 max_row_ele = 50; + u32 max_col_ele = max_cq_count/max_row_ele; + Vec2 screen_render_size = (state.render_scale * state.screen_size)/2.0f; + Vec2 based_size = Vec2{ + screen_render_size.x / max_row_ele, + screen_render_size.y / max_col_ele + }; + for (int i=0;i<max_cq_count;i++) { Vec3 pos_i = Vec3{ - (atom_size.x+based_size.x)*(r32)(i%max_row_ele), - (atom_size.y+based_size.y)*(r32)(i/max_row_ele), + (2.0f*based_size.x + atom_size.x)*(r32)(i%max_row_ele), + (2.0f*based_size.y + atom_size.y)*(r32)(i/max_row_ele), -5.0f }; - r32 color_factor = (r32)(1000-i)/1000.0f; + r32 cf_r = ((r32)(i%max_row_ele))/(r32)max_row_ele; + r32 cf_g = ((r32)i/(r32)max_col_ele)/(r32)max_col_ele; + r32 cf_b = (r32)(max_cq_count-i)/(r32)max_cq_count; +#if 0 + gl_draw_colored_quad( + renderer, + pos_i, + based_size, + Vec3{cf_r, cf_g, cf_b} + ); +#endif +#if 1 gl_draw_colored_quad_optimized( renderer, pos_i, - atom_size, - Vec3{color_factor, color_factor, color_factor} + based_size, + Vec3{cf_r, cf_g, cf_b} ); +#endif } gl_cq_flush(renderer); @@ -1385,44 +1374,43 @@ int main(int argc, char* argv[]) Vec3{0.0f, 0.0f, 0.0f}); // color } - char accel_output[50]; - sprintf(accel_output, "effective_force %f", effective_force); - gl_render_text(renderer, - accel_output, - Vec2{500.0f, 150.0f}, // position - 28.0f, // size - Vec3{0.0f, 0.0f, 0.0f}); // color - char move_state_output[50]; + //char accel_output[50]; + //sprintf(accel_output, "effective_force %f", effective_force); + //gl_render_text(renderer, + // accel_output, + // Vec2{500.0f, 150.0f}, // position + // 28.0f*render_scale, // size + // Vec3{0.0f, 0.0f, 0.0f}); // color char fmt_buffer[50]; - sprintf(fmt_buffer, "player moving? %d", is_key_down_x); - gl_render_text(renderer, - fmt_buffer, - Vec2{900.0f, 40.0f}, // position - 28.0f, // size - Vec3{0.0f, 0.0f, 0.0f}); // color + //sprintf(fmt_buffer, "player moving? %d", is_key_down_x); + //gl_render_text(renderer, + // fmt_buffer, + // Vec2{900.0f, 40.0f}, // position + // 28.0f*render_scale, // size + // Vec3{0.0f, 0.0f, 0.0f}); // color sprintf(fmt_buffer, "frametime: %f", timer.tDelta); gl_render_text(renderer, fmt_buffer, Vec2{900.0f, 90.0f}, // position - 280.0f, // size + 28.0f*render_scale, // size Vec3{0.0f, 0.0f, 0.0f}); // color - sprintf(fmt_buffer, "%f pixels", pd_1.x); - - gl_render_text(renderer, - fmt_buffer, - Vec2{500.0f, 200.0f}, // position - 28.0f, // size - Vec3{0.0f, 0.0f, 0.0f}); // color - - sprintf(fmt_buffer, "collide: x(%d),y(%d)", collidex, collidey); - gl_render_text(renderer, - fmt_buffer, - Vec2{500.0f, 1000.0f}, // position - 28.0f, // size - Vec3{0.0f, 0.0f, 0.0f}); // color + //sprintf(fmt_buffer, "%f pixels", pd_1.x); + + //gl_render_text(renderer, + // fmt_buffer, + // Vec2{500.0f, 200.0f}, // position + // 28.0f*render_scale, // size + // Vec3{0.0f, 0.0f, 0.0f}); // color + + //sprintf(fmt_buffer, "collide: x(%d),y(%d)", collidex, collidey); + //gl_render_text(renderer, + // fmt_buffer, + // Vec2{500.0f, 1000.0f}, // position + // 28.0f*render_scale, // size + // Vec3{0.0f, 0.0f, 0.0f}); // color SDL_GL_SwapWindow(window); } diff --git a/source/math.h b/source/math.h index 22966ec..3f20e43 100755 --- a/source/math.h +++ b/source/math.h @@ -9,9 +9,14 @@ #define MIN(x,y) ((x) < (y) ? (y) : (x)) // @todo: -// - convert these to column major calculations for the opengl path // - make everything simd +// @note: Regarding functions written for completeness sake +// These operations are just defined and not expressed. +// They are kept here for completeness sake BUT +// since I have not had to do anything related to these, I have not created them. + + r32 clampf(r32 x, r32 bottom, r32 top) { if (x < bottom) @@ -26,6 +31,7 @@ r32 clampf(r32 x, r32 bottom, r32 top) return x; } + // ==== Vector Math ==== union Vec2 { struct { @@ -81,6 +87,15 @@ union Vec2 { return res; } + + Vec2 operator/(r32 scaler) { + SDL_assert(scaler != 0); + Vec2 res; + res.x = this->x / scaler; + res.y = this->y / scaler; + + return res; + } }; union Vec3 { @@ -118,22 +133,23 @@ union Mat4 { }; // ==== Vec2 ==== -Vec2 v2(r32 v) { - return Vec2{v, v}; +Vec2 vec2(r32 s) { + return Vec2{s, s}; } -Vec2 v2(r32 x, r32 y) { - return Vec2{x, y}; -} +// @note: written for completeness sake +Vec2 add2vf(Vec2 v, r32 s); +Vec2 add2v(Vec2 a, Vec2 b); +Vec2 subtract2vf(Vec2 v, r32 s); +Vec2 subtract2v(Vec2 a, Vec2 b); +Vec2 multiply2v(Vec2 a, Vec2 b); -r32 dot2v(Vec2 a, Vec2 b) -{ +r32 dot2v(Vec2 a, Vec2 b) { r32 res = (a.x*b.x)+(a.y*b.y); return res; } -Vec2 mul2vf(Vec2 vec, r32 scaler) -{ +Vec2 multiply2vf(Vec2 vec, r32 scaler) { Vec2 res; res.x = vec.x * scaler; res.y = vec.y * scaler; @@ -141,8 +157,7 @@ Vec2 mul2vf(Vec2 vec, r32 scaler) return res; } -Vec2 div2vf(Vec2 vec, r32 scaler) -{ +Vec2 divide2vf(Vec2 vec, r32 scaler) { SDL_assert(scaler != 0); Vec2 res; res.x = vec.x / scaler; @@ -151,32 +166,35 @@ Vec2 div2vf(Vec2 vec, r32 scaler) return res; } -r32 magnitude2v(Vec2 v) -{ +Vec2 divide2v(Vec2 a, Vec2 b) { + SDL_assert(b.x != 0 && b.y != 0); + Vec2 res; + res.x = a.x / b.x; + res.y = a.y / b.y; + + return res; +} + +r32 magnitude2v(Vec2 v) { r32 res = sqrtf(SQUARE(v.x) + SQUARE(v.y)); return res; } -Vec2 normalize2v(Vec2 v) -{ +Vec2 normalize2v(Vec2 v) { r32 magnitude = magnitude2v(v); - Vec2 res = div2vf(v, magnitude); + Vec2 res = divide2vf(v, magnitude); return res; } // ========================================================== Vec3 ========================================================== -Vec3 init3v(r32 x, r32 y, r32 z) -{ - Vec3 res; - res.x = x; - res.y = y; - res.z = z; - - return res; -} +// @note: Written for completeness sake +Vec3 vec3(r32 s); +Vec3 subtract3vf(Vec3 v, r32 scaler); +Vec3 multiply3v(Vec3 a, Vec3 b); +Vec3 divide3v(Vec3 a, Vec3 b); -Vec3 scaler_add3v(Vec3 vec, r32 scaler) +Vec3 add3vf(Vec3 vec, r32 scaler) { Vec3 res; res.x = vec.x + scaler; @@ -186,48 +204,48 @@ Vec3 scaler_add3v(Vec3 vec, r32 scaler) return res; } -Vec3 scaler_multiply3v(Vec3 vec, r32 scaler) +Vec3 add3v(Vec3 a, Vec3 b) { - Vec3 res; - res.x = vec.x * scaler; - res.y = vec.y * scaler; - res.z = vec.z * scaler; - - return res; + Vec3 res; + res.x = a.x + b.x; + res.y = a.y + b.y; + res.z = a.z + b.z; + + return res; } -Vec3 scaler_divide3v(Vec3 vec, r32 scaler) +Vec3 subtract3v(Vec3 a, Vec3 b) { Vec3 res; - res.x = vec.x / scaler; - res.y = vec.y / scaler; - res.z = vec.z / scaler; + res.x = a.x - b.x; + res.y = a.y - b.y; + res.z = a.z - b.z; return res; } - -Vec3 add3v(Vec3 a, Vec3 b) +Vec3 multiply3vf(Vec3 vec, r32 scaler) { Vec3 res; - res.x = a.x + b.x; - res.y = a.y + b.y; - res.z = a.z + b.z; + res.x = vec.x * scaler; + res.y = vec.y * scaler; + res.z = vec.z * scaler; return res; } -Vec3 subtract3v(Vec3 a, Vec3 b) + +Vec3 divide3vf(Vec3 vec, r32 scaler) { Vec3 res; - res.x = a.x - b.x; - res.y = a.y - b.y; - res.z = a.z - b.z; + res.x = vec.x / scaler; + res.y = vec.y / scaler; + res.z = vec.z / scaler; return res; } -r32 dot_multiply3v(Vec3 a, Vec3 b) +r32 dot3v(Vec3 a, Vec3 b) { r32 x = a.x * b.x; r32 y = a.y * b.y; @@ -247,11 +265,11 @@ r32 magnitude3v(Vec3 vec) Vec3 normalize3v(Vec3 vec) { r32 magnitude = magnitude3v(vec); - Vec3 res = scaler_divide3v(vec, magnitude); + Vec3 res = divide3vf(vec, magnitude); return res; } -Vec3 cross_multiply3v(Vec3 a, Vec3 b) +Vec3 cross3v(Vec3 a, Vec3 b) { Vec3 res; res.x = (a.y * b.z) - (a.z * b.y); @@ -263,20 +281,39 @@ Vec3 cross_multiply3v(Vec3 a, Vec3 b) // ============================================== Vec4, Mat4 ============================================== -Vec4 init4v(r32 x, r32 y, r32 z, r32 w) +// ==================== Vec4 ==================== +Vec4 vec4(r32 s) { Vec4 res; - res.x = x; - res.y = y; - res.z = z; - res.w = w; + res.x = s; + res.y = s; + res.z = s; + res.w = s; return res; } -Mat4 init_value4m(r32 value) -{ - Mat4 res = {0}; +// @note: Written for completeness sake. +Vec4 add4vf(Vec4 vec, r32 scaler); +Vec4 add4v(Vec4 a, Vec4 b); +Vec4 subtract4vf(Vec4 vec, r32 scaler); +Vec4 subtract4v(Vec4 a, Vec4 b); +Vec4 multiply4vf(Vec4 vec, r32 scaler); +Vec4 multiply4v(Vec4 a, Vec4 b); +Vec4 divide4vf(Vec4 vec, r32 scaler); +Vec4 divide4v(Vec4 a, Vec4 b); +Vec4 dot4v(Vec4 a, Vec4 b); + +// =================== MAT4 =================== +Mat4 mat4(r32 s) { + Mat4 res; + memset(&res, s, sizeof(res)); + + return res; +} + +Mat4 diag4m(r32 value) { + Mat4 res = mat4(0.0f); res.data[0][0] = value; res.data[1][1] = value; res.data[2][2] = value; @@ -285,16 +322,6 @@ Mat4 init_value4m(r32 value) return res; } -// @note: These operations are just defined and not expressed. They are kept here for completeness sake BUT -// since I have not had to do anything related to these, I have not created them. -Vec4 scaler_add4v(Vec4 vec, r32 scaler); -Vec4 scaler_subtract4v(Vec4 vec, r32 scaler); -Vec4 scaler_multiply4v(Vec4 vec, r32 scaler); -Vec4 scaler_divide4v(Vec4 vec, r32 scaler); -Vec4 add4v(Vec4 a, Vec4 b); -Vec4 subtract4v(Vec4 a, Vec4 b); -Vec4 dot_multiply4v(Vec4 a, Vec4 b); - Mat4 add4m(Mat4 a, Mat4 b) { Mat4 res; @@ -351,7 +378,7 @@ Mat4 subtract4m(Mat4 a, Mat4 b) Vec4 multiply4mv(Mat4 m, Vec4 v) { - Vec4 res = {0}; + Vec4 res = vec4(0); res.x += v.x*m.data[0][0]; res.y += v.x*m.data[0][1]; @@ -392,7 +419,7 @@ Mat4 multiply4m(Mat4 a, Mat4 b) Mat4 scaling_matrix4m(r32 x, r32 y, r32 z) { // generates a 4x4 scaling matrix for scaling each of the x,y,z axis - Mat4 res = init_value4m(1.0f); + Mat4 res = diag4m(1.0f); res.data[0][0] = x; res.data[1][1] = y; res.data[2][2] = z; @@ -402,7 +429,7 @@ Mat4 scaling_matrix4m(r32 x, r32 y, r32 z) Mat4 translation_matrix4m(r32 x, r32 y, r32 z) { - Mat4 res = init_value4m(1.0f); + Mat4 res = diag4m(1.0f); res.row[3] = Vec4{x, y, z, 1.0f}; return res; @@ -410,7 +437,7 @@ Mat4 translation_matrix4m(r32 x, r32 y, r32 z) Mat4 rotation_matrix4m(r32 angle_radians, Vec3 axis) { - Mat4 res = init_value4m(1.0f); + Mat4 res = diag4m(1.0f); axis = normalize3v(axis); r32 cos_theta = cosf(angle_radians); @@ -434,7 +461,7 @@ Mat4 rotation_matrix4m(r32 angle_radians, Vec3 axis) Mat4 orthographic4m(r32 left, r32 right, r32 bottom, r32 top, r32 near, r32 far) { - Mat4 res = init_value4m(0); + Mat4 res = diag4m(0); res.data[0][0] = 2.0f/(right - left); res.data[1][1] = 2.0f/(top - bottom); @@ -457,14 +484,14 @@ Mat4 lookat4m(Vec3 up, Vec3 forward, Vec3 right, Vec3 position) * is a local. It won't be very clear from this illustration alone, so you would be best served watching the video and recollecting and understanding from there. * 2. This article (https://twodee.org/blog/17560) derives (or rather shows), in a very shallow way how we get to the look at matrix. */ - Mat4 res = init_value4m(1.0f); + Mat4 res = diag4m(1.0f); res.row[0] = Vec4{ right.x, up.x, forward.x, 0.0f }; res.row[1] = Vec4{ right.y, up.y, forward.y, 0.0f }; res.row[2] = Vec4{ right.z, up.z, forward.z, 0.0f }; - res.data[3][0] = -dot_multiply3v(right, position); - res.data[3][1] = -dot_multiply3v(up, position); - res.data[3][2] = -dot_multiply3v(forward, position); + res.data[3][0] = -dot3v(right, position); + res.data[3][1] = -dot3v(up, position); + res.data[3][2] = -dot3v(forward, position); res.data[3][3] = 1.0f; return res; @@ -488,59 +515,12 @@ Mat4 camera_create4m(Vec3 camera_pos, Vec3 camera_look, Vec3 camera_up) // If we did not do this, then the inward axis the camera looks at would be negative. // I am still learning from learnopengl.com but I imagine that this was done for conveniences' sake. Vec3 camera_forward_dir = normalize3v(subtract3v(camera_pos, camera_look)); - Vec3 camera_right_dir = normalize3v(cross_multiply3v(camera_up, camera_forward_dir)); - Vec3 camera_up_dir = normalize3v(cross_multiply3v(camera_forward_dir, camera_right_dir)); + Vec3 camera_right_dir = normalize3v(cross3v(camera_up, camera_forward_dir)); + Vec3 camera_up_dir = normalize3v(cross3v(camera_forward_dir, camera_right_dir)); Mat4 res = lookat4m(camera_up_dir, camera_forward_dir, camera_right_dir, camera_pos); return res; } -Mat4 calculate_mvp4m(Mat4 Model, Mat4 View, Mat4 Projection) -{ - Mat4 mv = multiply4m(View, Model); - Mat4 mvp = multiply4m(Projection, mv); - - return mvp; -} - -#if Disabled -Mat4RowMajor perspective_projection4m(r32 left, r32 right, r32 bottom, r32 top, r32 near, r32 far) -{ - Mat4RowMajor res = { 0 }; - - res.data[0][0] = (2.0 * near)/(right - left); - res.data[0][2] = (right + left)/(right - left); - - res.data[1][1] = (2.0 * near)/(top - bottom); - res.data[1][2] = (top + bottom)/(top - bottom); - - res.data[2][2] = -(far + near)/(far - near); - res.data[2][3] = -2.0*far*near/(far - near); - - res.data[3][2] = -1.0; - - return res; -} - -Mat4RowMajor perspective4m(r32 fov, r32 aspect_ratio, r32 near, r32 far) -{ - r32 cotangent = 1.0f / tanf(fov / 2.0f); - - Mat4RowMajor res = { 0 }; - - res.data[0][0] = cotangent / aspect_ratio; - - res.data[1][1] = cotangent; - - res.data[2][2] = -(far + near) / (far - near); - res.data[2][3] = -2.0f * far * near / (far - near); - - res.data[3][2] = -1.0f; - - return res; -} -#endif - - #endif diff --git a/source/shaders/cq_batched.vs.glsl b/source/shaders/cq_batched.vs.glsl index 84180e9..947b45a 100644 --- a/source/shaders/cq_batched.vs.glsl +++ b/source/shaders/cq_batched.vs.glsl @@ -3,8 +3,10 @@ layout(location=0) in vec4 aPos; layout(location=1) in vec3 aColor; out vec4 vertexColor; +uniform mat4 View; +uniform mat4 Projection; void main() { - gl_Position = aPos; + gl_Position = Projection * View * aPos; vertexColor = vec4(aColor, 1.0); } |