2018-12-07 19:39:00 +08:00
|
|
|
|
#include<hgl/graph/RenderDevice.h>
|
|
|
|
|
#include<hgl/graph/RenderDriver.h>
|
|
|
|
|
#include<hgl/graph/RenderWindow.h>
|
|
|
|
|
#include<iostream>
|
2019-03-27 21:14:23 +08:00
|
|
|
|
#include<hgl/graph/Renderable.h>
|
2018-12-07 19:39:00 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
2018-12-07 21:02:58 +08:00
|
|
|
|
constexpr uint screen_width=720;
|
2018-12-07 19:39:00 +08:00
|
|
|
|
constexpr uint screen_height=720;
|
|
|
|
|
|
2018-12-07 21:02:58 +08:00
|
|
|
|
uint texture_id=0;
|
|
|
|
|
|
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
namespace graph
|
|
|
|
|
{
|
|
|
|
|
bool LoadTGATexture(const std::string &filename,uint &tex_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-07 19:39:00 +08:00
|
|
|
|
constexpr char vertex_shader[]=R"(
|
|
|
|
|
#version 330 core
|
|
|
|
|
|
|
|
|
|
in vec2 Vertex;
|
|
|
|
|
in vec2 TexCoord;
|
|
|
|
|
|
|
|
|
|
out vec2 FragmentTexCoord;
|
|
|
|
|
|
|
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
FragmentTexCoord=TexCoord;
|
|
|
|
|
|
|
|
|
|
gl_Position=vec4(Vertex,0.0,1.0);
|
|
|
|
|
})";
|
|
|
|
|
|
|
|
|
|
constexpr char fragment_shader[]=R"(
|
|
|
|
|
#version 330 core
|
|
|
|
|
|
2018-12-07 21:20:12 +08:00
|
|
|
|
uniform sampler2D TextureLena;
|
2018-12-07 19:39:00 +08:00
|
|
|
|
|
2018-12-07 21:02:58 +08:00
|
|
|
|
in vec2 FragmentTexCoord;
|
2018-12-07 19:39:00 +08:00
|
|
|
|
out vec4 FragColor;
|
|
|
|
|
|
|
|
|
|
void main()
|
|
|
|
|
{
|
2018-12-07 21:20:12 +08:00
|
|
|
|
FragColor=texture(TextureLena,FragmentTexCoord);
|
2018-12-07 19:39:00 +08:00
|
|
|
|
})";
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 21:14:23 +08:00
|
|
|
|
ArrayBuffer *vb_vertex=nullptr;
|
|
|
|
|
ArrayBuffer *vb_texcoord=nullptr;
|
2018-12-07 19:39:00 +08:00
|
|
|
|
VertexArray *va=nullptr;
|
2019-03-27 21:14:23 +08:00
|
|
|
|
Renderable *render_obj=nullptr;
|
2018-12-07 19:39:00 +08:00
|
|
|
|
|
2018-12-08 13:50:50 +08:00
|
|
|
|
constexpr float vertex_data[]={ -0.75f, 0.75f,
|
|
|
|
|
-0.75f,-0.75f,
|
|
|
|
|
0.75f, 0.75f,
|
|
|
|
|
0.75f,-0.75f
|
2018-12-07 19:39:00 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr float texcoord_data[]={ 0,0,
|
|
|
|
|
0,1,
|
|
|
|
|
1,0,
|
|
|
|
|
1,1 };
|
|
|
|
|
|
|
|
|
|
void InitVertexBuffer()
|
|
|
|
|
{
|
2019-03-27 21:14:23 +08:00
|
|
|
|
vb_vertex=CreateVBO(VB2f(4,vertex_data));
|
|
|
|
|
vb_texcoord=CreateVBO(VB2f(4,texcoord_data));
|
2018-12-07 19:39:00 +08:00
|
|
|
|
|
2019-03-27 21:14:23 +08:00
|
|
|
|
va=new VertexArray();
|
2018-12-07 19:39:00 +08:00
|
|
|
|
|
|
|
|
|
const int vertex_location=shader.GetAttribLocation("Vertex"); ///<取得顶点数据输入流对应的shader地址
|
|
|
|
|
const int texcoord_location=shader.GetAttribLocation("TexCoord"); ///<取得纹理坐标数据输入流对应的shader地址
|
|
|
|
|
|
2019-03-27 21:14:23 +08:00
|
|
|
|
va->SetPosition(vertex_location,vb_vertex);
|
|
|
|
|
va->AddBuffer(texcoord_location,vb_texcoord);
|
|
|
|
|
|
|
|
|
|
render_obj=new Renderable(GL_TRIANGLE_STRIP,va);
|
2018-12-07 19:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-07 21:02:58 +08:00
|
|
|
|
bool InitTexture()
|
|
|
|
|
{
|
2018-12-07 21:20:12 +08:00
|
|
|
|
if(!LoadTGATexture("lena.tga",texture_id))
|
2018-12-07 21:02:58 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2018-12-07 21:20:12 +08:00
|
|
|
|
int texture_location=shader.GetUniformLocation("TextureLena");
|
2018-12-07 21:02:58 +08:00
|
|
|
|
|
|
|
|
|
glBindTextureUnit(0,texture_id);
|
|
|
|
|
|
|
|
|
|
shader.SetUniform1i(texture_location,0);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-07 19:39:00 +08:00
|
|
|
|
constexpr GLfloat clear_color[4]=
|
|
|
|
|
{
|
|
|
|
|
77.f/255.f,
|
|
|
|
|
78.f/255.f,
|
|
|
|
|
83.f/255.f,
|
|
|
|
|
1.f
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr GLfloat clear_depth=1.0f;
|
|
|
|
|
|
|
|
|
|
void draw()
|
|
|
|
|
{
|
|
|
|
|
glClearBufferfv(GL_COLOR,0,clear_color);
|
|
|
|
|
glClearBufferfv(GL_DEPTH,0,&clear_depth);
|
|
|
|
|
|
2019-03-27 21:14:23 +08:00
|
|
|
|
render_obj->Draw();
|
2018-12-07 19:39:00 +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;
|
|
|
|
|
|
|
|
|
|
ws.Name=U8_TEXT("Direct use \"OpenGL Core API\" Render");
|
|
|
|
|
|
|
|
|
|
RenderSetup rs;
|
|
|
|
|
|
|
|
|
|
RenderWindow *win=device->Create(screen_width,screen_height,&ws,&rs);
|
|
|
|
|
|
|
|
|
|
win->MakeToCurrent(); //切换当前窗口到前台
|
|
|
|
|
|
|
|
|
|
InitOpenGLDebug(); //初始化OpenGL调试输出
|
|
|
|
|
|
|
|
|
|
if(!InitShader())
|
|
|
|
|
{
|
|
|
|
|
std::cerr<<"init shader failed."<<std::endl;
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InitVertexBuffer();
|
|
|
|
|
|
2018-12-07 21:02:58 +08:00
|
|
|
|
if(!InitTexture())
|
|
|
|
|
return -4;
|
|
|
|
|
|
2018-12-07 19:39:00 +08:00
|
|
|
|
win->Show();
|
|
|
|
|
|
|
|
|
|
while(win->IsOpen())
|
|
|
|
|
{
|
|
|
|
|
draw();
|
|
|
|
|
|
|
|
|
|
win->SwapBuffer(); //交换前后台显示缓冲区
|
|
|
|
|
win->PollEvent(); //处理窗口事件
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 21:14:23 +08:00
|
|
|
|
delete render_obj;
|
|
|
|
|
delete va;
|
|
|
|
|
delete vb_texcoord;
|
|
|
|
|
delete vb_vertex;
|
|
|
|
|
|
2018-12-07 19:39:00 +08:00
|
|
|
|
delete win;
|
|
|
|
|
delete device;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|