From 409ee294ebb5bc12a255454cd790de1f3727c93f Mon Sep 17 00:00:00 2001 From: h4570 Date: Sun, 21 Aug 2022 16:40:26 +0200 Subject: [PATCH] finished tutorial 03 --- ROADMAP.txt | 2 - .../pipeline/minecraft/minecraft_pipeline.hpp | 2 +- engine/inc/renderer/3d/renderer_3d.hpp | 1 + .../inc/renderer/core/3d/camera_info_3d.hpp | 6 +- .../inc/renderer/core/paths/path3/path3.hpp | 2 +- .../renderer/core/texture/models/texture.hpp | 2 +- .../core/texture/renderer_core_texture.hpp | 2 +- .../texture/renderer_core_texture_sender.hpp | 6 +- .../pipeline/minecraft/minecraft_pipeline.cpp | 4 +- engine/src/renderer/3d/renderer_3d.cpp | 4 + .../src/renderer/core/3d/camera_info_3d.cpp | 6 +- .../src/renderer/core/paths/path3/path3.cpp | 7 +- .../core/texture/renderer_core_texture.cpp | 3 +- .../texture/renderer_core_texture_sender.cpp | 6 +- tutorials/02-sprite/Makefile | 2 +- tutorials/02-sprite/inc/tutorial_02.hpp | 1 - tutorials/02-sprite/src/tutorial_02.cpp | 3 + tutorials/03-minecraft/Makefile | 24 +++++ tutorials/03-minecraft/bin/.gitignore | 4 + tutorials/03-minecraft/inc/tutorial_03.hpp | 54 ++++++++++ tutorials/03-minecraft/inc/tutorial_block.hpp | 42 ++++++++ tutorials/03-minecraft/obj/.gitignore | 4 + tutorials/03-minecraft/res/.gitignore | 4 + tutorials/03-minecraft/run.ps1 | 4 + tutorials/03-minecraft/src/main.cpp | 27 +++++ tutorials/03-minecraft/src/tutorial_03.cpp | 102 ++++++++++++++++++ tutorials/03-minecraft/src/tutorial_block.cpp | 50 +++++++++ 27 files changed, 349 insertions(+), 25 deletions(-) create mode 100644 tutorials/03-minecraft/Makefile create mode 100644 tutorials/03-minecraft/bin/.gitignore create mode 100644 tutorials/03-minecraft/inc/tutorial_03.hpp create mode 100644 tutorials/03-minecraft/inc/tutorial_block.hpp create mode 100644 tutorials/03-minecraft/obj/.gitignore create mode 100644 tutorials/03-minecraft/res/.gitignore create mode 100644 tutorials/03-minecraft/run.ps1 create mode 100644 tutorials/03-minecraft/src/main.cpp create mode 100644 tutorials/03-minecraft/src/tutorial_03.cpp create mode 100644 tutorials/03-minecraft/src/tutorial_block.cpp diff --git a/ROADMAP.txt b/ROADMAP.txt index c732438..91b185b 100644 --- a/ROADMAP.txt +++ b/ROADMAP.txt @@ -1,7 +1,5 @@ ------------ Tyra's v2.0 roadmap to publish on GitHub ------------ -- [Tutorials] 3. Minecraft cube - explain this pipeline, and explain that there are many 3D pipelines - - [Tutorials] 4. Mesh rendering - explain staticPipeline, render .obj file via Mesh class, explain that this is high-level abstract 3D object class, and what it have (animation etc) diff --git a/engine/inc/renderer/3d/pipeline/minecraft/minecraft_pipeline.hpp b/engine/inc/renderer/3d/pipeline/minecraft/minecraft_pipeline.hpp index 29fef2e..7aaa14d 100644 --- a/engine/inc/renderer/3d/pipeline/minecraft/minecraft_pipeline.hpp +++ b/engine/inc/renderer/3d/pipeline/minecraft/minecraft_pipeline.hpp @@ -43,7 +43,7 @@ class MinecraftPipeline : public Renderer3DPipeline { * @param fullClipChecks false = faster, simple PS2 clipping. True = slower, * experimental "against each plane" clipping. */ - void render(std::vector blocks, Texture* t_tex, + void render(std::vector blocks, const Texture* t_tex, const bool& isMulti = false, const bool& fullClipChecks = false); inline const float& getTextureOffset() const { diff --git a/engine/inc/renderer/3d/renderer_3d.hpp b/engine/inc/renderer/3d/renderer_3d.hpp index 6d35cb6..dd5d92f 100644 --- a/engine/inc/renderer/3d/renderer_3d.hpp +++ b/engine/inc/renderer/3d/renderer_3d.hpp @@ -27,6 +27,7 @@ class Renderer3D { /** Deinitialize previous pipeline and initialize new pipeline */ void usePipeline(Renderer3DPipeline* pipeline); + void usePipeline(Renderer3DPipeline& pipeline); private: Renderer3DPipeline* currentPipeline; diff --git a/engine/inc/renderer/core/3d/camera_info_3d.hpp b/engine/inc/renderer/core/3d/camera_info_3d.hpp index 7331a79..dd1bd08 100644 --- a/engine/inc/renderer/core/3d/camera_info_3d.hpp +++ b/engine/inc/renderer/core/3d/camera_info_3d.hpp @@ -16,11 +16,11 @@ namespace Tyra { class CameraInfo3D { public: - CameraInfo3D(Vec4* cameraPosition, Vec4* cameraLooksAt, - Vec4* cameraUp = nullptr); + CameraInfo3D(const Vec4* cameraPosition, const Vec4* cameraLooksAt, + const Vec4* cameraUp = nullptr); ~CameraInfo3D(); - Vec4 *position, *looksAt, *up; + const Vec4 *position, *looksAt, *up; private: static Vec4 defaultCameraUp; diff --git a/engine/inc/renderer/core/paths/path3/path3.hpp b/engine/inc/renderer/core/paths/path3/path3.hpp index 4a75ca2..bb673c4 100644 --- a/engine/inc/renderer/core/paths/path3/path3.hpp +++ b/engine/inc/renderer/core/paths/path3/path3.hpp @@ -29,7 +29,7 @@ class Path3 { void sendDrawFinishTag(); void clearScreen(zbuffer_t* z, const Color& color); - void sendTexture(Texture* texture, + void sendTexture(const Texture* texture, const RendererCoreTextureBuffers& texBuffers); private: diff --git a/engine/inc/renderer/core/texture/models/texture.hpp b/engine/inc/renderer/core/texture/models/texture.hpp index e60aa27..ce58589 100644 --- a/engine/inc/renderer/core/texture/models/texture.hpp +++ b/engine/inc/renderer/core/texture/models/texture.hpp @@ -45,7 +45,7 @@ class Texture { float getSizeInMB() const; - inline texwrap_t* getWrapSettings() { return &wrap; } + inline const texwrap_t* getWrapSettings() const { return &wrap; } inline const std::vector& getTextureLinks() const { return links; diff --git a/engine/inc/renderer/core/texture/renderer_core_texture.hpp b/engine/inc/renderer/core/texture/renderer_core_texture.hpp index 5a14d9b..98a05b5 100644 --- a/engine/inc/renderer/core/texture/renderer_core_texture.hpp +++ b/engine/inc/renderer/core/texture/renderer_core_texture.hpp @@ -27,7 +27,7 @@ class RendererCoreTexture { clutbuffer_t clut; TextureRepository repository; - RendererCoreTextureBuffers useTexture(Texture* t_tex); + RendererCoreTextureBuffers useTexture(const Texture* t_tex); /** Called by renderer during initialization */ void init(RendererCoreGS* gs, Path3* path3); diff --git a/engine/inc/renderer/core/texture/renderer_core_texture_sender.hpp b/engine/inc/renderer/core/texture/renderer_core_texture_sender.hpp index 26bd251..ef8dfb1 100644 --- a/engine/inc/renderer/core/texture/renderer_core_texture_sender.hpp +++ b/engine/inc/renderer/core/texture/renderer_core_texture_sender.hpp @@ -27,7 +27,7 @@ class RendererCoreTextureSender { void init(Path3* path3, RendererCoreGS* gs); - RendererCoreTextureBuffers allocate(Texture* t_texture); + RendererCoreTextureBuffers allocate(const Texture* t_texture); void deallocate(const RendererCoreTextureBuffers& texBuffers); @@ -37,8 +37,8 @@ class RendererCoreTextureSender { RendererCoreGS* gs; Path3* path3; TextureBpp getBppByPsm(const u32& psm); - texbuffer_t* allocateTextureCore(Texture* t_texture); - texbuffer_t* allocateTextureClut(Texture* t_texture); + texbuffer_t* allocateTextureCore(const Texture* t_texture); + texbuffer_t* allocateTextureClut(const Texture* t_texture); }; } // namespace Tyra diff --git a/engine/src/renderer/3d/pipeline/minecraft/minecraft_pipeline.cpp b/engine/src/renderer/3d/pipeline/minecraft/minecraft_pipeline.cpp index 994b34b..557d3c0 100644 --- a/engine/src/renderer/3d/pipeline/minecraft/minecraft_pipeline.cpp +++ b/engine/src/renderer/3d/pipeline/minecraft/minecraft_pipeline.cpp @@ -83,8 +83,8 @@ void MinecraftPipeline::initBBox() { bbox = new RenderBBox(block.vertices, block.count); } -void MinecraftPipeline::render(std::vector blocks, Texture* t_tex, - const bool& isMulti, +void MinecraftPipeline::render(std::vector blocks, + const Texture* t_tex, const bool& isMulti, const bool& fullClipChecks) { auto texBuffers = rendererCore->texture.useTexture(t_tex); diff --git a/engine/src/renderer/3d/renderer_3d.cpp b/engine/src/renderer/3d/renderer_3d.cpp index 284d6b3..8531315 100644 --- a/engine/src/renderer/3d/renderer_3d.cpp +++ b/engine/src/renderer/3d/renderer_3d.cpp @@ -19,6 +19,10 @@ Renderer3D::~Renderer3D() {} void Renderer3D::init(RendererCore* core) { utility.init(core); } +void Renderer3D::usePipeline(Renderer3DPipeline& pipeline) { + usePipeline(&pipeline); +} + void Renderer3D::usePipeline(Renderer3DPipeline* pipeline) { if (currentPipeline != pipeline) { if (currentPipeline) currentPipeline->onUseEnd(); diff --git a/engine/src/renderer/core/3d/camera_info_3d.cpp b/engine/src/renderer/core/3d/camera_info_3d.cpp index 396848f..71cfb91 100644 --- a/engine/src/renderer/core/3d/camera_info_3d.cpp +++ b/engine/src/renderer/core/3d/camera_info_3d.cpp @@ -14,8 +14,9 @@ namespace Tyra { Vec4 CameraInfo3D::defaultCameraUp = Vec4(0.0F, 1.0F, 0.0F); -CameraInfo3D::CameraInfo3D(Vec4* t_cameraPosition, Vec4* t_cameraLooksAt, - Vec4* t_cameraUp) { +CameraInfo3D::CameraInfo3D(const Vec4* t_cameraPosition, + const Vec4* t_cameraLooksAt, + const Vec4* t_cameraUp) { position = t_cameraPosition; looksAt = t_cameraLooksAt; @@ -25,6 +26,7 @@ CameraInfo3D::CameraInfo3D(Vec4* t_cameraPosition, Vec4* t_cameraLooksAt, up = t_cameraUp; } } + CameraInfo3D::~CameraInfo3D() {} } // namespace Tyra diff --git a/engine/src/renderer/core/paths/path3/path3.cpp b/engine/src/renderer/core/paths/path3/path3.cpp index 6e2e471..42c956a 100644 --- a/engine/src/renderer/core/paths/path3/path3.cpp +++ b/engine/src/renderer/core/paths/path3/path3.cpp @@ -61,7 +61,7 @@ void Path3::clearScreen(zbuffer_t* z, const Color& color) { dma_channel_send_packet2(clearScreenPacket, DMA_CHANNEL_GIF, true); } -void Path3::sendTexture(Texture* texture, +void Path3::sendTexture(const Texture* texture, const RendererCoreTextureBuffers& texBuffers) { packet2_reset(texturePacket, false); @@ -83,8 +83,9 @@ void Path3::sendTexture(Texture* texture, packet2_chain_open_cnt(texturePacket, 0, 0, 0); packet2_update(texturePacket, - draw_texture_wrapping(texturePacket->next, 0, - texture->getWrapSettings())); + draw_texture_wrapping( + texturePacket->next, 0, + const_cast(texture->getWrapSettings()))); packet2_chain_close_tag(texturePacket); packet2_update(texturePacket, draw_texture_flush(texturePacket->next)); diff --git a/engine/src/renderer/core/texture/renderer_core_texture.cpp b/engine/src/renderer/core/texture/renderer_core_texture.cpp index ad67130..999effc 100644 --- a/engine/src/renderer/core/texture/renderer_core_texture.cpp +++ b/engine/src/renderer/core/texture/renderer_core_texture.cpp @@ -35,7 +35,8 @@ void RendererCoreTexture::updateClutBuffer(texbuffer_t* clutBuffer) { } } -RendererCoreTextureBuffers RendererCoreTexture::useTexture(Texture* t_tex) { +RendererCoreTextureBuffers RendererCoreTexture::useTexture( + const Texture* t_tex) { TYRA_ASSERT(t_tex != nullptr, "Provided nullptr texture!"); auto allocated = getAllocatedBuffersByTextureId(t_tex->id); diff --git a/engine/src/renderer/core/texture/renderer_core_texture_sender.cpp b/engine/src/renderer/core/texture/renderer_core_texture_sender.cpp index 50b5ad8..56c90e5 100644 --- a/engine/src/renderer/core/texture/renderer_core_texture_sender.cpp +++ b/engine/src/renderer/core/texture/renderer_core_texture_sender.cpp @@ -23,7 +23,7 @@ void RendererCoreTextureSender::init(Path3* t_path3, RendererCoreGS* t_gs) { } RendererCoreTextureBuffers RendererCoreTextureSender::allocate( - Texture* t_texture) { + const Texture* t_texture) { texbuffer_t* core = allocateTextureCore(t_texture); texbuffer_t* clut = nullptr; @@ -54,7 +54,7 @@ void RendererCoreTextureSender::deallocate( } texbuffer_t* RendererCoreTextureSender::allocateTextureCore( - Texture* t_texture) { + const Texture* t_texture) { auto* result = new texbuffer_t; const auto* core = t_texture->core; @@ -73,7 +73,7 @@ texbuffer_t* RendererCoreTextureSender::allocateTextureCore( } texbuffer_t* RendererCoreTextureSender::allocateTextureClut( - Texture* t_texture) { + const Texture* t_texture) { auto* result = new texbuffer_t; const auto* clut = t_texture->clut; diff --git a/tutorials/02-sprite/Makefile b/tutorials/02-sprite/Makefile index 0ecc73a..7c46faf 100644 --- a/tutorials/02-sprite/Makefile +++ b/tutorials/02-sprite/Makefile @@ -1,4 +1,4 @@ -TARGET := tutorial_01.elf +TARGET := tutorial_02.elf ENGINEDIR := ../../engine #The Directories, Source, Includes, Objects, Binary and Resources diff --git a/tutorials/02-sprite/inc/tutorial_02.hpp b/tutorials/02-sprite/inc/tutorial_02.hpp index f30479f..2a4fbbb 100644 --- a/tutorials/02-sprite/inc/tutorial_02.hpp +++ b/tutorials/02-sprite/inc/tutorial_02.hpp @@ -11,7 +11,6 @@ #pragma once #include -#include namespace Tyra { diff --git a/tutorials/02-sprite/src/tutorial_02.cpp b/tutorials/02-sprite/src/tutorial_02.cpp index 023d4d5..6695bf7 100644 --- a/tutorials/02-sprite/src/tutorial_02.cpp +++ b/tutorials/02-sprite/src/tutorial_02.cpp @@ -32,7 +32,10 @@ Tutorial02::~Tutorial02() { void Tutorial02::init() { engine->renderer.setClearScreenColor(Color(32.0F, 32.0F, 32.0F)); + /** Sprite contains rectangle information. */ loadSprite(); + + /** Texture contains png image. */ loadTexture(); } diff --git a/tutorials/03-minecraft/Makefile b/tutorials/03-minecraft/Makefile new file mode 100644 index 0000000..8dd5c5c --- /dev/null +++ b/tutorials/03-minecraft/Makefile @@ -0,0 +1,24 @@ +TARGET := tutorial_03.elf +ENGINEDIR := ../../engine + +#The Directories, Source, Includes, Objects, Binary and Resources +SRCDIR := src +INCDIR := inc +BUILDDIR := obj +TARGETDIR := bin +RESDIR := res +SRCEXT := cpp +VSMEXT := vsm +VCLEXT := vcl +VCLPPEXT := vclpp +DEPEXT := d +OBJEXT := o + +#Flags, Libraries and Includes +CFLAGS := +LIB := +LIBDIRS := +INC := -I$(INCDIR) +INCDEP := -I$(INCDIR) + +include ../Makefile.tutorials-base \ No newline at end of file diff --git a/tutorials/03-minecraft/bin/.gitignore b/tutorials/03-minecraft/bin/.gitignore new file mode 100644 index 0000000..44c5ea8 --- /dev/null +++ b/tutorials/03-minecraft/bin/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore \ No newline at end of file diff --git a/tutorials/03-minecraft/inc/tutorial_03.hpp b/tutorials/03-minecraft/inc/tutorial_03.hpp new file mode 100644 index 0000000..ff8f612 --- /dev/null +++ b/tutorials/03-minecraft/inc/tutorial_03.hpp @@ -0,0 +1,54 @@ +/* +# _____ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2022, tyra - https://github.com/h4570/tyra +# Licensed under Apache License 2.0 +# Sandro Sobczyński +*/ + +#pragma once + +#include +#include +#include "./tutorial_block.hpp" + +namespace Tyra { + +class Tutorial03 : public Game { + public: + Tutorial03(Engine* engine); + ~Tutorial03(); + + void init(); + void loop(); + + private: + void loadTexture(); + + TutorialBlock getBlock(const u8& atlasX, const u8& atlasY, + const float& translationX); + void rotateBlock(TutorialBlock& block); + + Engine* engine; + + std::vector blocks; + + Texture* textureAtlas; + + /** + * This is a 3D pipeline (renderer) which we will be using. + * + * Tyra has 3 pipelines: + * - Minecraft (this one) + * - Static (for static 3D objects) + * - Dynamic (for dynamic 3D objects) + */ + MinecraftPipeline mcpip; + + /** Our camera */ + Vec4 cameraPosition, cameraLookAt; +}; + +} // namespace Tyra diff --git a/tutorials/03-minecraft/inc/tutorial_block.hpp b/tutorials/03-minecraft/inc/tutorial_block.hpp new file mode 100644 index 0000000..b91eb89 --- /dev/null +++ b/tutorials/03-minecraft/inc/tutorial_block.hpp @@ -0,0 +1,42 @@ +/* +# _____ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2022, tyra - https://github.com/h4570/tyra +# Licensed under Apache License 2.0 +# Sandro Sobczyński +*/ + +#pragma once + +#include + +namespace Tyra { + +class TutorialBlock { + public: + TutorialBlock(); + TutorialBlock(const TutorialBlock& v); + ~TutorialBlock(); + + void updateModelMatrix(); + + M4x4 translation, rotation, scale; + + /** Result matrix with translation, rotation and scale */ + M4x4 model; + + /** Color */ + Color color; + + /** Texture atlas offset */ + Vec4 atlasOffset; + + McpipBlock renderData; + + private: + void setRenderData(); +}; + +} // namespace Tyra diff --git a/tutorials/03-minecraft/obj/.gitignore b/tutorials/03-minecraft/obj/.gitignore new file mode 100644 index 0000000..44c5ea8 --- /dev/null +++ b/tutorials/03-minecraft/obj/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore \ No newline at end of file diff --git a/tutorials/03-minecraft/res/.gitignore b/tutorials/03-minecraft/res/.gitignore new file mode 100644 index 0000000..44c5ea8 --- /dev/null +++ b/tutorials/03-minecraft/res/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore \ No newline at end of file diff --git a/tutorials/03-minecraft/run.ps1 b/tutorials/03-minecraft/run.ps1 new file mode 100644 index 0000000..74646cc --- /dev/null +++ b/tutorials/03-minecraft/run.ps1 @@ -0,0 +1,4 @@ +$ConfigFile = Join-Path $PSScriptRoot '../../windows-pcsx2.ps1' +. $ConfigFile + +RunPCSX2 diff --git a/tutorials/03-minecraft/src/main.cpp b/tutorials/03-minecraft/src/main.cpp new file mode 100644 index 0000000..d0bf5ce --- /dev/null +++ b/tutorials/03-minecraft/src/main.cpp @@ -0,0 +1,27 @@ +/* +# _____ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2022, tyra - https://github.com/h4570/tyra +# Licensed under Apache License 2.0 +# Sandro Sobczyński +*/ + +#include "engine.hpp" +#include "tutorial_03.hpp" + +/** + * In this tutorial we will learn: + * - What are the VU1 pipelines + * - How to create a 3D camera + * - What is the translation, rotation scale and model matrix + * - How to render 3D minecraft cubes + */ + +int main() { + Tyra::Engine engine; + Tyra::Tutorial03 game(&engine); + engine.run(&game); + return 0; +} diff --git a/tutorials/03-minecraft/src/tutorial_03.cpp b/tutorials/03-minecraft/src/tutorial_03.cpp new file mode 100644 index 0000000..9297d2c --- /dev/null +++ b/tutorials/03-minecraft/src/tutorial_03.cpp @@ -0,0 +1,102 @@ +/* +# _____ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2022, tyra - https://github.com/h4570/tyra +# Licensed under Apache License 2.0 +# Sandro Sobczyński +*/ + +#include +#include "tutorial_03.hpp" + +namespace Tyra { + +Tutorial03::Tutorial03(Engine* t_engine) : engine(t_engine) { + textureAtlas = nullptr; +} + +Tutorial03::~Tutorial03() { + if (textureAtlas != nullptr) { + engine->renderer.core.texture.repository.free(textureAtlas); + } +} + +void Tutorial03::init() { + loadTexture(); + + engine->renderer.setClearScreenColor(Color(32.0F, 32.0F, 32.0F)); + + cameraPosition = Vec4(0.0F, 0.0F, -10.0F); // Move camera one step back + cameraLookAt.unit(); // Look at the origin - (0,0,0) + + /** Every 3D pipeline, needs to set RendererCore to it */ + mcpip.setRenderer(&engine->renderer.core); + + blocks.push_back(getBlock(0, 1, 2.0F)); + blocks.push_back(getBlock(4, 0, -2.0F)); +} + +void Tutorial03::loop() { + rotateBlock(blocks[0]); + rotateBlock(blocks[1]); + + /** Set camera info */ + engine->renderer.beginFrame(CameraInfo3D(&cameraPosition, &cameraLookAt)); + { + /** + * usePipeline() uploads pipeline's VU1 programs into VU1 memory and allows + * us to use it. + */ + engine->renderer.renderer3D.usePipeline(mcpip); + { + /** Render cobblestone */ + mcpip.render({&blocks[0].renderData}, textureAtlas); + + /** Render block using 6 textures from atlas (top-bottom) */ + mcpip.render({&blocks[1].renderData}, textureAtlas, true); + } + } + engine->renderer.endFrame(); +} + +void Tutorial03::loadTexture() { + auto& repo = engine->renderer.getTextureRepository(); + textureAtlas = repo.add(FileUtils::fromCwd("atlas.png")); +} + +TutorialBlock Tutorial03::getBlock(const u8& atlasX, const u8& atlasY, + const float& translationX) { + TutorialBlock result; + + // --- Set texture atlas offset + + const u8 texturesInAtlasRow = 16; + const float textureOffset = 1.0F / texturesInAtlasRow; + + /** + * Offset is X/Y coordinate of left up texture corner. + * Percentage is in range of 0.0F to 1.0F. + */ + result.atlasOffset = Vec4(atlasX * textureOffset, atlasY * textureOffset); + + // --- Set default color + + result.color = Color(128.0F, 128.0F, 128.0F, 128.0F); + + result.translation.translateX(translationX); + + /** Build final matrix which will be used in renderer */ + result.updateModelMatrix(); + + return result; +} + +void Tutorial03::rotateBlock(TutorialBlock& block) { + block.rotation.rotate(Vec4(0.01F, 0.02F, 0.03F)); + + block.updateModelMatrix(); +} + +} // namespace Tyra diff --git a/tutorials/03-minecraft/src/tutorial_block.cpp b/tutorials/03-minecraft/src/tutorial_block.cpp new file mode 100644 index 0000000..2d7dc7a --- /dev/null +++ b/tutorials/03-minecraft/src/tutorial_block.cpp @@ -0,0 +1,50 @@ +/* +# _____ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2022, tyra - https://github.com/h4570/tyra +# Licensed under Apache License 2.0 +# Sandro Sobczyński +*/ + +#include +#include "tutorial_block.hpp" + +namespace Tyra { + +TutorialBlock::TutorialBlock() { + /** Set identity value for matrices (instead of random numbers) */ + translation = M4x4::Identity; + rotation = M4x4::Identity; + scale = M4x4::Identity; + + setRenderData(); +} + +/** Copy constructor */ +TutorialBlock::TutorialBlock(const TutorialBlock& v) { + translation = v.translation; + rotation = v.rotation; + scale = v.scale; + model = v.model; + color = v.color; + atlasOffset = v.atlasOffset; + + setRenderData(); +} + +TutorialBlock::~TutorialBlock() {} + +/** Fill block's render data object */ +void TutorialBlock::setRenderData() { + renderData.color = &color; + renderData.model = &model; + renderData.textureOffset = &atlasOffset; +} + +void TutorialBlock::updateModelMatrix() { + model = translation * rotation * scale; +} + +} // namespace Tyra