Tkap1

Untitled

Jul 16th, 2022 (edited)
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1.  
  2. #pragma pack(push, 1)
  3. struct s_model_draw_data
  4. {
  5.     float mix_weight;
  6.     s_v2 vertex_offset;
  7.     s_v4 color;
  8.     s_v3 mix_color;
  9.     s_m4 model;
  10. };
  11. #pragma pack(pop)
  12.  
  13.  
  14. struct s_mesh
  15. {
  16.     int vertices_count;
  17.     int draw_type;
  18.     s_vbo vertex_vbo;
  19.     s_vbo data_vbo;
  20.     u32 vao;
  21.  
  22.     int current_max[e_mesh_type_count][c_max_passes][e_shader_count][e_texture_count];
  23.     int wanted_max[e_mesh_type_count][c_max_passes][e_shader_count][e_texture_count];
  24.     int draw_count[e_mesh_type_count][c_max_passes][e_shader_count][e_texture_count];
  25.     void* draw_entries[e_mesh_type_count][c_max_passes][e_shader_count][e_texture_count];
  26. };
  27.  
  28.  
  29.  
  30. func void solid_render_pass(
  31.     e_pass pass,
  32.     s_m4 projection,
  33.     s_m4 light_mat,
  34.     s_v3 cam_pos,
  35.     s_v3 light_color
  36. )
  37. {
  38.     assert(pass < c_max_passes);
  39.     s_mesh* meshes = rendering->meshes;
  40.     e_mesh_type mesh_type = e_mesh_type_opaque;
  41.  
  42.     glEnable(GL_DEPTH_TEST);
  43.     for(int mesh_index = 0; mesh_index < e_mesh_count; mesh_index++)
  44.     {
  45.         s_mesh* mesh = &meshes[mesh_index];
  46.         if(g_mesh_data[mesh_index].dont_render) { continue; }
  47.  
  48.         glBindVertexArray(mesh->vao);
  49.         mesh->data_vbo.bind();
  50.  
  51.         for(int shader_index = 0; shader_index < e_shader_count; shader_index++)
  52.         {
  53.             assert(rendering->shaders[shader_index].gl_id);
  54.  
  55.             rendering->shaders[shader_index].use();
  56.  
  57.             if(g_shader_data[shader_index].use_projection)
  58.             {
  59.                 rendering->shaders[shader_index].set_matrix("projection", projection);
  60.             }
  61.  
  62.             for(int texture_index = 0; texture_index < e_texture_count; texture_index++)
  63.             {
  64.                 if(mesh->draw_count[mesh_type][pass][shader_index][texture_index] <= 0) { continue; }
  65.  
  66.                 assert(rendering->textures[texture_index].id);
  67.                 glActiveTexture(GL_TEXTURE0);
  68.                 glBindTexture(GL_TEXTURE_2D, rendering->textures[texture_index].id);
  69.  
  70.                 if(g_shader_data[shader_index].do_light)
  71.                 {
  72.                     rendering->shaders[shader_index].set_matrix("light_space_matrix", light_mat);
  73.                     glActiveTexture(GL_TEXTURE1);
  74.                     glBindTexture(GL_TEXTURE_2D, rendering->shadow_fbo.depth);
  75.  
  76.                     rendering->shaders[shader_index].set_v3("light_pos", rendering->sun_pos);
  77.                     rendering->shaders[shader_index].set_v3("view_pos", cam_pos);
  78.                     rendering->shaders[shader_index].set_v3("light_color", light_color);
  79.                 }
  80.  
  81.                 void* entries = mesh->draw_entries[mesh_type][pass][shader_index][texture_index];
  82.                 glBufferSubData(
  83.                     GL_ARRAY_BUFFER,
  84.                     0,
  85.                     mesh->data_vbo.stride * mesh->draw_count[mesh_type][pass][shader_index][texture_index],
  86.                     entries
  87.                 );
  88.                 glDrawArraysInstanced(mesh->draw_type, 0, mesh->vertices_count, mesh->draw_count[mesh_type][pass][shader_index][texture_index]);
  89.  
  90.                 mesh->draw_count[mesh_type][pass][shader_index][texture_index] = 0;
  91.  
  92.             }
  93.         }
  94.     }
  95. }
  96.  
  97.  
  98. func void draw_obj(s_draw_mesh dm)
  99. {
  100.     m_timed_function();
  101.  
  102.     assert(dm.color.r >= 0 && dm.color.r <= 1);
  103.     assert(dm.color.g >= 0 && dm.color.g <= 1);
  104.     assert(dm.color.b >= 0 && dm.color.b <= 1);
  105.     assert(dm.color.a >= 0 && dm.color.a <= 1);
  106.  
  107.     assert(in_range(dm.mesh_id, 0, e_mesh_count - 1));
  108.     assert(in_range(dm.shader_id, 0, e_shader_count - 1));
  109.     assert(in_range(dm.texture_id, 0, e_texture_count - 1));
  110.  
  111.     s_mesh* meshes = rendering->meshes;
  112.     e_pass pass = dm.pass;
  113.     int shader_id = dm.shader_id;
  114.     int texture_id = dm.texture_id;
  115.  
  116.     s_mesh* mesh = &meshes[dm.mesh_id];
  117.     assert(mesh->vertex_vbo.id > 0);
  118.     assert(mesh->data_vbo.id > 0);
  119.  
  120.     b8 transparent = dm.color.a < 1 || dm.force_transparent;
  121.     if(transparent)
  122.     {
  123.         shader_id = dm.transparent_shader_id;
  124.     }
  125.     assert(in_range(shader_id, 0, e_shader_count - 1));
  126.     assert(in_range(texture_id, 0, e_texture_count - 1));
  127.  
  128.     e_mesh_type mesh_type = transparent ? e_mesh_type_transparent : e_mesh_type_opaque;
  129.     int* count = &mesh->draw_count[mesh_type][pass][shader_id][texture_id];
  130.     int current_max = mesh->current_max[mesh_type][pass][shader_id][texture_id];
  131.     int* wanted_max = &mesh->wanted_max[mesh_type][pass][shader_id][texture_id];
  132.     u8* entries = (u8*)mesh->draw_entries[mesh_type][pass][shader_id][texture_id];
  133.     if(!can_draw_this_frame_and_update(*count, current_max, wanted_max))
  134.     {
  135.         return;
  136.     }
  137.  
  138.     s_m4 model = m4_identity();
  139.  
  140.     model = m4_multiply(model, m4_translate(dm.pos));
  141.     if(dm.rotation.z)
  142.     {
  143.         model = m4_multiply(model, m4_rotate(dm.rotation.z, v3(0, 0, 1)));
  144.     }
  145.     if(dm.rotation.x)
  146.     {
  147.         model = m4_multiply(model, m4_rotate(dm.rotation.x, v3(1, 0, 0)));
  148.     }
  149.     if(dm.rotation.y)
  150.     {
  151.         model = m4_multiply(model, m4_rotate(dm.rotation.y, v3(0, 1, 0)));
  152.     }
  153.     model = m4_multiply(model, m4_scale(dm.size));
  154.  
  155.     s_model_draw_data data = zero;
  156.     data.mix_weight = dm.mix_weight;
  157.  
  158.     data.vertex_offset.x = dm.vertex_offset.x;
  159.     data.vertex_offset.y = dm.vertex_offset.y;
  160.  
  161.     data.color.r = dm.color.r;
  162.     data.color.g = dm.color.g;
  163.     data.color.b = dm.color.b;
  164.     data.color.a = dm.color.a;
  165.  
  166.     data.mix_color.r = dm.mix_color.r;
  167.     data.mix_color.g = dm.mix_color.g;
  168.     data.mix_color.b = dm.mix_color.b;
  169.  
  170.     memcpy(data.model.elements, model.elements, sizeof(s_m4));
  171.     memcpy(entries + *count * mesh->data_vbo.stride, &data, mesh->data_vbo.stride);
  172.     *count += 1;
  173. }
Add Comment
Please, Sign In to add comment