Begin work on environment
All checks were successful
Build and Publish WASM version of demo / Build-And-Deploy (push) Successful in 16s

This commit is contained in:
Stephen Seo 2023-08-07 12:54:23 +09:00
parent bd3f760588
commit fe59f09af5
7 changed files with 83 additions and 53 deletions

View file

@ -16,14 +16,16 @@ SOURCES = \
src/screen_test.cc \
src/screen_trunner.cc \
src/3d_helpers.cc \
src/raymath.cc
src/raymath.cc \
src/ems.cc
HEADERS = \
src/game.h \
src/screen.h \
src/screen_test.h \
src/screen_trunner.h \
src/3d_helpers.h
src/3d_helpers.h \
src/ems.h
OBJECTS = $(addprefix ${OBJDIR}/,$(subst .cc,.cc.o,${SOURCES}))

View file

@ -117,6 +117,11 @@ Matrix translate_matrix_xyz(float x, float y, float z) {
0.0F, 0.0F, 1.0F, z, 0.0F, 0.0F, 0.0F, 1.0F};
}
Matrix scale_matrix_xyz(float x, float y, float z) {
return Matrix{x, 0.0F, 0.0F, 0.0F, 0.0F, y, 0.0F, 0.0F,
0.0F, 0.0F, z, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F};
}
Matrix operator*(const Matrix &a, const Matrix &b) {
return MatrixMultiply(a, b);
}

View file

@ -14,6 +14,8 @@ extern Matrix translate_matrix_y(float distance);
extern Matrix translate_matrix_z(float distance);
extern Matrix translate_matrix_xyz(float x, float y, float z);
extern Matrix scale_matrix_xyz(float x, float y, float z);
extern Matrix operator*(const Matrix &a, const Matrix &b);
// Unimplemented as this function isn't really needed and it exposes some

View file

