generated from RuiFPB/tyra-linux-template
Rendering minecraft Blocks using the MinecraftPipeline
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
TARGET := racer.elf
|
TARGET := test.elf
|
||||||
ENGINEDIR := /tyra/engine
|
ENGINEDIR := /tyra/engine
|
||||||
|
|
||||||
#The Directories, Source, Includes, Objects, Binary and Resources
|
#The Directories, Source, Includes, Objects, Binary and Resources
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <tyra>
|
||||||
|
|
||||||
|
namespace Test {
|
||||||
|
|
||||||
|
class Block {
|
||||||
|
public:
|
||||||
|
Block();
|
||||||
|
Block(const Block& v);
|
||||||
|
~Block();
|
||||||
|
|
||||||
|
void updateModelMatrix();
|
||||||
|
|
||||||
|
Tyra::M4x4 translation, rotation, scale;
|
||||||
|
|
||||||
|
// Result matrix after transformations
|
||||||
|
Tyra::M4x4 modelMatrix;
|
||||||
|
|
||||||
|
Tyra::Color color;
|
||||||
|
|
||||||
|
Tyra::Vec4 atlasOffset;
|
||||||
|
|
||||||
|
Tyra::McpipBlock renderData;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void setRenderData();
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <tyra>
|
|
||||||
|
|
||||||
namespace Racer {
|
|
||||||
|
|
||||||
class RacerGame : public Tyra::Game {
|
|
||||||
public:
|
|
||||||
RacerGame(Tyra::Engine* engine);
|
|
||||||
~RacerGame();
|
|
||||||
|
|
||||||
void init();
|
|
||||||
void loop();
|
|
||||||
|
|
||||||
private:
|
|
||||||
Tyra::Engine* engine;
|
|
||||||
void loadTexture();
|
|
||||||
void loadSprite();
|
|
||||||
|
|
||||||
Tyra::Sprite sprite;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Racer
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <tyra>
|
||||||
|
#include "block.hpp"
|
||||||
|
|
||||||
|
namespace Test {
|
||||||
|
|
||||||
|
class RenderTest : public Tyra::Game {
|
||||||
|
public:
|
||||||
|
RenderTest(Tyra::Engine* engine);
|
||||||
|
~RenderTest();
|
||||||
|
|
||||||
|
void init();
|
||||||
|
void loop();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Tyra::Engine* engine;
|
||||||
|
Tyra::Texture* textureAtlas;
|
||||||
|
|
||||||
|
Tyra::MinecraftPipeline mcpip;
|
||||||
|
Tyra::Vec4 cameraPosition, cameraLookAt;
|
||||||
|
|
||||||
|
void loadTexture();
|
||||||
|
Block getBlock(const u8& atlasX, const u8& atlasY, const float& translationX);
|
||||||
|
void rotateBlock(Block& block);
|
||||||
|
std::vector<Block> blocks;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Test
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 96 KiB |
@@ -0,0 +1,39 @@
|
|||||||
|
#include "block.hpp"
|
||||||
|
|
||||||
|
namespace Test {
|
||||||
|
|
||||||
|
using namespace Tyra;
|
||||||
|
|
||||||
|
Block::Block() {
|
||||||
|
// Set Matrices to identity
|
||||||
|
translation = M4x4::Identity;
|
||||||
|
rotation = M4x4::Identity;
|
||||||
|
scale = M4x4::Identity;
|
||||||
|
|
||||||
|
setRenderData();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy constructor
|
||||||
|
Block::Block(const Block& v) {
|
||||||
|
translation = v.translation;
|
||||||
|
rotation = v.rotation;
|
||||||
|
scale = v.scale;
|
||||||
|
modelMatrix = v.modelMatrix;
|
||||||
|
color = v.color;
|
||||||
|
atlasOffset = v.atlasOffset;
|
||||||
|
|
||||||
|
setRenderData();
|
||||||
|
}
|
||||||
|
Block::~Block() {}
|
||||||
|
|
||||||
|
void Block::updateModelMatrix() {
|
||||||
|
modelMatrix = translation * rotation * scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Block::setRenderData() {
|
||||||
|
renderData.color = &color;
|
||||||
|
renderData.model = &modelMatrix;
|
||||||
|
renderData.textureOffset = &atlasOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+5
-3
@@ -1,9 +1,11 @@
|
|||||||
#include <tyra>
|
#include <tyra>
|
||||||
#include "racer_game.hpp"
|
#include "render_test.hpp"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Tyra::Engine engine;
|
Tyra::EngineOptions options = { .writeLogsToFile = true };
|
||||||
Racer::RacerGame game(&engine);
|
|
||||||
|
Tyra::Engine engine(options);
|
||||||
|
Test::RenderTest game(&engine);
|
||||||
engine.run(&game);
|
engine.run(&game);
|
||||||
SleepThread();
|
SleepThread();
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
#include "racer_game.hpp"
|
|
||||||
|
|
||||||
namespace Racer {
|
|
||||||
|
|
||||||
using namespace Tyra;
|
|
||||||
|
|
||||||
RacerGame::RacerGame(Engine* t_engine) { engine = t_engine; }
|
|
||||||
RacerGame::~RacerGame() {
|
|
||||||
engine->renderer.getTextureRepository().freeBySprite(sprite);
|
|
||||||
}
|
|
||||||
|
|
||||||
void RacerGame::init() {
|
|
||||||
engine->renderer.setClearScreenColor(Color(32.0F, 32.0F, 32.0F));
|
|
||||||
|
|
||||||
loadSprite();
|
|
||||||
|
|
||||||
loadTexture();
|
|
||||||
}
|
|
||||||
|
|
||||||
void RacerGame::loop() {
|
|
||||||
auto& renderer = engine->renderer;
|
|
||||||
|
|
||||||
renderer.beginFrame();
|
|
||||||
|
|
||||||
renderer.renderer2D.render(sprite);
|
|
||||||
|
|
||||||
renderer.endFrame();
|
|
||||||
}
|
|
||||||
|
|
||||||
void RacerGame::loadSprite() {
|
|
||||||
const auto& screenSettings = engine->renderer.core.getSettings();
|
|
||||||
|
|
||||||
sprite.mode = SpriteMode::MODE_STRETCH;
|
|
||||||
|
|
||||||
sprite.size = Vec2(256.0F, 128.0F);
|
|
||||||
|
|
||||||
sprite.position =
|
|
||||||
Vec2(screenSettings.getWidth() / 2.0F - sprite.size.x / 2.0F,
|
|
||||||
screenSettings.getHeight() / 2.0F - sprite.size.y / 2.0F);
|
|
||||||
|
|
||||||
TYRA_LOG("Sprite created");
|
|
||||||
}
|
|
||||||
|
|
||||||
void RacerGame::loadTexture() {
|
|
||||||
auto& renderer = engine->renderer;
|
|
||||||
|
|
||||||
auto& textureRepository = renderer.getTextureRepository();
|
|
||||||
|
|
||||||
auto filepath = FileUtils::fromCwd("chicken.png");
|
|
||||||
|
|
||||||
auto* texture = textureRepository.add(filepath);
|
|
||||||
|
|
||||||
texture->addLink(sprite.id);
|
|
||||||
|
|
||||||
TYRA_LOG("Texture loaded and linked to sprite");
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Racer
|
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
#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
|
||||||
Reference in New Issue
Block a user