-
Notifications
You must be signed in to change notification settings - Fork 1
/
Unit_Pieces.pas
219 lines (169 loc) · 4.81 KB
/
Unit_Pieces.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
unit Unit_Pieces;
interface
uses Classes, dglOpenGL, Math, Unit_Vector;
type
TPieceLoc = (plDeck, plCursor, plIngot);
TPiece = class
private
fVerts: array of TVertice;
fPolys: array of TPoly3;
fVtxBuf: GLuint;
fIndBuf: GLuint;
//Where the piece should be located on Ingot
fGoal: TVector3f;
public
Id: Integer;
Location: TPieceLoc;
Selected: Boolean;
DeckPosition: TVector2i;
constructor Create(aVtxBuf, aIndBuf: GLuint);
procedure Init;
procedure Render2D;
procedure Render3D;
end;
TPiecesCollection = class
private
fPieces: TList;
fSelected: Integer;
function GetCount: Integer;
function GetPiece(aIndex: Integer): TPiece;
procedure SetSelected(aValue: Integer);
public
constructor Create;
destructor Destroy; override;
property Count: Integer read GetCount;
property Selected: Integer read fSelected write SetSelected;
property Pieces[aIndex: Integer]: TPiece read GetPiece; default;
function PieceById(aIndex: Integer): TPiece;
procedure LoadFromFile(aFilename: string);
end;
var
fPieces: TPiecesCollection;
implementation
uses
Unit_Render, Unit_ColorCoder;
{ TPiece }
constructor TPiece.Create(aVtxBuf, aIndBuf: GLuint);
begin
inherited Create;
fVtxBuf := aVtxBuf;
fIndBuf := aIndBuf;
end;
procedure TPiece.Init;
begin
glBindBuffer(GL_ARRAY_BUFFER, fVtxBuf);
glBufferData(GL_ARRAY_BUFFER, Length(fVerts) * SizeOf(TVertice), @fVerts[0].X, GL_STREAM_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, fIndBuf);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, Length(fPolys) * SizeOf(fPolys[0]), @fPolys[0,0], GL_STREAM_DRAW);
end;
//Render piece on the Deck
procedure TPiece.Render2D;
procedure SetRenderColor;
begin
if fRender.IsNormal then
begin
if Selected then
glColor3f(1,1,1)
else
glColor3f(0.5, 0.6, 0.5);
end
else
begin
SetColorCode(ccPiece, Id);
end;
end;
begin
SetRenderColor;
glBindBuffer(GL_ARRAY_BUFFER, fVtxBuf);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, fIndBuf);
//Setup vertex and UV layout and offsets
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, SizeOf(TVertice), Pointer(0));
if fRender.IsNormal then
begin
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, SizeOf(TVertice), Pointer(12));
end;
//Here and above OGL requests Pointer, but in fact it's just a number (offset within Array)
glDrawElements(GL_TRIANGLES, Length(fPolys) * Length(fPolys[0]), GL_UNSIGNED_INT, Pointer(0));
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
end;
//Render piece on the Ingot
procedure TPiece.Render3D;
begin
end;
{ TPiecesCollection }
constructor TPiecesCollection.Create;
begin
fPieces := TList.Create;
fSelected := -1;
end;
destructor TPiecesCollection.Destroy;
begin
fPieces.Free;
inherited;
end;
function TPiecesCollection.GetCount: Integer;
begin
Result := fPieces.Count;
end;
function TPiecesCollection.GetPiece(aIndex: Integer): TPiece;
begin
Result := fPieces[aIndex];
end;
procedure TPiecesCollection.LoadFromFile(aFilename: string);
var
I: Integer;
NewCount: Integer;
VtxBuf: array of GLuint;
IndBuf: array of GLuint;
NewPiece: TPiece;
begin
NewCount := 10;
//Generate all buffers in batch
SetLength(VtxBuf, NewCount);
SetLength(IndBuf, NewCount);
glGenBuffers(NewCount, @VtxBuf[0]);
glGenBuffers(NewCount, @IndBuf[0]);
for I := 0 to NewCount - 1 do
begin
NewPiece := TPiece.Create(VtxBuf[I], IndBuf[I]);
NewPiece.Id := I;
NewPiece.fGoal := Vector3(0.5, 0, 0);
SetLength(NewPiece.fVerts, 4);
NewPiece.fVerts[0] := Vertice(-0.1, 0.1, 0, -0.1, 0.1, 1);
NewPiece.fVerts[1] := Vertice( 0.1, 0.1, 0, 0.1, 0.1, 1);
NewPiece.fVerts[2] := Vertice( 0.1, -0.1, 0, 0.1, -0.1, 1);
NewPiece.fVerts[3] := Vertice(-0.1, -0.1, 0, -0.1, -0.1, 1);
SetLength(NewPiece.fPolys, 2);
NewPiece.fPolys[0] := Poly3(0, 2, 1);
NewPiece.fPolys[1] := Poly3(0, 2, 3);
NewPiece.DeckPosition := Vector2i(I, 0);
NewPiece.Init;
fPieces.Add(NewPiece);
end;
end;
function TPiecesCollection.PieceById(aIndex: Integer): TPiece;
var
I: Integer;
begin
Result := nil;
for I := 0 to Count - 1 do
if Pieces[I].Id = aIndex then
begin
Result := Pieces[I];
Break;
end;
end;
procedure TPiecesCollection.SetSelected(aValue: Integer);
begin
//Deselect previous
if InRange(fSelected, 0, Count - 1) then
Pieces[fSelected].Selected := False;
fSelected := aValue;
//Select current
if InRange(fSelected, 0, Count - 1) then
Pieces[fSelected].Selected := True;
end;
end.