-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglpreview.pas
149 lines (130 loc) · 3.13 KB
/
glpreview.pas
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
unit glpreview;
interface
uses
gl, Math, ChipmunkImport;
type
TStep = procedure(space: PcpSpace);
procedure drawCollisions(ptr: pointer; Data: pointer);
procedure drawCircle(x: cpFloat; y: cpFloat; r: cpFloat; a: cpFloat);
procedure drawCircleShape(shape: PcpShape);
procedure drawSegmentShape(shape: PcpShape);
procedure drawPolyShape(shape: PcpShape);
procedure drawObject(ptr: pointer; unused: pointer);
procedure previewGL(space : PcpSpace);
implementation
procedure drawCollisions(ptr: pointer; Data: pointer);
var
arb: PcpArbiter;
i: integer;
v: cpVect;
begin
arb := PcpArbiter(ptr);
for i := 0 to arb^.numContacts - 1 do
begin
v := arb^.contacts^[i].p;
glVertex2f(v.x, v.y);
end;
end;
procedure drawCircle(x: cpFloat; y: cpFloat; r: cpFloat; a: cpFloat);
const
segs = 15;
var
coef: cpFloat;
n: integer;
rads: cpFloat;
begin
coef := 2.0 * PI / segs;
glBegin(GL_LINE_STRIP);
for n := 0 to segs do
begin
rads := n * coef;
glVertex2f(r * cos(rads + a) + x, (r * sin(rads + a) + y));
end;
glVertex2f(x, y);
glEnd();
end;
procedure drawCircleShape(shape: PcpShape);
var
body: PcpBody;
circle: PcpCircleShape;
c: cpVect;
begin
body := shape^.body;
circle := PcpCircleShape(shape);
c := cpvadd(body^.p, cpvrotate(circle^.c, body^.rot));
drawCircle(c.x, c.y, circle^.r, body^.a);
end;
procedure drawSegmentShape(shape: PcpShape);
var
body: PcpBody;
seg: PcpSegmentShape;
a, b: cpVect;
begin
body := shape^.body;
seg := PcpSegmentShape(shape);
a := cpvadd(body^.p, cpvrotate(seg^.a, body^.rot));
b := cpvadd(body^.p, cpvrotate(seg^.b, body^.rot));
glBegin(GL_LINES);
glVertex2f(a.x, a.y);
glVertex2f(b.x, b.y);
glEnd();
end;
procedure drawPolyShape(shape: PcpShape);
var
body: PcpBody;
poly: PcpPolyShape;
num: integer;
i: integer;
v: cpVect;
begin
body := shape^.body;
poly := PcpPolyShape(shape);
num := poly^.numVerts;
glBegin(GL_LINE_LOOP);
for i := 0 to num - 1 do
begin
v := cpvadd(body^.p, cpvrotate(poly^.verts[i], body^.rot));
glVertex2f(v.x, v.y);
end;
glEnd();
end;
procedure drawObject(ptr: pointer; unused: pointer);
var
shape: PcpShape;
begin
shape := PcpShape(ptr);
case (shape^.cptype) of
CP_CIRCLE_SHAPE:
drawCircleShape(shape);
CP_SEGMENT_SHAPE:
drawSegmentShape(shape);
CP_POLY_SHAPE:
drawPolyShape(shape);
end;
end;
procedure previewGL(space : PcpSpace);
var
i, num: integer;
bodies: PcpArray;
body: PcpBody;
begin
glColor3f(0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
cpSpaceHashEach(space^.activeShapes, @drawObject, nil);
cpSpaceHashEach(space^.staticShapes, @drawObject, nil);
bodies := space^.bodies;
num := bodies^.num;
glBegin(GL_POINTS);
begin
glColor3f(0.0, 0.0, 1.0);
for i := 0 to num - 1 do
begin
body := PcpBody(bodies^.arr[i]);
glVertex2f(body^.p.x, body^.p.y);
end;
glColor3f(1.0, 0.0, 0.0);
cpArrayEach(space^.arbiters, @drawCollisions, nil);
end;
glEnd();
end;
end.