diff --git a/.gitignore b/.gitignore index d1350e8..489b289 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vscode/ +.idea/ # --- diff --git a/src/samples/cube/.gitignore b/src/samples/cube/.gitignore new file mode 100644 index 0000000..23c6dd9 --- /dev/null +++ b/src/samples/cube/.gitignore @@ -0,0 +1,3 @@ +.vscode/ +bin/** +fonts/** \ No newline at end of file diff --git a/src/samples/cube/Makefile b/src/samples/cube/Makefile new file mode 100644 index 0000000..09720cc --- /dev/null +++ b/src/samples/cube/Makefile @@ -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 diff --git a/src/samples/cube/README.MD b/src/samples/cube/README.MD new file mode 100644 index 0000000..51260e0 --- /dev/null +++ b/src/samples/cube/README.MD @@ -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 \ No newline at end of file diff --git a/src/samples/cube/camera.cpp b/src/samples/cube/camera.cpp new file mode 100644 index 0000000..4761802 --- /dev/null +++ b/src/samples/cube/camera.cpp @@ -0,0 +1,67 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Wellington Carvalho +*/ + +#include "camera.hpp" + +#include +#include + +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; +} diff --git a/src/samples/cube/camera.hpp b/src/samples/cube/camera.hpp new file mode 100644 index 0000000..ab5a263 --- /dev/null +++ b/src/samples/cube/camera.hpp @@ -0,0 +1,41 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Wellington Carvalho +*/ + +#ifndef _CAMERA_ +#define _CAMERA_ + +#include +#include +#include +#include +#include +#include + +/** 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 diff --git a/src/samples/cube/cubes.cpp b/src/samples/cube/cubes.cpp new file mode 100644 index 0000000..55608a5 --- /dev/null +++ b/src/samples/cube/cubes.cpp @@ -0,0 +1,46 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Wellington Carvalho +*/ + +#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); +} \ No newline at end of file diff --git a/src/samples/cube/cubes.hpp b/src/samples/cube/cubes.hpp new file mode 100644 index 0000000..8950d76 --- /dev/null +++ b/src/samples/cube/cubes.hpp @@ -0,0 +1,40 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Wellington Carvalho +*/ + +#ifndef _CUBES_ +#define _CUBES_ + +#include +#include +#include +#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 \ No newline at end of file diff --git a/src/samples/cube/main.cpp b/src/samples/cube/main.cpp new file mode 100644 index 0000000..a331814 --- /dev/null +++ b/src/samples/cube/main.cpp @@ -0,0 +1,21 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Wellington Carvalho +*/ + +#include "cubes.hpp" + +int main() +{ + PRINT_LOG("INIT"); + Engine engine = Engine(); + Cubes game = Cubes(&engine); + game.engine->init(&game, 128); + SleepThread(); + return 0; +} \ No newline at end of file diff --git a/src/samples/cube/objects/cube.cpp b/src/samples/cube/objects/cube.cpp new file mode 100644 index 0000000..45a8854 --- /dev/null +++ b/src/samples/cube/objects/cube.cpp @@ -0,0 +1,77 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Wellington Carvalho +*/ + +#include "cube.hpp" +#include +#include +#include + +// ---- +// 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); +} \ No newline at end of file diff --git a/src/samples/cube/objects/cube.hpp b/src/samples/cube/objects/cube.hpp new file mode 100644 index 0000000..77845e9 --- /dev/null +++ b/src/samples/cube/objects/cube.hpp @@ -0,0 +1,37 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2020, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Wellington Carvalho +*/ + +#ifndef _CUBE_ +#define _CUBE_ + +#include "../camera.hpp" +#include +#include +#include +#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