finished tutorial 03

This commit is contained in:
h4570
2022-08-21 16:40:26 +02:00
parent 4303cc2c97
commit 409ee294eb
27 changed files with 349 additions and 25 deletions
+24
View File
@@ -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
+4
View File
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
@@ -0,0 +1,54 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <tyra>
#include <vector>
#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<TutorialBlock> 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
@@ -0,0 +1,42 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#pragma once
#include <tyra>
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
+4
View File
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
+4
View File
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
+4
View File
@@ -0,0 +1,4 @@
$ConfigFile = Join-Path $PSScriptRoot '../../windows-pcsx2.ps1'
. $ConfigFile
RunPCSX2
+27
View File
@@ -0,0 +1,27 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#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;
}
+102
View File
@@ -0,0 +1,102 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#include <tyra>
#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
@@ -0,0 +1,50 @@
/*
# _____ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licensed under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#include <tyra>
#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