2018-11-30 13:57:57 +08:00
|
|
|
|
#include<hgl/render/RenderDevice.h>
|
|
|
|
|
#include<hgl/render/RenderWindow.h>
|
|
|
|
|
#include<iostream>
|
2018-11-30 16:10:41 +08:00
|
|
|
|
#include<GLEWCore/glew.h>
|
2018-11-30 16:50:08 +08:00
|
|
|
|
#include<hgl/render/Shader.h>
|
|
|
|
|
#include<hgl/math/Math.h>
|
2018-11-30 13:57:57 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
|
2018-11-30 16:50:08 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-30 16:10:41 +08:00
|
|
|
|
constexpr GLfloat clear_color[4]=
|
|
|
|
|
{
|
|
|
|
|
77.f/255.f,
|
|
|
|
|
78.f/255.f,
|
|
|
|
|
83.f/255.f,
|
|
|
|
|
1.f
|
|
|
|
|
};
|
2018-11-30 13:57:57 +08:00
|
|
|
|
|
2018-11-30 16:10:41 +08:00
|
|
|
|
constexpr GLfloat clear_depth=1.0f;
|
2018-11-30 13:57:57 +08:00
|
|
|
|
|
|
|
|
|
void draw()
|
|
|
|
|
{
|
2018-11-30 16:10:41 +08:00
|
|
|
|
glClearBufferfv(GL_COLOR,0,clear_color);
|
|
|
|
|
glClearBufferfv(GL_DEPTH,0,&clear_depth);
|
2018-11-30 13:57:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
RenderDevice *device=CreateRenderDeviceGLFW();
|
|
|
|
|
|
|
|
|
|
if(!device)
|
|
|
|
|
{
|
|
|
|
|
std::cerr<<"Create RenderDevice(GLFW) failed."<<std::endl;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!device->Init())
|
|
|
|
|
{
|
|
|
|
|
std::cerr<<"Init RenderDevice(GLFW) failed."<<std::endl;
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WindowSetup ws;
|
|
|
|
|
|
2018-11-30 16:10:41 +08:00
|
|
|
|
ws.Name=U8_TEXT("Direct use \"OpenGL Core API\" Render");
|
2018-11-30 13:57:57 +08:00
|
|
|
|
|
|
|
|
|
RenderSetup rs;
|
|
|
|
|
|
2018-11-30 17:25:58 +08:00
|
|
|
|
RenderWindow *win=device->Create(screen_width,screen_height,&ws,&rs);
|
2018-11-30 16:50:08 +08:00
|
|
|
|
|
|
|
|
|
win->MakeToCurrent(); //切换当前窗口到前台
|
|
|
|
|
|
|
|
|
|
InitMatrix();
|
|
|
|
|
if(!InitShader())
|
|
|
|
|
{
|
|
|
|
|
std::cerr<<"init shader failed."<<std::endl;
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
2018-11-30 13:57:57 +08:00
|
|
|
|
|
|
|
|
|
win->Show();
|
|
|
|
|
|
|
|
|
|
while(win->IsOpen())
|
|
|
|
|
{
|
|
|
|
|
draw();
|
|
|
|
|
|
|
|
|
|
win->SwapBuffer(); //交换前后台显示缓冲区
|
|
|
|
|
win->PollEvent(); //处理窗口事件
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete win;
|
|
|
|
|
delete device;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|