cppgl:一個對現代OpenGL的C++封裝

來源:互聯網
上載者:User

標籤:

a C++ wrapper for modern OpenGL (https://github.com/iichenbf/cppgl)

最近要搞個2D的圖形的玩意,可網上對OpenGL的C++封裝更新到c++11的幾乎沒有,那就比較糟糕了,還好GL封裝過程也比較簡單,現代的OpenGL雖然本身是C API但是在背後也有一定的對象的概念,於是就有了這個。主要的特性是,它使用了SOIL來載入圖片,如果你要port到其它平台,你可以用Libpng等改寫Image和IO部分;使用glm來做數學部分的定義和計算;GL對象用shared_prt來管理,所以記憶體方面沒什麼大問題;https://github.com/iichenbf/cppgl  

Example code:

 1 #include "cppgl.h" 2  3 #include <GLFW/glfw3.h> 4 #include <chrono> 5 #include <thread> 6  7  8 static const char* vertexSource = 9 "#version 150\n"10 "\n"11 "in vec2 position;\n"12 "in vec2 texcoord;\n"13 "out vec2 Texcoord;\n"14 "\n"15 "\n"16 "void main()\n"17 "{\n"18 "    Texcoord = vec2(texcoord.x, -texcoord.y);\n"19 "    gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n"20 "}\n";21 22 static const char* fragmentSource =23 "#version 150\n"24 "\n"25 "in vec2 Texcoord;\n"26 "\n"27 "out vec4 outColor;\n"28 "uniform sampler2D tex;\n"29 "\n"30 "void main()\n"31 "{\n"32 "    outColor = texture(tex, Texcoord);\n"33 "}\n";34 35 void cppgl_test()36 {37     makeContext();38     auto context = Context::Create();39     SPShader vert = Shader::create(ShaderType::Vertex, vertexSource);40     SPShader freg = Shader::create(ShaderType::Fragment, fragmentSource);41     SPProgram program = Program::create(vert, freg);42     context->useProgram(program);43 44     float vertices[] = {45         -0.5f, -0.5f, 0.0f, 0.0f,46         -0.5f, 0.5f, 0.0f, 1.0f,47         0.5f, 0.5f, 1.0f, 1.0f,48         0.5f, 0.5f, 1.0f, 1.0f,49         0.5f, -0.5f, 1.0f, 0.0f,50         -0.5f, -0.5f, 0.0f, 0.0f51     };52     auto image = Image::create("shooting_arrow.png");53     image->load();54     SPTexture texture = Texture::create(image, InternalFormat::RGBA);55     texture->setWrapping(Wrapping::Repeat, Wrapping::Repeat);56     texture->setFilters(Filter::Linear, Filter::Linear);57 58     context->bindTexture(texture, 0);59     SPVertexBuffer vbo = VertexBuffer::create(vertices, sizeof(vertices), BufferUsage::StaticDraw);60     SPVertexArray vao = VertexArray::create();61     vao->bindAttribute(program->getAttribute("position"), *vbo, Type::Float, 2, 4*sizeof(float), NULL);62     vao->bindAttribute(program->getAttribute("texcoord"), *vbo, Type::Float, 2, 4*sizeof(float), 2*sizeof(float));63     std::chrono::microseconds frameInterval{(long long)(1.0f/60.0f * 1000.0f * 1000.0f)};64 65     context->clearColor({0.5f, 0.0f, 0.0f, 1.0f});66 67     context->enable(Capability::Blend);68     context->blendFunc(BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA);69 70     while (!glfwWindowShouldClose(_glfwWindow))71     {72         auto last = std::chrono::steady_clock::now();73         context->clear();74         context->drawArrays(*vao, Primitive::Triangles, 0, 6);75         glfwSwapBuffers(_glfwWindow);76         glfwPollEvents();77 78         auto now = std::chrono::steady_clock::now();79 80         std::this_thread::sleep_for(frameInterval - (now - last));81     }82 83     glfwDestroyWindow(_glfwWindow);84     glfwTerminate();85 }86  87 88  

 

cppgl:一個對現代OpenGL的C++封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.