110 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			110 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #include "vulkan_engine/asset/object/model.h"
 | |
| 
 | |
| #include "vulkan_engine/vulkan/graphics.h"
 | |
| 
 | |
| namespace veng {
 | |
| 
 | |
| Model::~Model() {
 | |
|   if (graphics_ == nullptr) return;
 | |
| 
 | |
|   graphics_->DestroyTexture(material.texture_handle);
 | |
|   graphics_->DestroyBuffer(vertex_buffer);
 | |
|   graphics_->DestroyBuffer(index_buffer);
 | |
| }
 | |
| 
 | |
| template <typename T>
 | |
| void Append(std::vector<char>& buffer, const T& value) {
 | |
|   const char* data = reinterpret_cast<const char*>(&value);
 | |
|   buffer.insert(buffer.end(), data, data + sizeof(T));
 | |
| }
 | |
| 
 | |
| void Append(std::vector<char>& buffer, const std::string& value) {
 | |
|   size_t length = value.size();
 | |
|   Append(buffer, length);
 | |
|   buffer.insert(buffer.end(), value.begin(), value.end());
 | |
| }
 | |
| 
 | |
| template <typename T>
 | |
| void Read(const std::vector<char>& buffer, size_t& offset, T& out) {
 | |
|   std::memcpy(&out, buffer.data() + offset, sizeof(T));
 | |
|   offset += sizeof(T);
 | |
| }
 | |
| 
 | |
| void Read(const std::vector<char>& buffer, size_t& offset, std::string& out) {
 | |
|   size_t length = 0;
 | |
|   Read(buffer, offset, length);
 | |
|   out.assign(buffer.data() + offset, length);
 | |
|   offset += length;
 | |
| }
 | |
| 
 | |
| std::vector<char> Model::Serialize() {
 | |
|   std::vector<char> buffer;
 | |
| 
 | |
|   Append(buffer, ID);
 | |
|   Append(buffer, name);
 | |
|   Append(buffer, asset_name);
 | |
| 
 | |
|   Append(buffer, position);
 | |
|   Append(buffer, linear_velocity);
 | |
|   Append(buffer, linear_acceleration);
 | |
| 
 | |
|   Append(buffer, rotation);
 | |
|   Append(buffer, angular_velocity);
 | |
|   Append(buffer, angular_acceleration);
 | |
| 
 | |
|   Append(buffer, scale);
 | |
|   Append(buffer, transform);
 | |
| 
 | |
|   Append(buffer, original_offset);
 | |
|   Append(buffer, radius);
 | |
|   Append(buffer, lifespan);
 | |
| 
 | |
|   Append(buffer, visible);
 | |
|   Append(buffer, colision);
 | |
| 
 | |
|   return buffer;
 | |
| }
 | |
| 
 | |
| void Model::Deserialize(std::vector<char> data) {
 | |
|   size_t offset = 0;
 | |
| 
 | |
|   Read(data, offset, ID);
 | |
|   Read(data, offset, name);
 | |
|   Read(data, offset, asset_name);
 | |
| 
 | |
|   Read(data, offset, position);
 | |
|   Read(data, offset, linear_velocity);
 | |
|   Read(data, offset, linear_acceleration);
 | |
| 
 | |
|   Read(data, offset, rotation);
 | |
|   Read(data, offset, angular_velocity);
 | |
|   Read(data, offset, angular_acceleration);
 | |
| 
 | |
|   Read(data, offset, scale);
 | |
|   Read(data, offset, transform);
 | |
| 
 | |
|   Read(data, offset, original_offset);
 | |
|   Read(data, offset, radius);
 | |
|   Read(data, offset, lifespan);
 | |
| 
 | |
|   Read(data, offset, visible);
 | |
|   Read(data, offset, colision);
 | |
| }
 | |
| 
 | |
| void veng::Model::Update(float dt) {
 | |
|   linear_velocity += linear_acceleration * dt;
 | |
|   position += linear_velocity * dt;
 | |
| 
 | |
|   angular_velocity += angular_acceleration * dt;
 | |
|   if (glm::length(angular_velocity) > 1e-6f) {
 | |
|     rotation =
 | |
|         glm::normalize(glm::rotate(rotation, glm::length(angular_velocity * dt),
 | |
|                                    glm::normalize(angular_velocity)));
 | |
|   }
 | |
| 
 | |
|   transform = glm::translate(glm::mat4(1.0f), position) *
 | |
|               glm::mat4_cast(rotation) * glm::scale(glm::mat4(1.0f), scale);
 | |
| }
 | |
| 
 | |
| }  // namespace veng
 |