From e13616b5c40f912853be99f0603f0e4c97b22062 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 3 Apr 2019 21:56:51 +0800 Subject: *misc --- source/modules/asura-core/graphics/gl.cpp | 72 +++++++++++++++++++++++++-- source/modules/asura-core/graphics/gl.h | 13 +++-- source/modules/asura-core/graphics/image.h | 5 +- source/modules/asura-core/graphics/shader.cpp | 37 +++++++------- source/modules/asura-core/graphics/shader.h | 5 -- source/modules/asura-core/graphics/texture.h | 2 +- 6 files changed, 96 insertions(+), 38 deletions(-) (limited to 'source/modules/asura-core') diff --git a/source/modules/asura-core/graphics/gl.cpp b/source/modules/asura-core/graphics/gl.cpp index e199a41..47476a7 100644 --- a/source/modules/asura-core/graphics/gl.cpp +++ b/source/modules/asura-core/graphics/gl.cpp @@ -12,24 +12,24 @@ namespace AsuraEngine namespace Graphics { +#if ASURA_DEBUG static bool _instantiated = false; +#endif - // 单例 OpenGL gl; OpenGL::OpenGL() { - // 不要创建第二个实例 +#if ASURA_DEBUG ASSERT(!_instantiated); _instantiated = true; +#endif } OpenGL::~OpenGL() { } - //------------------------------------------------------------------------------// - void OpenGL::SetViewport(const Recti v) { glViewport(v.x, v.y, v.w, v.h); @@ -52,5 +52,69 @@ namespace AsuraEngine state.shader = nullptr; } + //------------------------------------------------------------------------------// + + void OpenGL::SetMatrixMode(MatrixMode mode) + { + state.matrixMode = mode; + } + + MatrixMode OpenGL::GetMatrixMode() + { + return state.matrixMode; + } + + void OpenGL::PushMatrix() + { + state.matrix[state.matrixMode].Push(); + } + + void OpenGL::PopMatrix() + { + state.matrix[state.matrixMode].Pop(); + } + + void OpenGL::LoadIdentity() + { + state.matrix[state.matrixMode].LoadIdentity(); + } + + void OpenGL::Rotate(float angle, float x, float y, float z) + { + state.matrix[state.matrixMode].Rotate(angle, x, y, z); + } + + void OpenGL::Translate(float x, float y, float z) + { + state.matrix[state.matrixMode].Translate(x, y, z); + } + + void OpenGL::Scale(float x, float y, float z) + { + state.matrix[state.matrixMode].Scale(x, y, z); + } + + void OpenGL::Ortho(float l, float r, float b, float t, float n, float f) + { + state.matrix[state.matrixMode].Ortho(l, r, b, t, n, f); + } + + AEMath::Matrix44& OpenGL::GetMatrix(MatrixMode mode) + { + return state.matrix[state.matrixMode].GetTop(); + } + + uint OpenGL::GetMatrixDepth() + { + return state.matrix[state.matrixMode].GetCapacity(); + } + + uint OpenGL::GetMatrixIndex() + { + return state.matrix[state.matrixMode].GetTopIndex(); + } + + //------------------------------------------------------------------------------// + } } \ No newline at end of file diff --git a/source/modules/asura-core/graphics/gl.h b/source/modules/asura-core/graphics/gl.h index 3104288..6838bc9 100644 --- a/source/modules/asura-core/graphics/gl.h +++ b/source/modules/asura-core/graphics/gl.h @@ -22,15 +22,14 @@ namespace AsuraEngine enum MatrixMode { MATRIX_PROJECTION = 0, - MATRIX_MODELVIEW, - _MATRIX_COUNT + MATRIX_MODELVIEW = 1, }; /// /// OpenGL上下文,用来做一些opengl状态的追踪。在编辑器多窗口环境下,一个窗口对应一个hwnd, /// 一个hdc,以及数个opengl context,如果使用wglMakeCurrent(hdc, glc)指定当前线程耳朵 /// 渲染窗口hdc和opengl上下文glc,gl中记录的就是任意一个线程的任意一个窗口的任意一个OpenGL - /// 上下文的状态, + /// 上下文的状态,不支持多上下文渲染。 /// class OpenGL : public AEScripting::Portable { @@ -69,10 +68,10 @@ namespace AsuraEngine /// struct { - Shader* shader; ///< 当前使用的shader - AEMath::Recti viewport; ///< 当前的视区,在切换HDC或者本窗口大小改变或者部分刷新时变动 - MatrixStack matrix[_MATRIX_COUNT]; ///< 投影矩阵 - MatrixMode matrixMode; ///< 当前操作的矩阵 + Shader* shader; ///< 当前使用的shader + AEMath::Recti viewport; ///< 当前的视区,在切换HDC或者本窗口大小改变或者部分刷新时变动 + MatrixStack matrix[2]; ///< 变换矩阵 + MatrixMode matrixMode; ///< 当前操作的矩阵 } state; private: diff --git a/source/modules/asura-core/graphics/image.h b/source/modules/asura-core/graphics/image.h index 2e6ced2..e4aecd1 100644 --- a/source/modules/asura-core/graphics/image.h +++ b/source/modules/asura-core/graphics/image.h @@ -28,9 +28,8 @@ namespace AsuraEngine /// 是一个只读类。主要是考虑到editor和engine使用不同的封装。 /// class Image ASURA_FINAL - : public Texture - , public Scripting::Portable - , public AEIO::Renewable + : public AEIO::Renewable + , public AEScripting::Portable { public: diff --git a/source/modules/asura-core/graphics/shader.cpp b/source/modules/asura-core/graphics/shader.cpp index fdcdf1b..c26ddf1 100644 --- a/source/modules/asura-core/graphics/shader.cpp +++ b/source/modules/asura-core/graphics/shader.cpp @@ -12,24 +12,25 @@ namespace AsuraEngine Shader::Shader() { - mProgram = glCreateProgram(); - if (mProgram == 0) - throw Exception("Cannot create OpenGL shader program."); - - mVertShader = glCreateShader(GL_VERTEX_SHADER); - if (mVertShader == 0) - { - glDeleteProgram(mProgram); - throw Exception("Cannot create OpenGL vertex shader."); - } - - mFragShader = glCreateShader(GL_FRAGMENT_SHADER); - if (mFragShader == 0) - { - glDeleteProgram(mProgram); - glDeleteShader(mVertShader); - throw Exception("Cannot create OpenGL fragment shader."); - } + //Fix: 等需要的时候再申请 + //mProgram = glCreateProgram(); + //if (mProgram == 0) + // throw Exception("Cannot create OpenGL shader program."); + + //mVertShader = glCreateShader(GL_VERTEX_SHADER); + //if (mVertShader == 0) + //{ + // glDeleteProgram(mProgram); + // throw Exception("Cannot create OpenGL vertex shader."); + //} + + //mFragShader = glCreateShader(GL_FRAGMENT_SHADER); + //if (mFragShader == 0) + //{ + // glDeleteProgram(mProgram); + // glDeleteShader(mVertShader); + // throw Exception("Cannot create OpenGL fragment shader."); + //} } Shader::~Shader() diff --git a/source/modules/asura-core/graphics/shader.h b/source/modules/asura-core/graphics/shader.h index 6d51b8e..f4bce25 100644 --- a/source/modules/asura-core/graphics/shader.h +++ b/source/modules/asura-core/graphics/shader.h @@ -81,11 +81,6 @@ namespace AsuraEngine private: - /// - /// 当前活动的shader - /// - static Shader* mCurrentShader; - /// /// 设置内置变量: /// vec2 Asura_Time x值为进入当前场景开始的时间,y值为上一帧的时间间隔 diff --git a/source/modules/asura-core/graphics/texture.h b/source/modules/asura-core/graphics/texture.h index f19f3a7..571c617 100644 --- a/source/modules/asura-core/graphics/texture.h +++ b/source/modules/asura-core/graphics/texture.h @@ -57,7 +57,7 @@ namespace AsuraEngine /// 面的上层会以笛卡尔坐标系为标准。在Editor里面界面和组件也是以左上角为原点,这样是为了 /// 方便。 /// - ASURA_ABSTRACT class Texture : virtual public AEScripting::NativeAccessor + ASURA_ABSTRACT class Texture : public AEScripting::Object { public: -- cgit v1.1-26-g67d0