要在两个不同的
Open
GL上下文之间复制纹理,我们可以将纹理映射到共享内存中。然后,我们可以在使用第一个
Open
GL上下文的线程中将纹理复制到共享内存中,然后在使用第二个
Open
GL上下文的线程中将共享内存中的纹理复制到第二个上下文。
下面是使用GLEW和glfw创建窗口并创建
Open
GL上下文的示例代码:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main()
// init glfw
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// create window and opengl context
GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
// init glew
glewExperimental = true;
glewInit();
// create first opengl context
GLuint tex1;
glGenTextures(1, &tex1);
glBindTexture(GL_TEXTURE_2D, tex1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
// map texture to shared memory
GLuint texMem;
glGenBuffers(1, &texMem);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texMem);
glBufferData(GL_PIXEL_UNPACK_BUFFER, 256 * 256 * 4, NULL, GL_STATIC_DRAW);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, tex1);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texMem);
glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE);
void* texData = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE);
glGetTexImage(GL_TEXTURE_2D,