textures almost work?
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
#include "../../engine/Shared.h"
|
||||
#include "../Global.h"
|
||||
#include "../Input.h"
|
||||
#include "../Instance.h"
|
||||
#include "../Log.h"
|
||||
#include "../Mesh.h"
|
||||
#include "Rendering.h"
|
||||
|
||||
#include "bgfx/defines.h"
|
||||
#include "bx/filepath.h"
|
||||
#include "bx/math.h"
|
||||
#include "bx/timer.h"
|
||||
#include <bgfx/bgfx.h>
|
||||
#include <bimg/bimg.h>
|
||||
#include <bx/file.h>
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
@@ -18,20 +21,34 @@ namespace Game
|
||||
{
|
||||
namespace
|
||||
{
|
||||
static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const bx::FilePath& _filePath)
|
||||
const bgfx::Memory* loadFile(const char* path, bool appendZero = false, int32_t retryCount = 1)
|
||||
{
|
||||
if (bx::open(_reader, _filePath))
|
||||
FILE* file;
|
||||
for (int32_t i = 0; i < retryCount; ++i)
|
||||
{
|
||||
uint32_t size = (uint32_t)bx::getSize(_reader);
|
||||
const bgfx::Memory* mem = bgfx::alloc(size + 1);
|
||||
bx::read(_reader, mem->data, size, bx::ErrorAssert{});
|
||||
bx::close(_reader);
|
||||
mem->data[mem->size - 1] = '\0';
|
||||
file = fopen(path, "rb");
|
||||
if (file == nullptr && i < retryCount - 1)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
break;
|
||||
}
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
long fileSize = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
|
||||
const bgfx::Memory* mem = bgfx::alloc(fileSize + 1);
|
||||
fread(mem->data, 1, fileSize, file);
|
||||
if (appendZero)
|
||||
{
|
||||
mem->data[mem->size - 1] = '\0';
|
||||
}
|
||||
fclose(file);
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
Log("Failed to load %s.", _filePath.getCPtr());
|
||||
return NULL;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bgfx::ShaderHandle loadShader(const char* FILENAME)
|
||||
@@ -72,31 +89,98 @@ namespace Game
|
||||
bx::strCat(buffer, sizeof(buffer), ".bin");
|
||||
|
||||
Log("Loading shader at %s", buffer);
|
||||
const bgfx::Memory* mem = loadFile(buffer, true, 3);
|
||||
|
||||
FILE* file;
|
||||
for (int32_t i = 0; i < 3; ++i)
|
||||
if (mem == nullptr)
|
||||
{
|
||||
file = fopen(buffer, "rb");
|
||||
if (file == nullptr)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
break;
|
||||
}
|
||||
Log("Failed to load shader %s", FILENAME);
|
||||
return {};
|
||||
}
|
||||
return bgfx::createShader(mem);
|
||||
}
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
long fileSize = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
bgfx::TextureHandle loadTexture(const bx::FilePath& _filePath,
|
||||
uint64_t _flags,
|
||||
uint8_t _skip,
|
||||
bgfx::TextureInfo* _info,
|
||||
bimg::Orientation::Enum* _orientation)
|
||||
{
|
||||
BX_UNUSED(_skip);
|
||||
bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
|
||||
bx::Error err;
|
||||
|
||||
const bgfx::Memory* mem = bgfx::alloc(fileSize + 1);
|
||||
fread(mem->data, 1, fileSize, file);
|
||||
mem->data[mem->size - 1] = '\0';
|
||||
fclose(file);
|
||||
|
||||
return bgfx::createShader(mem);
|
||||
const bgfx::Memory* data = loadFile(_filePath.getCPtr());
|
||||
if (data == nullptr)
|
||||
{
|
||||
Log("Failed to find image %s", _filePath.getCPtr());
|
||||
return handle;
|
||||
}
|
||||
bimg::ImageContainer imageContainer;
|
||||
if (!bimg::imageParse(imageContainer, data->data, data->size, &err))
|
||||
{
|
||||
Log("Failed to load image %s", _filePath.getCPtr());
|
||||
return handle;
|
||||
}
|
||||
if (NULL != _orientation)
|
||||
{
|
||||
*_orientation = imageContainer.m_orientation;
|
||||
}
|
||||
|
||||
Log("Failed to load shader %s", FILENAME);
|
||||
return {};
|
||||
const bgfx::Memory* mem = bgfx::makeRef(imageContainer.m_data, imageContainer.m_size);
|
||||
|
||||
if (NULL != _info)
|
||||
{
|
||||
bgfx::calcTextureSize(*_info,
|
||||
uint16_t(imageContainer.m_width),
|
||||
uint16_t(imageContainer.m_height),
|
||||
uint16_t(imageContainer.m_depth),
|
||||
imageContainer.m_cubeMap,
|
||||
1 < imageContainer.m_numMips,
|
||||
imageContainer.m_numLayers,
|
||||
bgfx::TextureFormat::Enum(imageContainer.m_format));
|
||||
}
|
||||
|
||||
if (imageContainer.m_cubeMap)
|
||||
{
|
||||
handle = bgfx::createTextureCube(uint16_t(imageContainer.m_width),
|
||||
1 < imageContainer.m_numMips,
|
||||
imageContainer.m_numLayers,
|
||||
bgfx::TextureFormat::Enum(imageContainer.m_format),
|
||||
_flags,
|
||||
mem);
|
||||
}
|
||||
else if (1 < imageContainer.m_depth)
|
||||
{
|
||||
handle = bgfx::createTexture3D(uint16_t(imageContainer.m_width),
|
||||
uint16_t(imageContainer.m_height),
|
||||
uint16_t(imageContainer.m_depth),
|
||||
1 < imageContainer.m_numMips,
|
||||
bgfx::TextureFormat::Enum(imageContainer.m_format),
|
||||
_flags,
|
||||
mem);
|
||||
}
|
||||
else if (bgfx::isTextureValid(0,
|
||||
false,
|
||||
imageContainer.m_numLayers,
|
||||
bgfx::TextureFormat::Enum(imageContainer.m_format),
|
||||
_flags))
|
||||
{
|
||||
handle = bgfx::createTexture2D(uint16_t(imageContainer.m_width),
|
||||
uint16_t(imageContainer.m_height),
|
||||
1 < imageContainer.m_numMips,
|
||||
imageContainer.m_numLayers,
|
||||
bgfx::TextureFormat::Enum(imageContainer.m_format),
|
||||
_flags,
|
||||
mem);
|
||||
}
|
||||
|
||||
if (bgfx::isValid(handle))
|
||||
{
|
||||
const bx::StringView name(_filePath);
|
||||
bgfx::setName(handle, name.getPtr(), name.getLength());
|
||||
}
|
||||
|
||||
return handle;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@@ -108,7 +192,6 @@ namespace Game
|
||||
bgfx::Init init;
|
||||
init.type = bgfx::RendererType::Direct3D12;
|
||||
init.debug = true;
|
||||
init.callback = &Callback;
|
||||
init.platformData.nwh = shared.Window.Handle;
|
||||
init.platformData.ndt = nullptr;
|
||||
init.platformData.type = bgfx::NativeWindowHandleType::Default;
|
||||
@@ -129,14 +212,18 @@ namespace Game
|
||||
bgfx::setDebug(BGFX_DEBUG_TEXT);
|
||||
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
|
||||
|
||||
DefaultSampler = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
|
||||
Textures[0].Handle =
|
||||
loadTexture(bx::FilePath{"models/body.dds"}, BGFX_TEXTURE_NONE | BGFX_SAMPLER_NONE, 0, nullptr, nullptr);
|
||||
Textures[0].SamplerHandle = DefaultSampler;
|
||||
LoadMesh(Models[0], "models/cube.gltf");
|
||||
LoadMesh(Models[1], "models/zurg.gltf");
|
||||
|
||||
Materials[0] = Material::LoadFromShader("vert", "frag");
|
||||
Materials[0] = Material::LoadFromShader("vert", "frag", Textures[0].Handle, Textures[0].SamplerHandle);
|
||||
|
||||
if (!GetInstance().IsInitialized)
|
||||
{
|
||||
GetInstance().StartTime = bx::getHPCounter();
|
||||
GetInstance().Time.StartTime = bx::getHPCounter();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,30 +257,6 @@ namespace Game
|
||||
|
||||
{
|
||||
GetInstance().GameLevel.Update();
|
||||
|
||||
float delta = GetInstance().Delta;
|
||||
constexpr float moveSpeed = 10.0f;
|
||||
constexpr float rotSpeed = 0.6f;
|
||||
|
||||
float forwardInput = (GetKey(ScanCode::W) ? 1.0f : 0.0f) + (GetKey(ScanCode::S) ? -1.0f : 0.0f);
|
||||
float rightInput = (GetKey(ScanCode::D) ? 1.0f : 0.0f) + (GetKey(ScanCode::A) ? -1.0f : 0.0f);
|
||||
bx::Vec3 moveInput = bx::Vec3{rightInput, forwardInput, 0.0f};
|
||||
moveInput = bx::normalize(moveInput);
|
||||
bx::Vec3 inputVec = {moveInput.x * delta * moveSpeed, 0.0f, moveInput.y * delta * moveSpeed};
|
||||
|
||||
bx::Vec3 camForward = Cam.Transform.Forward();
|
||||
bx::Vec3 camRight = Cam.Transform.Right();
|
||||
|
||||
bx::Vec3 rotInput = {
|
||||
shared.Window.MouseDeltaY * delta * rotSpeed, shared.Window.MouseDeltaX * delta * rotSpeed, 0.0f};
|
||||
Cam.FreelookXRot += rotInput.x;
|
||||
Cam.FreelookYRot += rotInput.y;
|
||||
bx::mtxRotateY(Cam.Transform.Rotation.M, Cam.FreelookYRot);
|
||||
Cam.Transform.RotateLocal({Cam.FreelookXRot, 0.0f, 0.0f});
|
||||
|
||||
Cam.Transform.TranslateLocal({0.0f, 0.0f, -inputVec.z});
|
||||
Cam.Transform.TranslateLocal({-inputVec.x, 0.0f, 0.0f});
|
||||
bgfx::dbgTextPrintf(1, 4, 0x0f, "Cam forward: %.2f %.2f %.2f", camForward.x, camForward.y, camForward.z);
|
||||
}
|
||||
|
||||
// Set view and projection matrix for view 0.
|
||||
@@ -205,8 +268,17 @@ namespace Game
|
||||
0.1f,
|
||||
1000.0f,
|
||||
bgfx::getCaps()->homogeneousDepth);
|
||||
Cam.Transform.UpdateMatrix();
|
||||
bgfx::setViewTransform(0, Cam.Transform.M.M, proj);
|
||||
|
||||
auto& player = GetInstance().Player;
|
||||
if (player.Mode == PlayerMode::Freefly)
|
||||
{
|
||||
player.FreeflyCamTransform.UpdateMatrix();
|
||||
bgfx::setViewTransform(0, player.FreeflyCamTransform.M.M, proj);
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
// Set view 0 default viewport.
|
||||
bgfx::setViewRect(0, 0, 0, shared.Window.WindowWidth, shared.Window.WindowHeight);
|
||||
@@ -215,9 +287,10 @@ namespace Game
|
||||
GetInstance().GameLevel.Cubes.Render(Models, Materials);
|
||||
GetInstance().GameLevel.Tests.Render(Models, Materials);
|
||||
|
||||
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1f", GetInstance().Now);
|
||||
bgfx::dbgTextPrintf(1, 2, 0x0F, "Frame: %u", GetInstance().FrameCounter);
|
||||
bgfx::dbgTextPrintf(1, 3, 0x0F, "Delta: %.3fms / %.0ffps", GetInstance().Delta, 1.0 / GetInstance().Delta);
|
||||
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1f", GetInstance().Time.Now);
|
||||
bgfx::dbgTextPrintf(1, 2, 0x0F, "Frame: %u", GetInstance().Time.FrameCounter);
|
||||
bgfx::dbgTextPrintf(
|
||||
1, 3, 0x0F, "Delta: %.3fms / %.0ffps", GetInstance().Time.Delta, 1.0 / GetInstance().Time.Delta);
|
||||
|
||||
bgfx::frame();
|
||||
}
|
||||
@@ -227,10 +300,14 @@ namespace Game
|
||||
bgfx::shutdown();
|
||||
}
|
||||
|
||||
Material Material::LoadFromShader(const char* vertPath, const char* fragPath)
|
||||
Material Material::LoadFromShader(const char* vertPath,
|
||||
const char* fragPath,
|
||||
bgfx::TextureHandle tex,
|
||||
bgfx::UniformHandle sampler)
|
||||
{
|
||||
bgfx::ShaderHandle vertexShader = loadShader("vert");
|
||||
bgfx::ShaderHandle fragmentShader = loadShader("frag");
|
||||
BX_ASSERT(vertPath != nullptr && fragPath != nullptr, "Invalid shader path!");
|
||||
bgfx::ShaderHandle vertexShader = loadShader(vertPath);
|
||||
bgfx::ShaderHandle fragmentShader = loadShader(fragPath);
|
||||
|
||||
Material mat;
|
||||
mat.Shader = bgfx::createProgram(vertexShader, fragmentShader, true);
|
||||
@@ -238,6 +315,8 @@ namespace Game
|
||||
BGFX_STATE_CULL_CCW | BGFX_STATE_MSAA;
|
||||
mat.Uniforms[Material::UTime] = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
|
||||
mat.Uniforms[Material::UDotColor] = bgfx::createUniform("u_testColor", bgfx::UniformType::Vec4);
|
||||
mat.Textures[0].Handle = tex;
|
||||
mat.Textures[0].SamplerHandle = sampler;
|
||||
return mat;
|
||||
}
|
||||
} // namespace Game
|
||||
|
||||
Reference in New Issue
Block a user