@ -11,12 +11,24 @@ EM_JS(int, canvas_get_width, (),
EM_JS(int, canvas_get_height, (),
{ return document.getElementById("canvas").clientHeight; });
EM_JS(int, js_get_random, (), { return Math.random(); });
int call_js_get_canvas_width() { return canvas_get_width(); }
int call_js_get_canvas_height() { return canvas_get_height(); }
float call_js_get_random() { return js_get_random(); }
#else
#include <random>
int call_js_get_canvas_width() { return 800; }
int call_js_get_canvas_height() { return 800; }
float call_js_get_random() {
static std::default_random_engine re{std::random_device()()};
static std::uniform_real_distribution<float> dist(0.0F, 1.0F);
return dist(re);
}
#endif

View file

@ -4,4 +4,6 @@
extern int call_js_get_canvas_width();
extern int call_js_get_canvas_height();
extern float call_js_get_random();
#endif

View file

@ -1,67 +1,48 @@
#include "screen_trunner.h"
// standard library includes
#include <raylib.h>
#include <cassert>
#include <cmath>
// local includes
#include "3d_helpers.h"
#include "ems.h"
TRunnerScreen::TRunnerScreen(std::weak_ptr<ScreenStack> stack)
: Screen(stack),
camera{Vector3{0.0F, 0.0F, 0.0F}, Vector3{0.0F, 0.0F, -1.0F},
camera{Vector3{0.0F, 1.0F, 0.5F}, Vector3{0.0F, 0.0F, 0.0F},
Vector3{0.0F, 1.0F, 0.0F}, 80.0F, CAMERA_PERSPECTIVE},
flags(),
default_material(LoadMaterialDefault()),
TEMP_cube_model(LoadModel("res/test_cube.obj")),
plane_mesh(GenMeshPlane(1.0F, 1.0F, 2, 2)),
TEMP_cube_texture(LoadTexture("res/test_cube_texture.png")),
TEMP_matrix(get_identity_matrix()),
TEMP_permanent_matrix(get_identity_matrix()),
TEMP_value(0.0F),
TEMP_current_state(0) {
camera_pos{0.0F, 4.0F, 4.0F},
camera_target{0.0F, 0.0F, 0.0F},
pos_value(0.0F) {
TEMP_cube_model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture =
TEMP_cube_texture;
TEMP_cube_model.transform = TEMP_cube_model.transform *
scale_matrix_xyz(0.5F, 0.5F, 0.5F) *
translate_matrix_y(0.5F);
}
TRunnerScreen::~TRunnerScreen() {
UnloadTexture(TEMP_cube_texture);
UnloadMesh(plane_mesh);
UnloadModel(TEMP_cube_model);
UnloadMaterial(default_material);
}
bool TRunnerScreen::update(float dt) {
TEMP_value += TEMP_VALUE_INC_RATE * dt;
if (TEMP_value < PI / 2.0F) {
TEMP_matrix = get_rotation_matrix_about_z(TEMP_value);
} else if (TEMP_value < PI) {
if (TEMP_current_state == 0) {
TEMP_current_state = 1;
TEMP_permanent_matrix =
TEMP_permanent_matrix * get_rotation_matrix_about_z(PI / 2.0F);
} else if (TEMP_current_state != 1) {
assert(!"unreachable");
}
TEMP_matrix = get_rotation_matrix_about_y(TEMP_value - PI / 2.0F);
} else if (TEMP_value < PI * 3.0F / 2.0F) {
if (TEMP_current_state == 1) {
TEMP_current_state = 2;
TEMP_permanent_matrix =
TEMP_permanent_matrix * get_rotation_matrix_about_y(PI / 2.0F);
} else if (TEMP_current_state != 2) {
assert(!"unreachable");
}
TEMP_matrix = get_rotation_matrix_about_x(TEMP_value - PI);
} else {
if (TEMP_current_state == 2) {
TEMP_current_state = 0;
TEMP_permanent_matrix =
TEMP_permanent_matrix * get_rotation_matrix_about_x(PI / 2.0F);
} else {
assert(!"unreachable");
}
TEMP_value -= PI * 3.0F / 2.0F;
TEMP_matrix = get_rotation_matrix_about_z(TEMP_value);
pos_value += dt * POS_VALUE_INC_RATE;
if (pos_value > PI * 2.0F) {
pos_value -= PI * 2.0F;
}
camera_pos.x = std::sin(pos_value) * 4.0F;
camera_pos.z = std::cos(pos_value) * 4.0F;
camera_to_targets(dt);
return false;
}
@ -70,10 +51,20 @@ bool TRunnerScreen::draw() {
ClearBackground(PixelToColor(Pixel::PIXEL_SKY));
BeginMode3D(camera);
// DrawMesh(TEMP_cube, TEMP_default_material, TEMP_matrix *
// TEMP_offset_matrix);
DrawModel(Model{.transform = TEMP_cube_model.transform *
TEMP_permanent_matrix * TEMP_matrix,
for (int z = -25; z <= 25; ++z) {
for (int x = -25; x <= 25; ++x) {
if (x == 0 && z == 0) {
default_material.maps[MATERIAL_MAP_DIFFUSE].color = RAYWHITE;
} else {
default_material.maps[MATERIAL_MAP_DIFFUSE].color =
Color{(unsigned char)(200 + x * 2), (unsigned char)(150 + z * 2),
20, 255};
}
DrawMesh(plane_mesh, default_material, translate_matrix_xyz(x, 0.0F, z));
}
}
DrawModel(Model{.transform = TEMP_cube_model.transform * TEMP_matrix,
.meshCount = TEMP_cube_model.meshCount,
.materialCount = TEMP_cube_model.materialCount,
.meshes = TEMP_cube_model.meshes,
@ -123,3 +114,19 @@ Color TRunnerScreen::PixelToColor(Pixel p) {
return Color{0, 0, 0, 255};
}
}
void TRunnerScreen::camera_to_targets(float dt) {
camera.position.x +=
(camera_pos.x - camera.position.x) * CAMERA_UPDATE_RATE * dt;
camera.position.y +=
(camera_pos.y - camera.position.y) * CAMERA_UPDATE_RATE * dt;
camera.position.z +=
(camera_pos.z - camera.position.z) * CAMERA_UPDATE_RATE * dt;
camera.target.x +=
(camera_target.x - camera.target.x) * CAMERA_UPDATE_RATE * dt;
camera.target.y +=
(camera_target.y - camera.target.y) * CAMERA_UPDATE_RATE * dt;
camera.target.z +=
(camera_target.z - camera.target.z) * CAMERA_UPDATE_RATE * dt;
}

View file

@ -10,7 +10,8 @@
// third party includes
#include <raylib.h>
constexpr float TEMP_VALUE_INC_RATE = 1.0F;
constexpr float POS_VALUE_INC_RATE = 0.5F;
constexpr float CAMERA_UPDATE_RATE = 1.0F;
class TRunnerScreen : public Screen {
public:
@ -39,19 +40,18 @@ class TRunnerScreen : public Screen {
static Color PixelToColor(Pixel p);
const Camera3D camera;
Camera3D camera;
std::bitset<64> flags;
Material default_material;
Model TEMP_cube_model;
Mesh plane_mesh;
Texture2D TEMP_cube_texture;
Matrix TEMP_matrix;
Matrix TEMP_permanent_matrix;
float TEMP_value;
/*
* 0 - rotating about z
* 1 - rotating about y
* 2 - rotating about x
*/
int TEMP_current_state;
Vector3 camera_pos;
Vector3 camera_target;
float pos_value;
void camera_to_targets(float dt);
};
#endif