Skip to content

Commit 0dcf9f7

Browse files
committed
Fixed instanced rendering rendering incorrectly
Changes committed: modified: Cargo.toml modified: src/core.rs modified: src/math.rs modified: src/shaders/final_shader.rs modified: src/shaders/glsl/VkModel.vert modified: src/shaders/model_shader.rs modified: src/shaders/sprv/VkModelVert.spv modified: src/shaders/texture_shader.rs modified: src/vulkan/window.rs
1 parent fe77f64 commit 0dcf9f7

File tree

9 files changed

+114
-20
lines changed

9 files changed

+114
-20
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ libc = "0.2.44"
1818
csv = "1.1"
1919

2020
[dependencies.gltf]
21-
version = "0.15"
21+
version = "0.15.1"
2222
features = ["extras", "names"]
2323

2424
[dependencies.cgmath]

src/core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,11 +785,11 @@ impl CoreRender for CoreMaat {
785785
}
786786

787787
fn show_cursor(&mut self) {
788-
788+
self.window.set_cursor_visible(true);
789789
}
790790

791791
fn hide_cursor(&mut self) {
792-
792+
self.window.set_cursor_visible(false);
793793
}
794794

795795
fn set_clear_colour(&mut self, r: f32, g: f32, b: f32, a: f32) {

src/math.rs

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,81 @@ pub fn sphere_intersect_AABB(sphere: Vector4<f32>, box_location: Vector3<f32>, b
7272
dist < sphere.w
7373
}
7474

75-
pub fn is_point_inside_circle(point: Vector2<f32>, sphere: Vector3<f32>) -> bool {
75+
pub fn is_point_inside_circle(point: Vector2<f32>, sphere: Vector2<f32>, radius: f32) -> bool {
7676
let dist = ((point.x - sphere.x) * (point.x - sphere.x) +
7777
(point.y - sphere.y) * (point.y - sphere.y)).sqrt();
7878

79-
dist < sphere.z // dist < radius
79+
dist < radius // dist < radius
80+
}
81+
82+
pub fn intersect_circle(sphere_a: Vector2<f32>, radius_a: f32, sphere_b: Vector2<f32>, radius_b: f32) -> bool {
83+
let dist = ((sphere_a.x - sphere_b.x) * (sphere_a.x - sphere_b.x) +
84+
(sphere_a.y - sphere_b.y) * (sphere_a.y - sphere_b.y)).sqrt();
85+
86+
dist < radius_a + radius_b // dist < a_radius+b_radius
87+
}
88+
89+
pub fn intersect_square(box_a_location: Vector2<f32>, box_a_size: Vector2<f32>, box_b_location: Vector2<f32>, box_b_size: Vector2<f32>) -> bool {
90+
let a_min_x = box_a_location.x - box_a_size.x*0.5;
91+
let a_max_x = box_a_location.x + box_a_size.x*0.5;
92+
let a_min_y = box_a_location.y - box_a_size.y*0.5;
93+
let a_max_y = box_a_location.y + box_a_size.y*0.5;
94+
95+
let b_min_x = box_b_location.x - box_b_size.x*0.5;
96+
let b_max_x = box_b_location.x + box_b_size.x*0.5;
97+
let b_min_y = box_b_location.y - box_b_size.y*0.5;
98+
let b_max_y = box_b_location.y + box_b_size.y*0.5;
99+
100+
(a_min_x <= b_max_x && a_max_x >= b_min_x) &&
101+
(a_min_y <= b_max_y && a_max_y >= b_min_y)
102+
}
103+
104+
pub fn circle_intersect_square(sphere: Vector2<f32>, radius: f32, box_location: Vector2<f32>, box_size: Vector2<f32>) -> bool {
105+
let min_x = box_location.x - box_size.x*0.5;
106+
let max_x = box_location.x + box_size.x*0.5;
107+
let min_y = box_location.y - box_size.y*0.5;
108+
let max_y = box_location.y + box_size.y*0.5;
109+
110+
let x = ((sphere.x).min(max_x)).max(min_x);
111+
let y = ((sphere.y).min(max_y)).max(min_y);
112+
113+
let dist = ((x - sphere.x) * (x - sphere.x) +
114+
(y - sphere.y) * (y - sphere.y)).sqrt();
115+
116+
dist < radius
117+
}
118+
119+
pub fn line_intersect_square(point_a: Vector2<f32>, point_b: Vector2<f32>, box_location: Vector2<f32>, box_size: Vector2<f32>) -> bool {
120+
let min_x = box_location.x - box_size.x*0.5;
121+
let max_x = box_location.x + box_size.x*0.5;
122+
let min_y = box_location.y - box_size.y*0.5;
123+
let max_y = box_location.y + box_size.y*0.5;
124+
125+
let line_min_x = point_a.x.min(point_b.x);
126+
let line_max_x = point_a.x.max(point_b.x);
127+
let line_min_y = point_a.y.min(point_b.y);
128+
let line_max_y = point_a.y.max(point_b.y);
129+
130+
if min_x > line_max_x || max_x < line_min_x {
131+
return false;
132+
}
133+
134+
if max_y < line_min_y || min_y > line_max_y {
135+
return false;
136+
}
137+
138+
// if box not axis aligned
139+
// let y_at_rect_min_x = line.calculate_y_for_x(box.left)
140+
// let y_at_rect_max_x = line.calculate_y_for_x(box.right)
141+
//if min_x > yatrectleft || max_x < yatrectright {
142+
// return false;
143+
//}
144+
145+
//if max_y < yatrectleft || min_y > yatrectright {
146+
// return false;
147+
//}
148+
149+
true
80150
}
81151

82152
pub trait Vector2Math<T> {

src/shaders/final_shader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl FinalShader {
244244
cmd.draw_indexed(Arc::clone(&device), &self.vertex_buffer.internal_object(0),
245245
&self.index_buffer.internal_object(0),
246246
index_count, &self.pipeline,
247-
vec!(if is_model_texture {
247+
vec!(if is_model_texture {
248248
*self.descriptor_sets[current_buffer].set(0)
249249
} else {
250250
*self.ds[current_buffer].set(0)

src/shaders/glsl/VkModel.vert

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,37 @@ vec4 quaternion_normalise(vec4 q) {
8383
return vec4(q.x/n, q.y/n, q.z/n, q.w/n);
8484
}
8585

86-
vec4 quaternion_from_rotation(vec3 axis, float deg_rotation) {
87-
vec4 q = vec4(0.0);
86+
vec4 quaternion_from_rotation(vec3 deg_rotation) {
87+
/*vec4 q = vec4(0.0);
8888
8989
float h_angle = (deg_rotation*0.5) * M_PI / 180.0;
9090
q.x = axis.x * sin(h_angle);
9191
q.y = axis.y * sin(h_angle);
9292
q.z = axis.z * sin(h_angle);
9393
q.w = cos(h_angle);
9494
95+
return q;*/
96+
//deg_rotation.x += 180.0;
97+
//deg_rotation.z += 270.0;
98+
//deg_rotation = vec3(0.0, 0.0, 0.0);
99+
float h = 0.5;
100+
float s_x = sin(to_radians(deg_rotation.x) * h); // 0
101+
float c_x = cos(to_radians(deg_rotation.x) * h); // 1
102+
103+
float s_y = sin(to_radians(deg_rotation.z) * h); // 0
104+
float c_y = cos(to_radians(deg_rotation.z) * h); // 1
105+
106+
float s_z = sin(to_radians(deg_rotation.y) * h); // 0
107+
float c_z = cos(to_radians(deg_rotation.y) * h); // 1
108+
109+
vec4 q = vec4(0.0);
110+
q.w = -s_x*s_y*s_z + c_x*c_y*c_z; //1
111+
q.x = s_x*c_y*c_z + s_y*s_z*c_x; //0
112+
q.z = -s_x*s_z*c_y + s_y*c_x*c_z; //0
113+
q.y = s_z*s_y*c_z + s_z*c_x*c_y; //
114+
95115
return q;
116+
//return vec4(0.0, 0.707, 0.0, 1.0);
96117
}
97118

98119
vec4 quaternion_conj(vec4 q) {
@@ -121,29 +142,27 @@ vec4 quaternion_mul(vec4 q1, vec4 q2) {
121142
return qr;
122143
}
123144

124-
vec3 rotate_vertex_position(vec3 position, vec3 axis, float angle) {
125-
vec4 q = quaternion_from_rotation(axis, angle);
145+
vec3 rotate_vertex_position(vec3 position, vec3 angle) {
146+
vec4 q = quaternion_normalise(quaternion_from_rotation(angle));
126147
vec3 v = position.xyz;
127148
return v + 2.0 * cross(q.xyz, cross(q.xyz, v) + q.w * v);
128149
}
129150

130151

131-
mat4 create_rotation_matrix_from_quaternion(vec3 axis, float deg_rotation) {
132-
vec4 q = quaternion_from_rotation(axis, deg_rotation);
133-
float s = sin(to_radians(deg_rotation) * 0.5);
152+
mat4 create_rotation_matrix_from_quaternion(vec3 deg_rotation) {
153+
vec4 q = quaternion_from_rotation(deg_rotation);
154+
/*float s = sin(to_radians(deg_rotation.x) * 0.5) + sin(to_radians(deg_rotation.y) * 0.5) + sin(to_radians(deg_rotation.z) * 0.5);
134155
mat4 rotation_matrix = mat4(
135156
vec4(1.0 - 2.0*s*(q.y*q.y + q.z*q.z), 2.0*s*(q.x*q.y - q.z*q.w), 2.0*s*(q.x*q.z + q.y*q.w), 0.0),
136157
vec4( 2.0*s*(q.x*q.y + q.z*q.w), 1.0 - 2.0*s*(q.x*q.x + q.z*q.z), 2.0*s*(q.y*q.z - q.x*q.w), 0.0),
137158
vec4( 2.0*s*(q.x*q.z - q.y*q.w), 2.0*s*(q.y*q.z + q.x*q.w), 1.0 - 2.0*s*(q.x*q.x + q.y*q.y), 0.0),
138159
vec4(0.0, 0.0, 0.0, 1.0));
139-
160+
*/
140161
return rotation_matrix;
141162
}
142163

143164
vec3 rotate_vector_by_angle(vec3 pos, vec3 rotation) {
144-
vec3 rotated_pos = rotate_vertex_position(pos, vec3(1.0, 0.0, 0.0), rotation.x);
145-
rotated_pos = rotate_vertex_position(rotated_pos, vec3(0.0, 1.0, 0.0), rotation.y);
146-
rotated_pos = rotate_vertex_position(rotated_pos, vec3(0.0, 0.0, 1.0), rotation.z);
165+
vec3 rotated_pos = rotate_vertex_position(pos, rotation);
147166

148167
return rotated_pos;
149168
}

src/shaders/model_shader.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,7 @@ impl ModelShader {
12661266
let camera_up = Vector4::new(c_up.x, c_up.y, c_up.z, scale.x);
12671267
let model = Vector4::new(position.x, position.y, position.z, scale.y);
12681268
let rotation = Vector4::new(rotation.x, rotation.y, rotation.z, scale.z);
1269+
println!("MS: {}: Rotation {:?}", model_reference, rotation);
12691270
let hologram = Vector4::new(if hologram { 1.0 } else { -1.0 }, self.scanline, 0.0, 0.0);
12701271

12711272
for j in 0..self.models[i].vertex_buffers.len() {

src/shaders/sprv/VkModelVert.spv

1.79 KB
Binary file not shown.

src/shaders/texture_shader.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,9 @@ impl TextureShader {
504504

505505
let height = self.camera.get_top();
506506
let width = self.camera.get_right();
507-
let top = height + height*0.01;
508-
let right = width + width*0.01;
509-
let pos = self.camera.get_position()+Vector2::new(0.0, -10.0);
507+
let top = height; //+ height*0.01;
508+
let right = width;// + width*0.01;
509+
let pos = self.camera.get_position();//+Vector2::new(0.0, -10.0);
510510
let projection_details = Vector4::new(pos.x, pos.y, right, top);
511511
let model = Vector4::new(position.x, position.y, scale.x, scale.y);
512512
let rotation = Vector4::new(rotation, 0.0, 0.0, 0.0);

src/vulkan/window.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ impl VkWindow {
387387
dpi as f32
388388
}
389389

390+
pub fn set_cursor_visible(&mut self, visible: bool) {
391+
self.window.set_cursor_visible(visible);
392+
}
393+
390394
pub fn set_cursor_position(&self, new_pos: LogicalPosition<f32>, logs: &mut Logs) {
391395
if let Err(e) = self.window.set_cursor_position(new_pos) {
392396
logs.error_msg(&e.to_string());

0 commit comments

Comments
 (0)