@ -11,7 +11,7 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2018-07-10: OpenGL: Added error output when shaders fail to compile/link.
// 2018-07-10: OpenGL: Support for more GLSL versions (based on the GLSL version string). Added error output when shaders fail to compile/link.
// 2018-06-08: Misc: Extracted imgui_impl_opengl3.cpp/.h away from the old combined GLFW/SDL+OpenGL3 examples.
// 2018-06-08: OpenGL: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
// 2018-05-25: OpenGL: Removed unnecessary backup/restore of GL_ELEMENT_ARRAY_BUFFER_BINDING since this is part of the VAO state.
@ -26,6 +26,24 @@
// 2016-09-05: OpenGL: Fixed save and restore of current scissor rectangle.
// 2016-07-29: OpenGL: Explicitly setting GL_UNPACK_ROW_LENGTH to reduce issues because SDL changes it. (#752)
//----------------------------------------
// OpenGL GLSL GLSL
// version version string
//----------------------------------------
// 2.0 110 "#version 110"
// 2.1 120
// 3.0 130
// 3.1 140
// 3.2 150 "#version 150"
// 3.3 330
// 4.0 400
// 4.1 410
// 4.2 420
// 4.3 430
// ES 2.0 100 "#version 100"
// ES 3.0 300 "#version 300 es"
//----------------------------------------
# if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
# define _CRT_SECURE_NO_WARNINGS
# endif
@ -40,7 +58,7 @@
//#include <glad/glad.h>
// OpenGL Data
static char g_GlslVersion [ 32 ] = " " ;
static char g_GlslVersion String [ 32 ] = " " ;
static GLuint g_FontTexture = 0 ;
static GLuint g_ShaderHandle = 0 , g_VertHandle = 0 , g_FragHandle = 0 ;
static int g_AttribLocationTex = 0 , g_AttribLocationProjMtx = 0 ;
@ -52,10 +70,10 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version)
{
// Store GLSL version string so we can refer to it later in case we recreate shaders. Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure.
if ( glsl_version = = NULL )
glsl_version = " #version 1 5 0" ;
IM_ASSERT ( ( int ) strlen ( glsl_version ) + 2 < IM_ARRAYSIZE ( g_GlslVersion ) ) ;
strcpy ( g_GlslVersion , glsl_version ) ;
strcat ( g_GlslVersion , " \n " ) ;
glsl_version = " #version 1 3 0" ;
IM_ASSERT ( ( int ) strlen ( glsl_version ) + 2 < IM_ARRAYSIZE ( g_GlslVersion String ) ) ;
strcpy ( g_GlslVersion String , glsl_version ) ;
strcat ( g_GlslVersion String , " \n " ) ;
return true ;
}
@ -285,8 +303,25 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects()
glGetIntegerv ( GL_ARRAY_BUFFER_BINDING , & last_array_buffer ) ;
glGetIntegerv ( GL_VERTEX_ARRAY_BINDING , & last_vertex_array ) ;
// Create shaders
const GLchar * vertex_shader =
// Parse GLSL version string
int glsl_version = 130 ;
sscanf ( g_GlslVersionString , " #version %d " , & glsl_version ) ;
const GLchar * vertex_shader_glsl_120 =
" uniform mat4 ProjMtx; \n "
" attribute vec2 Position; \n "
" attribute vec2 UV; \n "
" attribute vec4 Color; \n "
" varying vec2 Frag_UV; \n "
" varying vec4 Frag_Color; \n "
" void main() \n "
" { \n "
" Frag_UV = UV; \n "
" Frag_Color = Color; \n "
" gl_Position = ProjMtx * vec4(Position.xy,0,1); \n "
" } \n " ;
const GLchar * vertex_shader_glsl_130 =
" uniform mat4 ProjMtx; \n "
" in vec2 Position; \n "
" in vec2 UV; \n "
@ -300,23 +335,50 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects()
" gl_Position = ProjMtx * vec4(Position.xy,0,1); \n "
" } \n " ;
const GLchar * fragment_shader =
const GLchar * fragment_shader_glsl_120 =
" #ifdef GL_ES \n "
" precision mediump float; \n "
" #endif \n "
" uniform sampler2D Texture; \n "
" varying vec2 Frag_UV; \n "
" varying vec4 Frag_Color; \n "
" void main() \n "
" { \n "
" gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st); \n "
" } \n " ;
const GLchar * fragment_shader_glsl_130 =
" uniform sampler2D Texture; \n "
" in vec2 Frag_UV; \n "
" in vec4 Frag_Color; \n "
" out vec4 Out_Color; \n "
" void main() \n "
" { \n "
" Out_Color = Frag_Color * texture( Texture, Frag_UV.st); \n "
" Out_Color = Frag_Color * texture( Texture, Frag_UV.st);\n "
" } \n " ;
const GLchar * vertex_shader_with_version [ 2 ] = { g_GlslVersion , vertex_shader } ;
// Select shaders matching our GLSL versions
const GLchar * vertex_shader = NULL ;
const GLchar * fragment_shader = NULL ;
if ( glsl_version < 130 )
{
vertex_shader = vertex_shader_glsl_120 ;
fragment_shader = fragment_shader_glsl_120 ;
}
else
{
vertex_shader = vertex_shader_glsl_130 ;
fragment_shader = fragment_shader_glsl_130 ;
}
// Create shaders
const GLchar * vertex_shader_with_version [ 2 ] = { g_GlslVersionString , vertex_shader } ;
g_VertHandle = glCreateShader ( GL_VERTEX_SHADER ) ;
glShaderSource ( g_VertHandle , 2 , vertex_shader_with_version , NULL ) ;
glCompileShader ( g_VertHandle ) ;
CheckShader ( g_VertHandle , " vertex shader " ) ;
const GLchar * fragment_shader_with_version [ 2 ] = { g_GlslVersion , fragment_shader } ;
const GLchar * fragment_shader_with_version [ 2 ] = { g_GlslVersion String , fragment_shader } ;
g_FragHandle = glCreateShader ( GL_FRAGMENT_SHADER ) ;
glShaderSource ( g_FragHandle , 2 , fragment_shader_with_version , NULL ) ;
glCompileShader ( g_FragHandle ) ;