219 ImDrawData* draw_data = ImGui::GetDrawData();
225 int fbWidth = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
226 int fbHeight = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
227 if (fbWidth == 0 || fbHeight == 0)
229 draw_data->ScaleClipRects(io.DisplayFramebufferScale);
232 GLint last_active_texture;
233 glGetIntegerv(GL_ACTIVE_TEXTURE, &last_active_texture);
234 glActiveTexture(GL_TEXTURE0);
237 glGetIntegerv(GL_CURRENT_PROGRAM, &last_program);
239 glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
240 GLint last_array_buffer;
241 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
242 GLint last_element_array_buffer;
243 glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer);
244 GLint last_vertex_array;
245 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
246 GLint last_blend_src_rgb;
247 glGetIntegerv(GL_BLEND_SRC_RGB, &last_blend_src_rgb);
248 GLint last_blend_dst_rgb;
249 glGetIntegerv(GL_BLEND_DST_RGB, &last_blend_dst_rgb);
250 GLint last_blend_src_alpha;
251 glGetIntegerv(GL_BLEND_SRC_ALPHA, &last_blend_src_alpha);
252 GLint last_blend_dst_alpha;
253 glGetIntegerv(GL_BLEND_DST_ALPHA, &last_blend_dst_alpha);
254 GLint last_blend_equation_rgb;
255 glGetIntegerv(GL_BLEND_EQUATION_RGB, &last_blend_equation_rgb);
256 GLint last_blend_equation_alpha;
257 glGetIntegerv(GL_BLEND_EQUATION_ALPHA, &last_blend_equation_alpha);
258 GLint last_viewport[4];
259 glGetIntegerv(GL_VIEWPORT, last_viewport);
260 GLint last_scissor_box[4];
261 glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
263 GLboolean last_enable_blend = glIsEnabled(GL_BLEND);
264 GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE);
265 GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST);
266 GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST);
270 glBlendEquation(GL_FUNC_ADD);
271 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
272 glDisable(GL_CULL_FACE);
273 glDisable(GL_DEPTH_TEST);
274 glEnable(GL_SCISSOR_TEST);
283 glViewport((GLsizei)(viewportRect.
x * io.DisplayFramebufferScale.x),
284 (GLsizei)(viewportRect.
y * io.DisplayFramebufferScale.y),
285 (GLsizei)(viewportRect.
width * io.DisplayFramebufferScale.x),
286 (GLsizei)(viewportRect.
height * io.DisplayFramebufferScale.y));
290 const float ortho_projection[4][4] =
292 {2.0f / io.DisplaySize.x, 0.0f, 0.0f, 0.0f},
293 {0.0f, 2.0f / -io.DisplaySize.y, 0.0f, 0.0f},
294 {0.0f, 0.0f, -1.0f, 0.0f},
295 {-1.0f, 1.0f, 0.0f, 1.0f},
304 for (
int n = 0; n < draw_data->CmdListsCount; n++)
306 const ImDrawList* cmd_list = draw_data->CmdLists[n];
307 const ImDrawIdx* idx_buffer_offset =
nullptr;
310 glBufferData(GL_ARRAY_BUFFER,
311 (GLsizeiptr)cmd_list->VtxBuffer.Size * (GLsizeiptr)
sizeof(ImDrawVert),
312 (
const GLvoid*)cmd_list->VtxBuffer.Data,
316 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
317 (GLsizeiptr)cmd_list->IdxBuffer.Size * (GLsizeiptr)
sizeof(ImDrawIdx),
318 (
const GLvoid*)cmd_list->IdxBuffer.Data,
321 for (
int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
323 const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
324 if (pcmd->UserCallback)
326 pcmd->UserCallback(cmd_list, pcmd);
330 glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
333 glScissor((
int)pcmd->ClipRect.x,
334 (
int)(fbHeight - pcmd->ClipRect.w),
335 (
int)(pcmd->ClipRect.z - pcmd->ClipRect.x),
336 (
int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
338 glScissor((GLsizei)(viewportRect.
x * io.DisplayFramebufferScale.x),
339 (GLsizei)(viewportRect.
y * io.DisplayFramebufferScale.y),
340 (GLsizei)(viewportRect.
width * io.DisplayFramebufferScale.x),
341 (GLsizei)(viewportRect.
height * io.DisplayFramebufferScale.y));
343 glDrawElements(GL_TRIANGLES,
344 (GLsizei)pcmd->ElemCount,
345 sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
348 idx_buffer_offset += pcmd->ElemCount;
353 glUseProgram((
SLuint)last_program);
354 glBindTexture(GL_TEXTURE_2D, (
SLuint)last_texture);
355 glActiveTexture((
SLuint)last_active_texture);
356 glBindVertexArray((
SLuint)last_vertex_array);
357 glBindBuffer(GL_ARRAY_BUFFER, (
SLuint)last_array_buffer);
358 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (
SLuint)last_element_array_buffer);
359 glBlendEquationSeparate((
SLuint)last_blend_equation_rgb,
360 (
SLuint)last_blend_equation_alpha);
361 glBlendFuncSeparate((
SLuint)last_blend_src_rgb,
362 (
SLuint)last_blend_dst_rgb,
363 (
SLuint)last_blend_src_alpha,
364 (
SLuint)last_blend_dst_alpha);
365 if (last_enable_blend)
370 if (last_enable_cull_face)
371 glEnable(GL_CULL_FACE);
373 glDisable(GL_CULL_FACE);
375 if (last_enable_depth_test)
376 glEnable(GL_DEPTH_TEST);
378 glDisable(GL_DEPTH_TEST);
380 if (last_enable_scissor_test)
381 glEnable(GL_SCISSOR_TEST);
383 glDisable(GL_SCISSOR_TEST);
385 glViewport(last_viewport[0],
387 (GLsizei)last_viewport[2],
388 (GLsizei)last_viewport[3]);
390 glScissor(last_scissor_box[0],
392 (GLsizei)last_scissor_box[2],
393 (GLsizei)last_scissor_box[3]);
395 ImGui::SetCurrentContext(
nullptr);