Merge pull request #89 from Wellinator/cube-sample
Create basic cube sample
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
.vscode/
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
|
||||||
# ---
|
# ---
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
.vscode/
|
||||||
|
bin/**
|
||||||
|
fonts/**
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
EE_BIN = cube.elf
|
||||||
|
|
||||||
|
TYRA_DIR = ./../../engine
|
||||||
|
|
||||||
|
EE_OBJS = \
|
||||||
|
objects/cube.o \
|
||||||
|
camera.o \
|
||||||
|
cubes.o \
|
||||||
|
main.o
|
||||||
|
|
||||||
|
EE_LIBS = -ltyra
|
||||||
|
|
||||||
|
all: $(EE_BIN)
|
||||||
|
$(EE_STRIP) --strip-all $(EE_BIN)
|
||||||
|
mv $(EE_BIN) bin/$(EE_BIN)
|
||||||
|
rm $(EE_OBJS)
|
||||||
|
|
||||||
|
rebuild-engine:
|
||||||
|
cd $(TYRA_DIR) && make clean && make
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(EE_OBJS)
|
||||||
|
|
||||||
|
run: $(EE_BIN)
|
||||||
|
killall -v ps2client || true
|
||||||
|
ps2client reset
|
||||||
|
ps2client reset
|
||||||
|
$(EE_STRIP) --strip-all $(EE_BIN)
|
||||||
|
mv $(EE_BIN) bin/$(EE_BIN)
|
||||||
|
rm $(EE_OBJS)
|
||||||
|
cd bin/ && ps2client execee host:$(EE_BIN)
|
||||||
|
|
||||||
|
include $(TYRA_DIR)/Makefile.pref
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
## Cube Sample
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
- Rotating Cube
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Assets
|
||||||
|
|
||||||
|
[Download](http://apgcglz.cluster028.hosting.ovh.net/tyra/1.X.X/samples/cube/assets.zip) (Unpack this in "`/your-tyra-dir/samples/cube/bin`")
|
||||||
|
|
||||||
|
|
||||||
|
### Screenshot
|
||||||
|
|
||||||
|
[![Cube screenshot][Cube-screenshot]](#)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[Cube-screenshot]: http://apgcglz.cluster028.hosting.ovh.net/tyra/1.X.X/samples/cube/cube.gif
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
# ______ ____ ___
|
||||||
|
# | \/ ____| |___|
|
||||||
|
# | | | \ | |
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Copyright 2020, tyra - https://github.com/h4570/tyra
|
||||||
|
# Licenced under Apache License 2.0
|
||||||
|
# Wellington Carvalho <wellcoj@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "camera.hpp"
|
||||||
|
|
||||||
|
#include <utils/debug.hpp>
|
||||||
|
#include <fastmath.h>
|
||||||
|
|
||||||
|
const float CAMERA_Y = 30.0F;
|
||||||
|
|
||||||
|
// ----
|
||||||
|
// Constructors/Destructors
|
||||||
|
// ----
|
||||||
|
|
||||||
|
Camera::Camera(ScreenSettings *t_screen) : CameraBase(t_screen, &position)
|
||||||
|
{
|
||||||
|
PRINT_LOG("Initializing camera");
|
||||||
|
verticalLevel = 80.0F;
|
||||||
|
PRINT_LOG("Camera initialized!");
|
||||||
|
}
|
||||||
|
|
||||||
|
Camera::~Camera() {}
|
||||||
|
|
||||||
|
// ----
|
||||||
|
// Methods
|
||||||
|
// ----
|
||||||
|
|
||||||
|
void Camera::update(Pad &t_pad, Mesh &t_mesh)
|
||||||
|
{
|
||||||
|
rotate(t_pad);
|
||||||
|
followBy(t_mesh);
|
||||||
|
lookAt(t_mesh.position);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Set camera rotation by pad right joy and update unit circle
|
||||||
|
* https://en.wikipedia.org/wiki/Unit_circle
|
||||||
|
* https://www.desmos.com/calculator/3e7iypw4ow
|
||||||
|
*/
|
||||||
|
void Camera::rotate(Pad &t_pad)
|
||||||
|
{
|
||||||
|
if (t_pad.rJoyH <= 100)
|
||||||
|
horizontalLevel -= 0.08;
|
||||||
|
else if (t_pad.rJoyH >= 200)
|
||||||
|
horizontalLevel += 0.08;
|
||||||
|
if (t_pad.rJoyV <= 50 && verticalLevel > 25.0F)
|
||||||
|
verticalLevel -= 0.9F;
|
||||||
|
else if (t_pad.rJoyV >= 200 && verticalLevel < 80.0F)
|
||||||
|
verticalLevel += 0.9F;
|
||||||
|
unitCirclePosition.x = (sin(horizontalLevel) * verticalLevel);
|
||||||
|
unitCirclePosition.y = CAMERA_Y;
|
||||||
|
unitCirclePosition.z = (cos(horizontalLevel) * verticalLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Rotate camera around 3D object */
|
||||||
|
void Camera::followBy(Mesh &t_mesh)
|
||||||
|
{
|
||||||
|
position.x = unitCirclePosition.x + t_mesh.position.x;
|
||||||
|
position.y = unitCirclePosition.y + t_mesh.position.y;
|
||||||
|
position.z = unitCirclePosition.z + t_mesh.position.z;
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
# ______ ____ ___
|
||||||
|
# | \/ ____| |___|
|
||||||
|
# | | | \ | |
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Copyright 2020, tyra - https://github.com/h4570/tyra
|
||||||
|
# Licenced under Apache License 2.0
|
||||||
|
# Wellington Carvalho <wellcoj@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _CAMERA_
|
||||||
|
#define _CAMERA_
|
||||||
|
|
||||||
|
#include <modules/pad.hpp>
|
||||||
|
#include <models/mesh.hpp>
|
||||||
|
#include <models/math/vector3.hpp>
|
||||||
|
#include <models/screen_settings.hpp>
|
||||||
|
#include <modules/camera_base.hpp>
|
||||||
|
#include <tamtypes.h>
|
||||||
|
|
||||||
|
/** 3D camera which follow by 3D object. Can be rotated via pad */
|
||||||
|
class Camera : public CameraBase
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
Vector3 up, position, unitCirclePosition;
|
||||||
|
float horizontalLevel, verticalLevel;
|
||||||
|
|
||||||
|
Camera(ScreenSettings *t_screen);
|
||||||
|
~Camera();
|
||||||
|
|
||||||
|
void update(Pad &t_pad, Mesh &t_mesh);
|
||||||
|
void rotate(Pad &t_pad);
|
||||||
|
void followBy(Mesh &t_mesh);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Vector3 *getPosition() { return &position; };
|
||||||
|
ScreenSettings screen;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
# ______ ____ ___
|
||||||
|
# | \/ ____| |___|
|
||||||
|
# | | | \ | |
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Copyright 2020, tyra - https://github.com/h4570/tyra
|
||||||
|
# Licenced under Apache License 2.0
|
||||||
|
# Wellington Carvalho <wellcoj@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "cubes.hpp"
|
||||||
|
|
||||||
|
Cubes::Cubes(Engine *t_engine)
|
||||||
|
: engine(t_engine), camera(&t_engine->screen)
|
||||||
|
{
|
||||||
|
PRINT_LOG("Initing cubes sample");
|
||||||
|
}
|
||||||
|
|
||||||
|
Cubes::~Cubes()
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cubes::onInit(){
|
||||||
|
texRepo = engine->renderer->getTextureRepository();
|
||||||
|
cube = new Cube(texRepo);
|
||||||
|
setBgColorAndAmbientColor();
|
||||||
|
engine->renderer->setCameraDefinitions(&camera.view, &camera.unitCirclePosition, camera.planes);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cubes::onUpdate(){
|
||||||
|
camera.update(engine->pad, cube->mesh);
|
||||||
|
cube->update(engine->pad, camera);
|
||||||
|
engine->renderer->draw(cube->mesh);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cubes::setBgColorAndAmbientColor()
|
||||||
|
{
|
||||||
|
color_t bgColor;
|
||||||
|
bgColor.r = 0x20;
|
||||||
|
bgColor.g = 0x20;
|
||||||
|
bgColor.b = 0x20;
|
||||||
|
engine->renderer->setWorldColor(bgColor);
|
||||||
|
Vector3 ambient = Vector3(0.2F, 0.2F, 0.2F);
|
||||||
|
engine->renderer->setAmbientLight(ambient);
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
# ______ ____ ___
|
||||||
|
# | \/ ____| |___|
|
||||||
|
# | | | \ | |
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Copyright 2020, tyra - https://github.com/h4570/tyra
|
||||||
|
# Licenced under Apache License 2.0
|
||||||
|
# Wellington Carvalho <wellcoj@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _CUBES_
|
||||||
|
#define _CUBES_
|
||||||
|
|
||||||
|
#include <tamtypes.h>
|
||||||
|
#include <game.hpp>
|
||||||
|
#include <engine.hpp>
|
||||||
|
#include "modules/texture_repository.hpp"
|
||||||
|
#include "camera.hpp"
|
||||||
|
#include "objects/cube.hpp"
|
||||||
|
|
||||||
|
class Cubes : public Game
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
Cubes(Engine *t_engine);
|
||||||
|
~Cubes();
|
||||||
|
|
||||||
|
void onInit();
|
||||||
|
void onUpdate();
|
||||||
|
|
||||||
|
Engine *engine;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void setBgColorAndAmbientColor();
|
||||||
|
TextureRepository *texRepo;
|
||||||
|
Cube *cube;
|
||||||
|
Camera camera;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
# ______ ____ ___
|
||||||
|
# | \/ ____| |___|
|
||||||
|
# | | | \ | |
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Copyright 2020, tyra - https://github.com/h4570/tyra
|
||||||
|
# Licenced under Apache License 2.0
|
||||||
|
# Wellington Carvalho <wellcoj@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "cubes.hpp"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
PRINT_LOG("INIT");
|
||||||
|
Engine engine = Engine();
|
||||||
|
Cubes game = Cubes(&engine);
|
||||||
|
game.engine->init(&game, 128);
|
||||||
|
SleepThread();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
# ______ ____ ___
|
||||||
|
# | \/ ____| |___|
|
||||||
|
# | | | \ | |
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Copyright 2020, tyra - https://github.com/h4570/tyra
|
||||||
|
# Licenced under Apache License 2.0
|
||||||
|
# Wellington Carvalho <wellcoj@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "cube.hpp"
|
||||||
|
#include <models/mesh.hpp>
|
||||||
|
#include <loaders/obj_loader.hpp>
|
||||||
|
#include <utils/debug.hpp>
|
||||||
|
|
||||||
|
// ----
|
||||||
|
// Constructors/Destructors
|
||||||
|
// ----
|
||||||
|
|
||||||
|
Cube::Cube( TextureRepository *t_texRepo )
|
||||||
|
{
|
||||||
|
PRINT_LOG("Creating cube");
|
||||||
|
texRepo = t_texRepo;
|
||||||
|
speed = 1.2F;
|
||||||
|
|
||||||
|
mesh.loadObj("meshes/cube/", "cube", 10.0F, true);
|
||||||
|
mesh.position.set(20.00F, 40.00F, 10.00F);
|
||||||
|
mesh.rotation.x = -1.566F;
|
||||||
|
mesh.rotation.z = 1.566F;
|
||||||
|
|
||||||
|
texRepo->addByMesh("meshes/cube/", mesh, PNG);
|
||||||
|
|
||||||
|
PRINT_LOG("Cube object created!");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cube::update(const Pad &t_pad, const Camera &t_camera)
|
||||||
|
{
|
||||||
|
rotate(t_pad);
|
||||||
|
Vector3 *nextPos = getNextPosition(t_pad, t_camera);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector3 *Cube::getNextPosition(const Pad &t_pad, const Camera &t_camera)
|
||||||
|
{
|
||||||
|
Vector3 *result = new Vector3(mesh.position);
|
||||||
|
Vector3 normalizedCamera = Vector3(t_camera.unitCirclePosition);
|
||||||
|
normalizedCamera.normalize();
|
||||||
|
normalizedCamera *= speed;
|
||||||
|
if (t_pad.lJoyV <= 100)
|
||||||
|
{
|
||||||
|
result->x += -normalizedCamera.x;
|
||||||
|
result->z += -normalizedCamera.z;
|
||||||
|
}
|
||||||
|
else if (t_pad.lJoyV >= 200)
|
||||||
|
{
|
||||||
|
result->x += normalizedCamera.x;
|
||||||
|
result->z += normalizedCamera.z;
|
||||||
|
}
|
||||||
|
if (t_pad.lJoyH <= 100)
|
||||||
|
{
|
||||||
|
result->x += -normalizedCamera.z;
|
||||||
|
result->z += normalizedCamera.x;
|
||||||
|
}
|
||||||
|
else if (t_pad.lJoyH >= 200)
|
||||||
|
{
|
||||||
|
result->x += normalizedCamera.z;
|
||||||
|
result->z += -normalizedCamera.x;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cube::rotate(const Pad &t_pad){
|
||||||
|
//if (t_pad.rJoyH >= 200)
|
||||||
|
//else if (t_pad.rJoyH <= 100)
|
||||||
|
mesh.rotation.x += (0.01 * speed);
|
||||||
|
mesh.rotation.y += (0.01 * speed);
|
||||||
|
mesh.rotation.z += (0.01 * speed);
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
# ______ ____ ___
|
||||||
|
# | \/ ____| |___|
|
||||||
|
# | | | \ | |
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Copyright 2020, tyra - https://github.com/h4570/tyra
|
||||||
|
# Licenced under Apache License 2.0
|
||||||
|
# Wellington Carvalho <wellcoj@gmail.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _CUBE_
|
||||||
|
#define _CUBE_
|
||||||
|
|
||||||
|
#include "../camera.hpp"
|
||||||
|
#include <tamtypes.h>
|
||||||
|
#include <modules/pad.hpp>
|
||||||
|
#include <modules/timer.hpp>
|
||||||
|
#include "modules/texture_repository.hpp"
|
||||||
|
|
||||||
|
/** Cube 3D object class */
|
||||||
|
class Cube
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Mesh mesh;
|
||||||
|
Cube( TextureRepository *t_texRepo );
|
||||||
|
~Cube();
|
||||||
|
|
||||||
|
void update(const Pad &t_pad, const Camera &t_camera);
|
||||||
|
|
||||||
|
private:
|
||||||
|
TextureRepository *texRepo;
|
||||||
|
Vector3 *getNextPosition(const Pad &t_pad, const Camera &t_camera);
|
||||||
|
void rotate(const Pad &t_pad);
|
||||||
|
float speed;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user