diff --git a/data/models/weapons/v_ump_handfinal256.bmp.mtl b/data/models/weapons/v_ump_handfinal256.bmp.mtl new file mode 100644 index 0000000..0c746fd --- /dev/null +++ b/data/models/weapons/v_ump_handfinal256.bmp.mtl @@ -0,0 +1,13 @@ +# Blender MTL File: 'None' +# Material Count: 1 + +newmtl graveyard_terrain +Ns 225.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 +map_Kd data/textures/weapons/handfinal256.bmp diff --git a/data/models/weapons/v_ump_ump.bmp.mtl b/data/models/weapons/v_ump_ump.bmp.mtl new file mode 100644 index 0000000..96f235f --- /dev/null +++ b/data/models/weapons/v_ump_ump.bmp.mtl @@ -0,0 +1,13 @@ +# Blender MTL File: 'None' +# Material Count: 1 + +newmtl graveyard_terrain +Ns 225.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 +map_Kd data/textures/weapons/ump.bmp diff --git a/data/models/weapons/v_ump_ump2.bmp.mtl b/data/models/weapons/v_ump_ump2.bmp.mtl new file mode 100644 index 0000000..29929d3 --- /dev/null +++ b/data/models/weapons/v_ump_ump2.bmp.mtl @@ -0,0 +1,13 @@ +# Blender MTL File: 'None' +# Material Count: 1 + +newmtl graveyard_terrain +Ns 225.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 +map_Kd data/textures/weapons/ump2.bmp diff --git a/data/scripts/actors/actor_player.lua b/data/scripts/actors/actor_player.lua index c6dd04b..28d2d64 100644 --- a/data/scripts/actors/actor_player.lua +++ b/data/scripts/actors/actor_player.lua @@ -1,6 +1,8 @@ -- игрок actor_player = inherit_table(actor_base) +actor_player.m_camera_offset_y = 0.5 + function actor_player:on_init() actor_base.on_init(self) @@ -14,7 +16,7 @@ function actor_player:on_init() self.m_weapon_entity_id = ent:get_id() self.m_in_reload = false - + --local ent2 = engine.get_entity_from_id(self.m_weapon_entity_id) --console.print(ent2:get_classname()) end @@ -35,14 +37,15 @@ function actor_player:on_update(dt) if self:get_action() == ACTION_FIRE and self.m_in_reload == false then ent:play_animation(ent:find_animation("shoot1"), ANIM_PLAYBACK_NONE) + engine.play_sound("data/sounds/weapons/ump45_shoot.wav") end if self:get_action() == ACTION_RELOAD and self.m_in_reload == false then ent:play_animation(ent:find_animation("reload"), ANIM_PLAYBACK_NONE) - self.m_in_reload = true + engine.play_sound("data/sounds/weapons/ump45_reload.wav") + self.m_in_reload = true end - if ent:get_current_animation() == ent:find_animation("shoot1") and ent:get_current_animation_time() >= ent:get_animation_time(ent:find_animation("shoot1")) then ent:play_animation(ent:find_animation("idle1"), ANIM_PLAYBACK_REPEAT) @@ -61,7 +64,3 @@ end function actor_player:on_collide(other) console.print(string.format("actor_player:on_collide: %s", other:get_classname())) end - -function play_sound( filename, is_3d, posx, posy, posz ) - -- body -end diff --git a/data/scripts/help.lua b/data/scripts/help.lua new file mode 100644 index 0000000..ab26177 --- /dev/null +++ b/data/scripts/help.lua @@ -0,0 +1,65 @@ +Globals: +ACTION_FIRE = 0 +ACTION_ALT_FIRE = 1 +ACTION_RELOAD = 2 + +ANIM_PLAYBACK_NONE = 0 +ANIM_PLAYBACK_REPEAT = 1 + +engine.error(string message) +engine.warning(string message) +engine.create_entity(string classname) -- return an table to the new entity +engine.add_entity_to_world(entity) -- add entity to the world +engine.get_entity_from_id(integer id) -- return an entity table from +engine.play_sound(string filename) -- play 2d sound + +camera.get_position() -- return x, y, z +camera.get_front() -- return x, y, z +camera.get_right() -- return x, y, z +camera.get_up() -- return float +camera.get_pitch() -- return float + +console.print(string message) + + +Entity properties: +string m_name +integer m_id + + +Entity methods: +load_model(string filename) + +update_transform() + +translate(float x, float y, float z) -- addition to the current position + +set_position(float x, float y, float z) -- setting the position + +get_position() -- return x, y, z + +set_rotation(float x, float y, float z) -- setting the euler rotation + +set_rotation_from_vectors(float frontx, float fronty, float frontz, + float rightx, float righty, float rightz, + float upx, float upy, float upz) -- rotate around axis + +get_classname() -- return the classname of the entity +get_id() -- return the id of the entity + +find_animation(string name) -- find a animation in the model +play_animation(integer id, integer mode) -- play a animation with specified mode (ANIM_PLAYBACK_NONE, ANIM_PLAYBACK_REPEAT) +get_current_animation() -- return the id to the current playing animation +get_current_animation_time() -- return the current time of the current playing animation +get_animation_time(integer id) -- return the time of a animation + + + +ActorBase methods: +activate_camera() + +update_camera_look() +update_camera_movement(float nubmer) +create_body() +update_body_movement(float nubmer) +get_action() -- return an current mode (ACTION_FIRE, ACTION_ALT_FIRE, ACTION_RELOAD) \ No newline at end of file diff --git a/data/scripts/test_object.lua b/data/scripts/test_object.lua index 4282f6b..87ce5b6 100644 --- a/data/scripts/test_object.lua +++ b/data/scripts/test_object.lua @@ -27,10 +27,9 @@ function test_object:set_relative_position_to_camera( ent ) local rightX, rightY, rightZ = camera.get_right() local upX, upY, upZ = camera.get_up() - local yaw = camera.get_yaw() - local pitch = camera.get_pitch() - - self:set_rotation(pitch, -yaw, 0.0) + self:set_rotation_from_vectors(frontX, frontY, frontZ, + rightX, rightY, rightZ, + upX, upY, upZ) local offsetx = 0.0 local offsety = 0.0 diff --git a/data/sounds/weapons/ump45-1.wav b/data/sounds/weapons/ump45-1.wav new file mode 100644 index 0000000..7a80784 Binary files /dev/null and b/data/sounds/weapons/ump45-1.wav differ diff --git a/data/sounds/weapons/ump45_boltslap.wav b/data/sounds/weapons/ump45_boltslap.wav new file mode 100644 index 0000000..c638d4b Binary files /dev/null and b/data/sounds/weapons/ump45_boltslap.wav differ diff --git a/data/sounds/weapons/ump45_clipin.wav b/data/sounds/weapons/ump45_clipin.wav new file mode 100644 index 0000000..dacecff Binary files /dev/null and b/data/sounds/weapons/ump45_clipin.wav differ diff --git a/data/sounds/weapons/ump45_clipout.wav b/data/sounds/weapons/ump45_clipout.wav new file mode 100644 index 0000000..9d20a50 Binary files /dev/null and b/data/sounds/weapons/ump45_clipout.wav differ diff --git a/data/sounds/weapons/ump45_reload.wav b/data/sounds/weapons/ump45_reload.wav new file mode 100644 index 0000000..52305f8 Binary files /dev/null and b/data/sounds/weapons/ump45_reload.wav differ diff --git a/data/sounds/weapons/ump45_shoot.wav b/data/sounds/weapons/ump45_shoot.wav new file mode 100644 index 0000000..8634363 Binary files /dev/null and b/data/sounds/weapons/ump45_shoot.wav differ diff --git a/data/textures/weapons/handfinal256.bmp b/data/textures/weapons/handfinal256.bmp new file mode 100644 index 0000000..86f4aa1 Binary files /dev/null and b/data/textures/weapons/handfinal256.bmp differ diff --git a/data/textures/weapons/ump.bmp b/data/textures/weapons/ump.bmp new file mode 100644 index 0000000..1f797ad Binary files /dev/null and b/data/textures/weapons/ump.bmp differ diff --git a/data/textures/weapons/ump2.bmp b/data/textures/weapons/ump2.bmp new file mode 100644 index 0000000..cce2dc6 Binary files /dev/null and b/data/textures/weapons/ump2.bmp differ diff --git a/data/textures/weapons/v_m3.bmp b/data/textures/weapons/v_m3.bmp new file mode 100644 index 0000000..9e9c55d Binary files /dev/null and b/data/textures/weapons/v_m3.bmp differ diff --git a/data/textures/weapons/v_m32.BMP b/data/textures/weapons/v_m32.BMP new file mode 100644 index 0000000..a1531e8 Binary files /dev/null and b/data/textures/weapons/v_m32.BMP differ diff --git a/src/engine/camera.cpp b/src/engine/camera.cpp index 07aeb2d..10de1df 100644 --- a/src/engine/camera.cpp +++ b/src/engine/camera.cpp @@ -28,7 +28,8 @@ void Camera::SetYawPitch(float yaw, float pitch) m_front = glm::normalize(m_direction); - m_right = glm::normalize(glm::cross(m_front, m_up)); + m_right = glm::normalize(glm::cross(m_front, glm::vec3(0.0f, 1.0f, 0.0f))); + m_up = glm::normalize(glm::cross(m_right, m_front)); } glm::quat Camera::GetOrientation() diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index a2d38b6..95a9478 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -9,6 +9,7 @@ #include "camera.h" #include "inputmanager.h" #include "physics/physicsworld.h" +#include "soundsystem.h" // renderer #include "render.h" @@ -117,6 +118,10 @@ void Engine::Init() g_entityManager->Init(); + // initialize sound system + + g_soundSystem.Init(); + // Initialize game g_game->Init(); @@ -135,6 +140,8 @@ void Engine::Shutdown() g_game->Shutdown(); + g_soundSystem.Shutdown(); + g_entityManager->Shutdown(); if (g_render) { diff --git a/src/engine/ientity.cpp b/src/engine/ientity.cpp index f71cb18..9960aee 100644 --- a/src/engine/ientity.cpp +++ b/src/engine/ientity.cpp @@ -43,11 +43,13 @@ const glm::mat4& IEntityBase::GetWorldTransform() void IEntityBase::UpdateTransform() { - glm::vec3 radiansRotation = glm::vec3(glm::radians(m_rotation.x), glm::radians(m_rotation.y), glm::radians(m_rotation.z)); - glm::mat4 rotation = glm::toMat4(glm::quat(radiansRotation)); + glm::vec3 radiansRotation = glm::radians(m_rotation); - m_worldTM = glm::mat4(1.0f); - m_worldTM = glm::translate(m_worldTM, m_position) * rotation * glm::scale(m_worldTM, m_scale); + glm::mat4 T = glm::translate(glm::mat4(1.0f), m_position); + glm::mat4 R = glm::toMat4(glm::quat(radiansRotation)); + glm::mat4 S = glm::scale(glm::mat4(1.0f), m_scale); + + m_worldTM = T * R * S; } uint32_t IEntityBase::GetID() diff --git a/src/game/game.cpp b/src/game/game.cpp index 513952f..1f2fbe3 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -7,12 +7,58 @@ #include "entitymanager.h" #include "world.h" #include "game_object.h" +#include "soundsystem.h" #include +#include +#include + static Game s_game; Game* g_game = &s_game; +class GameSoundSystem +{ +public: + static GameSoundSystem& GetInstance(); + +public: + void PlaySound2D(const std::string& filename); + void CacheSound(const std::string& filename); + +private: + std::map m_sounds; +}; + +GameSoundSystem& GameSoundSystem::GetInstance() +{ + static GameSoundSystem inst; + return inst; +} + +void GameSoundSystem::PlaySound2D(const std::string& filename) +{ + auto it = m_sounds.find(filename); + if (it == m_sounds.end()) + { + CacheSound(filename); + it = m_sounds.find(filename); + } + + SDL_assert(it != m_sounds.end()); + + // .hack due unimplemented sound sources^ + if (g_soundSystem.IsPlaying(it->second)) + g_soundSystem.Stop(it->second); + + g_soundSystem.Play(it->second, false); +} + +void GameSoundSystem::CacheSound(const std::string& filename) +{ + m_sounds.emplace(filename, g_soundSystem.LoadSound(filename.c_str())); +} + void consoleMsg(const char* msg) { Logger::Msg("%s", msg); @@ -28,6 +74,11 @@ void engineWarning(const char* msg) Core::Warning("%s", msg); } +void enginePlaySound(const char* filename) +{ + GameSoundSystem::GetInstance().PlaySound2D(filename); +} + LuaPlus::LuaObject engineCreateEntity(const char* classname) { Entity* entity = static_cast(g_game->Lua_CreateEntity(classname)); @@ -151,6 +202,7 @@ void registerEngine() engineTable.RegisterDirect("create_entity", &engineCreateEntity); engineTable.RegisterDirect("add_entity_to_world", &engineAddEntityToWorld); engineTable.RegisterDirect("get_entity_from_id", &engineGetEntityFromID); + engineTable.RegisterDirect("play_sound", &enginePlaySound); LuaObject consoleTable = GetLuaState().GetGlobals().CreateTable("console"); consoleTable.RegisterDirect("print", &consoleMsg); diff --git a/src/game/game_object.cpp b/src/game/game_object.cpp index fe59268..7363cb6 100644 --- a/src/game/game_object.cpp +++ b/src/game/game_object.cpp @@ -236,6 +236,7 @@ void Entity::RegisterBaseFunctions() m_luaObject.Register("set_position", *this, &Entity::Lua_SetPosition); m_luaObject.Register("get_position", *this, &Entity::Lua_GetPosition); m_luaObject.Register("set_rotation", *this, &Entity::Lua_SetRotation); + m_luaObject.Register("set_rotation_from_vectors", *this, &Entity::Lua_SetRotationFromVectors); m_luaObject.Register("get_classname", *this, &Entity::Lua_GetClassname); m_luaObject.Register("get_id", *this, &Entity::Lua_GetID); @@ -282,6 +283,22 @@ void Entity::Help_SetRotation(float x, float y, float z) m_rotation.z = z; } +void Entity::Help_SetRotationFromVectors(const glm::vec3& front, const glm::vec3& right, const glm::vec3& up) +{ + glm::mat3 rot; + + rot[0] = right; + rot[1] = up; + rot[2] = -front; + + glm::quat q = glm::quat_cast(rot); + + glm::quat fix = glm::angleAxis(glm::radians(90.0f), glm::vec3(0, 1, 0)); + q = q * fix; + + m_rotation = glm::degrees(glm::eulerAngles(q)); +} + int Entity::Lua_LoadModel(LuaPlus::LuaState* state) { LuaPlus::LuaStack stack(state); @@ -345,6 +362,30 @@ int Entity::Lua_SetRotation(LuaPlus::LuaState* state) return 0; } +int Entity::Lua_SetRotationFromVectors(LuaPlus::LuaState* state) +{ + LuaPlus::LuaStack stack(state); + + glm::vec3 front( + stack[2].GetNumber(), + stack[3].GetNumber(), + stack[4].GetNumber()); + + glm::vec3 right( + stack[5].GetNumber(), + stack[6].GetNumber(), + stack[7].GetNumber()); + + glm::vec3 up( + stack[8].GetNumber(), + stack[9].GetNumber(), + stack[10].GetNumber()); + + Help_SetRotationFromVectors(front, right, up); + + return 0; +} + int Entity::Lua_GetClassname(LuaPlus::LuaState* state) { state->PushString(GetClassname()); @@ -525,7 +566,17 @@ void ActorBase::AfterEngineStep() //m_position = btVectorToGlm(xform.getOrigin()); m_position = btVectorToGlm(m_ph_motion_state.m_transform.getOrigin()); - m_camera.SetPosition(m_position); + + glm::vec3 cameraPos = m_position; + + if (m_luaObject.IsTable()) + { + LuaPlus::LuaObject m_camera_offset_y = m_luaObject.GetByName("m_camera_offset_y"); + if (m_camera_offset_y.IsNumber()) + cameraPos.y += m_camera_offset_y.ToNumber(); + } + + m_camera.SetPosition(cameraPos); } void ActorBase::UpdateCameraMovement(float dt) diff --git a/src/game/game_object.h b/src/game/game_object.h index cf9f4a5..254bb09 100644 --- a/src/game/game_object.h +++ b/src/game/game_object.h @@ -96,12 +96,14 @@ public: void Help_Translate(float x, float y, float z); void Help_SetPosition(float x, float y, float z); void Help_SetRotation(float x, float y, float z); + void Help_SetRotationFromVectors(const glm::vec3& front, const glm::vec3& right, const glm::vec3& up); int Lua_LoadModel(LuaPlus::LuaState* state); int Lua_Translate(LuaPlus::LuaState* state); int Lua_SetPosition(LuaPlus::LuaState* state); int Lua_GetPosition(LuaPlus::LuaState* state); int Lua_SetRotation(LuaPlus::LuaState* state); + int Lua_SetRotationFromVectors(LuaPlus::LuaState* state); int Lua_GetClassname(LuaPlus::LuaState* state); int Lua_GetID(LuaPlus::LuaState* state); int Lua_UpdateTransform(LuaPlus::LuaState* state); diff --git a/src/render/model.cpp b/src/render/model.cpp index a5801d5..f194a21 100644 --- a/src/render/model.cpp +++ b/src/render/model.cpp @@ -30,7 +30,6 @@ static std::string getFileNameWithoutExtension(const std::string& filename) Model::Model() { - m_AlbedoTexture = nullptr; m_numPoses = 0; //m_boundingBox.min = glm::vec3(0.0f); @@ -39,7 +38,6 @@ Model::Model() Model::~Model() { - m_AlbedoTexture = nullptr; for (int i = 0; i < m_meshes.size(); i++) ReleaseModelData(m_meshes[i]); @@ -268,6 +266,8 @@ void Model::LoadIqm(const char* filename) indices[j * 3 + 2] = a - pMesh->first_vertex; } + const char* pMaterialname = nullptr; + if (pHdr->ofs_text) { // #TODO: weird weird getting material name string is so wrong @@ -275,8 +275,9 @@ void Model::LoadIqm(const char* filename) const char* str = (char*)pHdr + pHdr->ofs_text; const char* materialname = &str[pMesh->material]; - //if (materialname) - // pMaterial = g_materialSystem.LoadMaterial(materialname); + if (materialname) + pMaterialname = materialname; + //pMaterial = g_materialSystem.LoadMaterial(materialname); } ModelData_t mesh = {}; @@ -286,6 +287,14 @@ void Model::LoadIqm(const char* filename) mesh.vbcount = pMesh->num_vertexes; mesh.ib = g_renderDevice->CreateIndexBuffer(indices.data(), pMesh->num_triangles * sizeof(iqmtriangle), false); mesh.ibcount = pMesh->num_triangles * 3; + + + std::string mtlfilename = getFileNameWithoutExtension(filename); + mtlfilename += "_"; + mtlfilename += pMaterialname; + mtlfilename += ".mtl"; + mesh.m_AlbedoTexture = LoadMtl(mtlfilename.c_str()); + //mesh.m_material = pMaterial; // //if (!mesh.m_material) @@ -549,17 +558,19 @@ void Model::LoadObj(const char* filename) std::string mtlfilename = getFileNameWithoutExtension(filename); mtlfilename += ".mtl"; - LoadMtl(mtlfilename.c_str()); + m_data.m_AlbedoTexture = LoadMtl(mtlfilename.c_str()); } -void Model::LoadMtl(const char* filename) +Texture2D* Model::LoadMtl(const char* filename) { Msg("Loading MTL file %s...", filename); + Texture2D* pAlbedo = nullptr; + FILE* file = fopen(filename, "r"); if (file == NULL) { - Msg("Model::LoadObj: Impossible to open the file !"); - return; + Msg("Model::LoadMtl: Impossible to open the file !"); + return nullptr; } while (1) { @@ -575,7 +586,7 @@ void Model::LoadMtl(const char* filename) fgets(stupidBuffer, 1000, file); const char* textureFilename = stupidBuffer + 1; - m_AlbedoTexture = g_texturesManager->LoadTexture2D(textureFilename, true); + pAlbedo = g_texturesManager->LoadTexture2D(textureFilename, true); } //if (strcmp(lineHeader, "v") == 0) { @@ -592,6 +603,8 @@ void Model::LoadMtl(const char* filename) } fclose(file); + + return pAlbedo; } void Model::Draw(const glm::mat4& model, SkeletonInstance* instance /*= nullptr*/) @@ -644,10 +657,10 @@ void Model::Draw(const glm::mat4& model, SkeletonInstance* instance /*= nullptr* g_shaderSystem->SetUniformMatrix(shader, UNIFORM_MVP_MATRIX, &mvp[0]); - if (!m_AlbedoTexture) - m_AlbedoTexture = g_texturesManager->LoadTexture2D("asdfasdf"); + if (!m_data.m_AlbedoTexture) + m_data.m_AlbedoTexture = g_texturesManager->LoadTexture2D("MustBeEvilHackButDontCare"); - g_texturesManager->SetTexture(0, m_AlbedoTexture); + g_texturesManager->SetTexture(0, m_data.m_AlbedoTexture); g_shaderSystem->SetUniformSampler(shader, SAMPLER_ALBEDO, 0); if (instance) diff --git a/src/render/model.h b/src/render/model.h index 32bfaee..06ded23 100644 --- a/src/render/model.h +++ b/src/render/model.h @@ -15,6 +15,7 @@ class Texture2D; struct ModelData_t { + Texture2D* m_AlbedoTexture; GPUBuffer* vb; GPUBuffer* ib; uint32_t vbcount; @@ -34,7 +35,7 @@ public: void LoadIqm(const char* filename); void LoadObj(const char* filename); - void LoadMtl(const char* filename); + Texture2D* LoadMtl(const char* filename); void Draw(const glm::mat4& model, SkeletonInstance* instance = nullptr); @@ -69,7 +70,6 @@ private: std::vector m_meshes; BoundingBox m_boundingBox; - Texture2D* m_AlbedoTexture; int m_numPoses; };