33#include " ../components/model-renderer.hpp"
44#include " ../mesh/mesh-utils.hpp"
55#include " ../texture/texture-utils.hpp"
6+ #include " components/weapon.hpp"
67
78namespace our {
89
@@ -132,9 +133,12 @@ namespace our {
132133 opaqueCommands.clear ();
133134 transparentCommands.clear ();
134135 sceneLights.clear ();
136+ weaponCommands.clear ();
135137 for (auto entity : world->getEntities ()) {
136138 // If we hadn't found a camera yet, we look for a camera in this entity
137139 if (!camera) camera = entity->getComponent <CameraComponent>();
140+
141+ bool isWeapon = entity->getComponent <gameplay::WeaponComponent>() != nullptr ;
138142 // If this entity has a mesh renderer component
139143 if (auto meshRenderer = entity->getComponent <MeshRendererComponent>(); meshRenderer) {
140144 // We construct a command from it
@@ -143,8 +147,9 @@ namespace our {
143147 command.center = glm::vec3 (command.localToWorld * glm::vec4 (0 , 0 , 0 , 1 ));
144148 command.mesh = meshRenderer->mesh ;
145149 command.material = meshRenderer->material ;
146- // if it is transparent, we add it to the transparent commands list
147- if (command.material ->transparent ) {
150+ if (isWeapon) {
151+ weaponCommands.push_back (command);
152+ } else if (command.material ->transparent ) {
148153 transparentCommands.push_back (command);
149154 } else {
150155 // Otherwise, we add it to the opaque command list
@@ -176,7 +181,10 @@ namespace our {
176181 command.center = glm::vec3 (command.localToWorld * glm::vec4 (0 , 0 , 0 , 1 ));
177182 command.mesh = submesh->mesh ;
178183 command.material = submesh->material ;
179- if (command.material ->transparent ) {
184+
185+ if (isWeapon) {
186+ weaponCommands.push_back (command);
187+ } else if (command.material ->transparent ) {
180188 transparentCommands.push_back (command);
181189 } else {
182190 opaqueCommands.push_back (command);
@@ -263,6 +271,22 @@ namespace our {
263271 command.mesh ->draw ();
264272 }
265273
274+ // render weapon at the end with depth testing cleared
275+ glClear (GL_DEPTH_BUFFER_BIT );
276+ for (const RenderCommand& command : weaponCommands) {
277+ command.material ->pipelineState .depthMask = false ;
278+ command.material ->setup ();
279+ glm::mat4 MVP = VP * command.localToWorld ;
280+ command.material ->shader ->set (" transform" , MVP );
281+ if (LitMaterial* litMaterial = dynamic_cast <LitMaterial*>(command.material ); litMaterial) {
282+ // if the material is a lit material, we need to set the light uniforms
283+ litMaterial->setLightUniforms (sceneLights);
284+ litMaterial->shader ->set (" cameraPos" , cameraPosition);
285+ litMaterial->shader ->set (" model" , command.localToWorld );
286+ }
287+ command.mesh ->draw ();
288+ }
289+
266290 // If there is a postprocess material, apply postprocessing
267291 if (postprocessMaterial) {
268292 glBindFramebuffer (GL_FRAMEBUFFER , 0 );
0 commit comments