1pub mod core;
2pub mod util;
3
4pub extern crate cgmath;
5pub extern crate gl;
6pub extern crate glfw;
7
8#[cfg(test)]
9mod test {
10
11 use crate::cgmath::{PerspectiveFov, Rad};
12 use crate::core::application::*;
13 use crate::core::attribute::Attribute;
14 use crate::core::camera::Camera;
15 use crate::core::gl_var_type::GLvartype;
16 use crate::core::scaffold::AppScaffold;
17 use crate::util::program_utils::ProgramUtils;
18 use crate::{gl, glfw};
19 use gl::types::GLuint;
20 use glfw::{Action, Key, Modifiers, MouseButton, Scancode};
21 use std::f32::consts::PI;
22
23 #[test]
24 fn three_d_spinning_cube() {
25 unsafe {
26 KartApple::start(PrismGL::new(), 700, 500, "window");
27 }
28 }
29
30 pub struct PrismGL {
31 cam: Option<Camera>,
32 degrees: f32,
33 vao: GLuint,
34 program: GLuint
35 }
36 impl PrismGL {
37 pub fn new() -> PrismGL {
38 PrismGL {
39 cam: None,
40 degrees: 0f32,
41 vao: GLuint::from(1u32),
42 program: GLuint::from(1u32)
43 }
44 }
45 }
46 impl AppScaffold for PrismGL {
47 unsafe fn on_init(&mut self, app: &mut KartApple) {
48 gl::Enable(gl::DEPTH_TEST);
49
50 let vert_code = include_str!("../shaders/vert.glsl").to_string();
51 let frag_code = include_str!("../shaders/frag.glsl").to_string();
52
53 self.program = ProgramUtils::create_program(&vert_code, &frag_code);
54
55 let mut vao = GLuint::from(1u32);
56 gl::GenVertexArrays(1, &mut vao);
57 gl::BindVertexArray(vao);
58 self.vao = vao;
59 let side_length = 0.75f32;
60
61 let vertices = [
62 -side_length,
64 -side_length,
65 -side_length,
66 side_length,
67 -side_length,
68 -side_length,
69 side_length,
70 side_length,
71 -side_length,
72 -side_length,
73 side_length,
74 -side_length,
75 -side_length,
77 side_length,
78 side_length,
79 -side_length,
80 -side_length,
81 side_length,
82 -side_length,
83 -side_length,
84 -side_length,
85 -side_length,
86 side_length,
87 -side_length,
88 -side_length,
90 side_length,
91 -side_length,
92 -side_length,
93 side_length,
94 side_length,
95 side_length,
96 side_length,
97 side_length,
98 side_length,
99 side_length,
100 -side_length,
101 side_length,
103 -side_length,
104 -side_length,
105 side_length,
106 -side_length,
107 side_length,
108 side_length,
109 side_length,
110 side_length,
111 side_length,
112 side_length,
113 -side_length,
114 -side_length,
116 side_length,
117 side_length,
118 -side_length,
119 -side_length,
120 side_length,
121 side_length,
122 -side_length,
123 side_length,
124 side_length,
125 side_length,
126 side_length,
127 -side_length,
129 -side_length,
130 -side_length,
131 side_length,
132 -side_length,
133 -side_length,
134 side_length,
135 -side_length,
136 side_length,
137 -side_length,
138 -side_length,
139 side_length,
140 ];
141
142 Attribute::init(&vertices);
143 Attribute::locate_attribute(self.program, "pos", GLvartype::Vec3);
144
145 let vert_colors = [
146 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
153 ];
154
155 Attribute::init(&vert_colors);
156 Attribute::locate_attribute(self.program, "vertColor", GLvartype::Vec3);
157
158 let mut cam = Camera::new(self.program, "model", "view", "proj");
159
160 let fov = 90f32;
161 cam.projection(PerspectiveFov {
162 fovy: Rad((PI / 180f32) * fov),
163 aspect: (app.window.width as f32 / app.window.height as f32),
164 near: (0.1f32),
165 far: (100f32),
166 });
167 cam.translate_view_z(-2.50f32);
168 cam.rotate_view_x(40.0);
169 self.cam = Some(cam);
170 self.degrees = 60f32;
171 }
172
173 unsafe fn on_loop(&mut self, app: &mut KartApple) {
174 gl::ClearColor(0.0, 0.0, 0.0, 1.0);
175 gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
176 gl::UseProgram(self.program);
177 self.cam
178 .as_mut()
179 .unwrap()
180 .rotate_model_y(app.delta.value as f32 * self.degrees / 1000000f32);
181 self.cam.as_mut().unwrap().update();
182
183 gl::DrawArrays(gl::QUADS, 0, 24);
184 }
185
186 unsafe fn on_key(&mut self, key: Key, scancode: Scancode, action: Action, modifiers: Modifiers, app: &mut KartApple) {
187 println!("key: {key:?}");
188
189 match key {
190 Key::W => {
191 self.degrees += 1.0;
192 }
193 _ => {}
194 }
195 }
196
197 unsafe fn on_mouse(&mut self, button: MouseButton, action: Action, modifiers: Modifiers, app: &mut KartApple) {
198 }
199
200 unsafe fn on_resize(&mut self, width: i32, height: i32, app: &mut KartApple) {
201 gl::Viewport(0, 0, width, height);
202 }
203
204 unsafe fn on_clean(&mut self, app: &mut KartApple) {
205 gl::DeleteVertexArrays(1, &self.vao);
206 gl::DeleteBuffers(1, &self.vao);
207 gl::DeleteProgram(self.program);
208
209 }
210 }
211}