-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebugDraw.cpp
114 lines (98 loc) · 2.37 KB
/
DebugDraw.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
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
#include "DebugDraw.h"
#include "Uniform.h"
#include "Draw.h"
//#include "shader.h"
DebugDraw::DebugDraw() {
mAttribs = new Attribute<vec3>();
mShader = new shader(
"#version 330 core\n"
"uniform mat4 mvp;\n"
"in vec3 position;\n"
"void main() {\n"
" gl_Position = mvp * vec4(position, 1.0);\n"
"}"
,
"#version 330 core\n"
"uniform vec3 color;\n"
"out vec4 FragColor;\n"
"void main() {\n"
" FragColor = vec4(color, 1);\n"
"}"
);
}
DebugDraw::DebugDraw(unsigned int size) {
mAttribs = new Attribute<vec3>();
mShader = new shader(
"#version 330 core\n"
"uniform mat4 mvp;\n"
"in vec3 position;\n"
"void main() {\n"
" gl_Position = mvp * vec4(position, 1.0);\n"
"}"
,
"#version 330 core\n"
"uniform vec3 color;\n"
"out vec4 FragColor;\n"
"void main() {\n"
" FragColor = vec4(color, 1);\n"
"}"
);
Resize(size);
}
DebugDraw::~DebugDraw() {
delete mAttribs;
delete mShader;
}
unsigned int DebugDraw::Size() {
return (unsigned int)mPoints.size();
}
void DebugDraw::Resize(unsigned int newSize) {
mPoints.resize(newSize);
}
vec3& DebugDraw::operator[](unsigned int index) {
return mPoints[index];
}
void DebugDraw::Push(const vec3& v) {
mPoints.push_back(v);
}
void DebugDraw::UpdateOpenGLBuffers() {
mAttribs->Set(mPoints);
}
void DebugDraw::FromPose(Pose& pose) {
unsigned int requiredVerts = 0;
unsigned int numJoints = pose.Size();
for (unsigned int i = 0; i < numJoints; ++i) {
if (pose.GetParent(i) < 0) {
continue;
}
requiredVerts += 2;
}
mPoints.resize(requiredVerts);
for (unsigned int i = 0; i < numJoints; ++i) {
if (pose.GetParent(i) < 0) {
continue;
}
mPoints.push_back(pose.GetGlobalTransform(i).position);
mPoints.push_back(pose.GetGlobalTransform(pose.GetParent(i)).position);
}
}
void DebugDraw::Draw(DebugDrawMode mode, const vec3& color, const mat4& mvp) {
mShader->Bind();
Uniform<mat4>::Set(mShader->GetUniform("mvp"), mvp);
Uniform<vec3>::Set(mShader->GetUniform("color"), color);
mAttribs->BindTo(mShader->GetAttribute("position"));
if (mode == DebugDrawMode::Lines) {
::Draw(Size(), DrawMode::Lines);
}
else if (mode == DebugDrawMode::Loop) {
::Draw(Size(), DrawMode::LineLoop);
}
else if (mode == DebugDrawMode::Strip) {
::Draw(Size(), DrawMode::LineStrip);
}
else {
::Draw(Size(), DrawMode::Points);
}
mAttribs->UnBindFrom(mShader->GetAttribute("position"));
mShader->UnBind();
}