From 0ea658cefa0f9b1dac8ead9572559228e117dbb5 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 7 Dec 2018 19:39:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=A7=8BDirectGLTexture=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=B7=A5=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/CMakeLists.txt | 1 + example/DirectGLTexture/CMakeLists.txt | 4 + example/DirectGLTexture/main.cpp | 178 +++++++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 example/DirectGLTexture/CMakeLists.txt create mode 100644 example/DirectGLTexture/main.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index f869b0ff..1e07a0d2 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -2,4 +2,5 @@ add_subdirectory(OutputGLInfo) add_subdirectory(NullWindow) add_subdirectory(DirectGLRender) +add_subdirectory(DirectGLTexture) # add_subdirectory(DrawTriangle) diff --git a/example/DirectGLTexture/CMakeLists.txt b/example/DirectGLTexture/CMakeLists.txt new file mode 100644 index 00000000..09cad961 --- /dev/null +++ b/example/DirectGLTexture/CMakeLists.txt @@ -0,0 +1,4 @@ +add_executable(DirectGLTexture main.cpp) + +target_link_libraries(DirectGLTexture PRIVATE ${ULRE}) + diff --git a/example/DirectGLTexture/main.cpp b/example/DirectGLTexture/main.cpp new file mode 100644 index 00000000..302b26c8 --- /dev/null +++ b/example/DirectGLTexture/main.cpp @@ -0,0 +1,178 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace hgl; +using namespace hgl::graph; + +constexpr uint screen_width=1280; +constexpr uint screen_height=720; + +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 + +sampler2D Texture0; + +in vec4 FragmentTexCoord; +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); +} + +VB2f *vb_vertex=nullptr; +VB2f *vb_texcoord=nullptr; +VertexArray *va=nullptr; + +constexpr float vertex_data[]={ -0.5f, 0.5f, + -0.5f,-0.5f, + 0.5f, 0.5f, + 0.5f,-0.5f +}; + +constexpr float texcoord_data[]={ 0,0, + 0,1, + 1,0, + 1,1 }; + + +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); + glVertexArrayVertexBuffer(vao,binding_index,vb->GetBufferIndex(),0,vb->GetStride()); +} + +void InitVertexBuffer() +{ + vb_vertex=new VB2f(4,vertex_data); + vb_texcoord=new VB2f(4,texcoord_data); + + va=new VertexArray(GL_TRIANGLES, //画三角形 + 2); //两个属性 + + const int vertex_location=shader.GetAttribLocation("Vertex"); ///<取得顶点数据输入流对应的shader地址 + const int texcoord_location=shader.GetAttribLocation("TexCoord"); ///<取得纹理坐标数据输入流对应的shader地址 + + int binding_index=0; //绑定点 + + const int vao=va->GetVAO(); + + va->SetVertexBuffer(vb_vertex); + va->AddVertexAttribBuffer(vb_texcoord); + + BindVBO2VAO(vao,binding_index,vertex_location,vb_vertex); + ++binding_index; + BindVBO2VAO(vao,binding_index,texcoord_location,vb_texcoord); +} + +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); + + va->Draw(); +} + +int main(void) +{ + RenderDevice *device=CreateRenderDeviceGLFW(); + + if(!device) + { + std::cerr<<"Create RenderDevice(GLFW) failed."<Init()) + { + std::cerr<<"Init RenderDevice(GLFW) failed."<Create(screen_width,screen_height,&ws,&rs); + + win->MakeToCurrent(); //切换当前窗口到前台 + + InitOpenGLDebug(); //初始化OpenGL调试输出 + + if(!InitShader()) + { + std::cerr<<"init shader failed."<Show(); + + while(win->IsOpen()) + { + draw(); + + win->SwapBuffer(); //交换前后台显示缓冲区 + win->PollEvent(); //处理窗口事件 + } + + delete win; + delete device; + + return 0; +}