update shader

This commit is contained in:
ouczbs 2024-03-24 21:40:57 +08:00
parent 8f0b9b634b
commit 13aaa8cd63
6 changed files with 33 additions and 5 deletions

View File

@ -37,4 +37,22 @@ namespace engineapi {
return text;
}
vector<char> AssetManager::LoadBinaryFile(const string& path)
{
// ate:在文件末尾开始读取,从文件末尾开始读取的优点是我们可以使用读取位置来确定文件的大小并分配缓冲区
ifstream file(path, std::ios::ate | std::ios::binary);
if (!file.is_open())
zlog::info("Failed to load binary file: {}", path);
// 使用读取位置来确定文件的大小并分配缓冲区
size_t fileSize = (size_t)file.tellg();
vector<char> data(fileSize);
// 返回文件开头,真正读取内容
file.seekg(0);
file.read(data.data(), fileSize);
file.close();
return data;
}
}

View File

@ -49,6 +49,7 @@ namespace engineapi
AssetManager() = default;
public:
static string LoadTextFile(const string& path);
static vector<char> LoadBinaryFile(const string& path);
};
}
template<>

View File

@ -7,19 +7,23 @@ namespace engineapi {
Scene::Scene()
{
mCamera = new Camera();
auto flags = Asset::ASSET_SHARED_FLAG | Asset::ASSET_ASYNC_FLAG;
Material* material = new Material("assets/shader/simple", flags);
{
ActorProperty property;
property.id = 1;
property.flags = Asset::ASSET_SHARED_FLAG | Asset::ASSET_ASYNC_FLAG;
property.flags = flags;
property.path = "assets/models/cube.obj";
actor1 = ActorMesh::New(property);
actor1->Ptr().SetMaterial(material);
}
{
ActorProperty property;
property.id = 1;
property.flags = Asset::ASSET_SHARED_FLAG | Asset::ASSET_ASYNC_FLAG;
property.flags = flags;
property.path = "assets/models/box.ply";
actor2 = ActorMesh::New(property);
actor2->Ptr().SetMaterial(material);
}
}
Scene::~Scene()

View File

@ -1,10 +1,11 @@
#include "material.h"
#include "asset/asset_manager.h"
#include "../renderapi.h"
namespace engineapi {
Material::Material(string name, uint32_t flags)
:Asset(name, flags)
{
auto vertShader = AssetManager::LoadBinaryFile(name);
}
Material::~Material()
{

View File

@ -1,6 +1,5 @@
#include "model.h"
#include "zlog.h"
#include "mesh.h"
#include "assimp/Importer.hpp"
#include "assimp/scene.h"
#include "assimp/postprocess.h"

View File

@ -1,5 +1,7 @@
#pragma once
#include "asset_render.h"
#include "mesh.h"
#include "material.h"
class aiNode;
class aiMesh;
class aiScene;
@ -7,7 +9,7 @@ namespace engineapi {
class Model : public Asset {
protected:
vector<Mesh*> mMeshes;
Material* mMaterial;
public:
using Asset::Asset;
void BeginLoad()override;
@ -16,6 +18,9 @@ namespace engineapi {
vector<Mesh*>& GetMeshs() {
return mMeshes;
}
void SetMaterial(Material* material) {
mMaterial = material;
}
void Use();
};
};