From c8a6a8e2dd6f015a31b4f8191ad945a78fe77f3d Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 3 Apr 2019 09:00:13 +0800 Subject: *misc --- source/modules/asura-core/graphics/binding/_gl.cpp | 127 +++++++++++++++++++++ .../modules/asura-core/graphics/binding/_image.cpp | 8 +- source/modules/asura-core/graphics/gl.h | 55 ++++++++- source/modules/asura-core/graphics/image.cpp | 5 +- source/modules/asura-core/graphics/image.h | 6 +- source/modules/asura-core/graphics/matrix_stack.h | 2 +- source/modules/asura-core/graphics/shader.cpp | 2 +- source/modules/asura-core/graphics/shader.h | 4 +- source/modules/asura-utils/io/renewable.h | 8 +- source/modules/asura-utils/math/matrix44.cpp | 7 ++ source/modules/asura-utils/math/matrix44.h | 77 +------------ 11 files changed, 204 insertions(+), 97 deletions(-) create mode 100644 source/modules/asura-core/graphics/binding/_gl.cpp (limited to 'source/modules') diff --git a/source/modules/asura-core/graphics/binding/_gl.cpp b/source/modules/asura-core/graphics/binding/_gl.cpp new file mode 100644 index 0000000..4c0605f --- /dev/null +++ b/source/modules/asura-core/graphics/binding/_gl.cpp @@ -0,0 +1,127 @@ +#include "../gl.h" + +using namespace std; +using namespace Luax; + +namespace AsuraEngine +{ + namespace Graphics + { + + LUAX_REGISTRY(OpenGL) + { + LUAX_REGISTER_METHODS(state, + { "SetMatrixMode", _SetMatrixMode }, + { "GetMatrixMode", _GetMatrixMode }, + { "PushMatrix", _PushMatrix }, + { "PopMatrix", _PopMatrix }, + { "LoadIdentity", _LoadIdentity }, + { "Rotate", _Rotate }, + { "Translate", _Translate }, + { "Scale", _Scale }, + { "Ortho", _Ortho }, + { "GetMatrixDepth", _GetMatrixDepth }, + { "GetMatrixIndex", _GetMatrixIndex } + ); + } + + LUAX_POSTPROCESS(OpenGL) + { + LUAX_REGISTER_ENUM(state, "EMatrixMode", + { "PROJECTION", MATRIX_PROJECTION }, + { "MODELVIEW", MATRIX_MODELVIEW } + ); + + } + + // GL.SetMatrixMode() + LUAX_IMPL_METHOD(OpenGL, _SetMatrixMode) + { + LUAX_PREPARE(L, OpenGL); + MatrixMode mode = (MatrixMode)state.CheckValue(1); + gl.SetMatrixMode(mode); + return 0; + } + + // GL.GetMatrixMode() + LUAX_IMPL_METHOD(OpenGL, _GetMatrixMode) + { + LUAX_PREPARE(L, OpenGL); + + return 0; + } + + // GL.PushMatrix() + LUAX_IMPL_METHOD(OpenGL, _PushMatrix) + { + LUAX_PREPARE(L, OpenGL); + + return 0; + } + + // GL.PopMatrix() + LUAX_IMPL_METHOD(OpenGL, _PopMatrix) + { + LUAX_PREPARE(L, OpenGL); + + return 0; + } + + // GL.LoadIdentity() + LUAX_IMPL_METHOD(OpenGL, _LoadIdentity) + { + LUAX_PREPARE(L, OpenGL); + + return 0; + } + + // GL.Rotate() + LUAX_IMPL_METHOD(OpenGL, _Rotate) + { + LUAX_PREPARE(L, OpenGL); + + return 0; + } + + // GL.Translate() + LUAX_IMPL_METHOD(OpenGL, _Translate) + { + LUAX_PREPARE(L, OpenGL); + + return 0; + } + + // GL.Scale() + LUAX_IMPL_METHOD(OpenGL, _Scale) + { + LUAX_PREPARE(L, OpenGL); + + return 0; + } + + // GL.Ortho() + LUAX_IMPL_METHOD(OpenGL, _Ortho) + { + LUAX_PREPARE(L, OpenGL); + + return 0; + } + + // GL.GetMatrixDepth() + LUAX_IMPL_METHOD(OpenGL, _GetMatrixDepth) + { + LUAX_PREPARE(L, OpenGL); + + return 0; + } + + // GL.GetMatrixIndex() + LUAX_IMPL_METHOD(OpenGL, _GetMatrixIndex) + { + LUAX_PREPARE(L, OpenGL); + + return 0; + } + + } +} diff --git a/source/modules/asura-core/graphics/binding/_image.cpp b/source/modules/asura-core/graphics/binding/_image.cpp index 913bf5c..6179706 100644 --- a/source/modules/asura-core/graphics/binding/_image.cpp +++ b/source/modules/asura-core/graphics/binding/_image.cpp @@ -13,7 +13,7 @@ namespace AsuraEngine LUAX_REGISTER_METHODS(state, { "New", _New }, - { "Renew", _Renew }, + { "Update", _Update }, { "GetWidth", _GetWidth }, { "GetHeight", _GetHeight }, { "GetSize", _GetSize }, @@ -34,12 +34,12 @@ namespace AsuraEngine return 1; } - // successed = image:Renew(imgData) - LUAX_IMPL_METHOD(Image, _Renew) + // successed = image:Update(imgData) + LUAX_IMPL_METHOD(Image, _Update) { LUAX_PREPARE(L, Image); ImageData* imgData = state.CheckUserdata(2); - state.Push(self->Renew(imgData)); + state.Push(self->Update(imgData)); return 1; } diff --git a/source/modules/asura-core/graphics/gl.h b/source/modules/asura-core/graphics/gl.h index 9ca1f44..3104288 100644 --- a/source/modules/asura-core/graphics/gl.h +++ b/source/modules/asura-core/graphics/gl.h @@ -5,6 +5,7 @@ #include +#include #include #include @@ -18,15 +19,25 @@ namespace AsuraEngine class Profiler; class Shader; + enum MatrixMode + { + MATRIX_PROJECTION = 0, + MATRIX_MODELVIEW, + _MATRIX_COUNT + }; + /// /// OpenGL上下文,用来做一些opengl状态的追踪。在编辑器多窗口环境下,一个窗口对应一个hwnd, /// 一个hdc,以及数个opengl context,如果使用wglMakeCurrent(hdc, glc)指定当前线程耳朵 /// 渲染窗口hdc和opengl上下文glc,gl中记录的就是任意一个线程的任意一个窗口的任意一个OpenGL /// 上下文的状态, /// - class OpenGL + class OpenGL : public AEScripting::Portable { public: + + LUAX_DECL_SINGLETON(GL); + OpenGL(); ~OpenGL(); @@ -36,6 +47,19 @@ namespace AsuraEngine void UseShader(Shader* shader); void UnuseShader(); + void SetMatrixMode(MatrixMode mode); + MatrixMode GetMatrixMode(); + void PushMatrix(); + void PopMatrix(); + void LoadIdentity(); + void Rotate(float angle, float x, float y, float z); + void Translate(float x, float y, float z); + void Scale(float x, float y, float z); + void Ortho(float l, float r, float b, float t, float n, float f); + AEMath::Matrix44& GetMatrix(MatrixMode mode); + uint GetMatrixDepth(); + uint GetMatrixIndex(); + /// /// OpenGL3.0以后由用户管理矩阵变换、视口、shader等参数,这里保存一些OpenGL状态。注意 /// 似乎全进程的,也就是说,Asura不支持多线程渲染。OpenGL上下文的创建使得一个上下 @@ -45,16 +69,37 @@ namespace AsuraEngine /// struct { - Shader* shader; ///< 当前使用的shader - AEMath::Recti viewport; ///< 当前的视区,在切换HDC或者本窗口大小改变或者部分刷新时变动 - MatrixStack projectionMatrix; ///< 投影矩阵 - MatrixStack modelViewMatrix; ///< 变换矩阵 + Shader* shader; ///< 当前使用的shader + AEMath::Recti viewport; ///< 当前的视区,在切换HDC或者本窗口大小改变或者部分刷新时变动 + MatrixStack matrix[_MATRIX_COUNT]; ///< 投影矩阵 + MatrixMode matrixMode; ///< 当前操作的矩阵 } state; private: friend class Profiler; + //----------------------------------------------------------------------------// + + LUAX_DECL_ENUM(MatrixMode, 0); + + LUAX_DECL_METHOD(_SetMatrixMode); + LUAX_DECL_METHOD(_GetMatrixMode); + LUAX_DECL_METHOD(_PushMatrix); + LUAX_DECL_METHOD(_PopMatrix); + LUAX_DECL_METHOD(_LoadIdentity); + LUAX_DECL_METHOD(_Rotate); + LUAX_DECL_METHOD(_Translate); + LUAX_DECL_METHOD(_Scale); + LUAX_DECL_METHOD(_Ortho); + LUAX_DECL_METHOD(_GetMatrixDepth); + LUAX_DECL_METHOD(_GetMatrixIndex); + + LUAX_DECL_METHOD(_UseShader); + LUAX_DECL_METHOD(_UnuseShader); + + //----------------------------------------------------------------------------// + }; /// diff --git a/source/modules/asura-core/graphics/image.cpp b/source/modules/asura-core/graphics/image.cpp index 530ea97..4cbe826 100644 --- a/source/modules/asura-core/graphics/image.cpp +++ b/source/modules/asura-core/graphics/image.cpp @@ -20,13 +20,12 @@ namespace AsuraEngine { } - bool Image::Renew(DecodedData* data) + bool Image::Update(DecodedData* data) { if (!data) return false; ImageData* imgData = static_cast(data); if (!imgData) return false; - // 如果没有纹理资源,申请一个 if (mTex == 0) { glGenTextures(1, &mTex); @@ -60,7 +59,7 @@ namespace AsuraEngine return true; } - bool Image::Renew(AEIO::DecodedData* data, const AEMath::Vector2i& pos) + bool Image::Update(AEIO::DecodedData* data, const AEMath::Vector2i& pos) { if (!data) return false; ImageData* imgData = static_cast(data); diff --git a/source/modules/asura-core/graphics/image.h b/source/modules/asura-core/graphics/image.h index d60bd24..2e6ced2 100644 --- a/source/modules/asura-core/graphics/image.h +++ b/source/modules/asura-core/graphics/image.h @@ -44,8 +44,8 @@ namespace AsuraEngine /// 将解析后的图像数据提交到GPU,更新像素信息。用来重新构建image,使用glTexImage2D重 /// 新提交image的像素数据。 /// - bool Renew(AEIO::DecodedData* decodeData) override; - bool Renew(AEIO::DecodedData* decodeData, const AEMath::Vector2i& pos); + bool Update(AEIO::DecodedData* decodeData) override; + bool Update(AEIO::DecodedData* decodeData, const AEMath::Vector2i& pos); uint GetWidth(); uint GetHeight(); @@ -58,7 +58,7 @@ namespace AsuraEngine //----------------------------------------------------------------------------// LUAX_DECL_METHOD(_New); - LUAX_DECL_METHOD(_Renew); + LUAX_DECL_METHOD(_Update); LUAX_DECL_METHOD(_GetWidth); LUAX_DECL_METHOD(_GetHeight); LUAX_DECL_METHOD(_GetSize); diff --git a/source/modules/asura-core/graphics/matrix_stack.h b/source/modules/asura-core/graphics/matrix_stack.h index 1923b30..e69ee98 100644 --- a/source/modules/asura-core/graphics/matrix_stack.h +++ b/source/modules/asura-core/graphics/matrix_stack.h @@ -50,7 +50,7 @@ namespace AsuraEngine /// /// 投影变换 /// - void Ortho(float left, float right, float bottom, float top, float near, float far); + void Ortho(float l, float r, float b, float t, float n, float f); //void Perspective(float fov, float aspect, float near, float far); //void Frustum(float left, float right, float top, float bottom, float near, float far); //void LookAt(float x, float y, float z, float cx, float cy, float cz, float ux, float uy, float uz); diff --git a/source/modules/asura-core/graphics/shader.cpp b/source/modules/asura-core/graphics/shader.cpp index a4738cd..fdcdf1b 100644 --- a/source/modules/asura-core/graphics/shader.cpp +++ b/source/modules/asura-core/graphics/shader.cpp @@ -39,7 +39,7 @@ namespace AsuraEngine glDeleteProgram(mProgram); } - bool Shader::Renew(AEIO::DecodedData* db) + bool Shader::Update(AEIO::DecodedData* db) { if (!db) return false; ShaderSouce* shaderSource = static_cast(db); diff --git a/source/modules/asura-core/graphics/shader.h b/source/modules/asura-core/graphics/shader.h index 9077599..6d51b8e 100644 --- a/source/modules/asura-core/graphics/shader.h +++ b/source/modules/asura-core/graphics/shader.h @@ -45,7 +45,7 @@ namespace AsuraEngine /// 从代码编译shader,编译时会先检测是否有上次缓存的uniforms location map。使用 /// glAttachShader重新编译生成着色器,不会重新申请着色器程序。 /// - bool Renew(AEIO::DecodedData* decodeData) override; + bool Update(AEIO::DecodedData* decodeData) override; /// /// 将当期shader设置为活动 @@ -111,7 +111,7 @@ namespace AsuraEngine LUAX_DECL_METHOD(_Use); LUAX_DECL_METHOD(_Unuse); LUAX_DECL_METHOD(_Load); - LUAX_DECL_METHOD(_Renew); + LUAX_DECL_METHOD(_Update); LUAX_DECL_METHOD(_HasUniform); LUAX_DECL_METHOD(_GetUniformLocation); LUAX_DECL_METHOD(_SetBuiltInUniforms); diff --git a/source/modules/asura-utils/io/renewable.h b/source/modules/asura-utils/io/renewable.h index a624c2c..4d047ea 100644 --- a/source/modules/asura-utils/io/renewable.h +++ b/source/modules/asura-utils/io/renewable.h @@ -21,12 +21,12 @@ namespace AsuraEngine virtual ~Renewable() {}; /// - /// 继承Renewable的需要提供一个Renew方法 + /// 继承Renewable的需要提供一个Update方法 /// - /// 依据Effective C++条款09.应该禁止在构造函数中调用virtual方法,所以这里的Renew - /// 被从构造函数中抽离,需要手动调用Renew。 + /// 依据Effective C++条款09.应该禁止在构造函数中调用virtual方法,所以这里的Update + /// 被从构造函数中抽离,需要手动调用Update。 /// - virtual bool Renew(AEIO::DecodedData* decode_data) = 0; + virtual bool Update(AEIO::DecodedData* decode_data) = 0; }; diff --git a/source/modules/asura-utils/math/matrix44.cpp b/source/modules/asura-utils/math/matrix44.cpp index 10c9ece..a1dc933 100644 --- a/source/modules/asura-utils/math/matrix44.cpp +++ b/source/modules/asura-utils/math/matrix44.cpp @@ -178,6 +178,13 @@ namespace AsuraEngine this->operator *=(t); } + void Matrix44::Transform(float x, float y, float angle, float sx, float sy, float ox, float oy) + { + Matrix44 t; + t.SetTransformation(x, y, angle, sx, sy, ox, oy); + this->operator *=(t); + } + // | x | // | y | // | 0 | diff --git a/source/modules/asura-utils/math/matrix44.h b/source/modules/asura-utils/math/matrix44.h index c0cea92..fa5be33 100644 --- a/source/modules/asura-utils/math/matrix44.h +++ b/source/modules/asura-utils/math/matrix44.h @@ -10,119 +10,48 @@ namespace AsuraEngine /// 不需要转置的OpenGL矩阵,类似glm::mat4。 /// https://blog.csdn.net/candycat1992/article/details/8830894 /// - class Matrix44 { public: static const Matrix44 Identity; - /// - /// Creates a new identity matrix. - /// Matrix44(); - /// - /// Copy constructor. - /// Matrix44(const Matrix44& m); - /// - /// Destructor. - /// ~Matrix44(); void operator = (const Matrix44& m); void SetOrtho(float _left, float _right, float _bottom, float _top, float _near, float _far); - /// - /// Multiplies this Matrix44 with another Matrix44, changing neither. - /// @param m The Matrix44 to multiply with this Matrix44. - /// @return The combined matrix. - /// Matrix44 operator * (const Matrix44 & m) const; - /// - /// Multiplies a Matrix44 into this Matrix44. - /// @param m The Matrix44 to combine into this Matrix44. - /// void operator *= (const Matrix44 & m); - /// - /// Gets a pointer to the 16 array elements. - /// @return The array elements. - /// const float* GetElements() const; - /// - /// ReSets this Matrix44 to the identity matrix. - /// void SetIdentity(); - /// - /// ReSets this Matrix44 to a translation. - /// @param x Translation along x-axis. - /// @param y Translation along y-axis. - /// void SetTranslation(float x, float y); - /// - /// ReSets this Matrix44 to a rotation. - /// @param r The angle in radians. - /// void SetRotation(float r); - /// - /// ReSets this Matrix44 to a scale transformation. - /// @param sx Scale factor along the x-axis. - /// @param sy Scale factor along the y-axis. - /// void SetScale(float sx, float sy); - /// - /// ReSets this Matrix44 to a shear transformation. - /// @param kx Shear along x-axis. - /// @param ky Shear along y-axis. - /// void SetShear(float kx, float ky); - /// - /// Creates a transformation with a certain position, orientation, scale - /// and offSet. Perfect for Drawables -- what a coincidence! - /// - /// @param x The translation along the x-axis. - /// @param y The translation along the y-axis. - /// @param angle The rotation (rad) around the center with offSet (ox,oy). - /// @param sx Scale along x-axis. - /// @param sy Scale along y-axis. - /// @param ox The offSet for rotation along the x-axis. - /// @param oy The offSet for rotation along the y-axis. - /// @param kx Shear along x-axis - /// @param ky Shear along y-axis - /// void SetTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy); - /// - /// Multiplies this Matrix44 with a translation. - /// @param x Translation along x-axis. - /// @param y Translation along y-axis. - /// void Translate(float x, float y); - /// - /// Multiplies this Matrix44 with a rotation. - /// @param r Angle in radians. - /// void Rotate(float r); - /// - /// Multiplies this Matrix44 with a scale transformation. - /// @param sx Scale factor along the x-axis. - /// @param sy Scale factor along the y-axis. - /// void Scale(float sx, float sy); + void Transform(float x, float y, float angle, float sx, float sy, float ox, float oy); + /// /// Multiplies this Matrix44 with a shear transformation. /// @param kx Shear along the x-axis. @@ -157,4 +86,4 @@ namespace AsuraEngine namespace AEMath = AsuraEngine::Math; -#endif \ No newline at end of file +#endif \ No newline at end of file -- cgit v1.1-26-g67d0