Skip to content

Commit fc737d2

Browse files
Omar Sherif Fathyocornut
Omar Sherif Fathy
authored andcommitted
Examples; imgui_impl_opengl3.cpp to work with Emscripten (#1941)
* Add some ifdefs to add emscripten specific params and includes * Update imgui_impl_opengl3.cpp * Update imgui_impl_opengl3.cpp * Update imgui_impl_opengl3.cpp * replace __EMSCRIPTEN_BUILD__ with __EMSCRIPTEN__ * replace GLFW_INCLUDE_ES3 with direct header * removing useless glfw include * Making call to glPolygonMode() optional
1 parent f9e8b5c commit fc737d2

File tree

1 file changed

+76
-6
lines changed

1 file changed

+76
-6
lines changed

examples/imgui_impl_opengl3.cpp

+76-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// 3.2 150 "#version 150"
3838
// 3.3 330
3939
// 4.0 400
40-
// 4.1 410
40+
// 4.1 410 "#version 410 core"
4141
// 4.2 420
4242
// 4.3 430
4343
// ES 2.0 100 "#version 100"
@@ -57,7 +57,11 @@
5757
#include <stdint.h> // intptr_t
5858
#endif
5959

60-
#include <GL/gl3w.h> // This example is using gl3w to access OpenGL functions. You may use another OpenGL loader/header such as: glew, glext, glad, glLoadGen, etc.
60+
#ifdef __EMSCRIPTEN__
61+
#include <GLES3/gl3.h>
62+
#else
63+
#include <GL/gl3w.h> // This example is using gl3w to access OpenGL functions. You may use another OpenGL loader/header such as: glew, glext, glad, glLoadGen, etc.
64+
#endif
6165
//#include <glew.h>
6266
//#include <glext.h>
6367
//#include <glad/glad.h>
@@ -114,7 +118,9 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
114118
GLint last_sampler; glGetIntegerv(GL_SAMPLER_BINDING, &last_sampler);
115119
GLint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer);
116120
GLint last_vertex_array; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array);
117-
GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode);
121+
#ifdef GL_POLYGON_MODE
122+
GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode);
123+
#endif
118124
GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
119125
GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box);
120126
GLenum last_blend_src_rgb; glGetIntegerv(GL_BLEND_SRC_RGB, (GLint*)&last_blend_src_rgb);
@@ -135,7 +141,9 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
135141
glDisable(GL_CULL_FACE);
136142
glDisable(GL_DEPTH_TEST);
137143
glEnable(GL_SCISSOR_TEST);
138-
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
144+
#ifdef glPolygonMode
145+
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
146+
#endif
139147

140148
// Setup viewport, orthographic projection matrix
141149
// Our visible imgui space lies from draw_data->DisplayPps (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin is typically (0,0) for single viewport apps.
@@ -221,7 +229,9 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
221229
if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
222230
if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
223231
if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
224-
glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]);
232+
#ifdef glPolygonMode
233+
glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]);
234+
#endif
225235
glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
226236
glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]);
227237
}
@@ -340,6 +350,35 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects()
340350
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
341351
"}\n";
342352

353+
const GLchar* vertex_shader_glsl_300_es =
354+
"precision mediump float;\n"
355+
"layout (location = 0) in vec2 Position;\n"
356+
"layout (location = 1) in vec2 UV;\n"
357+
"layout (location = 2) in vec4 Color;\n"
358+
"uniform mat4 ProjMtx;\n"
359+
"out vec2 Frag_UV;\n"
360+
"out vec4 Frag_Color;\n"
361+
"void main()\n"
362+
"{\n"
363+
" Frag_UV = UV;\n"
364+
" Frag_Color = Color;\n"
365+
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
366+
"}\n";
367+
368+
const GLchar* vertex_shader_glsl_410_core =
369+
"layout (location = 0) in vec2 Position;\n"
370+
"layout (location = 1) in vec2 UV;\n"
371+
"layout (location = 2) in vec4 Color;\n"
372+
"uniform mat4 ProjMtx;\n"
373+
"out vec2 Frag_UV;\n"
374+
"out vec4 Frag_Color;\n"
375+
"void main()\n"
376+
"{\n"
377+
" Frag_UV = UV;\n"
378+
" Frag_Color = Color;\n"
379+
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n"
380+
"}\n";
381+
343382
const GLchar* fragment_shader_glsl_120 =
344383
"#ifdef GL_ES\n"
345384
" precision mediump float;\n"
@@ -362,10 +401,41 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects()
362401
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
363402
"}\n";
364403

404+
const GLchar* fragment_shader_glsl_300_es =
405+
"precision mediump float;\n"
406+
"uniform sampler2D Texture;\n"
407+
"in vec2 Frag_UV;\n"
408+
"in vec4 Frag_Color;\n"
409+
"layout (location = 0) out vec4 Out_Color;\n"
410+
"void main()\n"
411+
"{\n"
412+
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
413+
"}\n";
414+
415+
const GLchar* fragment_shader_glsl_410_core =
416+
"in vec2 Frag_UV;\n"
417+
"in vec4 Frag_Color;\n"
418+
"uniform sampler2D Texture;\n"
419+
"layout (location = 0) out vec4 Out_Color;\n"
420+
"void main()\n"
421+
"{\n"
422+
" Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
423+
"}\n";
424+
365425
// Select shaders matching our GLSL versions
366426
const GLchar* vertex_shader = NULL;
367427
const GLchar* fragment_shader = NULL;
368-
if (glsl_version < 130)
428+
if(glsl_version == 410)
429+
{
430+
vertex_shader = vertex_shader_glsl_410_core;
431+
fragment_shader = fragment_shader_glsl_410_core;
432+
}
433+
else if(glsl_version == 300)
434+
{
435+
vertex_shader = vertex_shader_glsl_300_es;
436+
fragment_shader = fragment_shader_glsl_300_es;
437+
}
438+
else if (glsl_version < 130)
369439
{
370440
vertex_shader = vertex_shader_glsl_120;
371441
fragment_shader = fragment_shader_glsl_120;

0 commit comments

Comments
 (0)