2018-11-30 17:46:58 +08:00
|
|
|
|
#include<hgl/graph/RenderDevice.h>
|
2018-12-07 16:04:27 +08:00
|
|
|
|
#include<hgl/graph/RenderDriver.h>
|
2018-11-30 17:46:58 +08:00
|
|
|
|
#include<hgl/graph/RenderWindow.h>
|
2018-11-30 13:57:57 +08:00
|
|
|
|
#include<iostream>
|
2018-11-30 16:10:41 +08:00
|
|
|
|
#include<GLEWCore/glew.h>
|
2018-11-30 17:46:58 +08:00
|
|
|
|
#include<hgl/graph/Shader.h>
|
2018-11-30 16:50:08 +08:00
|
|
|
|
#include<hgl/math/Math.h>
|
2018-12-05 21:58:03 +08:00
|
|
|
|
#include<hgl/graph/VertexArray.h>
|
2018-11-30 13:57:57 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
2018-11-30 19:38:36 +08:00
|
|
|
|
using namespace hgl::graph;
|
2018-11-30 13:57:57 +08:00
|
|
|
|
|
2018-11-30 16:50:08 +08:00
|
|
|
|
constexpr uint screen_width=1280;
|
|
|
|
|
constexpr uint screen_height=720;
|
|
|
|
|
|
|
|
|
|
constexpr char vertex_shader[]=R"(
|
|
|
|
|
#version 330 core
|
|
|
|
|
|
|
|
|
|
in vec2 Vertex;
|
|
|
|
|
in vec3 Color;
|
|
|
|
|
|
|
|
|
|
out vec4 FragmentColor;
|
|
|
|
|
|
|
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
FragmentColor=vec4(Color,1.0);
|
|
|
|
|
|
2018-12-07 16:04:27 +08:00
|
|
|
|
gl_Position=vec4(Vertex,0.0,1.0);
|
2018-11-30 16:50:08 +08:00
|
|
|
|
})";
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-07 16:04:27 +08:00
|
|
|
|
VB2f *vb_vertex=nullptr;
|
2018-12-05 21:58:03 +08:00
|
|
|
|
VB3f *vb_color=nullptr;
|
|
|
|
|
VertexArray *va=nullptr;
|
|
|
|
|
|
2018-12-07 16:04:27 +08:00
|
|
|
|
constexpr float vertex_data[]={0.0f,0.5f, -0.5f,-0.5f, 0.5f,-0.5f };
|
2018-12-05 21:58:03 +08:00
|
|
|
|
constexpr float color_data[]={1,0,0, 0,1,0, 0,0,1 };
|
|
|
|
|
|
2018-12-07 16:04:27 +08:00
|
|
|
|
void BindVBO2VAO(const int vao,const int binding_index,const int shader_location,VertexBufferBase *vb)
|
|
|
|
|
{
|
|
|
|
|
glVertexArrayAttribBinding(vao,shader_location,binding_index);
|
|
|
|
|
glVertexArrayAttribFormat(vao,shader_location,vb->GetComponent(),vb->GetDataType(),GL_FALSE,0);
|
|
|
|
|
glEnableVertexArrayAttrib(vao,shader_location);
|
2018-12-07 16:43:10 +08:00
|
|
|
|
glVertexArrayVertexBuffer(vao,binding_index,vb->GetBufferIndex(),0,vb->GetStride());
|
2018-12-07 16:04:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 21:58:03 +08:00
|
|
|
|
void InitVertexBuffer()
|
|
|
|
|
{
|
2018-12-07 16:43:10 +08:00
|
|
|
|
vb_vertex=new VB2f(3,vertex_data);
|
|
|
|
|
vb_color=new VB3f(3,color_data);
|
2018-12-05 21:58:03 +08:00
|
|
|
|
|
|
|
|
|
va=new VertexArray(GL_TRIANGLES, //画三角形
|
|
|
|
|
2); //两个属性
|
|
|
|
|
|
2018-12-07 19:38:20 +08:00
|
|
|
|
const int vertex_location=shader.GetAttribLocation("Vertex"); ///<取得顶点数据输入流对应的shader地址
|
|
|
|
|
const int color_location=shader.GetAttribLocation("Color"); ///<取得颜色数据输入流对应的shader地址
|
2018-12-05 21:58:03 +08:00
|
|
|
|
|
|
|
|
|
int binding_index=0; //绑定点
|
|
|
|
|
|
2018-12-07 16:04:27 +08:00
|
|
|
|
const int vao=va->GetVAO();
|
|
|
|
|
|
|
|
|
|
va->SetVertexBuffer(vb_vertex);
|
|
|
|
|
va->SetColorBuffer(vb_color,HGL_PC_RGB);
|
|
|
|
|
|
|
|
|
|
BindVBO2VAO(vao,binding_index,vertex_location,vb_vertex);
|
|
|
|
|
++binding_index;
|
|
|
|
|
BindVBO2VAO(vao,binding_index,color_location,vb_color);
|
2018-12-05 21:58:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
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-12-07 16:04:27 +08:00
|
|
|
|
|
|
|
|
|
va->Draw();
|
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(); //切换当前窗口到前台
|
|
|
|
|
|
2018-12-07 16:04:27 +08:00
|
|
|
|
InitOpenGLDebug(); //初始化OpenGL调试输出
|
|
|
|
|
|
2018-11-30 16:50:08 +08:00
|
|
|
|
if(!InitShader())
|
|
|
|
|
{
|
|
|
|
|
std::cerr<<"init shader failed."<<std::endl;
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
2018-11-30 13:57:57 +08:00
|
|
|
|
|
2018-12-05 21:58:03 +08:00
|
|
|
|
InitVertexBuffer();
|
|
|
|
|
|
2018-11-30 13:57:57 +08:00
|
|
|
|
win->Show();
|
|
|
|
|
|
|
|
|
|
while(win->IsOpen())
|
|
|
|
|
{
|
|
|
|
|
draw();
|
|
|
|
|
|
|
|
|
|
win->SwapBuffer(); //交换前后台显示缓冲区
|
|
|
|
|
win->PollEvent(); //处理窗口事件
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete win;
|
|
|
|
|
delete device;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|