diff options
-rw-r--r-- | bin/win64/01-window.exe | bin | 1461248 -> 1460736 bytes | |||
-rw-r--r-- | source/Asura.Editor/graphics/shaders/image.shader.h | 11 | ||||
-rw-r--r-- | source/Asura.Editor/graphics/shaders/image_slice.shader.h | 64 | ||||
-rw-r--r-- | source/Asura.Editor/graphics/shaders/polygon.shader.h | 7 | ||||
-rw-r--r-- | source/modules/asura-core/graphics/binding/_shader.cpp | 104 | ||||
-rw-r--r-- | source/modules/asura-core/graphics/gl.cpp | 28 | ||||
-rw-r--r-- | source/modules/asura-core/graphics/gl.h | 13 | ||||
-rw-r--r-- | source/modules/asura-core/graphics/shader.cpp | 31 | ||||
-rw-r--r-- | source/modules/asura-core/graphics/shader.h | 19 | ||||
-rw-r--r-- | source/modules/asura-utils/math/matrix44.cpp | 1 | ||||
-rw-r--r-- | source/tests/win32/01-window/03_sub_menu.cpp | 25 |
11 files changed, 235 insertions, 68 deletions
diff --git a/bin/win64/01-window.exe b/bin/win64/01-window.exe Binary files differindex 025f552..fe5b0b6 100644 --- a/bin/win64/01-window.exe +++ b/bin/win64/01-window.exe diff --git a/source/Asura.Editor/graphics/shaders/image.shader.h b/source/Asura.Editor/graphics/shaders/image.shader.h index ee6be43..fbca5b5 100644 --- a/source/Asura.Editor/graphics/shaders/image.shader.h +++ b/source/Asura.Editor/graphics/shaders/image.shader.h @@ -2,20 +2,17 @@ #include "../shader.h" #endif -// static AsuraEditor::Graphics::ShaderProgram image_shader = { R"( in vec2 asura_position; in vec2 asura_texcoord0; -uniform mat4 asura_model_matrix; -uniform mat4 asura_view_matrix; -uniform mat4 asura_projection_matrix; +uniform asura_mvp_matrix; void main() { - gl_Position = asura_projection_matrix * asura_view_matrix * asura_model_matrix * vec4(asura_position, 0, 1); + gl_Position = asura_mvp_matrix * vec4(asura_position, 0, 1); uv = asura_texcoord0; } @@ -28,8 +25,8 @@ uniform sampler2D asura_maintex; void main() { - + gl_FragColor = texture2D(asura_maintex, uv); } )" -};
\ No newline at end of file +}; diff --git a/source/Asura.Editor/graphics/shaders/image_slice.shader.h b/source/Asura.Editor/graphics/shaders/image_slice.shader.h new file mode 100644 index 0000000..58be2b1 --- /dev/null +++ b/source/Asura.Editor/graphics/shaders/image_slice.shader.h @@ -0,0 +1,64 @@ +#ifndef __ASURA_EDITOR_SHADER_H__ +#include "../shader.h" +#endif + +// ŹָͼƬ +static AsuraEditor::Graphics::ShaderProgram image_slice_shader = +{ +R"( +in vec2 _position; + +uniform _mvp_matrix; + +out vec2 offset; // top-left + +void main() +{ + gl_Position = _mvp_matrix * vec4(_position, 0, 1); + offset = _position - vec2(0.5, 0.5); +} + +)", + +R"( +in vec2 offset; + +uniform vec4 _sliceline; // l, r, t, b +uniform vec2 _texsize; // w0, h0 in pixel +uniform sampler2D _maintex; +uniform vec2 _area; // w, h +// (a, b]Χڣ10 +int factor(float v, float a, float b) +{ + return clamp(sign(v-a), 0, 1) * step(-b, -v); +} + +void main() +{ + float x = offset.x; + float y = offset.y; + float l = _sliceline.x; + float r = _sliceline.y; + float t = _sliceline.z; + float b = _sliceline.w; + float w0 = _texsize.x; + float h0 = _texsize.y; + float w = _area.x; + float h = _area.y; + + vec2 uv = vec2(0, 0); + + //uv.x *= 0; // x = 0 + uv.x *= factor(x, 0, l) * x / w0; // 0<x<=l + uv.x *= factor(x, l, w-r) * (l/w0 + (x-l)/(w-l-r) * (w0-l-r) / w0); // l<x<=w-r + uv.x *= factor(x, w-r, w) * (1 - (w-x)/w0); // w-r<x<=w + + //uv.y *= 0; // y = 0 + + + gl_FragColor = texture2D(_maintex, uv); + +} + +)" +}; diff --git a/source/Asura.Editor/graphics/shaders/polygon.shader.h b/source/Asura.Editor/graphics/shaders/polygon.shader.h index eed4f5a..ee80f32 100644 --- a/source/Asura.Editor/graphics/shaders/polygon.shader.h +++ b/source/Asura.Editor/graphics/shaders/polygon.shader.h @@ -2,17 +2,16 @@ #include "../shader.h" #endif -// static AsuraEditor::Graphics::ShaderProgram polygon_shader = { R"( in vec2 position; -uniform mat4 mvp_matrix; +uniform mat4 asura_mvp_matrix; void main() { - gl_Position = mvp_matrix * vec4(position, 0, 1); + gl_Position = asura_mvp_matrix * vec4(position, 0, 1); } )", @@ -26,4 +25,4 @@ void main() } )" -};
\ No newline at end of file +}; diff --git a/source/modules/asura-core/graphics/binding/_shader.cpp b/source/modules/asura-core/graphics/binding/_shader.cpp index 71f4e28..34bc98b 100644 --- a/source/modules/asura-core/graphics/binding/_shader.cpp +++ b/source/modules/asura-core/graphics/binding/_shader.cpp @@ -11,19 +11,25 @@ namespace AsuraEngine LUAX_REGISTRY(Shader) { LUAX_REGISTER_METHODS(state, - { "New", _New }, - { "Load", _Load }, - { "Update", _Update }, - { "HasUniform", _HasUniform }, - { "GetUniformLocation", _GetUniformLocation }, - { "SetBuiltInUniforms", _SetBuiltInUniforms }, - { "SetUniformFloat", _SetUniformFloat }, - { "SetUniformTexture", _SetUniformTexture }, - { "SetUniformVector2", _SetUniformVector2 }, - { "SetUniformVector3", _SetUniformVector3 }, - { "SetUniformVector4", _SetUniformVector4 }, - { "SetUniformColor", _SetUniformColor }, - { "SetBuiltInUniforms", _SetBuiltInUniforms } + { "New", _New }, + { "Load", _Load }, + { "Update", _Update }, + { "HasUniform", _HasUniform }, + { "GetUniformLocation", _GetUniformLocation }, + { "SetUniformFloat", _SetUniformFloat }, + { "SetUniformTexture", _SetUniformTexture }, + { "SetUniformVector2", _SetUniformVector2 }, + { "SetUniformVector3", _SetUniformVector3 }, + { "SetUniformVector4", _SetUniformVector4 }, + { "SetUniformColor", _SetUniformColor }, + { "GetAttributeLocation", _GetAttributeLocation }, + { "SetAttribute", _SetAttribute }, + { "DisableAttribute", _DisableAttribute }, + { "SetBuiltInModelMatrix", _SetBuiltInModelMatrix }, + { "SetBuiltInViewMatrix", _SetBuiltInViewMatrix }, + { "SetBuiltInProjectionMatrix", _SetBuiltInProjectionMatrix }, + { "SetBuiltInMVPMatrix", _SetBuiltInMVPMatrix }, + { "SetBuiltInDrawColor", _SetBuiltInDrawColor } ); } @@ -72,14 +78,6 @@ namespace AsuraEngine return 0; } - // shader:SetBuiltInUniforms() - LUAX_IMPL_METHOD(Shader, _SetBuiltInUniforms) - { - LUAX_PREPARE(L, Shader); - - return 0; - } - // shader:SetUniformFloat() LUAX_IMPL_METHOD(Shader, _SetUniformFloat) { @@ -128,5 +126,69 @@ namespace AsuraEngine return 0; } + // shader:GetAttributeLocation() + LUAX_IMPL_METHOD(Shader, _GetAttributeLocation) + { + LUAX_PREPARE(L, Shader); + + return 0; + } + + // shader:SetAttribute() + LUAX_IMPL_METHOD(Shader, _SetAttribute) + { + LUAX_PREPARE(L, Shader); + + return 0; + } + + // shader:DisableAttribute() + LUAX_IMPL_METHOD(Shader, _DisableAttribute) + { + LUAX_PREPARE(L, Shader); + + return 0; + } + + // shader:SetBuiltInModelMatrix() + LUAX_IMPL_METHOD(Shader, _SetBuiltInModelMatrix) + { + LUAX_PREPARE(L, Shader); + + return 0; + } + + // shader:SetBuiltInViewMatrix() + LUAX_IMPL_METHOD(Shader, _SetBuiltInViewMatrix) + { + LUAX_PREPARE(L, Shader); + + return 0; + } + + // shader:SetBuiltInProjectionMatrix() + LUAX_IMPL_METHOD(Shader, _SetBuiltInProjectionMatrix) + { + LUAX_PREPARE(L, Shader); + + return 0; + } + + // shader:SetBuiltInMVPMatrix() + LUAX_IMPL_METHOD(Shader, _SetBuiltInMVPMatrix) + { + LUAX_PREPARE(L, Shader); + + return 0; + } + + // shader:SetBuiltInColor() + LUAX_IMPL_METHOD(Shader, _SetBuiltInDrawColor) + { + LUAX_PREPARE(L, Shader); + + return 0; + } + } } diff --git a/source/modules/asura-core/graphics/gl.cpp b/source/modules/asura-core/graphics/gl.cpp index 08c50c3..5e6f216 100644 --- a/source/modules/asura-core/graphics/gl.cpp +++ b/source/modules/asura-core/graphics/gl.cpp @@ -21,6 +21,7 @@ namespace AsuraEngine OpenGL gl; OpenGL::OpenGL() + : mUpdateMVPMatrix(true) { #if ASURA_DEBUG ASSERT(!_instantiated); @@ -137,36 +138,50 @@ namespace AsuraEngine void OpenGL::PushMatrix() { state.matrix[state.matrixMode].Push(); + + mUpdateMVPMatrix = true; } void OpenGL::PopMatrix() { state.matrix[state.matrixMode].Pop(); + + mUpdateMVPMatrix = true; } void OpenGL::LoadIdentity() { state.matrix[state.matrixMode].LoadIdentity(); + + mUpdateMVPMatrix = true; } void OpenGL::Rotate(float angle) { state.matrix[state.matrixMode].Rotate(angle); + + mUpdateMVPMatrix = true; } void OpenGL::Translate(float x, float y) { state.matrix[state.matrixMode].Translate(x, y); + + mUpdateMVPMatrix = true; } void OpenGL::Scale(float x, float y) { state.matrix[state.matrixMode].Scale(x, y); + + mUpdateMVPMatrix = true; } 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); + + mUpdateMVPMatrix = true; } AEMath::Matrix44& OpenGL::GetMatrix(MatrixMode mode) @@ -176,9 +191,16 @@ namespace AsuraEngine AEMath::Matrix44 OpenGL::GetMVPMatrix() { - return state.matrix[MATRIX_MODE_MODEL].GetTop() - * state.matrix[MATRIX_MODE_MODEL].GetTop() - * state.matrix[MATRIX_MODE_MODEL].GetTop(); + if (mUpdateMVPMatrix) + { + Matrix44& m = state.matrix[MATRIX_MODE_MODEL].GetTop(); + Matrix44& v = state.matrix[MATRIX_MODE_VIEW].GetTop(); + Matrix44& p = state.matrix[MATRIX_MODE_PROJECTION].GetTop(); + state.mvpMatrix = p * (v * m); + + mUpdateMVPMatrix = false; + } + return state.mvpMatrix; } uint OpenGL::GetMatrixDepth() diff --git a/source/modules/asura-core/graphics/gl.h b/source/modules/asura-core/graphics/gl.h index a4d9de2..6c6ff30 100644 --- a/source/modules/asura-core/graphics/gl.h +++ b/source/modules/asura-core/graphics/gl.h @@ -105,11 +105,12 @@ namespace AsuraEngine /// struct { - Shader* shader; ///< ǰʹõshader - AEMath::Recti viewport; ///< ǰлHDC߱ڴСı߲ˢʱ䶯 - MatrixStack matrix[3]; ///< model, view, projection - MatrixMode matrixMode; ///< ǰľ - Color drawColor; ///< Ƶɫ + Shader* shader; ///< ǰʹõshader + AEMath::Recti viewport; ///< ǰлHDC߱ڴСı߲ˢʱ䶯 + MatrixStack matrix[3]; ///< model, view, projection + MatrixMode matrixMode; ///< ǰľ + AEMath::Matrix44 mvpMatrix; ///< mvp matrix + Color drawColor; ///< Ƶɫ } state; #if ASURA_GL_PROFILE @@ -149,6 +150,8 @@ namespace AsuraEngine //----------------------------------------------------------------------------// + bool mUpdateMVPMatrix; + }; /// diff --git a/source/modules/asura-core/graphics/shader.cpp b/source/modules/asura-core/graphics/shader.cpp index a0c14b4..833cef0 100644 --- a/source/modules/asura-core/graphics/shader.cpp +++ b/source/modules/asura-core/graphics/shader.cpp @@ -176,12 +176,6 @@ namespace AsuraEngine glUniform4f(loc, color.r, color.g, color.b, color.a); } - //void Shader::GetUniform() - //{ - // //if(gl.state.shader == this) - // // glGetUniformfv() - //} - uint Shader::GetGLTextureUnitCount() { GLint maxTextureUnits; @@ -250,5 +244,30 @@ namespace AsuraEngine glDisableVertexAttribArray(loc); } + void Shader::SetBuiltInModelMatrix(uint loc) + { + SetUniformMatrix44(loc, gl.GetMatrix(MATRIX_MODE_MODEL)); + } + + void Shader::SetBuiltInViewMatrix(uint loc) + { + SetUniformMatrix44(loc, gl.GetMatrix(MATRIX_MODE_VIEW)); + } + + void Shader::SetBuiltInProjectionMatrix(uint loc) + { + SetUniformMatrix44(loc, gl.GetMatrix(MATRIX_MODE_PROJECTION)); + } + + void Shader::SetBuiltInMVPMatrix(uint loc) + { + SetUniformMatrix44(loc, gl.GetMVPMatrix()); + } + + void Shader::SetBuiltInDrawColor(uint loc) + { + SetUniformColor(loc, gl.GetDrawColor()); + } + } }
\ No newline at end of file diff --git a/source/modules/asura-core/graphics/shader.h b/source/modules/asura-core/graphics/shader.h index b7bc70d..913332b 100644 --- a/source/modules/asura-core/graphics/shader.h +++ b/source/modules/asura-core/graphics/shader.h @@ -67,11 +67,14 @@ namespace AsuraEngine void SetUniformMatrix44(uint loc, const Math::Matrix44& mat44); bool SetUniformTexture(uint loc, const Texture& texture); - float GetUniformFloat(uint loc); - AEMath::Vector2f GetUniformVector2(uint loc); - AEMath::Vector3f GetUniformVector3(uint loc); - AEMath::Vector4f GetUniformVector4s(uint loc); - AEMath::Matrix44 GetUniformMatrix44(uint loc); + /// + /// ñ + /// + void SetBuiltInModelMatrix(uint loc); + void SetBuiltInViewMatrix(uint loc); + void SetBuiltInProjectionMatrix(uint loc); + void SetBuiltInMVPMatrix(uint loc); + void SetBuiltInDrawColor(uint loc); GLuint GetGLProgram(); @@ -99,7 +102,11 @@ namespace AsuraEngine LUAX_DECL_METHOD(_SetAttribute); LUAX_DECL_METHOD(_DisableAttribute); // uniform - LUAX_DECL_METHOD(_SetBuiltInUniforms); + LUAX_DECL_METHOD(_SetBuiltInModelMatrix); + LUAX_DECL_METHOD(_SetBuiltInViewMatrix); + LUAX_DECL_METHOD(_SetBuiltInProjectionMatrix); + LUAX_DECL_METHOD(_SetBuiltInMVPMatrix); + LUAX_DECL_METHOD(_SetBuiltInDrawColor); //----------------------------------------------------------------------------// diff --git a/source/modules/asura-utils/math/matrix44.cpp b/source/modules/asura-utils/math/matrix44.cpp index 4472cd8..39fc1de 100644 --- a/source/modules/asura-utils/math/matrix44.cpp +++ b/source/modules/asura-utils/math/matrix44.cpp @@ -57,6 +57,7 @@ namespace AsuraEngine // | e1 e5 e9 e13 | // | e2 e6 e10 e14 | // | e3 e7 e11 e15 | + // ҳ˾ת Matrix44 Matrix44::operator * (const Matrix44 & m) const { diff --git a/source/tests/win32/01-window/03_sub_menu.cpp b/source/tests/win32/01-window/03_sub_menu.cpp index 9ddc8d7..41023ea 100644 --- a/source/tests/win32/01-window/03_sub_menu.cpp +++ b/source/tests/win32/01-window/03_sub_menu.cpp @@ -33,15 +33,13 @@ string vert = R"( in vec2 position; in vec2 uv; -uniform mat4 asura_projection_matrix; -uniform mat4 asura_model_matrix; -uniform mat4 asura_view_matrix; +uniform mat4 asura_mvp; out vec2 texCoord; void main() { - gl_Position = asura_projection_matrix * asura_view_matrix * asura_model_matrix * vec4(position, 0, 1); + gl_Position = asura_mvp * vec4(position, 0, 1); texCoord = uv; } )"; @@ -64,9 +62,7 @@ struct { int pos; int tex; - int m; - int v; - int p; + int mvp; int color; } locs; @@ -170,13 +166,12 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, gl.SetMatrixMode(MATRIX_MODE_MODEL); gl.LoadIdentity(); gl.Translate(100, 100); - shader->SetUniformMatrix44(locs.m, gl.GetMatrix(MATRIX_MODE_MODEL)); - shader->SetUniformMatrix44(locs.v, gl.GetMatrix(MATRIX_MODE_VIEW)); - shader->SetUniformMatrix44(locs.p, gl.GetMatrix(MATRIX_MODE_PROJECTION)); + gl.SetDrawColor(1, 1, 1, 1); + shader->SetBuiltInMVPMatrix(locs.mvp); + shader->SetBuiltInDrawColor(locs.color); shader->SetAttribute(locs.pos, vb, 0, 4); shader->SetAttribute(locs.tex, vb, 2, 4); - gl.SetDrawColor(1, 1, 0, 1); - shader->SetUniformColor(locs.color, gl.GetDrawColor()); + //glLineWidth(1); gl.DrawArrays(GL_LINE_STRIP, 0, 5); //gl.DrawArrays(GL_TRIANGLE_STRIP, 0, 4); @@ -216,7 +211,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, wglMakeCurrent(hdc, glc); RECT rect; - GetWindowRect(hwnd, &rect); + GetClientRect(hwnd, &rect); viewport.Set(0, 0, rect.right - rect.left, rect.bottom - rect.top); if (!gl.Init(viewport)) return 0; @@ -255,9 +250,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, vb = new VertexBuffer(BUFFER_USAGE_STATIC, BUFFER_DATA_TYPE_FLOAT, sizeof(v)); vb->Fill(v, sizeof(v)); }; - locs.m = shader->GetUniformLocation("asura_model_matrix"); - locs.v = shader->GetUniformLocation("asura_view_matrix"); - locs.p = shader->GetUniformLocation("asura_projection_matrix"); + locs.mvp = shader->GetUniformLocation("asura_mvp"); locs.color = shader->GetUniformLocation("color"); locs.pos = shader->GetAttributeLocation("position"); locs.tex = shader->GetAttributeLocation("uv"); |