diff --git a/example/DirectGLRender/main.cpp b/example/DirectGLRender/main.cpp index cca14813..b50141de 100644 --- a/example/DirectGLRender/main.cpp +++ b/example/DirectGLRender/main.cpp @@ -2,9 +2,77 @@ #include #include #include +#include +#include using namespace hgl; +constexpr uint screen_width=1280; +constexpr uint screen_height=720; + +Matrix4f ortho_2d_matrix; + +void InitMatrix() +{ + ortho_2d_matrix=ortho2d(screen_width,screen_height, //2D画面宽高 + false); //Y轴使用底为0顶为1 +} + + +constexpr char vertex_shader[]=R"( +#version 330 core + +uniform mat4 ModelViewProjectionMatrix; + +in vec2 Vertex; +in vec3 Color; + +out vec4 FragmentColor; + +void main() +{ + vec4 Position; + + FragmentColor=vec4(Color,1.0); + + Position=vec4(Vertex,0.0,1.0); + + gl_Position=Position*ModelViewProjectionMatrix; +})"; + +constexpr char fragment_shader[]=R"( +#version 330 core + +in vec4 FragmentColor; +out vec4 FragColor; + +void main() +{ + FragColor=vec4(FragmentColor.rgb,1); +})"; + +Shader shader; + +bool InitShader() +{ + if(!shader.AddVertexShader(vertex_shader)) + return(false); + + if(!shader.AddFragmentShader(fragment_shader)) + return(false); + + if(!shader.Build()) + return(false); + + if(!shader.Use()) + return(false); + + if(!shader.SetUniformMatrix4fv("ModelViewProjectionMatrix",ortho_2d_matrix)) + return(false); + + return(true); +} + constexpr GLfloat clear_color[4]= { 77.f/255.f, @@ -43,9 +111,17 @@ int main(void) RenderSetup rs; - RenderWindow *win=device->CreateWindow(1280,720,&ws,&rs); + RenderWindow *win=device->CreateWindow(screen_width,screen_height,&ws,&rs); + + win->MakeToCurrent(); //切换当前窗口到前台 + + InitMatrix(); + if(!InitShader()) + { + std::cerr<<"init shader failed."<MakeToCurrent(); //切换当前窗口到前台 win->Show(); while(win->IsOpen()) diff --git a/inc/hgl/algorithm/Math.h b/inc/hgl/math/Math.h similarity index 93% rename from inc/hgl/algorithm/Math.h rename to inc/hgl/math/Math.h index 953ce538..5ff80fdf 100644 --- a/inc/hgl/algorithm/Math.h +++ b/inc/hgl/math/Math.h @@ -1,4 +1,4 @@ -#ifndef HGL_ALGORITHM_VECTOR_MATH_INCLUDE +#ifndef HGL_ALGORITHM_VECTOR_MATH_INCLUDE #define HGL_ALGORITHM_VECTOR_MATH_INCLUDE #include @@ -6,11 +6,11 @@ //注:GLM/CML(OpenGLMode)是列矩阵,计算坐标matrix*pos // 而MGL是行矩阵,需要反过来pos*matrix -#include // Game Math and Geometry Library +#include // Game Math and Geometry Library namespace hgl { - namespace algorithm + namespace math { double Lsin(int angle); ///<低精度sin计算,注意传入的参数为角度而非弧度 double Lcos(int angle); ///<低精度cos计算,注意传入的参数为角度而非弧度 @@ -150,6 +150,6 @@ namespace hgl result.x = center.x + ((source.x - center.x)*ac - (source.y - center.y)*as); result.y = center.y + ((source.x - center.x)*as + (source.y - center.y)*ac); } - }//namespace algorithm + }//namespace math }//namespace hgl #endif//HGL_ALGORITHM_VECTOR_MATH_INCLUDE diff --git a/inc/hgl/algorithm/MathMGL.h b/inc/hgl/math/MathMGL.h similarity index 90% rename from inc/hgl/algorithm/MathMGL.h rename to inc/hgl/math/MathMGL.h index 67eb7052..df53c5e2 100644 --- a/inc/hgl/algorithm/MathMGL.h +++ b/inc/hgl/math/MathMGL.h @@ -1,4 +1,4 @@ -#ifndef HGL_ALGORITHM_VECTOR_MATH_MGL_INCLUDE +#ifndef HGL_ALGORITHM_VECTOR_MATH_MGL_INCLUDE #define HGL_ALGORITHM_VECTOR_MATH_MGL_INCLUDE #ifdef _MSC_VER @@ -119,11 +119,21 @@ namespace hgl return m.Inverted(); } - inline Matrix4f ortho2d(float width,float height,float znear=0,float zfar=1) + /** + * 生成一个2D正角视图矩阵 + * @param width 宽 + * @param height 高 + * @param top_to_bottom 从上到下(是否最顶部y轴为0,默认否) + * @param znear 近平面z值 + * @param zfar 远平台z值 + */ + inline Matrix4f ortho2d(float width,float height,bool top_to_bottom=false,float znear=0,float zfar=1) { //MathGeoLib生成的2D正交矩阵中心是0,0,所以需要偏移 - return Matrix4f::OpenGLOrthoProjRH(znear,zfar,width,height)*Matrix4f::Scale(1,-1,1)*Matrix4f::Translate(-(width/2.0f),-(height/2.0f),0); + return Matrix4f::OpenGLOrthoProjRH(znear,zfar,width,height) + *Matrix4f::Scale(1,top_to_bottom?-1:1,1) + *Matrix4f::Translate(-(width/2.0f),-(height/2.0f),0); } inline Matrix4f translate(const Vector3f &v) diff --git a/inc/hgl/render/Shader.h b/inc/hgl/render/Shader.h index 65ea082a..9817aa2c 100644 --- a/inc/hgl/render/Shader.h +++ b/inc/hgl/render/Shader.h @@ -3,7 +3,7 @@ #include #include -#include +#include // #include // #include namespace hgl diff --git a/src/RenderDriver/CMakeLists.txt b/src/RenderDriver/CMakeLists.txt index 9ae3c2d7..3a8652b1 100644 --- a/src/RenderDriver/CMakeLists.txt +++ b/src/RenderDriver/CMakeLists.txt @@ -1,2 +1,4 @@ -add_library(ULRE.RenderDriver STATIC GLCore/RenderDriverGLCore.cpp) +add_library(ULRE.RenderDriver STATIC GLCore/RenderDriverGLCore.cpp + GLSL.cpp + Shader.cpp) diff --git a/src/RenderDriver/GLSL.cpp b/src/RenderDriver/GLSL.cpp index 76ad356c..841add90 100644 --- a/src/RenderDriver/GLSL.cpp +++ b/src/RenderDriver/GLSL.cpp @@ -2,7 +2,7 @@ //#include #include #include -#include +#include namespace hgl { @@ -67,7 +67,7 @@ namespace hgl delete[] log; - LOG_ERROR(shader_source); + //LOG_ERROR(shader_source); glDeleteShader(shader); @@ -105,7 +105,7 @@ namespace hgl if(!shader_codes ||!(*shader_codes))return(false); - shader_index[shader_type]=CompileShader(OpenGLShaderType[shader_type],ShaderName[shader_type],shader_codes); + shader_index[shader_type]=CompileShader(OpenGLShaderType[shader_type],ShaderTypeName[shader_type],shader_codes); if(!shader_index[shader_type]) return(false);