Thanks for the reply brecht. draw_pixels() eventually calls into a function in a DLL, that looks something like the code below. I’m not exactly sure how am I supposed to restore the OpenGL state.
if (s_blenderImage == nullptr)
return;
GLenum err = glewInit();
if (GLEW_OK != err)
return;
std::array<GLfloat, 8> position = {0.0, 0.0,
(float) s_blenderImage->width,
0.0,
(float) s_blenderImage->width,
(float) s_blenderImage->height,
0.0,
(float) s_blenderImage->height};
std::array<GLfloat, 8> texture_coords = {0.0, 1.0, 1.0, 1.0,
1.0, 0.0, 0.0, 0.0};
GLuint vertex_array;
std::array<GLint, 2> vertex_buffer;
GLuint texture_id;
GLuint texcoord_location;
GLuint position_location;
GLint shader_program_id;
GLuint texture_vbo_id;
GLuint vertex_vbo_id;
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA32F,
static_cast<GLsizei>(s_blenderImage->width),
static_cast<GLsizei>(s_blenderImage->height),
0,
GL_RGBA,
GL_FLOAT,
(float*) s_blenderImage->framebuffer);
glBindTexture(GL_TEXTURE_2D, 0);
// Get shader program set by Blender.
glGetIntegerv(GL_CURRENT_PROGRAM, &shader_program_id);
// Generate vertex array
glGenVertexArrays(1, &vertex_array);
glBindVertexArray(vertex_array);
texcoord_location = glGetAttribLocation(shader_program_id, "texCoord");
position_location = glGetAttribLocation(shader_program_id, "pos");
glEnableVertexAttribArray(texcoord_location);
glEnableVertexAttribArray(position_location);
glGenBuffers(1, &texture_vbo_id);
glBindBuffer(GL_ARRAY_BUFFER, texture_vbo_id);
glBufferData(GL_ARRAY_BUFFER, texture_coords.size() * sizeof(float),
&texture_coords[0], GL_STATIC_DRAW);
glVertexAttribPointer(texcoord_location, 2, GL_FLOAT, GL_FALSE,
2 * sizeof(float), reinterpret_cast<void*>(0));
glGenBuffers(1, &vertex_vbo_id);
glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo_id);
glBufferData(GL_ARRAY_BUFFER, position.size() * sizeof(float),
&position[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(position_location);
glVertexAttribPointer(position_location, 2, GL_FLOAT, GL_FALSE,
2 * sizeof(float), reinterpret_cast<void*>(0));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture_id);
glBindVertexArray(vertex_array);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);