2014-04-30 08:50:12 +08:00
|
|
|
/**
|
|
|
|
Copyright 2013 BlackBerry Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-04-30 08:50:12 +08:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
Original file from GamePlay3D: http://gameplay3d.org
|
|
|
|
|
|
|
|
This file was modified to fit the cocos2d-x project
|
|
|
|
*/
|
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "math/Mat4.h"
|
2016-07-28 05:49:41 +08:00
|
|
|
|
|
|
|
#include <cmath>
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "math/Quaternion.h"
|
|
|
|
#include "math/MathUtil.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2014-04-02 12:09:51 +08:00
|
|
|
NS_CC_MATH_BEGIN
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4::Mat4()
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-14 01:26:25 +08:00
|
|
|
*this = IDENTITY;
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4::Mat4(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24,
|
2014-04-02 10:33:07 +08:00
|
|
|
float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44)
|
|
|
|
{
|
|
|
|
set(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4::Mat4(const float* mat)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-04-16 10:58:34 +08:00
|
|
|
set(mat);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4::Mat4(const Mat4& copy)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
memcpy(m, copy.m, MATRIX_SIZE);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4::~Mat4()
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createLookAt(const Vec3& eyePosition, const Vec3& targetPosition, const Vec3& up, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
createLookAt(eyePosition.x, eyePosition.y, eyePosition.z, targetPosition.x, targetPosition.y, targetPosition.z,
|
|
|
|
up.x, up.y, up.z, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createLookAt(float eyePositionX, float eyePositionY, float eyePositionZ,
|
2014-04-02 10:33:07 +08:00
|
|
|
float targetPositionX, float targetPositionY, float targetPositionZ,
|
2014-05-15 01:07:09 +08:00
|
|
|
float upX, float upY, float upZ, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 eye(eyePositionX, eyePositionY, eyePositionZ);
|
|
|
|
Vec3 target(targetPositionX, targetPositionY, targetPositionZ);
|
|
|
|
Vec3 up(upX, upY, upZ);
|
2014-04-02 10:33:07 +08:00
|
|
|
up.normalize();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 zaxis;
|
|
|
|
Vec3::subtract(eye, target, &zaxis);
|
2014-04-02 10:33:07 +08:00
|
|
|
zaxis.normalize();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 xaxis;
|
|
|
|
Vec3::cross(up, zaxis, &xaxis);
|
2014-04-02 10:33:07 +08:00
|
|
|
xaxis.normalize();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 yaxis;
|
|
|
|
Vec3::cross(zaxis, xaxis, &yaxis);
|
2014-04-02 10:33:07 +08:00
|
|
|
yaxis.normalize();
|
|
|
|
|
|
|
|
dst->m[0] = xaxis.x;
|
|
|
|
dst->m[1] = yaxis.x;
|
|
|
|
dst->m[2] = zaxis.x;
|
|
|
|
dst->m[3] = 0.0f;
|
|
|
|
|
|
|
|
dst->m[4] = xaxis.y;
|
|
|
|
dst->m[5] = yaxis.y;
|
|
|
|
dst->m[6] = zaxis.y;
|
|
|
|
dst->m[7] = 0.0f;
|
|
|
|
|
|
|
|
dst->m[8] = xaxis.z;
|
|
|
|
dst->m[9] = yaxis.z;
|
|
|
|
dst->m[10] = zaxis.z;
|
|
|
|
dst->m[11] = 0.0f;
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
dst->m[12] = -Vec3::dot(xaxis, eye);
|
|
|
|
dst->m[13] = -Vec3::dot(yaxis, eye);
|
|
|
|
dst->m[14] = -Vec3::dot(zaxis, eye);
|
2014-04-02 10:33:07 +08:00
|
|
|
dst->m[15] = 1.0f;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createPerspective(float fieldOfView, float aspectRatio,
|
|
|
|
float zNearPlane, float zFarPlane, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
GP_ASSERT(zFarPlane != zNearPlane);
|
|
|
|
|
|
|
|
float f_n = 1.0f / (zFarPlane - zNearPlane);
|
|
|
|
float theta = MATH_DEG_TO_RAD(fieldOfView) * 0.5f;
|
2016-07-28 05:49:41 +08:00
|
|
|
if (std::abs(std::fmod(theta, MATH_PIOVER2)) < MATH_EPSILON)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-04-16 10:58:34 +08:00
|
|
|
CCLOGERROR("Invalid field of view value (%f) causes attempted calculation tan(%f), which is undefined.", fieldOfView, theta);
|
2014-04-02 10:33:07 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-07-28 05:49:41 +08:00
|
|
|
float divisor = std::tan(theta);
|
2014-04-02 10:33:07 +08:00
|
|
|
GP_ASSERT(divisor);
|
|
|
|
float factor = 1.0f / divisor;
|
|
|
|
|
|
|
|
memset(dst, 0, MATRIX_SIZE);
|
|
|
|
|
|
|
|
GP_ASSERT(aspectRatio);
|
|
|
|
dst->m[0] = (1.0f / aspectRatio) * factor;
|
|
|
|
dst->m[5] = factor;
|
|
|
|
dst->m[10] = (-(zFarPlane + zNearPlane)) * f_n;
|
|
|
|
dst->m[11] = -1.0f;
|
|
|
|
dst->m[14] = -2.0f * zFarPlane * zNearPlane * f_n;
|
metal support for cocos2d-x (#19305)
* remove deprecated files
* remove some deprecated codes
* remove more deprecated codes
* remove ui deprecated codes
* remove more deprecated codes
* remove deprecated codes in ccmenuitem
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes
* remove more deprecated codes
* remove more deprecated codes
* remove vr related codes and ignore some modules
* remove allocator
* remove some config
* 【Feature】add back-end project file
* [Feature] add back-end file
* add pipeline descriptor and shader cache
* [Feature] support sprite for backend
* [Feature] remove unneeded code
* [Feature] according to es2.0 spec, you must use clamp-to-edge as texture wrap mode, and no mipmapping for non-power-of-two texture
* [Feature] set texture wrap mode to clamp-to-edge, and no mipmapping for non-power-of-two texture
* [Feature] remove macro define to .cpp file
* [Feature] add log info
* [Feature] add PipelineDescriptor for TriangleCommand
* [Feature] add PipelineDescriptor object as member of TriangleCommand
* [Feature] add getPipelineDescriptor method
* add renderbackend
* complete pipeline descriptor
* [Feature] add viewport in RenderCommand
* set viewport when rendrering
* [Feature] occur error when using RendererBackend, to be fixed.
* a workaround to fix black screen on macOS 10.14 (#19090)
* add rendererbackend init function
* fix typo
* [Feature] modify testFile
* [BugFix] modify shader path
* [Feature] set default viewport
* fix projection
* [Feature] modify log info
* [BugFix] change viewport data type to int
* [BugFix] add BindGroup to PipelienDescriptor
* [BugFix] change a_position to vec3 in sprite.vert
* [BugFix] set vertexLayout according to V3F_C4B_T2F structure
* [Feature] revert a_position to vec4
* [Feature] renderer should not use gl codes directly
* [Feature] it's better not use default value parameter
* fix depth test setting
* rendererbackend -> renderer
* clear color and depth at begin
* add metal backend
* metal support normalized attribute
* simplify codes
* update external
* add render pass desctriptor in pipeline descriptor
* fix warnings
* fix crash and memeory leak
* refactor Texture2D
* put pipeline descriptor into render command
* simplify codes
* [Feature] update Sprite
* fix crash when closing app
* [Feature] update SpriteBatchNode and TextureAtlas
* support render texture(not finish)
* [Feature] remove unused code
* make tests work on mac
* fix download-deps path error
* make tests work on iOS
* [Feature] support ttf under normal label effect
* refactor triangle command processing
* let renderer handle more common commands
* refactor backend
* make render texture work
* [Feature] refactor backend for GL
* [Feature]Renaming to make it easy to understand
* [Feature] change warp mode to CLAMP_TO_EDGE
* fix ghost
* simplify visit render queue logic
* support progress timer without rial mode
* support partcile system
* Feature/update label (#149)
* [BugFix] fix compile error
* [Feature] support outline effect in ios
* [Feature] add shader file
* [BugFix] fix begin and end RenderPass
* [Feature] update CustomCommand
* [Feature] revert project.pbxproj
* [Feature] simplify codes
* [BugFix] pack AI88 to RGBA8888 only when outline enable
* [Feature] support shadow effect in Label
* [Feature] support BMFont
* [Feature] support glow effect
* [Feature] simplify shader files
* LabelAtlas work
* handle blend function correctly
* support tile map
* don't share buffer in metal
* alloc buffer size as needed
* support more tilemap
* Merge branch 'minggo/metal-support' into feature/updateLabel
* minggo/metal-support:
support tile map
handle blend function correctly
LabelAtlas work
Feature/update label (#149)
support partcile system
# Conflicts:
# cocos/2d/CCLabel.cpp
# cocos/2d/CCSprite.cpp
# cocos/2d/CCSpriteBatchNode.cpp
# cocos/renderer/CCQuadCommand.cpp
# cocos/renderer/CCQuadCommand.h
* render texture work without saving file
* use global viewport
* grid3d works
* remove grabber
* tiled3d works
* [BugFix] fix label bug
* [Feature] add updateSubData for buffer
* [Feature] remove setVertexCount
* support depth test
* add callback command
* [Feature] add UITest
* [Feature] update UITest
* [Feature] remove unneeded codes
* fix custom command issue
* fix layer color blend issue
* [BugFix] fix iOS compile error
* [Feature] remove unneeded codes
* [Feature] fix updateVertexBuffer
* layerradial works
* add draw test back
* fix batch issue
* fix compiling error
* [BugFix] support ETC1
* [BugFix] get the correct pipelineDescriptor
* [BugFix] skip draw when backendTexture nullptr
* clipping node support
* [Feature] add shader files
* fix stencil issue in metal
* [Feature] update UILayoutTest
* [BugFix] skip drawing when vertexCount is zero
* refactor renderer
* add set global z order for stencil manager commands
* fix warnings caused by type
* remove viewport in render command
* [Feature] fix warnings caused by type
* [BugFix] clear vertexCount and indexCount for CustomComand when needed
* [Feature] update clear for CustomCommand
* ios use metal
* fix viewport issue
* fix LayerColorGradient crash
* [cmake] transport to android and windows (#160)
* save point 1
* compile on windows
* run on android
* revert useless change
* android set CC_ENABLE_CACHE_TEXTURE_DATA to 1
* add initGlew
* fix android crash
* add TODO new-renderer
* review update
* revert onGLFWWindowPosCallback
* fix android compiling error
* Impl progress radial (#162)
* progresstimer add radial impl
* default drawType to element
* dec invoke times of createVertexBuffer (#163)
* support depth/stencil format for gl backend
* simplify progress timer codes
* support motionstreak, effect is wrong
* fix motionstreak issue
* [Feature] update Scissor Test (#161)
* [Feature] update Scissor Test
* [Feature] update ScissorTest
* [Feature] rename function
* [Feature] get constant reference if needed
* [Feature] show render status (#164)
* improve performance
* fix depth state
* fill error that triangle vertex/index number bigger than buffer
* fix compiline error in release mode
* fix buffer conflict between CPU and GPU on iOS/macOS
* Renderer refactor (#165)
* use one vertes/index buffer with opengl
* fix error on windows
* custom command support index format config
* CCLayer: compact vertex data structure
* update comment
* fix doc
* support fast tilemap
* pass index format instead
* fix some wrong effect
* fix render texture error
* fix texture per-element size
* fix texture format error
* BlendFunc type refactor, GLenum -> backend::BlendFactor (#167)
* BlendFunc use backend::BlendFactor as inner field
* update comments
* use int to replace GLenum
* update xcode project fiel
* rename to GLBlendConst
* add ccConstants.h
* update xcode project file
* update copyright
* remove primitive command
* remove CCPrimitive.cpp/.h
* remove deprecated files
* remove unneeded files
* remove multiple view support
* remove multiple view support
* remove the usage of frame buffer in camera
* director don't use frame buffer
* remove FrameBuffer
* remove BatchCommand
* add some api reference
* add physics2d back
* fix crash when close app on mac
* improve render texture
* fix rendertexture issue
* fix rendertexture issue
* simplify codes
* CMake support for mac & ios (#169)
* update cmake
* fix compile error
* update 3rd libs version
* remove CCThread.h/.cpp
* remove ccthread
* use audio engine to implement simple audio engine
* remove unneeded codes
* remove deprecated codes
* remove winrt macro
* remove CC_USE_WIC
* set partcile blend function in more elegant way
* remove unneeded codes
* remove unneeded codes
* cmake works on windows
* update project setting
* improve performance
* GLFloat -> float
* sync v3 cmake improvements into metal-support (#172)
* pick: modern cmake, compile definitions improvement (#19139)
* modern cmake, use target_compile_definitions partly
* simplify macro define, remove USE_*
* modern cmake, macro define
* add physics 2d macro define into ccConfig.h
* remove USE_CHIPMUNK macro in build.gradle
* remove CocosSelectModule.cmake
* shrink useless define
* simplify compile options config, re-add if necessary
* update external for tmp CI test
* un-quote target_compile_options value
* add "-g" parameter only when debug mode
* keep single build type when generator Xcode & VS projecy
* update external for tmp CI tes
* add static_cast<char>(-1), fix -Wc++11-narrowing
* simplify win32 compile define
* not modify code, only improve compile options
# Conflicts:
# .gitignore
# cmake/Modules/CocosConfigDepend.cmake
# cocos/CMakeLists.txt
# external/config.json
# tests/cpp-tests/CMakeLists.txt
* modern cmake, improve cmake_compiler_flags (#19145)
* cmake_compiler_flags
* Fix typo
* Fix typo2
* Remove chanages from Android.mk
* correct lua template cmake build (#19149)
* don't add -Wno-deprecated into jsb target
* correct lua template cmake build
* fix win32 lua template compile error
* prevent cmake in-source-build friendly (#19151)
* pick: Copy resources to "Resources/" on win32 like in linux configuration
* add "/Z7" for cpp-tests on windows
* [cmake] fix iOS xcode property setting failed (#19208)
* fix iOS xcode property setting failed
* use search_depend_libs_recursive at dlls collect
* fix typo
* [cmake] add find_host_library into iOS toolchain file (#19230)
* pick: [lua android] use luajit & template cmake update (#19239)
* increase cmake stability , remove tests/CMakeLists.txt (#19261)
* cmake win32 Precompiled header (#19273)
* Precompiled header
* Fix
* Precompiled header for cocos
* Precompiled header jscocos2d
* Fix for COCOS2D_DEBUG is always 1 on Android (#19291)
Related #19289
* little build fix, tests cpp-tests works on mac
* sync v3 build related codes into metal-support (#173)
* strict initialization for std::array
* remove proj.win32 project configs
* modern cmake, cmake_cleanup_remove_unused_variables (#19146)
* Switch travis CI to xenial (#19207)
* Switch travis CI to xenial
* Remove language: android
* Set language: cpp
* Fix java problem
* Update sdkmanager
* Fix sdkmanger
* next sdkmanager fix
* Remove xenial from android
* revert to sdk-tools-{system}-3859397
* Remove linux cmake install
* Update before-install.sh
* Update .travis.yml
* Simplify install-deps-linux.sh, tested on Ubuntu 16.04 (#19212)
* Simplify install-deps-linux.sh
* Cleanup
* pick: install ninja
* update cocos2d-console submodule
* for metal-support alpha release, we only test cpp
* add HelloCpp into project(Cocos2d-x) for tmp test
* update extenal metal-support-4
* update uniform setting
* [Feature] update BindGroup
* [Feature] empty-test
* [Feature] cpp-test
* [Feature] fix GL compiler error
* [Feature] fix GL crash
* [Feature] empty-test
* [Feature] cpp-tests
* [feature] improve frameRate
* [feature] fix opengl compile error
* [feature] fix opengl compile error
* [BugFix] fix compute maxLocation error
* [Feature] update setting unifrom
* [Feature] fix namespace
* [Feature] remove unneeded code
* [Bugfix] fix project file
* [Feature] update review
* [texture2d] impl texture format support (#175)
* texture update
* update
* update texture
* commit
* compile on windows
* ddd
* rename
* rename methods
* no crash
* save gl
* save
* save
* rename
* move out pixel format convert functions
* metal crash
* update
* update android
* support gles compressed texture format
* support more compress format
* add more conversion methods
* ss
* save
* update conversion methods
* add PVRTC format support
* reformat
* add marco linux
* fix GL marcro
* pvrtc supported only by ios 8.0+
* remove unused cmake
* revert change
* refactor Texture2D::initWithData
* fix conversion log
* refactor Texture2D::initWithData
* remove some OpenGL constants for PVRTC
* add todo
* fix typo
* AutoTest works on mac/iOS by disable part cases, sync v3 bug fix (#174)
* review cpp-tests, and fix part issues on start auto test
* sync png format fix: Node:Particle3D abnormal texture effects #19204
* fix cpp-tests SpritePolygon crash, wrong png format (#19170)
* fix wrong png convert format from sRGB to Gray
* erase plist index if all frames was erased
* test_A8.png have I8 format, fix it
* [CCSpriteCache] allow re-add plist & add testcase (#19175)
* allow re-add plist & add testcase
* remove comments/rename method/update testcase
* fix isSpriteFramesWithFileLoaded & add testcase
* remove used variable
* remove unused variable
* fix double free issues when js/lua-tests exit on iOS (#19236)
* disable part cases, AutoTest works without crash on mac
* update cocos2dx files json, to test cocos new next
* fix spritecache plist parsing issue (#19269)
* [linux] Fix FileUtils::getContents with folder (#19157)
* fix FileUtils::getContents on linux/mac
* use stat.st_mode
* simplify
* [CCFileUtils] win32 getFileSize (#19176)
* win32 getFileSize
* fix stat
* [cpp test-Android]20:FileUtils/2 change title (#19197)
* sync #19200
* sync #19231
* [android lua] improve performance of lua loader (#19234)
* [lua] improve performance of lua loader
* remove cache fix
* Revert "fix spritecache plist parsing issue (#19269)"
This reverts commit f3a85ece4307a7b90816c34489d1ed2c8fd11baf.
* remove win32 project files ref in template.json
* add metal framework lnk ref into cpp template
* test on iOS, and disable part cases
* alBufferData instead of alBufferDataStatic for small audio file on Apple (#19227)
* changes AudioCache to use alBufferData instead of alBufferDataStatic
(also makes test 19 faster to trigger openal bugs faster)
The original problem: CrashIfClientProvidedBogusAudioBufferList
https://github.com/cocos2d/cocos2d-x/issues/18948
is not happening anymore, but there's still a not very frequent issue
that makes OpenAL crash with a call stack like this.
AudioCache::readDataTask > alBufferData > CleanUpDeadBufferList
It happes more frequently when the device is "cold", which means after
half an hour of not using the device (locked).
I could not find the actual source code for iOS OpenAL, so I used the
macOS versions:
https://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp.auto.html
They seem to use CAGuard.h to make sure the dead buffer list
has no threading issues. I'm worried because the CAGuard code I found
has macos and win32 define but no iOS, so I'm not sure. I guess the
iOS version is different and has the guard.
I could not find a place in the code that's unprotected by the locks
except the InitializeBufferMap() which should not be called more than
once from cocos, and there's a workaround in AudioEngine-impl for it.
I reduced the occurence of the CleanUpDeadBufferList crash by moving
the guard in ~AudioCache to cover the alDeleteBuffers call.
* remove hack method "setTimeout" on audio
* AutoTest works on iOS
* support set ios deployment target for root project
* enable all texture2d cases, since Jiang have fixed
* add CCTextureUtils to xcode project file (#176)
* add leak cases for SpriteFrameCache (#177)
* re-add SpriteFrameCache cases
* update template file json
* Update SpriteFrameCacheTest.cpp
* fix compiling error
2019-01-18 15:08:25 +08:00
|
|
|
|
|
|
|
// https://metashapes.com/blog/opengl-metal-projection-matrix-problem/
|
|
|
|
#ifdef CC_USE_METAL
|
|
|
|
dst->m[10] = -(zFarPlane) * f_n;
|
|
|
|
dst->m[14] = -(zFarPlane * zNearPlane) * f_n;
|
|
|
|
#endif
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createOrthographic(float width, float height, float zNearPlane, float zFarPlane, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
float halfWidth = width / 2.0f;
|
|
|
|
float halfHeight = height / 2.0f;
|
|
|
|
createOrthographicOffCenter(-halfWidth, halfWidth, -halfHeight, halfHeight, zNearPlane, zFarPlane, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createOrthographicOffCenter(float left, float right, float bottom, float top,
|
|
|
|
float zNearPlane, float zFarPlane, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
GP_ASSERT(right != left);
|
|
|
|
GP_ASSERT(top != bottom);
|
|
|
|
GP_ASSERT(zFarPlane != zNearPlane);
|
|
|
|
|
|
|
|
memset(dst, 0, MATRIX_SIZE);
|
|
|
|
dst->m[0] = 2 / (right - left);
|
|
|
|
dst->m[5] = 2 / (top - bottom);
|
2014-04-10 17:02:36 +08:00
|
|
|
dst->m[10] = 2 / (zNearPlane - zFarPlane);
|
|
|
|
|
2014-04-02 10:33:07 +08:00
|
|
|
dst->m[12] = (left + right) / (left - right);
|
|
|
|
dst->m[13] = (top + bottom) / (bottom - top);
|
2014-04-10 17:09:48 +08:00
|
|
|
dst->m[14] = (zNearPlane + zFarPlane) / (zNearPlane - zFarPlane);
|
2014-04-02 10:33:07 +08:00
|
|
|
dst->m[15] = 1;
|
metal support for cocos2d-x (#19305)
* remove deprecated files
* remove some deprecated codes
* remove more deprecated codes
* remove ui deprecated codes
* remove more deprecated codes
* remove deprecated codes in ccmenuitem
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes
* remove more deprecated codes
* remove more deprecated codes
* remove vr related codes and ignore some modules
* remove allocator
* remove some config
* 【Feature】add back-end project file
* [Feature] add back-end file
* add pipeline descriptor and shader cache
* [Feature] support sprite for backend
* [Feature] remove unneeded code
* [Feature] according to es2.0 spec, you must use clamp-to-edge as texture wrap mode, and no mipmapping for non-power-of-two texture
* [Feature] set texture wrap mode to clamp-to-edge, and no mipmapping for non-power-of-two texture
* [Feature] remove macro define to .cpp file
* [Feature] add log info
* [Feature] add PipelineDescriptor for TriangleCommand
* [Feature] add PipelineDescriptor object as member of TriangleCommand
* [Feature] add getPipelineDescriptor method
* add renderbackend
* complete pipeline descriptor
* [Feature] add viewport in RenderCommand
* set viewport when rendrering
* [Feature] occur error when using RendererBackend, to be fixed.
* a workaround to fix black screen on macOS 10.14 (#19090)
* add rendererbackend init function
* fix typo
* [Feature] modify testFile
* [BugFix] modify shader path
* [Feature] set default viewport
* fix projection
* [Feature] modify log info
* [BugFix] change viewport data type to int
* [BugFix] add BindGroup to PipelienDescriptor
* [BugFix] change a_position to vec3 in sprite.vert
* [BugFix] set vertexLayout according to V3F_C4B_T2F structure
* [Feature] revert a_position to vec4
* [Feature] renderer should not use gl codes directly
* [Feature] it's better not use default value parameter
* fix depth test setting
* rendererbackend -> renderer
* clear color and depth at begin
* add metal backend
* metal support normalized attribute
* simplify codes
* update external
* add render pass desctriptor in pipeline descriptor
* fix warnings
* fix crash and memeory leak
* refactor Texture2D
* put pipeline descriptor into render command
* simplify codes
* [Feature] update Sprite
* fix crash when closing app
* [Feature] update SpriteBatchNode and TextureAtlas
* support render texture(not finish)
* [Feature] remove unused code
* make tests work on mac
* fix download-deps path error
* make tests work on iOS
* [Feature] support ttf under normal label effect
* refactor triangle command processing
* let renderer handle more common commands
* refactor backend
* make render texture work
* [Feature] refactor backend for GL
* [Feature]Renaming to make it easy to understand
* [Feature] change warp mode to CLAMP_TO_EDGE
* fix ghost
* simplify visit render queue logic
* support progress timer without rial mode
* support partcile system
* Feature/update label (#149)
* [BugFix] fix compile error
* [Feature] support outline effect in ios
* [Feature] add shader file
* [BugFix] fix begin and end RenderPass
* [Feature] update CustomCommand
* [Feature] revert project.pbxproj
* [Feature] simplify codes
* [BugFix] pack AI88 to RGBA8888 only when outline enable
* [Feature] support shadow effect in Label
* [Feature] support BMFont
* [Feature] support glow effect
* [Feature] simplify shader files
* LabelAtlas work
* handle blend function correctly
* support tile map
* don't share buffer in metal
* alloc buffer size as needed
* support more tilemap
* Merge branch 'minggo/metal-support' into feature/updateLabel
* minggo/metal-support:
support tile map
handle blend function correctly
LabelAtlas work
Feature/update label (#149)
support partcile system
# Conflicts:
# cocos/2d/CCLabel.cpp
# cocos/2d/CCSprite.cpp
# cocos/2d/CCSpriteBatchNode.cpp
# cocos/renderer/CCQuadCommand.cpp
# cocos/renderer/CCQuadCommand.h
* render texture work without saving file
* use global viewport
* grid3d works
* remove grabber
* tiled3d works
* [BugFix] fix label bug
* [Feature] add updateSubData for buffer
* [Feature] remove setVertexCount
* support depth test
* add callback command
* [Feature] add UITest
* [Feature] update UITest
* [Feature] remove unneeded codes
* fix custom command issue
* fix layer color blend issue
* [BugFix] fix iOS compile error
* [Feature] remove unneeded codes
* [Feature] fix updateVertexBuffer
* layerradial works
* add draw test back
* fix batch issue
* fix compiling error
* [BugFix] support ETC1
* [BugFix] get the correct pipelineDescriptor
* [BugFix] skip draw when backendTexture nullptr
* clipping node support
* [Feature] add shader files
* fix stencil issue in metal
* [Feature] update UILayoutTest
* [BugFix] skip drawing when vertexCount is zero
* refactor renderer
* add set global z order for stencil manager commands
* fix warnings caused by type
* remove viewport in render command
* [Feature] fix warnings caused by type
* [BugFix] clear vertexCount and indexCount for CustomComand when needed
* [Feature] update clear for CustomCommand
* ios use metal
* fix viewport issue
* fix LayerColorGradient crash
* [cmake] transport to android and windows (#160)
* save point 1
* compile on windows
* run on android
* revert useless change
* android set CC_ENABLE_CACHE_TEXTURE_DATA to 1
* add initGlew
* fix android crash
* add TODO new-renderer
* review update
* revert onGLFWWindowPosCallback
* fix android compiling error
* Impl progress radial (#162)
* progresstimer add radial impl
* default drawType to element
* dec invoke times of createVertexBuffer (#163)
* support depth/stencil format for gl backend
* simplify progress timer codes
* support motionstreak, effect is wrong
* fix motionstreak issue
* [Feature] update Scissor Test (#161)
* [Feature] update Scissor Test
* [Feature] update ScissorTest
* [Feature] rename function
* [Feature] get constant reference if needed
* [Feature] show render status (#164)
* improve performance
* fix depth state
* fill error that triangle vertex/index number bigger than buffer
* fix compiline error in release mode
* fix buffer conflict between CPU and GPU on iOS/macOS
* Renderer refactor (#165)
* use one vertes/index buffer with opengl
* fix error on windows
* custom command support index format config
* CCLayer: compact vertex data structure
* update comment
* fix doc
* support fast tilemap
* pass index format instead
* fix some wrong effect
* fix render texture error
* fix texture per-element size
* fix texture format error
* BlendFunc type refactor, GLenum -> backend::BlendFactor (#167)
* BlendFunc use backend::BlendFactor as inner field
* update comments
* use int to replace GLenum
* update xcode project fiel
* rename to GLBlendConst
* add ccConstants.h
* update xcode project file
* update copyright
* remove primitive command
* remove CCPrimitive.cpp/.h
* remove deprecated files
* remove unneeded files
* remove multiple view support
* remove multiple view support
* remove the usage of frame buffer in camera
* director don't use frame buffer
* remove FrameBuffer
* remove BatchCommand
* add some api reference
* add physics2d back
* fix crash when close app on mac
* improve render texture
* fix rendertexture issue
* fix rendertexture issue
* simplify codes
* CMake support for mac & ios (#169)
* update cmake
* fix compile error
* update 3rd libs version
* remove CCThread.h/.cpp
* remove ccthread
* use audio engine to implement simple audio engine
* remove unneeded codes
* remove deprecated codes
* remove winrt macro
* remove CC_USE_WIC
* set partcile blend function in more elegant way
* remove unneeded codes
* remove unneeded codes
* cmake works on windows
* update project setting
* improve performance
* GLFloat -> float
* sync v3 cmake improvements into metal-support (#172)
* pick: modern cmake, compile definitions improvement (#19139)
* modern cmake, use target_compile_definitions partly
* simplify macro define, remove USE_*
* modern cmake, macro define
* add physics 2d macro define into ccConfig.h
* remove USE_CHIPMUNK macro in build.gradle
* remove CocosSelectModule.cmake
* shrink useless define
* simplify compile options config, re-add if necessary
* update external for tmp CI test
* un-quote target_compile_options value
* add "-g" parameter only when debug mode
* keep single build type when generator Xcode & VS projecy
* update external for tmp CI tes
* add static_cast<char>(-1), fix -Wc++11-narrowing
* simplify win32 compile define
* not modify code, only improve compile options
# Conflicts:
# .gitignore
# cmake/Modules/CocosConfigDepend.cmake
# cocos/CMakeLists.txt
# external/config.json
# tests/cpp-tests/CMakeLists.txt
* modern cmake, improve cmake_compiler_flags (#19145)
* cmake_compiler_flags
* Fix typo
* Fix typo2
* Remove chanages from Android.mk
* correct lua template cmake build (#19149)
* don't add -Wno-deprecated into jsb target
* correct lua template cmake build
* fix win32 lua template compile error
* prevent cmake in-source-build friendly (#19151)
* pick: Copy resources to "Resources/" on win32 like in linux configuration
* add "/Z7" for cpp-tests on windows
* [cmake] fix iOS xcode property setting failed (#19208)
* fix iOS xcode property setting failed
* use search_depend_libs_recursive at dlls collect
* fix typo
* [cmake] add find_host_library into iOS toolchain file (#19230)
* pick: [lua android] use luajit & template cmake update (#19239)
* increase cmake stability , remove tests/CMakeLists.txt (#19261)
* cmake win32 Precompiled header (#19273)
* Precompiled header
* Fix
* Precompiled header for cocos
* Precompiled header jscocos2d
* Fix for COCOS2D_DEBUG is always 1 on Android (#19291)
Related #19289
* little build fix, tests cpp-tests works on mac
* sync v3 build related codes into metal-support (#173)
* strict initialization for std::array
* remove proj.win32 project configs
* modern cmake, cmake_cleanup_remove_unused_variables (#19146)
* Switch travis CI to xenial (#19207)
* Switch travis CI to xenial
* Remove language: android
* Set language: cpp
* Fix java problem
* Update sdkmanager
* Fix sdkmanger
* next sdkmanager fix
* Remove xenial from android
* revert to sdk-tools-{system}-3859397
* Remove linux cmake install
* Update before-install.sh
* Update .travis.yml
* Simplify install-deps-linux.sh, tested on Ubuntu 16.04 (#19212)
* Simplify install-deps-linux.sh
* Cleanup
* pick: install ninja
* update cocos2d-console submodule
* for metal-support alpha release, we only test cpp
* add HelloCpp into project(Cocos2d-x) for tmp test
* update extenal metal-support-4
* update uniform setting
* [Feature] update BindGroup
* [Feature] empty-test
* [Feature] cpp-test
* [Feature] fix GL compiler error
* [Feature] fix GL crash
* [Feature] empty-test
* [Feature] cpp-tests
* [feature] improve frameRate
* [feature] fix opengl compile error
* [feature] fix opengl compile error
* [BugFix] fix compute maxLocation error
* [Feature] update setting unifrom
* [Feature] fix namespace
* [Feature] remove unneeded code
* [Bugfix] fix project file
* [Feature] update review
* [texture2d] impl texture format support (#175)
* texture update
* update
* update texture
* commit
* compile on windows
* ddd
* rename
* rename methods
* no crash
* save gl
* save
* save
* rename
* move out pixel format convert functions
* metal crash
* update
* update android
* support gles compressed texture format
* support more compress format
* add more conversion methods
* ss
* save
* update conversion methods
* add PVRTC format support
* reformat
* add marco linux
* fix GL marcro
* pvrtc supported only by ios 8.0+
* remove unused cmake
* revert change
* refactor Texture2D::initWithData
* fix conversion log
* refactor Texture2D::initWithData
* remove some OpenGL constants for PVRTC
* add todo
* fix typo
* AutoTest works on mac/iOS by disable part cases, sync v3 bug fix (#174)
* review cpp-tests, and fix part issues on start auto test
* sync png format fix: Node:Particle3D abnormal texture effects #19204
* fix cpp-tests SpritePolygon crash, wrong png format (#19170)
* fix wrong png convert format from sRGB to Gray
* erase plist index if all frames was erased
* test_A8.png have I8 format, fix it
* [CCSpriteCache] allow re-add plist & add testcase (#19175)
* allow re-add plist & add testcase
* remove comments/rename method/update testcase
* fix isSpriteFramesWithFileLoaded & add testcase
* remove used variable
* remove unused variable
* fix double free issues when js/lua-tests exit on iOS (#19236)
* disable part cases, AutoTest works without crash on mac
* update cocos2dx files json, to test cocos new next
* fix spritecache plist parsing issue (#19269)
* [linux] Fix FileUtils::getContents with folder (#19157)
* fix FileUtils::getContents on linux/mac
* use stat.st_mode
* simplify
* [CCFileUtils] win32 getFileSize (#19176)
* win32 getFileSize
* fix stat
* [cpp test-Android]20:FileUtils/2 change title (#19197)
* sync #19200
* sync #19231
* [android lua] improve performance of lua loader (#19234)
* [lua] improve performance of lua loader
* remove cache fix
* Revert "fix spritecache plist parsing issue (#19269)"
This reverts commit f3a85ece4307a7b90816c34489d1ed2c8fd11baf.
* remove win32 project files ref in template.json
* add metal framework lnk ref into cpp template
* test on iOS, and disable part cases
* alBufferData instead of alBufferDataStatic for small audio file on Apple (#19227)
* changes AudioCache to use alBufferData instead of alBufferDataStatic
(also makes test 19 faster to trigger openal bugs faster)
The original problem: CrashIfClientProvidedBogusAudioBufferList
https://github.com/cocos2d/cocos2d-x/issues/18948
is not happening anymore, but there's still a not very frequent issue
that makes OpenAL crash with a call stack like this.
AudioCache::readDataTask > alBufferData > CleanUpDeadBufferList
It happes more frequently when the device is "cold", which means after
half an hour of not using the device (locked).
I could not find the actual source code for iOS OpenAL, so I used the
macOS versions:
https://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp.auto.html
They seem to use CAGuard.h to make sure the dead buffer list
has no threading issues. I'm worried because the CAGuard code I found
has macos and win32 define but no iOS, so I'm not sure. I guess the
iOS version is different and has the guard.
I could not find a place in the code that's unprotected by the locks
except the InitializeBufferMap() which should not be called more than
once from cocos, and there's a workaround in AudioEngine-impl for it.
I reduced the occurence of the CleanUpDeadBufferList crash by moving
the guard in ~AudioCache to cover the alDeleteBuffers call.
* remove hack method "setTimeout" on audio
* AutoTest works on iOS
* support set ios deployment target for root project
* enable all texture2d cases, since Jiang have fixed
* add CCTextureUtils to xcode project file (#176)
* add leak cases for SpriteFrameCache (#177)
* re-add SpriteFrameCache cases
* update template file json
* Update SpriteFrameCacheTest.cpp
* fix compiling error
2019-01-18 15:08:25 +08:00
|
|
|
|
|
|
|
//// https://metashapes.com/blog/opengl-metal-projection-matrix-problem/
|
|
|
|
#ifdef CC_USE_METAL
|
|
|
|
dst->m[10] = 1 / (zNearPlane - zFarPlane);
|
|
|
|
dst->m[14] = zNearPlane / (zNearPlane - zFarPlane);
|
|
|
|
#endif
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createBillboard(const Vec3& objectPosition, const Vec3& cameraPosition,
|
|
|
|
const Vec3& cameraUpVector, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
createBillboardHelper(objectPosition, cameraPosition, cameraUpVector, nullptr, dst);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createBillboard(const Vec3& objectPosition, const Vec3& cameraPosition,
|
|
|
|
const Vec3& cameraUpVector, const Vec3& cameraForwardVector,
|
|
|
|
Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
createBillboardHelper(objectPosition, cameraPosition, cameraUpVector, &cameraForwardVector, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createBillboardHelper(const Vec3& objectPosition, const Vec3& cameraPosition,
|
|
|
|
const Vec3& cameraUpVector, const Vec3* cameraForwardVector,
|
|
|
|
Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 delta(objectPosition, cameraPosition);
|
2014-04-02 10:33:07 +08:00
|
|
|
bool isSufficientDelta = delta.lengthSquared() > MATH_EPSILON;
|
|
|
|
|
|
|
|
dst->setIdentity();
|
|
|
|
dst->m[3] = objectPosition.x;
|
|
|
|
dst->m[7] = objectPosition.y;
|
|
|
|
dst->m[11] = objectPosition.z;
|
|
|
|
|
|
|
|
// As per the contracts for the 2 variants of createBillboard, we need
|
|
|
|
// either a safe default or a sufficient distance between object and camera.
|
|
|
|
if (cameraForwardVector || isSufficientDelta)
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 target = isSufficientDelta ? cameraPosition : (objectPosition - *cameraForwardVector);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
// A billboard is the inverse of a lookAt rotation
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 lookAt;
|
2014-04-02 10:33:07 +08:00
|
|
|
createLookAt(objectPosition, target, cameraUpVector, &lookAt);
|
|
|
|
dst->m[0] = lookAt.m[0];
|
|
|
|
dst->m[1] = lookAt.m[4];
|
|
|
|
dst->m[2] = lookAt.m[8];
|
|
|
|
dst->m[4] = lookAt.m[1];
|
|
|
|
dst->m[5] = lookAt.m[5];
|
|
|
|
dst->m[6] = lookAt.m[9];
|
|
|
|
dst->m[8] = lookAt.m[2];
|
|
|
|
dst->m[9] = lookAt.m[6];
|
|
|
|
dst->m[10] = lookAt.m[10];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
// void Mat4::createReflection(const Plane& plane, Mat4* dst)
|
2014-04-02 11:21:48 +08:00
|
|
|
// {
|
2014-05-15 01:07:09 +08:00
|
|
|
// Vec3 normal(plane.getNormal());
|
2014-04-02 11:21:48 +08:00
|
|
|
// float k = -2.0f * plane.getDistance();
|
|
|
|
|
|
|
|
// dst->setIdentity();
|
|
|
|
|
|
|
|
// dst->m[0] -= 2.0f * normal.x * normal.x;
|
|
|
|
// dst->m[5] -= 2.0f * normal.y * normal.y;
|
|
|
|
// dst->m[10] -= 2.0f * normal.z * normal.z;
|
|
|
|
// dst->m[1] = dst->m[4] = -2.0f * normal.x * normal.y;
|
|
|
|
// dst->m[2] = dst->m[8] = -2.0f * normal.x * normal.z;
|
|
|
|
// dst->m[6] = dst->m[9] = -2.0f * normal.y * normal.z;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2014-04-02 11:21:48 +08:00
|
|
|
// dst->m[3] = k * normal.x;
|
|
|
|
// dst->m[7] = k * normal.y;
|
|
|
|
// dst->m[11] = k * normal.z;
|
|
|
|
// }
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createScale(const Vec3& scale, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
2014-05-14 01:26:25 +08:00
|
|
|
memcpy(dst, &IDENTITY, MATRIX_SIZE);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
dst->m[0] = scale.x;
|
|
|
|
dst->m[5] = scale.y;
|
|
|
|
dst->m[10] = scale.z;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createScale(float xScale, float yScale, float zScale, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
2014-05-14 01:26:25 +08:00
|
|
|
memcpy(dst, &IDENTITY, MATRIX_SIZE);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
dst->m[0] = xScale;
|
|
|
|
dst->m[5] = yScale;
|
|
|
|
dst->m[10] = zScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createRotation(const Quaternion& q, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
|
|
|
float x2 = q.x + q.x;
|
|
|
|
float y2 = q.y + q.y;
|
|
|
|
float z2 = q.z + q.z;
|
|
|
|
|
|
|
|
float xx2 = q.x * x2;
|
|
|
|
float yy2 = q.y * y2;
|
|
|
|
float zz2 = q.z * z2;
|
|
|
|
float xy2 = q.x * y2;
|
|
|
|
float xz2 = q.x * z2;
|
|
|
|
float yz2 = q.y * z2;
|
|
|
|
float wx2 = q.w * x2;
|
|
|
|
float wy2 = q.w * y2;
|
|
|
|
float wz2 = q.w * z2;
|
|
|
|
|
|
|
|
dst->m[0] = 1.0f - yy2 - zz2;
|
|
|
|
dst->m[1] = xy2 + wz2;
|
|
|
|
dst->m[2] = xz2 - wy2;
|
|
|
|
dst->m[3] = 0.0f;
|
|
|
|
|
|
|
|
dst->m[4] = xy2 - wz2;
|
|
|
|
dst->m[5] = 1.0f - xx2 - zz2;
|
|
|
|
dst->m[6] = yz2 + wx2;
|
|
|
|
dst->m[7] = 0.0f;
|
|
|
|
|
|
|
|
dst->m[8] = xz2 + wy2;
|
|
|
|
dst->m[9] = yz2 - wx2;
|
|
|
|
dst->m[10] = 1.0f - xx2 - yy2;
|
|
|
|
dst->m[11] = 0.0f;
|
|
|
|
|
|
|
|
dst->m[12] = 0.0f;
|
|
|
|
dst->m[13] = 0.0f;
|
|
|
|
dst->m[14] = 0.0f;
|
|
|
|
dst->m[15] = 1.0f;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createRotation(const Vec3& axis, float angle, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
|
|
|
float x = axis.x;
|
|
|
|
float y = axis.y;
|
|
|
|
float z = axis.z;
|
|
|
|
|
|
|
|
// Make sure the input axis is normalized.
|
|
|
|
float n = x*x + y*y + z*z;
|
|
|
|
if (n != 1.0f)
|
|
|
|
{
|
|
|
|
// Not normalized.
|
2016-07-28 05:49:41 +08:00
|
|
|
n = std::sqrt(n);
|
2014-04-02 10:33:07 +08:00
|
|
|
// Prevent divide too close to zero.
|
|
|
|
if (n > 0.000001f)
|
|
|
|
{
|
|
|
|
n = 1.0f / n;
|
|
|
|
x *= n;
|
|
|
|
y *= n;
|
|
|
|
z *= n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 05:49:41 +08:00
|
|
|
float c = std::cos(angle);
|
|
|
|
float s = std::sin(angle);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
float t = 1.0f - c;
|
|
|
|
float tx = t * x;
|
|
|
|
float ty = t * y;
|
|
|
|
float tz = t * z;
|
|
|
|
float txy = tx * y;
|
|
|
|
float txz = tx * z;
|
|
|
|
float tyz = ty * z;
|
|
|
|
float sx = s * x;
|
|
|
|
float sy = s * y;
|
|
|
|
float sz = s * z;
|
|
|
|
|
|
|
|
dst->m[0] = c + tx*x;
|
|
|
|
dst->m[1] = txy + sz;
|
|
|
|
dst->m[2] = txz - sy;
|
|
|
|
dst->m[3] = 0.0f;
|
|
|
|
|
|
|
|
dst->m[4] = txy - sz;
|
|
|
|
dst->m[5] = c + ty*y;
|
|
|
|
dst->m[6] = tyz + sx;
|
|
|
|
dst->m[7] = 0.0f;
|
|
|
|
|
|
|
|
dst->m[8] = txz + sy;
|
|
|
|
dst->m[9] = tyz - sx;
|
|
|
|
dst->m[10] = c + tz*z;
|
|
|
|
dst->m[11] = 0.0f;
|
|
|
|
|
|
|
|
dst->m[12] = 0.0f;
|
|
|
|
dst->m[13] = 0.0f;
|
|
|
|
dst->m[14] = 0.0f;
|
|
|
|
dst->m[15] = 1.0f;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createRotationX(float angle, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
2014-05-14 01:26:25 +08:00
|
|
|
memcpy(dst, &IDENTITY, MATRIX_SIZE);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2016-07-28 05:49:41 +08:00
|
|
|
float c = std::cos(angle);
|
|
|
|
float s = std::sin(angle);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
dst->m[5] = c;
|
|
|
|
dst->m[6] = s;
|
|
|
|
dst->m[9] = -s;
|
|
|
|
dst->m[10] = c;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createRotationY(float angle, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
2014-05-14 01:26:25 +08:00
|
|
|
memcpy(dst, &IDENTITY, MATRIX_SIZE);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2016-07-28 05:49:41 +08:00
|
|
|
float c = std::cos(angle);
|
|
|
|
float s = std::sin(angle);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
dst->m[0] = c;
|
|
|
|
dst->m[2] = -s;
|
|
|
|
dst->m[8] = s;
|
|
|
|
dst->m[10] = c;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createRotationZ(float angle, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
2014-05-14 01:26:25 +08:00
|
|
|
memcpy(dst, &IDENTITY, MATRIX_SIZE);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2016-07-28 05:49:41 +08:00
|
|
|
float c = std::cos(angle);
|
|
|
|
float s = std::sin(angle);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
dst->m[0] = c;
|
|
|
|
dst->m[1] = s;
|
|
|
|
dst->m[4] = -s;
|
|
|
|
dst->m[5] = c;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createTranslation(const Vec3& translation, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
2014-05-14 01:26:25 +08:00
|
|
|
memcpy(dst, &IDENTITY, MATRIX_SIZE);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
dst->m[12] = translation.x;
|
|
|
|
dst->m[13] = translation.y;
|
|
|
|
dst->m[14] = translation.z;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::createTranslation(float xTranslation, float yTranslation, float zTranslation, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
2014-05-14 01:26:25 +08:00
|
|
|
memcpy(dst, &IDENTITY, MATRIX_SIZE);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
dst->m[12] = xTranslation;
|
|
|
|
dst->m[13] = yTranslation;
|
|
|
|
dst->m[14] = zTranslation;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::add(float scalar)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
add(scalar, this);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::add(float scalar, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
2014-09-09 23:01:37 +08:00
|
|
|
#ifdef __SSE__
|
2014-09-08 20:58:59 +08:00
|
|
|
MathUtil::addMatrix(col, scalar, dst->col);
|
|
|
|
#else
|
2014-04-02 10:33:07 +08:00
|
|
|
MathUtil::addMatrix(m, scalar, dst->m);
|
2014-09-08 20:58:59 +08:00
|
|
|
#endif
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::add(const Mat4& mat)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-04-16 10:58:34 +08:00
|
|
|
add(*this, mat, this);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::add(const Mat4& m1, const Mat4& m2, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
2014-09-09 23:01:37 +08:00
|
|
|
#ifdef __SSE__
|
2014-09-08 20:58:59 +08:00
|
|
|
MathUtil::addMatrix(m1.col, m2.col, dst->col);
|
|
|
|
#else
|
2014-04-02 10:33:07 +08:00
|
|
|
MathUtil::addMatrix(m1.m, m2.m, dst->m);
|
2014-09-08 20:58:59 +08:00
|
|
|
#endif
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool Mat4::decompose(Vec3* scale, Quaternion* rotation, Vec3* translation) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
if (translation)
|
|
|
|
{
|
|
|
|
// Extract the translation.
|
|
|
|
translation->x = m[12];
|
|
|
|
translation->y = m[13];
|
|
|
|
translation->z = m[14];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing left to do.
|
2014-07-10 00:45:27 +08:00
|
|
|
if (scale == nullptr && rotation == nullptr)
|
2014-04-02 10:33:07 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Extract the scale.
|
|
|
|
// This is simply the length of each axis (row/column) in the matrix.
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 xaxis(m[0], m[1], m[2]);
|
2014-04-02 10:33:07 +08:00
|
|
|
float scaleX = xaxis.length();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 yaxis(m[4], m[5], m[6]);
|
2014-04-02 10:33:07 +08:00
|
|
|
float scaleY = yaxis.length();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 zaxis(m[8], m[9], m[10]);
|
2014-04-02 10:33:07 +08:00
|
|
|
float scaleZ = zaxis.length();
|
|
|
|
|
|
|
|
// Determine if we have a negative scale (true if determinant is less than zero).
|
|
|
|
// In this case, we simply negate a single axis of the scale.
|
|
|
|
float det = determinant();
|
|
|
|
if (det < 0)
|
|
|
|
scaleZ = -scaleZ;
|
|
|
|
|
|
|
|
if (scale)
|
|
|
|
{
|
|
|
|
scale->x = scaleX;
|
|
|
|
scale->y = scaleY;
|
|
|
|
scale->z = scaleZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing left to do.
|
2014-07-10 00:45:27 +08:00
|
|
|
if (rotation == nullptr)
|
2014-04-02 10:33:07 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Scale too close to zero, can't decompose rotation.
|
2016-07-28 05:49:41 +08:00
|
|
|
if (scaleX < MATH_TOLERANCE || scaleY < MATH_TOLERANCE || std::abs(scaleZ) < MATH_TOLERANCE)
|
2014-04-02 10:33:07 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
float rn;
|
|
|
|
|
|
|
|
// Factor the scale out of the matrix axes.
|
|
|
|
rn = 1.0f / scaleX;
|
|
|
|
xaxis.x *= rn;
|
|
|
|
xaxis.y *= rn;
|
|
|
|
xaxis.z *= rn;
|
|
|
|
|
|
|
|
rn = 1.0f / scaleY;
|
|
|
|
yaxis.x *= rn;
|
|
|
|
yaxis.y *= rn;
|
|
|
|
yaxis.z *= rn;
|
|
|
|
|
|
|
|
rn = 1.0f / scaleZ;
|
|
|
|
zaxis.x *= rn;
|
|
|
|
zaxis.y *= rn;
|
|
|
|
zaxis.z *= rn;
|
|
|
|
|
|
|
|
// Now calculate the rotation from the resulting matrix (axes).
|
|
|
|
float trace = xaxis.x + yaxis.y + zaxis.z + 1.0f;
|
|
|
|
|
|
|
|
if (trace > MATH_EPSILON)
|
|
|
|
{
|
2016-07-28 05:49:41 +08:00
|
|
|
float s = 0.5f / std::sqrt(trace);
|
2014-04-02 10:33:07 +08:00
|
|
|
rotation->w = 0.25f / s;
|
|
|
|
rotation->x = (yaxis.z - zaxis.y) * s;
|
|
|
|
rotation->y = (zaxis.x - xaxis.z) * s;
|
|
|
|
rotation->z = (xaxis.y - yaxis.x) * s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Note: since xaxis, yaxis, and zaxis are normalized,
|
|
|
|
// we will never divide by zero in the code below.
|
|
|
|
if (xaxis.x > yaxis.y && xaxis.x > zaxis.z)
|
|
|
|
{
|
2016-07-28 05:49:41 +08:00
|
|
|
float s = 0.5f / std::sqrt(1.0f + xaxis.x - yaxis.y - zaxis.z);
|
2014-04-02 10:33:07 +08:00
|
|
|
rotation->w = (yaxis.z - zaxis.y) * s;
|
|
|
|
rotation->x = 0.25f / s;
|
|
|
|
rotation->y = (yaxis.x + xaxis.y) * s;
|
|
|
|
rotation->z = (zaxis.x + xaxis.z) * s;
|
|
|
|
}
|
|
|
|
else if (yaxis.y > zaxis.z)
|
|
|
|
{
|
2016-07-28 05:49:41 +08:00
|
|
|
float s = 0.5f / std::sqrt(1.0f + yaxis.y - xaxis.x - zaxis.z);
|
2014-04-02 10:33:07 +08:00
|
|
|
rotation->w = (zaxis.x - xaxis.z) * s;
|
|
|
|
rotation->x = (yaxis.x + xaxis.y) * s;
|
|
|
|
rotation->y = 0.25f / s;
|
|
|
|
rotation->z = (zaxis.y + yaxis.z) * s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-28 05:49:41 +08:00
|
|
|
float s = 0.5f / std::sqrt(1.0f + zaxis.z - xaxis.x - yaxis.y);
|
2014-04-02 10:33:07 +08:00
|
|
|
rotation->w = (xaxis.y - yaxis.x ) * s;
|
|
|
|
rotation->x = (zaxis.x + xaxis.z ) * s;
|
|
|
|
rotation->y = (zaxis.y + yaxis.z ) * s;
|
|
|
|
rotation->z = 0.25f / s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
float Mat4::determinant() const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
float a0 = m[0] * m[5] - m[1] * m[4];
|
|
|
|
float a1 = m[0] * m[6] - m[2] * m[4];
|
|
|
|
float a2 = m[0] * m[7] - m[3] * m[4];
|
|
|
|
float a3 = m[1] * m[6] - m[2] * m[5];
|
|
|
|
float a4 = m[1] * m[7] - m[3] * m[5];
|
|
|
|
float a5 = m[2] * m[7] - m[3] * m[6];
|
|
|
|
float b0 = m[8] * m[13] - m[9] * m[12];
|
|
|
|
float b1 = m[8] * m[14] - m[10] * m[12];
|
|
|
|
float b2 = m[8] * m[15] - m[11] * m[12];
|
|
|
|
float b3 = m[9] * m[14] - m[10] * m[13];
|
|
|
|
float b4 = m[9] * m[15] - m[11] * m[13];
|
|
|
|
float b5 = m[10] * m[15] - m[11] * m[14];
|
|
|
|
|
|
|
|
// Calculate the determinant.
|
|
|
|
return (a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::getScale(Vec3* scale) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
decompose(scale, nullptr, nullptr);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool Mat4::getRotation(Quaternion* rotation) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
return decompose(nullptr, rotation, nullptr);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::getTranslation(Vec3* translation) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
decompose(nullptr, nullptr, translation);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::getUpVector(Vec3* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
|
|
|
dst->x = m[4];
|
|
|
|
dst->y = m[5];
|
|
|
|
dst->z = m[6];
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::getDownVector(Vec3* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
|
|
|
dst->x = -m[4];
|
|
|
|
dst->y = -m[5];
|
|
|
|
dst->z = -m[6];
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::getLeftVector(Vec3* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
|
|
|
dst->x = -m[0];
|
|
|
|
dst->y = -m[1];
|
|
|
|
dst->z = -m[2];
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::getRightVector(Vec3* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
|
|
|
dst->x = m[0];
|
|
|
|
dst->y = m[1];
|
|
|
|
dst->z = m[2];
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::getForwardVector(Vec3* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
|
|
|
dst->x = -m[8];
|
|
|
|
dst->y = -m[9];
|
|
|
|
dst->z = -m[10];
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::getBackVector(Vec3* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
|
|
|
dst->x = m[8];
|
|
|
|
dst->y = m[9];
|
|
|
|
dst->z = m[10];
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 Mat4::getInversed() const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 mat(*this);
|
2014-05-01 01:59:36 +08:00
|
|
|
mat.inverse();
|
|
|
|
return mat;
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool Mat4::inverse()
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
float a0 = m[0] * m[5] - m[1] * m[4];
|
|
|
|
float a1 = m[0] * m[6] - m[2] * m[4];
|
|
|
|
float a2 = m[0] * m[7] - m[3] * m[4];
|
|
|
|
float a3 = m[1] * m[6] - m[2] * m[5];
|
|
|
|
float a4 = m[1] * m[7] - m[3] * m[5];
|
|
|
|
float a5 = m[2] * m[7] - m[3] * m[6];
|
|
|
|
float b0 = m[8] * m[13] - m[9] * m[12];
|
|
|
|
float b1 = m[8] * m[14] - m[10] * m[12];
|
|
|
|
float b2 = m[8] * m[15] - m[11] * m[12];
|
|
|
|
float b3 = m[9] * m[14] - m[10] * m[13];
|
|
|
|
float b4 = m[9] * m[15] - m[11] * m[13];
|
|
|
|
float b5 = m[10] * m[15] - m[11] * m[14];
|
|
|
|
|
|
|
|
// Calculate the determinant.
|
|
|
|
float det = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
|
|
|
|
|
|
|
|
// Close to zero, can't invert.
|
2016-07-28 05:49:41 +08:00
|
|
|
if (std::abs(det) <= MATH_TOLERANCE)
|
2014-04-02 10:33:07 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Support the case where m == dst.
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 inverse;
|
2014-04-02 10:33:07 +08:00
|
|
|
inverse.m[0] = m[5] * b5 - m[6] * b4 + m[7] * b3;
|
|
|
|
inverse.m[1] = -m[1] * b5 + m[2] * b4 - m[3] * b3;
|
|
|
|
inverse.m[2] = m[13] * a5 - m[14] * a4 + m[15] * a3;
|
|
|
|
inverse.m[3] = -m[9] * a5 + m[10] * a4 - m[11] * a3;
|
|
|
|
|
|
|
|
inverse.m[4] = -m[4] * b5 + m[6] * b2 - m[7] * b1;
|
|
|
|
inverse.m[5] = m[0] * b5 - m[2] * b2 + m[3] * b1;
|
|
|
|
inverse.m[6] = -m[12] * a5 + m[14] * a2 - m[15] * a1;
|
|
|
|
inverse.m[7] = m[8] * a5 - m[10] * a2 + m[11] * a1;
|
|
|
|
|
|
|
|
inverse.m[8] = m[4] * b4 - m[5] * b2 + m[7] * b0;
|
|
|
|
inverse.m[9] = -m[0] * b4 + m[1] * b2 - m[3] * b0;
|
|
|
|
inverse.m[10] = m[12] * a4 - m[13] * a2 + m[15] * a0;
|
|
|
|
inverse.m[11] = -m[8] * a4 + m[9] * a2 - m[11] * a0;
|
|
|
|
|
|
|
|
inverse.m[12] = -m[4] * b3 + m[5] * b1 - m[6] * b0;
|
|
|
|
inverse.m[13] = m[0] * b3 - m[1] * b1 + m[2] * b0;
|
|
|
|
inverse.m[14] = -m[12] * a3 + m[13] * a1 - m[14] * a0;
|
|
|
|
inverse.m[15] = m[8] * a3 - m[9] * a1 + m[10] * a0;
|
|
|
|
|
2014-05-01 01:59:36 +08:00
|
|
|
multiply(inverse, 1.0f / det, this);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool Mat4::isIdentity() const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-14 01:26:25 +08:00
|
|
|
return (memcmp(m, &IDENTITY, MATRIX_SIZE) == 0);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::multiply(float scalar)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
multiply(scalar, this);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::multiply(float scalar, Mat4* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
multiply(*this, scalar, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::multiply(const Mat4& m, float scalar, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
2014-09-09 23:01:37 +08:00
|
|
|
#ifdef __SSE__
|
2014-09-08 20:58:59 +08:00
|
|
|
MathUtil::multiplyMatrix(m.col, scalar, dst->col);
|
|
|
|
#else
|
2014-04-02 10:33:07 +08:00
|
|
|
MathUtil::multiplyMatrix(m.m, scalar, dst->m);
|
2014-09-08 20:58:59 +08:00
|
|
|
#endif
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::multiply(const Mat4& mat)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-04-16 10:58:34 +08:00
|
|
|
multiply(*this, mat, this);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::multiply(const Mat4& m1, const Mat4& m2, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
2014-09-09 23:01:37 +08:00
|
|
|
#ifdef __SSE__
|
2014-09-08 20:58:59 +08:00
|
|
|
MathUtil::multiplyMatrix(m1.col, m2.col, dst->col);
|
|
|
|
#else
|
2014-04-02 10:33:07 +08:00
|
|
|
MathUtil::multiplyMatrix(m1.m, m2.m, dst->m);
|
2014-09-08 20:58:59 +08:00
|
|
|
#endif
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::negate()
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-09-09 23:01:37 +08:00
|
|
|
#ifdef __SSE__
|
2014-09-08 20:58:59 +08:00
|
|
|
MathUtil::negateMatrix(col, col);
|
|
|
|
#else
|
2014-05-01 01:59:36 +08:00
|
|
|
MathUtil::negateMatrix(m, m);
|
2014-09-08 20:58:59 +08:00
|
|
|
#endif
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 Mat4::getNegated() const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 mat(*this);
|
2014-05-01 01:59:36 +08:00
|
|
|
mat.negate();
|
|
|
|
return mat;
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::rotate(const Quaternion& q)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
rotate(q, this);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::rotate(const Quaternion& q, Mat4* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 r;
|
2014-04-02 10:33:07 +08:00
|
|
|
createRotation(q, &r);
|
|
|
|
multiply(*this, r, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::rotate(const Vec3& axis, float angle)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
rotate(axis, angle, this);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::rotate(const Vec3& axis, float angle, Mat4* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 r;
|
2014-04-02 10:33:07 +08:00
|
|
|
createRotation(axis, angle, &r);
|
|
|
|
multiply(*this, r, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::rotateX(float angle)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
rotateX(angle, this);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::rotateX(float angle, Mat4* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 r;
|
2014-04-02 10:33:07 +08:00
|
|
|
createRotationX(angle, &r);
|
|
|
|
multiply(*this, r, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::rotateY(float angle)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
rotateY(angle, this);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::rotateY(float angle, Mat4* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 r;
|
2014-04-02 10:33:07 +08:00
|
|
|
createRotationY(angle, &r);
|
|
|
|
multiply(*this, r, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::rotateZ(float angle)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
rotateZ(angle, this);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::rotateZ(float angle, Mat4* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 r;
|
2014-04-02 10:33:07 +08:00
|
|
|
createRotationZ(angle, &r);
|
|
|
|
multiply(*this, r, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::scale(float value)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
scale(value, this);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::scale(float value, Mat4* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
scale(value, value, value, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::scale(float xScale, float yScale, float zScale)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
scale(xScale, yScale, zScale, this);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::scale(float xScale, float yScale, float zScale, Mat4* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 s;
|
2014-04-02 10:33:07 +08:00
|
|
|
createScale(xScale, yScale, zScale, &s);
|
|
|
|
multiply(*this, s, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::scale(const Vec3& s)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
scale(s.x, s.y, s.z, this);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::scale(const Vec3& s, Mat4* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
scale(s.x, s.y, s.z, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::set(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24,
|
2014-04-02 10:33:07 +08:00
|
|
|
float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44)
|
|
|
|
{
|
|
|
|
m[0] = m11;
|
|
|
|
m[1] = m21;
|
|
|
|
m[2] = m31;
|
|
|
|
m[3] = m41;
|
|
|
|
m[4] = m12;
|
|
|
|
m[5] = m22;
|
|
|
|
m[6] = m32;
|
|
|
|
m[7] = m42;
|
|
|
|
m[8] = m13;
|
|
|
|
m[9] = m23;
|
|
|
|
m[10] = m33;
|
|
|
|
m[11] = m43;
|
|
|
|
m[12] = m14;
|
|
|
|
m[13] = m24;
|
|
|
|
m[14] = m34;
|
|
|
|
m[15] = m44;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::set(const float* mat)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-04-16 10:58:34 +08:00
|
|
|
GP_ASSERT(mat);
|
|
|
|
memcpy(this->m, mat, MATRIX_SIZE);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::set(const Mat4& mat)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-04-16 10:58:34 +08:00
|
|
|
memcpy(this->m, mat.m, MATRIX_SIZE);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::setIdentity()
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-14 01:26:25 +08:00
|
|
|
memcpy(m, &IDENTITY, MATRIX_SIZE);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::setZero()
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
memset(m, 0, MATRIX_SIZE);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::subtract(const Mat4& mat)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-04-16 10:58:34 +08:00
|
|
|
subtract(*this, mat, this);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::subtract(const Mat4& m1, const Mat4& m2, Mat4* dst)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
2014-09-09 23:01:37 +08:00
|
|
|
#ifdef __SSE__
|
2014-09-08 20:58:59 +08:00
|
|
|
MathUtil::subtractMatrix(m1.col, m2.col, dst->col);
|
|
|
|
#else
|
2014-04-02 10:33:07 +08:00
|
|
|
MathUtil::subtractMatrix(m1.m, m2.m, dst->m);
|
2014-09-08 20:58:59 +08:00
|
|
|
#endif
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::transformVector(Vec3* vector) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(vector);
|
|
|
|
transformVector(vector->x, vector->y, vector->z, 0.0f, vector);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::transformVector(const Vec3& vector, Vec3* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
transformVector(vector.x, vector.y, vector.z, 0.0f, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::transformVector(float x, float y, float z, float w, Vec3* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
MathUtil::transformVec4(m, x, y, z, w, (float*)dst);
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::transformVector(Vec4* vector) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(vector);
|
|
|
|
transformVector(*vector, vector);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::transformVector(const Vec4& vector, Vec4* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
GP_ASSERT(dst);
|
2014-09-09 23:01:37 +08:00
|
|
|
#ifdef __SSE__
|
2014-09-08 20:58:59 +08:00
|
|
|
MathUtil::transformVec4(col, vector.v, dst->v);
|
|
|
|
#else
|
2014-05-15 01:07:09 +08:00
|
|
|
MathUtil::transformVec4(m, (const float*) &vector, (float*)dst);
|
2014-09-08 20:58:59 +08:00
|
|
|
#endif
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::translate(float x, float y, float z)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
translate(x, y, z, this);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::translate(float x, float y, float z, Mat4* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 t;
|
2014-04-02 10:33:07 +08:00
|
|
|
createTranslation(x, y, z, &t);
|
|
|
|
multiply(*this, t, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::translate(const Vec3& t)
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
translate(t.x, t.y, t.z, this);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::translate(const Vec3& t, Mat4* dst) const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
translate(t.x, t.y, t.z, dst);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Mat4::transpose()
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-09-09 23:01:37 +08:00
|
|
|
#ifdef __SSE__
|
2014-09-08 20:58:59 +08:00
|
|
|
MathUtil::transposeMatrix(col, col);
|
|
|
|
#else
|
2014-05-01 01:59:36 +08:00
|
|
|
MathUtil::transposeMatrix(m, m);
|
2014-09-08 20:58:59 +08:00
|
|
|
#endif
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 Mat4::getTransposed() const
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 mat(*this);
|
2014-05-01 01:59:36 +08:00
|
|
|
mat.transpose();
|
|
|
|
return mat;
|
2014-04-02 10:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
const Mat4 Mat4::IDENTITY = Mat4(
|
2014-05-14 01:26:25 +08:00
|
|
|
1.0f, 0.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
const Mat4 Mat4::ZERO = Mat4(
|
2014-05-14 01:26:25 +08:00
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0 );
|
|
|
|
|
2014-04-02 12:09:51 +08:00
|
|
|
NS_CC_MATH_END
|