Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma pack(push, 1)
- struct s_model_draw_data
- {
- float mix_weight;
- s_v2 vertex_offset;
- s_v4 color;
- s_v3 mix_color;
- s_m4 model;
- };
- #pragma pack(pop)
- struct s_mesh
- {
- int vertices_count;
- int draw_type;
- s_vbo vertex_vbo;
- s_vbo data_vbo;
- u32 vao;
- int current_max[e_mesh_type_count][c_max_passes][e_shader_count][e_texture_count];
- int wanted_max[e_mesh_type_count][c_max_passes][e_shader_count][e_texture_count];
- int draw_count[e_mesh_type_count][c_max_passes][e_shader_count][e_texture_count];
- void* draw_entries[e_mesh_type_count][c_max_passes][e_shader_count][e_texture_count];
- };
- func void solid_render_pass(
- e_pass pass,
- s_m4 projection,
- s_m4 light_mat,
- s_v3 cam_pos,
- s_v3 light_color
- )
- {
- assert(pass < c_max_passes);
- s_mesh* meshes = rendering->meshes;
- e_mesh_type mesh_type = e_mesh_type_opaque;
- glEnable(GL_DEPTH_TEST);
- for(int mesh_index = 0; mesh_index < e_mesh_count; mesh_index++)
- {
- s_mesh* mesh = &meshes[mesh_index];
- if(g_mesh_data[mesh_index].dont_render) { continue; }
- glBindVertexArray(mesh->vao);
- mesh->data_vbo.bind();
- for(int shader_index = 0; shader_index < e_shader_count; shader_index++)
- {
- assert(rendering->shaders[shader_index].gl_id);
- rendering->shaders[shader_index].use();
- if(g_shader_data[shader_index].use_projection)
- {
- rendering->shaders[shader_index].set_matrix("projection", projection);
- }
- for(int texture_index = 0; texture_index < e_texture_count; texture_index++)
- {
- if(mesh->draw_count[mesh_type][pass][shader_index][texture_index] <= 0) { continue; }
- assert(rendering->textures[texture_index].id);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, rendering->textures[texture_index].id);
- if(g_shader_data[shader_index].do_light)
- {
- rendering->shaders[shader_index].set_matrix("light_space_matrix", light_mat);
- glActiveTexture(GL_TEXTURE1);
- glBindTexture(GL_TEXTURE_2D, rendering->shadow_fbo.depth);
- rendering->shaders[shader_index].set_v3("light_pos", rendering->sun_pos);
- rendering->shaders[shader_index].set_v3("view_pos", cam_pos);
- rendering->shaders[shader_index].set_v3("light_color", light_color);
- }
- void* entries = mesh->draw_entries[mesh_type][pass][shader_index][texture_index];
- glBufferSubData(
- GL_ARRAY_BUFFER,
- 0,
- mesh->data_vbo.stride * mesh->draw_count[mesh_type][pass][shader_index][texture_index],
- entries
- );
- glDrawArraysInstanced(mesh->draw_type, 0, mesh->vertices_count, mesh->draw_count[mesh_type][pass][shader_index][texture_index]);
- mesh->draw_count[mesh_type][pass][shader_index][texture_index] = 0;
- }
- }
- }
- }
- func void draw_obj(s_draw_mesh dm)
- {
- m_timed_function();
- assert(dm.color.r >= 0 && dm.color.r <= 1);
- assert(dm.color.g >= 0 && dm.color.g <= 1);
- assert(dm.color.b >= 0 && dm.color.b <= 1);
- assert(dm.color.a >= 0 && dm.color.a <= 1);
- assert(in_range(dm.mesh_id, 0, e_mesh_count - 1));
- assert(in_range(dm.shader_id, 0, e_shader_count - 1));
- assert(in_range(dm.texture_id, 0, e_texture_count - 1));
- s_mesh* meshes = rendering->meshes;
- e_pass pass = dm.pass;
- int shader_id = dm.shader_id;
- int texture_id = dm.texture_id;
- s_mesh* mesh = &meshes[dm.mesh_id];
- assert(mesh->vertex_vbo.id > 0);
- assert(mesh->data_vbo.id > 0);
- b8 transparent = dm.color.a < 1 || dm.force_transparent;
- if(transparent)
- {
- shader_id = dm.transparent_shader_id;
- }
- assert(in_range(shader_id, 0, e_shader_count - 1));
- assert(in_range(texture_id, 0, e_texture_count - 1));
- e_mesh_type mesh_type = transparent ? e_mesh_type_transparent : e_mesh_type_opaque;
- int* count = &mesh->draw_count[mesh_type][pass][shader_id][texture_id];
- int current_max = mesh->current_max[mesh_type][pass][shader_id][texture_id];
- int* wanted_max = &mesh->wanted_max[mesh_type][pass][shader_id][texture_id];
- u8* entries = (u8*)mesh->draw_entries[mesh_type][pass][shader_id][texture_id];
- if(!can_draw_this_frame_and_update(*count, current_max, wanted_max))
- {
- return;
- }
- s_m4 model = m4_identity();
- model = m4_multiply(model, m4_translate(dm.pos));
- if(dm.rotation.z)
- {
- model = m4_multiply(model, m4_rotate(dm.rotation.z, v3(0, 0, 1)));
- }
- if(dm.rotation.x)
- {
- model = m4_multiply(model, m4_rotate(dm.rotation.x, v3(1, 0, 0)));
- }
- if(dm.rotation.y)
- {
- model = m4_multiply(model, m4_rotate(dm.rotation.y, v3(0, 1, 0)));
- }
- model = m4_multiply(model, m4_scale(dm.size));
- s_model_draw_data data = zero;
- data.mix_weight = dm.mix_weight;
- data.vertex_offset.x = dm.vertex_offset.x;
- data.vertex_offset.y = dm.vertex_offset.y;
- data.color.r = dm.color.r;
- data.color.g = dm.color.g;
- data.color.b = dm.color.b;
- data.color.a = dm.color.a;
- data.mix_color.r = dm.mix_color.r;
- data.mix_color.g = dm.mix_color.g;
- data.mix_color.b = dm.mix_color.b;
- memcpy(data.model.elements, model.elements, sizeof(s_m4));
- memcpy(entries + *count * mesh->data_vbo.stride, &data, mesh->data_vbo.stride);
- *count += 1;
- }
Add Comment
Please, Sign In to add comment