-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimPlot3DDemo.nelua
More file actions
233 lines (202 loc) · 7.18 KB
/
imPlot3DDemo.nelua
File metadata and controls
233 lines (202 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
-- Notice:
-- Compile with Clang compiler.
--
--
require 'os'
require'math'
--
require'cimgui'
require'simple'
require'imgui_backend_opengl3'
require'imgui_backend_glfw'
require'loadImage'
require'utils'
require'IconsFontAwesome6'
--
require'cimplot'
--
require'cimplot3d'
--- External functions
local function setupFonts() <cimport> end -- from utils/setupFonts.c
--- Initial Window size
local MainWinWidth <const> = 1280
local MainWinHeight <const> = 800
--- Constants
local IMPLOT_AUTO: float32 = -1
--- Global vars
local versions : [][2]int32 = {{4, 6} ,{4, 5} ,{4, 4} ,{4, 3} ,{4, 2} ,{4, 1} ,{4, 0} ,{3, 3} }
--- Forward definitions
---------
--- main
---------
local main = function()
glfwInit()
defer glfwTerminate() end
local glfwWin: *GLFWwindow = nilptr
local glsl_version:cstring -- = "#version 330"
for _, ver in ipairs(versions) do
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, ver[0])
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, ver[1])
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE)
glfwWin = glfwCreateWindow(MainWinWidth, MainWinHeight, "ImGui with Nelua 2025/05", nilptr, nilptr)
if nilptr ~= glfwWin then
glsl_version = "#version " .. tostring( ver[0] * 100 + ver[1] * 10)
break
end
end
if nilptr == glfwWin then
print("Could not create window")
os.exit(false)
end
defer glfwDestroyWindow(glfwWin) end
glfwMakeContextCurrent(glfwWin)
glfwSwapInterval(1) -- # Enable vsync
------------------------
--- Load title bar icon
------------------------
local IconName = "res/img/nl.png"
LoadTitleBarIcon(glfwWin, IconName)
if gladLoadGL(glfwGetProcAddress) == 0 then
error 'Could not initialize GLAD'
end
print('OpenGL loaded:', (@cstring)(glGetString(GL_VERSION)))
local context = ImGui_CreateContext()
defer ImGui_DestroyContext(context) end
ImGui_ImplGlfw_InitForOpenGL(glfwWin, true)
defer ImGui_ImplGlfw_Shutdown() end
ImGui_ImplOpenGL3_Init(glsl_version)
defer ImGui_ImplOpenGL3_Shutdown() end
setupFonts()
--igStyleColorsDark()
ImGui_StyleColorsClassic()
local showWindowDelay = 1 -- TODO : Avoid flickering window at start up.
local clearColor: [3]cfloat = {0.03,0.15,0.2}
local pio = ImGui_GetIO()
--------------------------
--- ImPlot/ImPlot3D create context
--------------------------
local imPlotContext = ImPlot_CreateContext()
defer ImPlot_DestroyContext(imPlotContext) end
--- 3D
local imPlot3DContext = ImPlot3D_CreateContext()
defer ImPlot3D_DestroyContext(imPlot3DContext) end
--------------------
--- Main while loop
--------------------
while glfwWindowShouldClose(glfwWin) == 0 do
glfwPollEvents()
ImGui_ImplOpenGL3_NewFrame() -- # start imgui frame
ImGui_ImplGlfw_NewFrame()
ImGui_NewFrame()
ImPlot3D_ShowDemoWindow(nilptr)
---------------------
--- DemoSurfacePlots
---------------------
do ImGui_Begin("ImPlot3D Surface plots with Nelua lang. " .. ICON_FA_CHART_COLUMN, nilptr, 0)
defer ImGui_End() end
-- Static vars
local N <const> = 20
local xs: [400]float32 <static>
local ys: [400]float32 <static>
local zs: [400]float32 <static>
local t:float32 <static> = 0.0
--
local selected_fill:cint <static> = 1 -- Colormap by default
local sel_colormap :cint <static> = 5 -- Jet by default
local solid_color <static> = ImVec4_c{0.8, 0.8, 0.2, 0.6}
-- Generate colormaps
local colormaps:[]cstring <static> = {"Viridis", "Plasma", "Hot", "Cool", "Pink", "Jet", "Twilight", "RdBu", "BrBG", "PiYG", "Spectral", "Greys"}
--
local custom_range <static> = false
local range_min:float32 <static> = -1.0
local range_max:float32 <static> = 1.0
ImGui_Text("Frame rate %.3f ms/frame (%.1f FPS)" , 1000.0 / pio.Framerate, pio.Framerate)
t = t + ImGui_GetIO().DeltaTime
-- Define the range for X and Y
local min_val = -1.0
local max_val = 1.0
local step = (max_val - min_val) / (N - 1)
-- Populate the xs, ys, and zs arrays
for i=0, N -1 do
local j = 0
while j < N do
local idx = i * N + j
xs[idx] = min_val + j * step -- X values are constant along rows
ys[idx] = min_val + i * step -- Y values are constant along columns
zs[idx] = math.sin(2 * t + math.sqrt((xs[idx] * xs[idx] + ys[idx] * ys[idx]))) -- z = sin(2t + sqrt(x^2 + y^2))
j = j + 1
end
end
-- Choose fill color
ImGui_Text("Fill color")
do ImGui_Indent()
-- Choose solid color
ImGui_RadioButtonIntPtr("Solid", &selected_fill, 0)
if selected_fill == 0 then
ImGui_SameLine()
ImGui_ColorEdit4("##SurfaceSolidColor", (@pointer(array(float32,4)))(&solid_color), 0)
end
-- Choose colormap
ImGui_RadioButtonIntPtr("Colormap", &selected_fill, 1)
if selected_fill == 1 then
ImGui_SameLine()
ImGui_ComboChar("##SurfaceColormap", &sel_colormap, &colormaps, #colormaps)
end
ImGui_Unindent()
end
-- Choose range
ImGui_Checkbox("Custom range", &custom_range)
do ImGui_Indent()
if not custom_range then ImGui_BeginDisabled() end
ImGui_SliderFloat("Range min", &range_min, -1.0 , range_max - 0.01)
ImGui_SliderFloat("Range max", &range_max, range_min + 0.01, 1.0)
if not custom_range then ImGui_EndDisabled() end
ImGui_Unindent()
end
-- Begin the plot
if selected_fill == 1 then
ImPlot3D_PushColormap_Str(colormaps[sel_colormap])
end
if ImPlot3D_BeginPlot("Surface Plots", ImVec2_c{-1, 400}, ImPlot3DFlags_NoClip) then
-- Set styles
ImPlot3D_SetupAxesLimits(-1, 1, -1, 1, -1.5, 1.5)
ImPlot3D_PushStyleVar_Float(ImPlot3DStyleVar_FillAlpha, 0.8)
if selected_fill == 0 then
ImPlot3D_SetNextFillStyle(solid_color)
end
local IMPLOT3D_AUTO <const> = -1 -- Deduce variable automatically
local vec4:ImVec4_c
vec4 = ImPlot3D_GetColormapColor(1, IMPLOT3D_AUTO)
ImPlot3D_SetNextLineStyle(vec4, IMPLOT3D_AUTO)
-- Plot the surface
if custom_range then
ImPlot3D_PlotSurface_FloatPtr("Wave Surface", &xs, &ys, &zs, N, N, range_min, range_max, 0, 0, #float32)
else
ImPlot3D_PlotSurface_FloatPtr("Wave Surface", &xs, &ys, &zs, N, N, 0, 0, 0, 0, #float32)
end
-- End the plot
ImPlot3D_PopStyleVar()
ImPlot3D_EndPlot()
end
if selected_fill == 1 then
ImPlot3D_PopColormap()
end
end
---------
ImGui_Render()
glClearColor(clearColor[0], clearColor[1], clearColor[2], 1.0)
glClear(GL_COLOR_BUFFER_BIT)
ImGui_ImplOpenGL3_RenderDrawData(ImGui_GetDrawData())
glfwSwapBuffers(glfwWin)
if showWindowDelay > 0 then
showWindowDelay = showWindowDelay - 1
elseif showWindowDelay == 0 then
showWindowDelay = -1
glfwShowWindow(glfwWin)
end
end -- main while loop end
end -- main() end
main()