-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug_renderer.cpp
88 lines (71 loc) · 2.54 KB
/
debug_renderer.cpp
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
#include "debug_renderer.h"
#include "gameos.hpp"
#include <algorithm>
class DebugRenderer {
mat4 vp_;
public:
void setup(const mat4& view, const mat4& proj) {
vp_ = proj * view;
}
void render(const RenderPacket &rp) {
const RenderMesh& ro = rp.mesh_;
HGOSRENDERMATERIAL mat = gos_getRenderMaterial("debug");
mat4 wvp = vp_ * rp.m_;
gos_SetRenderMaterialParameterMat4(mat, "wvp_", (const float*)wvp);
gos_SetRenderMaterialParameterFloat4(mat, "colour_", (const float*)rp.debug_color);
gos_ApplyRenderMaterial(mat);
if (ro.ib_) {
gos_RenderIndexedArray(ro.ib_, ro.vb_, ro.vdecl_, ro.prim_type_);
} else if(ro.inst_vb_) {
gos_RenderArrayInstanced(ro.vb_, ro.inst_vb_, ro.num_instances, ro.vdecl_, ro.prim_type_);
} else {
gos_RenderArray(ro.vb_, ro.vdecl_, ro.prim_type_, ro.vb_first_, ro.vb_count_);
}
}
};
void RenderDebugObjects(const RenderPacketList_t& rpl, const mat4& view, const mat4& proj)
{
RenderPacketList_t::const_iterator it = rpl.begin();
RenderPacketList_t::const_iterator end = rpl.end();
DebugRenderer dr;
dr.setup(view, proj);
std::vector<RenderPacket> tmp;
for(;it!=end;++it)
{
const RenderPacket& rp = (*it);
if(rp.is_debug_pass)
tmp.push_back(rp);
}
struct RPSorter {
mat4 view_;
bool operator()(const RenderPacket& a, const RenderPacket& b) {
if(a.is_gizmo_pass && !b.is_gizmo_pass) {
return true;
} else if(!a.is_gizmo_pass && b.is_gizmo_pass) {
return false;
}
return (view_ * a.m_.getCol3()).z < (view_ * b.m_.getCol3()).z;
}
};
const bool depth_test = false;
const bool b_double_sided = true;
const gos_CullMode cull_mode = b_double_sided ? gos_Cull_None: gos_Cull_CCW;
RPSorter sorter;
sorter.view_ = view;
std::sort(tmp.begin(), tmp.end(), sorter);
//#define TEST_POINT_LIGTHS
#if defined(TEST_POINT_LIGTHS)
gos_SetRenderState(gos_State_ZCompare, 3);
gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
#else
gos_SetRenderState(gos_State_ZCompare, depth_test);
gos_SetRenderState(gos_State_Culling, cull_mode);
#endif
//gos_SetRenderState(gos_State_AlphaMode, gos_Alpha_OneZero);
gos_SetRenderState(gos_State_AlphaMode, gos_Alpha_AlphaOne);
gos_SetRenderState(gos_State_ZWrite, 1);
for(auto& rp: tmp)
dr.render(rp);
gos_SetRenderState(gos_State_AlphaMode, gos_Alpha_OneZero);
gos_SetRenderState(gos_State_ZWrite, 1);
}