码迷,mamicode.com
首页 > 编程语言 > 详细

cppgl:一个对现代OpenGL的C++封装

时间:2015-05-04 23:53:30      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

技术分享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.0f
51     };
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++封装

标签:

原文地址:http://www.cnblogs.com/iichenbf/p/cppgl-a-cpp-opengl-wrapper.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!