summaryrefslogtreecommitdiff
path: root/bindgen/rust/celeritas-sys/examples/glfw.rs
blob: 36abd8fea1a8ed4dddb8128871f13408007a90f3 (plain)
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
use std::ffi::CString;
use std::ptr::addr_of_mut;

use celeritas_sys::*;
use egui_backend::egui::{vec2, Pos2, Rect};
use egui_glfw as egui_backend;
use egui_glfw::glfw::{fail_on_errors, Context};

use egui_glfw::glfw;

const SCREEN_WIDTH: u32 = 1000;
const SCREEN_HEIGHT: u32 = 1000;

fn main() {
    unsafe {
        let mut glfw = glfw::init(glfw::fail_on_errors!()).unwrap();
        glfw.window_hint(glfw::WindowHint::ContextVersion(4, 1));
        glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true));
        glfw.window_hint(glfw::WindowHint::OpenGlProfile(
            glfw::OpenGlProfileHint::Core,
        ));
        glfw.window_hint(glfw::WindowHint::DoubleBuffer(true));
        glfw.window_hint(glfw::WindowHint::Resizable(false));

        let (mut window, events) = glfw
            .create_window(
                SCREEN_WIDTH,
                SCREEN_HEIGHT,
                "Egui in GLFW!",
                glfw::WindowMode::Windowed,
            )
            .expect("Failed to create GLFW window.");

        window.set_all_polling(true);
        window.make_current();
        // glfw.set_swap_interval(glfw::SwapInterval::None);
        glfw.set_swap_interval(glfw::SwapInterval::Adaptive);

        gl::load_with(|symbol| window.get_proc_address(symbol) as *const _);

        let window_ptr = window.window_ptr();
        unsafe {
            // Cast the window pointer to the expected type
            let window_ptr = window_ptr as *mut celeritas_sys::GLFWwindow;
            Core_Bringup(window_ptr);
        };

        let mut painter = egui_backend::Painter::new(&mut window);
        let egui_ctx = egui::Context::default();

        let (width, height) = window.get_framebuffer_size();
        let native_pixels_per_point = window.get_content_scale().0;
        let native_pixels_per_point = 2.0;
        egui_ctx.set_pixels_per_point(2.0);

        let mut egui_input_state = egui_backend::EguiInputState::new(egui::RawInput {
            screen_rect: Some(Rect::from_min_size(
                Pos2::new(0f32, 0f32),
                vec2(width as f32, height as f32) / native_pixels_per_point,
            )),
            ..Default::default()
        });

        egui_input_state.input.time = Some(0.01);

        // let triangle = triangle::Triangle::new();
        let slider = &mut 0.0;

        // C data
        let camera_pos = Vec3 {
            x: 18.9,
            y: 10.6,
            z: 11.6,
        };
        let camera_front = Vec3 {
            x: -0.6,
            y: -0.2,
            z: -0.7,
        };
        let mut camera = Camera_Create(
            camera_pos,
            camera_front,
            Vec3 {
                x: 0.0,
                y: 1.0,
                z: 0.0,
            },
            45.0,
        );
        SetCamera(camera);

        let mut cube_geo = Geo_CreateCuboid(f32x3 {
            x: 2.0,
            y: 2.0,
            z: 2.0,
        });
        let mut crate_mesh = Mesh_Create(addr_of_mut!(cube_geo), false);
        let albedo_map = TextureLoadFromFile(
            CString::new("assets/demo/crate/Wood_Crate_001_basecolor.jpg")
                .unwrap()
                .as_ptr() as *const i8,
        );
        let roughness_map = TextureLoadFromFile(
            CString::new("assets/demo/crate/Wood_Crate_001_roughness.jpg")
                .unwrap()
                .as_ptr() as *const i8,
        );
        let normal_map = TextureLoadFromFile(
            CString::new("assets/demo/crate/Wood_Crate_001_normal.jpg")
                .unwrap()
                .as_ptr() as *const i8,
        );
        let ao_map = TextureLoadFromFile(
            CString::new("assets/demo/crate/Wood_Crate_001_ambientOcclusion.jpg")
                .unwrap()
                .as_ptr() as *const i8,
        );
        let name: [i8; 64] = [0; 64];
        let mut crate_mat = Material {
            name: name,
            kind: 0,
            param_albedo: Vec3 {
                x: 0.0,
                y: 0.0,
                z: 0.0,
            },
            param_metallic: 0.0,
            param_roughness: 0.0,
            param_ao: 0.0,
            pbr_albedo_map: albedo_map,
            pbr_normal_map: normal_map,
            metal_roughness_combined: true,
            pbr_metallic_map: TextureHandle { raw: 99999 },
            pbr_roughness_map: roughness_map,
            pbr_ao_map: ao_map,
        };
        let crate_renderent = RenderEnt {
            mesh: addr_of_mut!(crate_mesh),
            material: addr_of_mut!(crate_mat),
            affine: mat4_ident(),
            casts_shadows: true,
        };
        let mut render_ents: [RenderEnt; 1] = [crate_renderent];
        let mut skybox = Skybox_Default();

        // Main rendering loop
        while !window.should_close() {
            glfw.poll_events();

            egui_ctx.begin_frame(egui_input_state.input.take());

            unsafe {
                gl::ClearColor(0.455, 0.302, 0.663, 1.0);
                gl::Clear(gl::COLOR_BUFFER_BIT);
                gl::Clear(gl::DEPTH_TEST);
            }

            Frame_Begin();
            gl::Enable(gl::DEPTH_TEST);
            gl::Enable(gl::CULL_FACE);

            Camera_Update(addr_of_mut!(camera));
            SetCamera(camera);

            Skybox_Draw(addr_of_mut!(skybox), camera);
            Render_RenderEntities(render_ents.as_mut_ptr(), render_ents.len());

            // Frame_End();

            // triangle.draw();

            gl::Disable(gl::DEPTH_TEST);
            gl::Disable(gl::CULL_FACE);

            egui::Window::new("Egui with GLFW").show(&egui_ctx, |ui| {
                ui.label("Celeritas in-game editor");
                let btn_m = &mut ui.button("-");
                let btn_p = &mut ui.button("+");

                ui.add(egui::Slider::new(slider, 0.0..=100.0).text("My value"));

                if btn_m.clicked() && *slider > 0.0 {
                    *slider -= 1.0;
                }

                if btn_p.clicked() && *slider < 100.0 {
                    *slider += 1.0;
                }
            });

            let egui::FullOutput {
                platform_output,
                textures_delta,
                shapes,
                ..
            } = egui_ctx.end_frame();

            //Handle cut, copy text from egui
            if !platform_output.copied_text.is_empty() {
                egui_backend::copy_to_clipboard(&mut egui_input_state, platform_output.copied_text);
            }

            //Note: passing a bg_color to paint_jobs will clear any previously drawn stuff.
            //Use this only if egui is being used for all drawing and you aren't mixing your own Open GL
            //drawing calls with it.
            //Since we are custom drawing an OpenGL Triangle we don't need egui to clear the background.

            let clipped_shapes = egui_ctx.tessellate(shapes, native_pixels_per_point);
            painter.paint_and_update_textures(
                native_pixels_per_point,
                &clipped_shapes,
                &textures_delta,
            );

            for (_, event) in glfw::flush_messages(&events) {
                match event {
                    glfw::WindowEvent::Close => window.set_should_close(true),
                    _ => {
                        egui_backend::handle_event(event, &mut egui_input_state);
                    }
                }
            }

            window.swap_buffers();
        }
    }
}