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 +- 8 files changed, 190 insertions(+), 19 deletions(-) create mode 100644 source/modules/asura-core/graphics/binding/_gl.cpp (limited to 'source/modules/asura-core') 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); -- cgit v1.1-26-g67d0