Math相关头文件移到hgl/math目录

This commit is contained in:
hyzboy 2018-11-30 16:50:08 +08:00
parent d5450325ba
commit 4fb13ced34
6 changed files with 102 additions and 14 deletions

View File

@ -2,9 +2,77 @@
#include<hgl/render/RenderWindow.h>
#include<iostream>
#include<GLEWCore/glew.h>
#include<hgl/render/Shader.h>
#include<hgl/math/Math.h>
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."<<std::endl;
return -3;
}
win->MakeToCurrent(); //切换当前窗口到前台
win->Show();
while(win->IsOpen())

View File

@ -1,4 +1,4 @@
#ifndef HGL_ALGORITHM_VECTOR_MATH_INCLUDE
#ifndef HGL_ALGORITHM_VECTOR_MATH_INCLUDE
#define HGL_ALGORITHM_VECTOR_MATH_INCLUDE
#include<hgl/type/DataType.h>
@ -6,11 +6,11 @@
//注GLM/CML(OpenGLMode)是列矩阵,计算坐标matrix*pos
// 而MGL是行矩阵需要反过来pos*matrix
#include<hgl/algorithm/MathMGL.h> // Game Math and Geometry Library
#include<hgl/math/MathMGL.h> // 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

View File

@ -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)

View File

@ -3,7 +3,7 @@
#include<hgl/type/BaseString.h>
#include<hgl/type/Map.h>
#include<hgl/algorithm/Math.h>
#include<hgl/math/Math.h>
// #include<hgl/graph/UBO.h>
// #include<hgl/graph/SSBO.h>
namespace hgl

View File

@ -1,2 +1,4 @@
add_library(ULRE.RenderDriver STATIC GLCore/RenderDriverGLCore.cpp)
add_library(ULRE.RenderDriver STATIC GLCore/RenderDriverGLCore.cpp
GLSL.cpp
Shader.cpp)

View File

@ -2,7 +2,7 @@
//#include<hgl/LogInfo.h>
#include<hgl/type/Smart.h>
#include<malloc.h>
#include<GL/glext.h>
#include<GLEWCore/glew.h>
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);