diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 4a604ba..e9ce377 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -41,13 +41,13 @@ Engine::~Engine() {} void Engine::setDefaultScreen() { - screen.projectionScale = 4096.0F; screen.nearPlaneDist = 2.0F; screen.farPlaneDist = 2000.0F; screen.fov = 60.0F; - screen.aspectRatio = 4.0F / 3.0F; screen.width = 640.0F; screen.height = 480.0F; + screen.aspectRatio = screen.width / screen.height; + screen.projectionScale = 4096.0F; // PS2 have 4k drawing area } void Engine::init(Game *t_game, u32 t_gifPacketSize) diff --git a/src/engine/include/models/math/matrix.hpp b/src/engine/include/models/math/matrix.hpp index d14855b..46b5140 100644 --- a/src/engine/include/models/math/matrix.hpp +++ b/src/engine/include/models/math/matrix.hpp @@ -85,58 +85,70 @@ public: */ inline void unit() { identity(); }; - /** Create translation matrix. */ - inline void translation(const Vector3 &t_val) + /** Translate matrix. */ + inline void translate(const Vector3 &t_val) { - identity(); - translate(t_val); + Matrix temp = Matrix(true); + temp.translation(t_val); + cross(this->data, temp.data, this->data); } - /** Create rotation matrix. */ - inline void rotation(const Vector3 &t_val) + /** Rotate matrix. */ + inline void rotate(const Vector3 &t_val) { - identity(); - Matrix temp = Matrix(); + Matrix temp = Matrix(true); + + temp.rotationZ(t_val.z); + cross(this->data, temp.data, this->data); temp.identity(); - temp.rotateX(t_val.x); - operator*=(temp); + temp.rotationY(t_val.y); + cross(this->data, temp.data, this->data); temp.identity(); - temp.rotateY(t_val.y); - operator*=(temp); - - temp.identity(); - temp.rotateZ(t_val.z); - operator*=(temp); + temp.rotationX(t_val.x); + cross(this->data, temp.data, this->data); } - /** Create X rotation matrix. */ - inline void rotationX(const float &t_radians) + /** Rotate matrix by X. */ + inline void rotateX(const float &t_radians) { - identity(); - rotateX(t_radians); + Matrix temp = Matrix(true); + temp.rotationX(t_radians); + cross(this->data, temp.data, this->data); } - /** Create Y rotation matrix. */ - inline void rotationY(const float &t_radians) + /** Rotate matrix by Y. */ + inline void rotateY(const float &t_radians) { - identity(); - rotateY(t_radians); + Matrix temp = Matrix(true); + temp.rotationY(t_radians); + cross(this->data, temp.data, this->data); } - /** Create Z rotation matrix. */ - inline void rotationZ(const float &t_radians) + /** Rotate matrix by Z. */ + inline void rotateZ(const float &t_radians) { - identity(); - rotateZ(t_radians); + Matrix temp = Matrix(true); + temp.rotationZ(t_radians); + cross(this->data, temp.data, this->data); } - /** Create angle rotation matrix. */ - void rotationByAngle(const float &t_angle, const Vector3 &t_axis); + /** Rotate matrix by angle. */ + void rotateByAngle(const float &t_angle, const Vector3 &t_axis) + { + Matrix temp; + temp.rotationByAngle(t_angle, t_axis); + cross(this->data, temp.data, this->data); + } - /** Create scale matrix. */ - void setScale(const Vector3 &t_val); + /** Scale matrix. */ + void scale(const Vector3 &t_val) + { + Matrix temp = Matrix(true); + temp.setScale(t_val); + cross(this->data, temp.data, this->data); + } /** * Create perspective projection matrix. @@ -161,12 +173,14 @@ public: const void print() const; private: - void rotateX(const float &t_radians); - void rotateY(const float &t_radians); - void rotateZ(const float &t_radians); - void translate(const Vector3 &t_val); - void cross(float res[16], const float a[16], const float b[16]) const; + void rotationX(const float &t_radians); + void rotationY(const float &t_radians); + void rotationZ(const float &t_radians); + void rotationByAngle(const float &t_angle, const Vector3 &t_axis); + void translation(const Vector3 &t_val); + void setScale(const Vector3 &t_val); void setCamera(const float t_pos[4], const float t_vz[4], const float t_vy[4]); + void cross(float res[16], const float a[16], const float b[16]) const; }; #endif diff --git a/src/engine/include/models/render_data.hpp b/src/engine/include/models/render_data.hpp index 4045fd8..f4a89c7 100644 --- a/src/engine/include/models/render_data.hpp +++ b/src/engine/include/models/render_data.hpp @@ -22,10 +22,18 @@ struct RenderData { LightBulb *bulbs; u16 bulbsCount; - /** Camera (lookAt) */ - Matrix *worldView; - /** Perspective projection */ - Matrix *perspective; + /** + * Camera matrix (glLookAt) + * OpenGL name: View + * Sony name: World view + */ + Matrix *view; + /** + * Projection matrix (glPerspective) + * OpenGL name: Projection + * Sony name: View screen + */ + Matrix *projection; Vector3 *cameraPosition; Plane *frustumPlanes; prim_t *prim; diff --git a/src/engine/include/modules/camera_base.hpp b/src/engine/include/modules/camera_base.hpp index 3cefcad..b74ff89 100644 --- a/src/engine/include/modules/camera_base.hpp +++ b/src/engine/include/modules/camera_base.hpp @@ -31,10 +31,10 @@ public: Plane planes[6]; /** - * Projection matrix + * View matrix * Set by lookAt() method. */ - Matrix worldView; + Matrix view; protected: /** Pointer to screen settings */ @@ -42,7 +42,7 @@ protected: /** * Adaption of OpenGL lookAt camera. - * Calculates worldView matrix and update frustum planes + * Calculates view matrix and update frustum planes * Should be called every frame. */ void lookAt(Vector3 &t_target); diff --git a/src/engine/include/modules/vif_sender.hpp b/src/engine/include/modules/vif_sender.hpp index a3e1e36..53153c5 100644 --- a/src/engine/include/modules/vif_sender.hpp +++ b/src/engine/include/modules/vif_sender.hpp @@ -39,6 +39,16 @@ private: void drawVertices(Mesh &t_mesh, u32 t_start, u32 t_end, VECTOR *t_vertices, VECTOR *t_coordinates, prim_t *t_prim, texbuffer_t *textureBuffer); packet2_t *packets[2] __attribute__((aligned(64))); packet2_t *currPacket; + /** + * OpenGL name: Model + * Sony name: local world + */ + Matrix model; + /** + * OpenGL name: ModelViewProjection + * Sony name: local screen + */ + Matrix modelViewProj; u8 context; packet2_t *matricesPacket __attribute__((aligned(64))); VECTOR position, rotation; diff --git a/src/engine/models/math/matrix.cpp b/src/engine/models/math/matrix.cpp index 805c433..41896a5 100644 --- a/src/engine/models/math/matrix.cpp +++ b/src/engine/models/math/matrix.cpp @@ -93,47 +93,6 @@ void Matrix::identity() : "r"(this->data)); } -void Matrix::rotationByAngle(const float &t_angle, const Vector3 &t_axis) -{ - Vector3 localAxis = Vector3(t_axis); - localAxis.normalize(); - float x = localAxis.x; - float y = localAxis.y; - float z = localAxis.z; - - float c = Math::cos(t_angle); - float s = Math::sin(t_angle); - - this->data[0] = x * x * (1 - c) + c; - this->data[1] = y * x * (1 - c) + z * s; - this->data[2] = x * z * (1 - c) - y * s; - this->data[3] = 0.0F; - - this->data[4] = x * y * (1 - c) - z * s; - this->data[5] = y * y * (1 - c) + c; - this->data[6] = y * z * (1 - c) + x * s; - this->data[7] = 0.0F; - - this->data[8] = x * z * (1 - c) + y * s; - this->data[9] = y * z * (1 - c) - x * s; - this->data[10] = z * z * (1 - c) + c; - this->data[11] = 0.0F; - - this->data[12] = 0.0F; - this->data[13] = 0.0F; - this->data[14] = 0.0F; - this->data[15] = 1.0F; -} - -void Matrix::setScale(const Vector3 &t_val) -{ - this->identity(); - this->data[0] = t_val.x; - this->data[5] = t_val.y; - this->data[10] = t_val.z; - this->data[15] = 1.0F; -} - void Matrix::setPerspective(const ScreenSettings &t_screen) { float fovYdiv2 = Math::HALF_ANG2RAD * t_screen.fov; @@ -232,10 +191,8 @@ const void Matrix::print() const // Private // ---- -void Matrix::rotateX(const float &t_radians) +void Matrix::rotationX(const float &t_radians) { - Matrix temp = Matrix(); - temp.identity(); float c = Math::cos(t_radians); float s = Math::sin(t_radians); this->data[5] = c; // 1,1 @@ -244,10 +201,8 @@ void Matrix::rotateX(const float &t_radians) this->data[10] = c; // 2,2 } -void Matrix::rotateY(const float &t_radians) +void Matrix::rotationY(const float &t_radians) { - Matrix temp = Matrix(); - temp.identity(); float c = Math::cos(t_radians); float s = Math::sin(t_radians); this->data[0] = c; // 0,0 @@ -256,10 +211,8 @@ void Matrix::rotateY(const float &t_radians) this->data[10] = c; // 2,2 } -void Matrix::rotateZ(const float &t_radians) +void Matrix::rotationZ(const float &t_radians) { - Matrix temp = Matrix(); - temp.identity(); float c = Math::cos(t_radians); float s = Math::sin(t_radians); this->data[0] = c; // 0,0 @@ -268,11 +221,51 @@ void Matrix::rotateZ(const float &t_radians) this->data[5] = c; // 1,1 } -void Matrix::translate(const Vector3 &t_val) +void Matrix::rotationByAngle(const float &t_angle, const Vector3 &t_axis) { - this->data[12] += t_val.x; // 3,0 - this->data[13] += t_val.y; // 3,1 - this->data[14] += t_val.z; // 3,2 + Vector3 localAxis = Vector3(t_axis); + localAxis.normalize(); + float x = localAxis.x; + float y = localAxis.y; + float z = localAxis.z; + + float c = Math::cos(t_angle); + float s = Math::sin(t_angle); + + this->data[0] = x * x * (1 - c) + c; + this->data[1] = y * x * (1 - c) + z * s; + this->data[2] = x * z * (1 - c) - y * s; + this->data[3] = 0.0F; + + this->data[4] = x * y * (1 - c) - z * s; + this->data[5] = y * y * (1 - c) + c; + this->data[6] = y * z * (1 - c) + x * s; + this->data[7] = 0.0F; + + this->data[8] = x * z * (1 - c) + y * s; + this->data[9] = y * z * (1 - c) - x * s; + this->data[10] = z * z * (1 - c) + c; + this->data[11] = 0.0F; + + this->data[12] = 0.0F; + this->data[13] = 0.0F; + this->data[14] = 0.0F; + this->data[15] = 1.0F; +} + +void Matrix::translation(const Vector3 &t_val) +{ + this->data[12] = t_val.x; // 3,0 + this->data[13] = t_val.y; // 3,1 + this->data[14] = t_val.z; // 3,2 +} + +void Matrix::setScale(const Vector3 &t_val) +{ + this->data[0] = t_val.x; + this->data[5] = t_val.y; + this->data[10] = t_val.z; + this->data[15] = 1.0F; } void Matrix::setCamera(const float t_pos[4], const float t_vz[4], const float t_vy[4]) diff --git a/src/engine/modules/camera_base.cpp b/src/engine/modules/camera_base.cpp index 9576306..8453012 100644 --- a/src/engine/modules/camera_base.cpp +++ b/src/engine/modules/camera_base.cpp @@ -41,7 +41,7 @@ CameraBase::CameraBase(ScreenSettings *t_screen, Vector3 *t_position) void CameraBase::lookAt(Vector3 &t_target) { updatePlanes(t_target); - worldView.lookAt(*p_position, t_target); + view.lookAt(*p_position, t_target); } void CameraBase::updatePlanes(Vector3 t_target) diff --git a/src/engine/modules/gif_sender.cpp b/src/engine/modules/gif_sender.cpp index 08dad58..ab55fc1 100644 --- a/src/engine/modules/gif_sender.cpp +++ b/src/engine/modules/gif_sender.cpp @@ -130,12 +130,7 @@ void GifSender::addClear(zbuffer_t *t_zBuffer, color_t *t_rgb) packet2_chain_close_tag(currentPacket); } -/** Adds 3D objects to current packet - * @param worldView Matrix - * @param perspective Matrix with .setPerpsective() used [clone of gluPerspective] - * @param objects3D Array of 3D objects pointers - * @param amount Amount of 3D objects - */ +/** Adds meshes to current packet */ void GifSender::addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, LightBulb *t_bulbs, u16 t_bulbsCount, texbuffer_t *textureBuffer) { packet2_chain_open_cnt(currentPacket, 0, 0, 0); @@ -145,7 +140,7 @@ void GifSender::addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCoun xyz = new xyz_t[vertexCount]; rgbaq = new color_t[vertexCount]; st = new texel_t[vertexCount]; - calc3DObject(*t_renderData->perspective, t_mesh, vertexCount, vertices, normals, coordinates, t_renderData, t_bulbs, t_bulbsCount); + calc3DObject(*t_renderData->projection, t_mesh, vertexCount, vertices, normals, coordinates, t_renderData, t_bulbs, t_bulbsCount); addCurrentCalcs(vertexCount); delete[] xyz; delete[] rgbaq; @@ -155,10 +150,7 @@ void GifSender::addObject(RenderData *t_renderData, Mesh &t_mesh, u32 vertexCoun } /** Calculates 3D object data into xyz, rgbq, st - * After it addCurrentSTQ() can be done - * @param worldView Matrix - * @param perspective Matrix with .setPerpsective() used [clone of gluPerspective] - * @param mesh 3D object + * After it addCurrentCalcs() can be done */ void GifSender::calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount, VECTOR *vertices, VECTOR *normals, VECTOR *coordinates, RenderData *t_renderData, LightBulb *t_bulbs, u16 t_bulbsCount) { @@ -176,7 +168,7 @@ void GifSender::calc3DObject(Matrix t_perspective, Mesh &t_mesh, u32 vertexCount create_local_light(localLight, rotation); // I cant put perspective from renderData here. ee-gcc bug? - create_local_screen(localScreen, localWorld, t_renderData->worldView->data, t_perspective.data); + create_local_screen(localScreen, localWorld, t_renderData->view->data, t_perspective.data); if (SHOULD_BE_LIGHTED) { diff --git a/src/engine/modules/renderer.cpp b/src/engine/modules/renderer.cpp index 1a7cc2d..5ed1027 100644 --- a/src/engine/modules/renderer.cpp +++ b/src/engine/modules/renderer.cpp @@ -51,7 +51,7 @@ Renderer::Renderer(u32 t_packetSize, ScreenSettings *t_screen) gifSender = new GifSender(t_packetSize, t_screen, &light); vifSender = new VifSender(&light); perspective.setPerspective(*t_screen); - renderData.perspective = &perspective; + renderData.projection = &perspective; PRINT_LOG("Renderer initialized!"); } @@ -277,7 +277,8 @@ void Renderer::draw(Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) if (!t_mesh.isDataLoaded()) PRINT_ERR("Can't draw, because no mesh data was loaded!"); - camRotation.rotation(-t_mesh.rotation); + camRotation.identity(); + camRotation.rotate(-t_mesh.rotation); Vector3 rotatedCamera = Vector3(camRotation * *renderData.cameraPosition); if (t_mesh.getCurrentAnimationFrame() != t_mesh.getNextAnimationFrame()) @@ -287,9 +288,9 @@ void Renderer::draw(Mesh &t_mesh, LightBulb *t_bulbs, u16 t_bulbsCount) if (t_mesh.shouldBeFrustumCulled && !t_mesh.getMaterial(i).isInFrustum(renderData.frustumPlanes, t_mesh.position)) return; u32 vertCount = t_mesh.getMaterial(i).getFacesCount(); - VECTOR __attribute__((aligned(16))) vertices[vertCount]; - VECTOR __attribute__((aligned(16))) normals[vertCount]; - VECTOR __attribute__((aligned(16))) coordinates[vertCount]; + VECTOR vertices[vertCount] __attribute__((aligned(16))); + VECTOR normals[vertCount] __attribute__((aligned(16))); + VECTOR coordinates[vertCount] __attribute__((aligned(16))); Texture *tex = textureRepo.getByMesh(t_mesh.getId(), t_mesh.getMaterial(i).getId()); changeTexture(tex); vertCount = t_mesh.getDrawData(i, vertices, normals, coordinates, rotatedCamera); @@ -305,7 +306,7 @@ void Renderer::draw(Mesh &t_mesh) { draw(t_mesh, NULL, 0); } void Renderer::setCameraDefinitions(Matrix *t_worldView, Vector3 *t_cameraPos, Plane *t_planes) { - renderData.worldView = t_worldView; + renderData.view = t_worldView; renderData.cameraPosition = t_cameraPos; renderData.frustumPlanes = t_planes; } diff --git a/src/engine/modules/vif_sender.cpp b/src/engine/modules/vif_sender.cpp index 2ef738a..2b7f20c 100644 --- a/src/engine/modules/vif_sender.cpp +++ b/src/engine/modules/vif_sender.cpp @@ -68,22 +68,22 @@ void VifSender::uploadMicroProgram() dma_channel_send_packet2(packet2, DMA_CHANNEL_VIF1, 1); packet2_free(packet2); } - +#include void VifSender::sendMatrices(const RenderData &t_renderData, const Vector3 &t_position, const Vector3 &t_rotation) { - vec3ToNative(position, t_position, 1.0F); - vec3ToNative(rotation, t_rotation, 1.0F); - MATRIX localWorld, localScreen; - create_local_world(localWorld, position, rotation); - create_local_screen(localScreen, localWorld, t_renderData.worldView->data, t_renderData.perspective->data); - // Small undo, because im testing new matrix funcs, which are DIFFERENT than PS2SDK, so cant be used right now. - // Matrix viewProj = Matrix(); - // viewProj.rotation(t_rotation); // model matrix - // viewProj.translate(t_position); // model matrix - // viewProj *= *t_renderData.worldView * *t_renderData.perspective; // viewproj = model * (view * projection) + + Matrix model; + model.identity(); + model.rotate(t_rotation); + model.translate(t_position); + + modelViewProj.identity(); + modelViewProj = model * modelViewProj; + modelViewProj = *t_renderData.view * modelViewProj; + modelViewProj = *t_renderData.projection * modelViewProj; + packet2_reset(matricesPacket, false); - // packet2_utils_vu_add_unpack_data(matricesPacket, 0, &viewProj.data, 8, 0); - packet2_utils_vu_add_unpack_data(matricesPacket, 0, localScreen, 8, 0); + packet2_utils_vu_add_unpack_data(matricesPacket, 0, &modelViewProj.data, 8, 0); packet2_utils_vu_add_end_tag(matricesPacket); dma_channel_wait(DMA_CHANNEL_VIF1, 0); dma_channel_send_packet2(matricesPacket, DMA_CHANNEL_VIF1, 1); diff --git a/src/samples/dolphin/dolphin.cpp b/src/samples/dolphin/dolphin.cpp index 6471834..11f4032 100755 --- a/src/samples/dolphin/dolphin.cpp +++ b/src/samples/dolphin/dolphin.cpp @@ -34,7 +34,7 @@ Dolphin::~Dolphin() void Dolphin::onInit() { player.init(engine); - engine->renderer->setCameraDefinitions(&camera.worldView, &camera.position, camera.planes); + engine->renderer->setCameraDefinitions(&camera.view, &camera.position, camera.planes); engine->audio.loadSong("sound/dirediredocks.wav"); engine->audio.playSong(); diff --git a/src/samples/floors/floors.cpp b/src/samples/floors/floors.cpp index 2a55ab1..10e9872 100644 --- a/src/samples/floors/floors.cpp +++ b/src/samples/floors/floors.cpp @@ -38,7 +38,7 @@ Floors::~Floors() void Floors::onInit() { setBgColorAndAmbientColor(); - engine->renderer->setCameraDefinitions(&camera.worldView, &camera.unitCirclePosition, camera.planes); + engine->renderer->setCameraDefinitions(&camera.view, &camera.unitCirclePosition, camera.planes); engine->audio.addSongListener(this); engine->audio.loadSong("sounds/mafikizolo-loot.wav"); engine->audio.playSong(); @@ -61,7 +61,7 @@ void Floors::onUpdate() engine->renderer->draw(player->mesh); engine->renderer->draw(enemy->getMeshes(), enemy->getMeshesCount()); for (u16 i = 0; i < FLOORS_COUNT; i++) - engine->renderer->drawByPath3(floorManager->floors[i].mesh, lightManager.bulbs, lightManager.bulbsCount); + engine->renderer->draw(floorManager->floors[i].mesh, lightManager.bulbs, lightManager.bulbsCount); ui->render(engine->renderer); // 2D rendering ist LAST step, because layers gonna play there. } diff --git a/src/samples/floors/objects/enemy.cpp b/src/samples/floors/objects/enemy.cpp index 858e79c..b7a46a8 100644 --- a/src/samples/floors/objects/enemy.cpp +++ b/src/samples/floors/objects/enemy.cpp @@ -30,14 +30,17 @@ Enemy::Enemy(TextureRepository *t_texRepo) meshes[0].loadMD2("meshes/enemy/", "poss_head", 0.3F, false); meshes[0].rotation.x = -1.566F; meshes[0].rotation.z = 1.566F; + meshes[0].shouldBeFrustumCulled = false; meshes[1].loadMD2("meshes/enemy/", "poss_body", 0.3F, false); meshes[1].rotation.x = -1.566F; meshes[1].rotation.z = 1.566F; + meshes[1].shouldBeFrustumCulled = false; meshes[2].loadMD2("meshes/enemy/", "poss_weapon", 0.3F, false); meshes[2].rotation.x = -1.566F; meshes[2].rotation.z = 1.566F; + meshes[2].shouldBeFrustumCulled = false; Vector3 initPos = Vector3(0.00F, 40.00F, 0.00F); setPosition(initPos);