-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_utils.cpp
More file actions
85 lines (69 loc) · 3.87 KB
/
render_utils.cpp
File metadata and controls
85 lines (69 loc) · 3.87 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
#include "render_utils.h"
#include "renderer.h"
#include "scene.h"
#include "res_man.h"
void add_debug_mesh(struct RenderFrameContext *rfc, const RenderMesh *mesh, const mat4 &mat,
const vec4 &color, uint32_t selection_id/* = 0*/) {
RenderList *frame_render_list = rfc->rl_;
frame_render_list->ReservePackets(1);
RenderPacket *rp = frame_render_list->AddPacket();
rp->mesh_ = *mesh;
rp->m_ = mat;
rp->id_ = selection_id;
rp->debug_color = color;
rp->is_debug_pass = 1;
rp->is_selection_pass = (selection_id && selection_id > ReservedObjIds::kGizmoLast) ? 1 : 0;
rp->is_gizmo_pass = (selection_id && selection_id <= ReservedObjIds::kGizmoLast) ? 1 : 0;
rp->is_opaque_pass = 0;
rp->is_render_to_shadow = 0;
rp->is_particle_pass = 0;
}
void add_debug_sphere_constant_size(struct RenderFrameContext *rfc, const vec3& pos, float no_scale_distance, const vec4& color) {
const float oo_no_scale_distance = 1.0f / no_scale_distance;
const float cam_z = rfc->b_is_perspective_ ? (rfc->view_ * vec4(pos, 1)).z : no_scale_distance;
const mat4 tr = mat4::translation(pos) * mat4::scale(vec3(cam_z * oo_no_scale_distance));
add_debug_mesh(rfc, res_man_load_mesh("sphere"), tr, color);
}
// no_scale_distance - distance at which desired object will be drawn with no scale applied
void add_debug_mesh_constant_size(struct RenderFrameContext *rfc, const RenderMesh *mesh,
const vec4 &color, const mat4 &tr_m,
const float no_scale_distance/* = 1.0f*/,
uint32_t selection_id/* = 0*/) {
const float oo_no_scale_distance = 1.0f / no_scale_distance;
const float cam_z = rfc->b_is_perspective_ ? (rfc->view_ * tr_m.getTranslationPoint()).z : no_scale_distance;
const float k = (1.0f/rfc->proj_.elem[0][0]) / (rfc->viewport_.z / 2.0f);
const mat4 tr = tr_m * mat4::scale(vec3(k*cam_z * oo_no_scale_distance));
add_debug_mesh(rfc, mesh, tr, color, selection_id);
}
// only useful for 2d screen aligned objects
// pixels - approx amount of pixels object should have on the screen
void add_debug_mesh_constant_size_px(struct RenderFrameContext *rfc, const RenderMesh *mesh,
const vec4 &color, const mat4 &tr_m,
const uint32_t pixels,
uint32_t selection_id/* = 0*/) {
// for real we must take AABB, project it on screen and calculate from this
const float cam_z = rfc->b_is_perspective_ ? (rfc->view_ * tr_m.getTranslationPoint()).z : 1.0f;
const float oo_w = rfc->proj_.elem[0][0];
const float oo_h = rfc->proj_.elem[1][1];
float width_by_2 = rfc->viewport_.z / 2.0f;
float height_by_2 = rfc->viewport_.w / 2.0f;
float size_x = pixels * (cam_z * (1.0f/oo_w)) / width_by_2;
float size_y = pixels * (cam_z * (1.0f/oo_h)) / height_by_2;
const mat4 tr = tr_m * mat4::scale(vec3(size_x, size_y, 1.0f));// * mat4::translation(vec3(-0.5f, 0.5f, 0));
add_debug_mesh(rfc, mesh, tr, color, selection_id);
}
void add_debug_mesh_constant_size_px(struct RenderFrameContext *rfc, const RenderMesh *mesh, float world_bounds,
const vec4 &color, const mat4 &tr_m,
const uint32_t pixels,
uint32_t selection_id/* = 0*/) {
// for real we must take AABB, project it on screen and calculate from this
const float cam_z = rfc->b_is_perspective_ ? (rfc->view_ * tr_m.getTranslationPoint()).z : 1.0f;
const float oo_w = rfc->proj_.elem[0][0];
//const float oo_h = rfc->proj_.elem[1][1];
float width_by_2 = rfc->viewport_.z/2.0f;//800.0f / 2.0f;
//float height_by_2 = rfc->viewport_.w/2.0f;//600.0f / 2.0f;
float size = pixels * (cam_z * (1.0f/oo_w)) / width_by_2;
//float size_y = pixels * (cam_z * (1.0f/oo_h)) / height_by_2;
const mat4 tr = tr_m * mat4::scale((1.0f/world_bounds)*vec3(size));// * mat4::translation(vec3(-0.5f, 0.5f, 0));
add_debug_mesh(rfc, mesh, tr, color, selection_id);
}