aboutsummaryrefslogtreecommitdiffstats
path: root/Game/Code/Classes/UWebCam.pas
blob: 9457a28dbfd5100dc7db4512efd0602740950410 (plain) (blame)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
unit UWebCam;

interface

uses
     math,
     gl,
     glu,
     glext,
     SysUtils,
     UIni,
     UTime,
     SDL,
     UCaptureWDM;

function wStartWebCam: boolean;
procedure wStopWebCam;

procedure wInit;
procedure wClose;
procedure wDraw(DoDraw: boolean; Screen: integer);


var
  WebCamReady:        boolean;
  FGrabFrameFlag :    boolean;

  FTex:               glUint;
  FTexX, FTexY:       integer;
  FdataX, FdataY:     integer;
  FrameDataPtr:       PByteArray;

  frame:              Pointer;

  WDMSample:          TSampleClass;

implementation

uses
  UGraphic,
  ULog,
  UDisplay;

function wStartWebCam(): boolean;
begin
  if not WebCamReady then
    wInit();

  Result := WebCamReady;
  if WebCamReady and not FGrabFrameFlag then
  begin
    glBindTexture(GL_TEXTURE_2D, FTex);

    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, FTexX, FTexY,
      GL_BGR, GL_UNSIGNED_BYTE, @FrameDataPtr[0]);
    glBindTexture(GL_TEXTURE_2D, 0);
      
    FGrabFrameFlag := true;

    WDMSample.Start;
  end;
end;

procedure wStopWebCam();
begin
  if WebCamReady and FGrabFrameFlag then
  begin
    FGrabFrameFlag := false;
    WDMSample.Stop;
  end;
end;

procedure wInit;
const
  width = 320;
  height = 240;

var
  IWebCamDevice:  TList;

begin
  if WebCamReady then
    exit;

  WebCamReady := false;

  if(Ini.EnableWebCam=0) then
    exit;

  GetCapDevices(IWebCamDevice);
  if (Length(IWebCamDevice)-1 < Ini.WebCamID) then
    Exit;

  try
    WDMSample := TSampleClass.Create(Ini.WebCamID, Ini.WebCamMediaID);
  except
    wClose;
    Log.LogError('Error init WDM (UWebCam.wInitWDM)');
    Exit;
  end;

  FTexX := width;
  FTexY := height;
  FdataX := Round(Power(2, Ceil(Log2(FTexX))));
  FdataY := Round(Power(2, Ceil(Log2(FTexY))));

  FrameDataPtr:=WDMSample.FramePtr;

  glGenTextures(1, @FTex);
  glBindTexture(GL_TEXTURE_2D, FTex);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

  glTexImage2D(GL_TEXTURE_2D, 0, 3, FdataX, FdataY, 0,
    GL_BGR, GL_UNSIGNED_BYTE, nil);
  glBindTexture(GL_TEXTURE_2D, 0);

  FGrabFrameFlag := false;
  WebCamReady := true;
end;

procedure UploadNewFrame;
begin
  if (not WDMSample.CapStatus) then
    exit;

  FrameDataPtr := WDMSample.FramePtr;

  if (FTexX<>WDMSample.GetWidth) or (FTexY<>WDMSample.GetHeight) then
  begin
    FTexX := WDMSample.GetWidth;
    FTexY := WDMSample.GetHeight;
    FdataX := Round(Power(2, Ceil(Log2(FTexX))));
    FdataY := Round(Power(2, Ceil(Log2(FTexY))));

    glGenTextures(1, @FTex);
    glBindTexture(GL_TEXTURE_2D, FTex);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, 3, FdataX, FdataY, 0,
      GL_BGR, GL_UNSIGNED_BYTE, nil);
    glBindTexture(GL_TEXTURE_2D, 0);
  end;

  glBindTexture(GL_TEXTURE_2D, FTex);

  glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, FTexX, FTexY,
    GL_BGR, GL_UNSIGNED_BYTE, @FrameDataPtr[0]);
  glBindTexture(GL_TEXTURE_2D, 0);

  WDMSample.TriggerCapture;
end;


procedure wDraw(DoDraw: boolean; Screen: integer);
var
  SRect: record
    left, right, upper, lower:  double;
  end;

  ScreenAspect:       double;
  CamAspect:          double;
  ScaledVideoWidth:   double;
  ScaledVideoHeight:  double;

begin
  if not WebCamReady then
    exit;

  if DoDraw then
  begin
    try
      UploadNewFrame;
    except
      wClose;
      Log.LogError('Error Uploading new Frame (UWebCam.wDraw)');
      Exit;
    end;
  end else
    Exit;

  ScreenAspect := (ScreenW/Screens) / ScreenH;
  CamAspect := FTexX/FTexY;
  if (ScreenAspect >= 1) then
  begin
    ScaledVideoWidth  := RenderW;
    ScaledVideoHeight := RenderH * ScreenAspect/CamAspect;
  end else
  begin
    ScaledVideoHeight := RenderH;
    ScaledVideoWidth  := RenderW * CamAspect/ScreenAspect;
  end;

  SRect.left := (RenderW - ScaledVideoWidth) / 2;
  SRect.right := SRect.left + ScaledVideoWidth;
  SRect.lower := (RenderH - ScaledVideoHeight) / 2;
  SRect.upper := SRect.lower + ScaledVideoHeight;

  if (Screen=1) then
  begin
    glClearColor(0,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  end;

  glEnable(GL_TEXTURE_2D);
  glEnable(GL_BLEND);
  
  glColor4f(1, 1, 1, 1);
  glBindTexture(GL_TEXTURE_2D, FTex);
  glbegin(gl_quads);
    // upper-left coord
    glTexCoord2f(FTexX/FdataX, 0);
    glVertex2f(SRect.left, SRect.upper);
    // lower-left coord
    glTexCoord2f(FTexX/FdataX, FTexY/FdataY);
    glVertex2f(SRect.left, SRect.lower);
    // lower-right coord
    glTexCoord2f(0, FTexY/FdataY);
    glVertex2f(SRect.Right, SRect.lower);
    // upper-right coord
    glTexCoord2f(0, 0);
    glVertex2f(SRect.Right, SRect.upper);
  glEnd;

  glDisable(GL_BLEND);
end;

procedure wClose();
begin
  WebCamReady := false;
  FGrabFrameFlag := false;
  if (WDMSample<>nil) then
  begin
    WDMSample.Terminate;
    WDMSample.WaitFor;
    WDMSample.Free;
    WDMSample := nil;
  end;

  if(frame<>nil) then
    FreeMem(frame);
  frame := nil;

  WebCamReady := false;
  glDeleteTextures(1, @FTex);
end;

end.