Files
tyra-rendering-test/src/render_test.cpp
T

83 lines
2.2 KiB
C++

#include "render_test.hpp"
namespace Test {
using namespace Tyra;
RenderTest::RenderTest(Engine* t_engine) : engine(t_engine) {
textureAtlas = nullptr;
}
RenderTest::~RenderTest() {
if (textureAtlas != nullptr) {
engine->renderer.getTextureRepository().free(textureAtlas);
}
}
void RenderTest::init() {
loadTexture();
engine->renderer.setClearScreenColor(Color(32.0F, 32.0F, 32.0F));
cameraPosition = Vec4(0.0F, 0.0F, -10.0F);
cameraLookAt.unit();
mcpip.setRenderer(&engine->renderer.core);
TYRA_LOG("RendererCore set for MinecraftPipeline");
blocks.push_back(getBlock(0, 1, 2.0F));
blocks.push_back(getBlock(4, 0, -2.0F));
TYRA_LOG("Blocks created and added to the scene");
}
void RenderTest::loop() {
for (auto& block : blocks) {
rotateBlock(block);
}
engine->renderer.beginFrame(CameraInfo3D(&cameraPosition, &cameraLookAt));
{
engine->renderer.renderer3D.usePipeline(&mcpip);
{
mcpip.render({&blocks[0].renderData}, textureAtlas);
mcpip.render({&blocks[1].renderData}, textureAtlas, true);
}
}
engine->renderer.endFrame();
}
void RenderTest::loadTexture() {
auto& repo = engine->renderer.getTextureRepository();
textureAtlas = repo.add(FileUtils::fromCwd("atlas.png"));
TYRA_LOG("Texture atlas loaded");
}
Block RenderTest::getBlock(const u8& atlasX, const u8& atlasY, const float& translationX) {
Block result;
const u8 texturesInAtlasRow = 16;
const float textureOffset = 1.0F / texturesInAtlasRow;
result.atlasOffset = Vec4(atlasX * textureOffset, atlasY * textureOffset);
result.color = Color(128.0F, 128.0F, 128.0F, 128.0F);
result.translation.translateX(translationX);
result.updateModelMatrix();
TYRA_LOG("Block created with atlas coordinates (" + std::to_string(atlasX) + ", " + std::to_string(atlasY) + ") and translationX " + std::to_string(translationX));
return result;
}
void RenderTest::rotateBlock(Block& block) {
block.rotation.rotate(Vec4(0.01F, 0.02F, 0.03F));
block.updateModelMatrix();
}
} // namespace Test