Skip to content

Commit

Permalink
Merge pull request #132 from Gnimuc/gl-version
Browse files Browse the repository at this point in the history
Add support for setting the OpenGL version
  • Loading branch information
JamesWrigley authored Jul 20, 2024
2 parents 41313a4 + 0c78db7 commit b8b2ff6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
4 changes: 4 additions & 0 deletions docs/src/_changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ CurrentModule = CImGui
This documents notable changes in CImGui.jl. The format is based on [Keep a
Changelog](https://keepachangelog.com).

## Unreleased

### Added
- The OpenGL version can now be set with [`render()`](@ref).

## [v2.0.0] - 2024-06-27

Expand Down
49 changes: 30 additions & 19 deletions ext/GlfwOpenGLBackend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@ import CImGui.lib as lib
import GLFW
import ModernGL as GL

const g_ImageTexture = Dict{Int, GL.GLuint}()

@static if Sys.isapple()
# OpenGL 3.2 + GLSL 150
const glsl_version = 150
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 2)
GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE) # 3.2+ only
GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL.GL_TRUE) # required on Mac
else
# OpenGL 3.0 + GLSL 130
const glsl_version = 130
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 0)
# GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE) # 3.2+ only
# GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE) # 3.0+ only
# Helper function to get the GLSL version
function get_glsl_version(gl_version)
gl2glsl = Dict(v"2.0" => 110,
v"2.1" => 120,
v"3.0" => 130,
v"3.1" => 140,
v"3.2" => 150)

if gl_version < v"3.3"
gl2glsl[gl_version]
else
gl_version.major * 100 + gl_version.minor * 10
end
end

const g_ImageTexture = Dict{Int, GL.GLuint}()

function CImGui._create_image_texture(::Val{:GlfwOpenGL3}, image_width, image_height; format=GL.GL_RGBA, type=GL.GL_UNSIGNED_BYTE)
id = GL.GLuint(0)
Expand Down Expand Up @@ -55,17 +54,30 @@ function CImGui._render(ui, ctx::Ptr{lib.ImGuiContext}, ::Val{:GlfwOpenGL3};
clear_color=Cfloat[0.45, 0.55, 0.60, 1.00],
window_size=(1280, 720),
window_title="CImGui",
engine=nothing)
engine=nothing,
opengl_version=v"3.2")
# Validate arguments
if clear_color isa Ref && !isassigned(clear_color)
throw(ArgumentError("'clear_color' is a unassigned reference, it must be initialized properly."))
elseif Sys.isapple() && opengl_version < v"3.2"
throw(ArgumentError("Only OpenGL 3.2+ is supported on OSX, but $(opengl_version) was requested"))
end

# Configure GLFW
glsl_version = get_glsl_version(opengl_version)
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, opengl_version.major)
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, opengl_version.minor)

if Sys.isapple()
GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE)
GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL.GL_TRUE)
end

# Start the test engine, if we have one
if !isnothing(engine)
CImGui._start_test_engine(engine, ctx)
end

GLFW.Init()

# Create window
window = GLFW.CreateWindow(window_size[1], window_size[2], window_title)
@assert window != C_NULL
Expand Down Expand Up @@ -138,7 +150,6 @@ function CImGui._render(ui, ctx::Ptr{lib.ImGuiContext}, ::Val{:GlfwOpenGL3};
lib.ImGui_ImplGlfw_Shutdown()
CImGui.DestroyContext(ctx)
GLFW.DestroyWindow(window)
GLFW.Terminate()
end
end

Expand Down
1 change: 1 addition & 0 deletions src/CImGui.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Keyword arguments:
- `engine=nothing`: An optional `ImGuiTestEngine.Engine` instance. If there are
any tests registered with the test engine they will be queued and run
automatically.
- `opengl_version::VersionNumber=v"3.2"`: The OpenGL version to use.
"""
function render(args...; kwargs...)
_check_backend()
Expand Down

0 comments on commit b8b2ff6

Please sign in to comment.