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

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  
时间: 2024-08-07 04:32:48

cppgl:一个对现代OpenGL的C++封装的相关文章

OGLplus 0.54.0 发布,OpenGL 的 C++ 封装库

OGLplus 0.54.0 发布,此版本主要是 bug 修复,更新了快速开始文档,构建系统,重构了反馈帮助类,修复了移植系统后平台不支持原始重启指数的问题. 改进列表 ======= - 以命名空间级别重新实现了 ``TransformFeedback::Activator`` 和 ``TranformFeedback::Pauser`` 类 - A new overload of ``Context::ClearColor`` was added. - A bug in multiplica

OGLplus 0.55.0 发布,OpenGL 的 C++ 封装库

OGLplus 0.55.0 发布,此版本添加了 ``ClientContext`` 类,用来维护(暂时只有部分) GL 状态,允许 push/pop 独立状态变量或者是高效查询和改变他们的值.此版本同时还更新了快速入门文档:平台的移植不再至此和原始重启参数:一些 bug 修复. 改进记录 ======= - The ``oglplus::ClientContext`` class was added. - The ``EnumToClass`` template and its special

A Simple OpenGL Shader Example II

A Simple OpenGL Shader Example II [email protected] Abstract. The OpenGL Shading Language syntax comes from the C family of programming languages. Tokes, identifiers, semicolons, nesting with curly braces, control-flow, and many key words look like C

【OpenGL】VAO与VBO

1.我们先了解什么是OpenGL对象(OpenGL Object) 根据OpenGL Wiki的定义: An OpenGL Object is an OpenGL construct that contains some state. When they are bound to the context, the state that they contain is mapped into the context's state. Thus, changes to context state w

CSharpGL(29)初步封装Texture和Framebuffer

+BIT祝威+悄悄在此留下版了个权的信息说: CSharpGL(29)初步封装Texture和Framebuffer +BIT祝威+悄悄在此留下版了个权的信息说: Texture和Framebuffer Texture和Framebuffer是OpenGL进行3D渲染高级效果必不可少的利器.有了Texture和Framebuffer就可以实现体渲染(Volume Rendering)等效果.现在到了对Texture和Framebuffer的创建.修改.使用进行封装的时候. +BIT祝威+悄悄在此

JRDB:iOS对FMDB的超好用封装

在自己开发中,每次用到数据库都会纠结是使用CoreData还是FMDB.CoreData虽然Api简单,但是调用栈非常复杂,要初始化一个Context需要至少20行代码... 显然,对于这种这么恶心的情况,我们的大Github必须有人会跳出来解决这种问题.于是就出现了MagicRecord这个对CoreData的封装库.一开始遇到这个库的时候,好用到几乎让我想把所有项目的数据库都换成CoreData了.两句话解决CoreData调用栈的初始化,一句话完成数据库版本升级和自动数据合并更新(虽然我们

自动化测试--封装JDBCUnit

在进行测试的时候,经常需要对数据库进行操作.我们知道,通过代码与数据库交互,需要以下几步: 1.加载驱动 之前有盆友问我,为什么Selenium操作浏览器的时候,非要下载浏览器驱动?为啥对数据库进行操作的时候,直接加载driver就可以了呢?--------之类稍作解释:浏览器并没有供一个API给java来直接操作浏览器,而是提供了一套API给selenium中的XXXDriver.exe(如Chrome的ChromeDriver)来对自己进行各种操作,此时我们想自动化测试浏览器就要下载sele

OPENGL中glad、glew、glfw、Freeglut的区别

glew(The OpenGL Extension Wrangler Library)是对底层OpenGL接口的封装,可以让你的代码跨平台. glad与glew作用相同,可以看作它的升级版. Freeglut(OpenGL Utility Toolkit)主要用于创建OpenGL上下文.接收一些鼠标键盘事件等等. glfw(Graphics Library Framework)是Freeglut升级版,作用基本一样. 通常来说glad和glfw配合使用,比如我上面发的那个网站就是. glew和F

[转]C&amp;C++图形图像处理开源库

本文章已收录于: .embody { padding: 10px 10px 10px; margin: 0 -20px; border-bottom: solid 1px #ededed } .embody_b { margin: 0; padding: 10px 0 } .embody .embody_t,.embody .embody_c { display: inline-block; margin-right: 10px } .embody_t { font-size: 12px; co