1 {
2 *****************************************************************************
3 *                                                                           *
4 *  This file is part of the ZCAD                                            *
5 *                                                                           *
6 *  See the file COPYING.modifiedLGPL.txt, included in this distribution,    *
7 *  for details about the copyright.                                         *
8 *                                                                           *
9 *  This program is distributed in the hope that it will be useful,          *
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
12 *                                                                           *
13 *****************************************************************************
14 }
15 {
16 @author(Andrey Zubarev <zamtmn@yandex.ru>)
17 }
18 
19 unit uzgloglstatemanager;
20 {$INCLUDE def.inc}
21 
22 interface
23 uses uzbgeomtypes,uzepalette,uzbtypesbase,uzbtypes,{$IFNDEF DELPHI}LCLType,{$ENDIF}
24      {$IFNDEF DELPHI}gl,{glu,}glext,{$ELSE}dglOpenGL,windows,{$ENDIF}
25      {$IFDEF SLINUX}glx,{$ENDIF}
26      {$IFDEF WINDOWS}windows,{$ENDIF}
27      uzgldrawergeneral,uzegeometry,sysutils;
28 type
29     GLenum={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GLenum;
30     TViewPortArray = array [0..3] of GLint;//ВРЕМЕННО
31 const ls = $AAAA;
32       ps:array [0..31] of LONGWORD=(
33                                    $33333333,$33333333,
34                                    $CCCCCCCC,$CCCCCCCC,
35                                    $33333333,$33333333,
36                                    $CCCCCCCC,$CCCCCCCC,
37                                    $33333333,$33333333,
38                                    $CCCCCCCC,$CCCCCCCC,
39                                    $33333333,$33333333,
40                                    $CCCCCCCC,$CCCCCCCC,
41                                    $33333333,$33333333,
42                                    $CCCCCCCC,$CCCCCCCC,
43                                    $33333333,$33333333,
44                                    $CCCCCCCC,$CCCCCCCC,
45                                    $33333333,$33333333,
46                                    $CCCCCCCC,$CCCCCCCC,
47                                    $33333333,$33333333,
48                                    $CCCCCCCC,$CCCCCCCC
49                                   );
50       GL_lines={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_lines;
51       GL_LINE_STRIP={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_LINE_STRIP;
52       GL_line_loop={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_line_loop;
53       GL_POINT_SMOOTH={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_POINT_SMOOTH;
54       GL_LINE_SMOOTH={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_LINE_SMOOTH;
55       GL_points={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_points;
56       GL_QUADS={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_QUADS;
57       GL_ALWAYS={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_ALWAYS;
58       GL_LINE_STIPPLE={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_LINE_STIPPLE;
59       GL_POLYGON_STIPPLE={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_POLYGON_STIPPLE;
60       GL_TRIANGLES={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_TRIANGLES;
61       GL_TRIANGLE_FAN={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_TRIANGLE_FAN;
62       GL_TRIANGLE_STRIP={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_TRIANGLE_STRIP;
63 
64       GL_PROJECTION={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_PROJECTION;
65       GL_MODELVIEW={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_MODELVIEW;
66       GL_VIEWPORT={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_VIEWPORT;
67       GL_LIGHTING={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_LIGHTING;
68       GL_LIGHT0={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_LIGHT0;
69       GL_COLOR_MATERIAL={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_COLOR_MATERIAL;
70       GL_POSITION={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_POSITION;
71       GL_FRONT_AND_BACK={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_FRONT_AND_BACK;
72       GL_SHININESS={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_SHININESS;
73       GL_SPECULAR={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_SPECULAR;
74       GL_LIGHT_MODEL_TWO_SIDE={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_LIGHT_MODEL_TWO_SIDE;
75       GL_AMBIENT_AND_DIFFUSE={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_AMBIENT_AND_DIFFUSE;
76 
77       GL_COLOR_LOGIC_OP={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_COLOR_LOGIC_OP;
78       GL_OR={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_OR;
79       GL_XOR={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_XOR;
80 
81       GL_TEXTURE_2D={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_TEXTURE_2D;
82       GL_DEPTH_TEST={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_DEPTH_TEST;
83       GL_STENCIL_TEST={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_STENCIL_TEST;
84       GL_BLEND={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_BLEND;
85       GL_SRC_ALPHA={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_SRC_ALPHA;
86       GL_ONE_MINUS_SRC_ALPHA={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_ONE_MINUS_SRC_ALPHA;
87       GL_LINE_SMOOTH_HINT={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_LINE_SMOOTH_HINT;
88       GL_NICEST={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_NICEST;
89       GL_KEEP={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_KEEP;
90       GL_COLOR_BUFFER_BIT={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_COLOR_BUFFER_BIT;
91       GL_DEPTH_BUFFER_BIT={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_DEPTH_BUFFER_BIT;
92       GL_STENCIL_BUFFER_BIT={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_STENCIL_BUFFER_BIT;
93       GL_LOAD={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_LOAD;
94       GL_return={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_return;
95       GL_AUX0={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_AUX0;
96       GL_BACK={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_BACK;
97       GL_COLOR={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_COLOR;
98       GL_BGRA_EXT={$IFNDEF DELPHI}glext.{$ELSE}dglOpenGL.{$ENDIF}GL_BGRA_EXT;
99       GL_NEVER={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_NEVER;
100       GL_REPLACE={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_REPLACE;
101       GL_UNSIGNED_BYTE={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_UNSIGNED_BYTE;
102       GL_EQUAL={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_EQUAL;
103       GL_TEXTURE_MIN_FILTER={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_TEXTURE_MIN_FILTER;
104       GL_RGB={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_RGB;
105       GL_NEAREST={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_NEAREST;
106       GL_TEXTURE_MAG_FILTER={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_TEXTURE_MAG_FILTER;
107       GL_VENDOR={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_VENDOR;
108       GL_RENDERER={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_RENDERER;
109       GL_VERSION={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_VERSION;
110       GL_LINE_WIDTH_RANGE={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_LINE_WIDTH_RANGE;
111       GL_point_size_RANGE={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_point_size_RANGE;
112       GL_PROJECTION_MATRIX={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_PROJECTION_MATRIX;
113       GL_EXTENSIONS={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_EXTENSIONS;
114       GL_MAP1_VERTEX_4={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_MAP1_VERTEX_4;
115       GL_MAP1_VERTEX_3={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_MAP1_VERTEX_3;
116       GL_TRUE={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_TRUE;
117       GL_FALSE={$IFNDEF DELPHI}gl.{$ELSE}dglOpenGL.{$ENDIF}GL_FALSE;
118 
119       maxmybufer=1024;
120 type
121     tmyscrbuf = array [0..maxmybufer] of GLuint;
122     {$IFNDEF DELPHI}
123     {if FPC_FULlVERSION>20600}
124     {glu}//TViewPortArray = array [0..3] of GLint;
125     {ENDIF}
126     {$ENDIF}
127     {$IFDEF DELPHI}
128     //TViewPortArray = {array [0..3] of GLint}TVector4i;
129     {$ENDIF}
130 
131     TessObj=Pointer;
132     GLUnurbsObj=Pointer;
133 
134     TOGLContextDesk=record
135                           hrc: {HGLRC}thandle;
136                           dc:HDC;
137                     end;
138     PTOGLStateManager=^TOGLStateManager;
139     TOGLStateManager=object
140                            currentpointsize,currentlinewidth:GLfloat;
141                            currentmode,lastmode:GLenum;
142 
143                            _myglStencilfunc: GLenum;
144                            _myglStencilref: GLint;
145                            _myglStencilmask: GLuint;
146 
147                            _myglStencilfail,
148                            _myglStencilzfail,
149                            _myglStencilzpass: GLenum;
150 
151                            _myglLogicOpCode:GLenum;
152 
153                            _glMatrixMode:GLenum;
154 
155                            _LineStipplefactor: GLint;
156                            _LineStipplepattern: GLushort;
157                            _ppolygonpattern:pointer;
158 
159                            _colour:uzepalette.TRGB;
160 
161                            procedure myglbegin(mode:GLenum);inline;
162                            procedure myglend;inline;
163                            procedure mytotalglend;inline;
164                            procedure myglEnable(const cap: GLenum);inline;
165                            procedure myglDisable(const cap: GLenum);inline;
166                            procedure myglPointSize(const size: GLfloat);virtual;//inline;
167                            procedure myglLineWidth(const width: GLfloat);virtual;//inline;
168                            procedure myglStencilFunc(const func: GLenum;const  ref: GLint;const  mask: GLuint);inline;
169                            procedure myglStencilOp(const fail, zfail, zpass: GLenum);inline;
170                            procedure myglLogicOp(const opcode: GLenum);inline;
171                            procedure myglPushMatrix;virtual;//inline;
172                            procedure myglPopMatrix;virtual;//inline;
173                            procedure myglMultMatrixD(const matrix:DMatrix4D);virtual;//inline;
174                            procedure myglMultMatrixF(const matrix:DMatrix4F);virtual;//inline;
175                            procedure myglMatrixMode(const mode: GLenum);inline;
176                            procedure myglLineStipple(const factor: GLint; const pattern: GLushort);inline;
177                            procedure myglPolygonStipple(const ppattern:pointer);inline;
178                            constructor init;
179 
180                            procedure glcolor3ub(const red, green, blue: GLubyte);virtual;//inline;
181                            procedure glcolor4ub(const red, green, blue,alpha: GLubyte);virtual;//inline;
182                            procedure glColor3ubv(const v: TRGB);virtual;//inline;
183 
184                            procedure myglNormal3dV(const V:PGDBVertex);inline;
185                            //procedure myglColor3ub(const red, green, blue: GLubyte);inline;
186                            procedure myglVertex3d(const V:GDBVertex);virtual;//inline;
187                            procedure myglVertex2d(const x,y:GDBDouble);virtual;//inline;
188                            procedure myglVertex2f(const x,y:GLFloat);virtual;//inline;
189                            procedure myglvertex2dv(const V:GDBPointer);virtual;//inline;
190                            procedure myglvertex2iv(const V:GDBPointer);virtual;//inline;
191                            procedure myglVertex2i(x, y: GLint);virtual;//inline;
192                            procedure myglVertex(const x,y,z:GDBDouble);virtual;//inline;
193                            procedure myglVertex3dV(const V:PGDBVertex);virtual;//inline;
194                            procedure myglVertex3fV(const V:PGDBVertex3S);virtual;//inline;
195                            procedure startrender;virtual;//inline;
196                            procedure endrender;virtual;//inline;
197                            {$IFDEF SINGLEPRECISIONGEOMETRY}
198                            procedure glVertex3dv(const v: PGDBVertex);inline;
199                            {$ENDIF}
200                            procedure myglViewport(const x,y,width,height:GDBInteger);inline;
201                            procedure myglGetIntegerv(pname: GLenum; params: PGLint);inline;
202                            procedure myglLoadMatrixd(const m: PGLdouble);inline;
203                            procedure myglLightfv(light, pname: GLenum; const params: PGLfloat);inline;
204                            procedure myglMaterialf(face, pname: GLenum; param: GLfloat);inline;
205                            procedure myglMaterialfv(face, pname: GLenum; const params: PGLfloat);inline;
206                            procedure myglColorMaterial(face, mode: GLenum);inline;
207                            procedure myglLightModeli(pname: GLenum; param: GLint);inline;
208                            procedure myglLoadIdentity;inline;
209                            procedure myglOrtho(left, right, bottom, top, zNear, zFar: GLdouble);inline;
210                            procedure myglScalef(x, y, z: GLfloat);inline;
211                            procedure myglTranslated(x, y, z: GLdouble);inline;
212                            procedure myglBindTexture(target: GLenum; texture: GLuint);inline;
213                            procedure myglCopyTexSubImage2D(target: GLenum; level, xoffset, yoffset, x, y: GLint; width, height: GLsizei);inline;
214                            procedure myglTexCoord2d(s, t: GLdouble);inline;
215                            procedure myglClearColor(red, green, blue, alpha: GLclampf);inline;
216                            procedure myglBlendFunc(sfactor, dfactor: GLenum);inline;
217                            procedure myglHint(target, mode: GLenum);inline;
218                            procedure myglClear(mask: GLbitfield);inline;
219                            procedure myglAccum(op: GLenum; value: GLfloat);inline;
220                            procedure myglDrawBuffer(mode: GLenum);inline;
221                            procedure myglReadBuffer(mode: GLenum);inline;
222                            procedure myglCopyPixels(x, y: GLint; width, height: GLsizei; atype: GLenum);inline;
223                            procedure myglReadPixels(x, y: GLint; width, height: GLsizei; format, atype: GLenum; pixels: Pointer);inline;
224                            procedure myglRasterPos2i(x, y: GLint);inline;
225                            procedure myglDrawPixels(width, height: GLsizei; format, atype: GLenum; const pixels: Pointer);inline;
226                            procedure myglDeleteTextures(n: GLsizei; const textures: PGLuint);inline;
227                            procedure myglGenTextures(n: GLsizei; textures: PGLuint);inline;
228                            procedure myglTexImage2D(target: GLenum; level: GLInt; internalformat: GLEnum; width, height: GLsizei; border: GLint; format, atype: GLenum; const pixels: Pointer);inline;
229                            procedure myglTexParameteri(target: GLenum; pname: GLenum; param: GLint);inline;
230                            procedure myglGetDoublev(pname: GLenum; params: PGLdouble);//inline;
myglGetStringnull231                            function myglGetString(name: GLenum): PAnsiChar;inline;
232     end;
233 
234 var
235    GLRasterizer:TOGLStateManager;
236    OGLSM:PTOGLStateManager;
ntervalnull237    wglSwapIntervalEXT: function(interval: GLint): BOOL;{$IFDEF Windows}stdcall{$ELSE}cdecl{$ENDIF};
238 const
239      MY_EmptyMode=1000000;
240 procedure SetDCPixelFormat(oglc:TOGLContextDesk);
isOpenGLErrornull241 function isOpenGLError:GLenum;
242 //(const v: PGLdouble); stdcall;
243 //procedure myglVertex3dV(V:PGDBVertex);
244 procedure MyglMakeCurrent(oglc:TOGLContextDesk);
245 procedure MySwapBuffers(oglc:TOGLContextDesk);
246 procedure MywglDeleteContext(oglc:TOGLContextDesk);
247 procedure MywglCreateContext(var oglc:TOGLContextDesk);
248 
249 {$IFDEF WINDOWS}
wglGetProcAddressnull250 function wglGetProcAddress(ProcName:LPCSTR):PROC;stdcall; external 'opengl32' name 'wglGetProcAddress';
251 {$ENDIF}
252 
253 Procedure DrawAABB(const BoundingBox:TBoundingBox);
254 var
255    bcount:integer;
256    primcount,pointcount,bathcount:GDBInteger;
257    middlepoint:GDBVertex;
258 implementation
259 procedure MywglCreateContext(var oglc:TOGLContextDesk);
260 begin
261      //oglc.hrc := wglCreateContext(oglc.DC);
262 end;
263 
264 procedure MywglDeleteContext(oglc:TOGLContextDesk);
265 begin
266      //wglDeleteContext(oglc.hrc);
267 end;
268 
269 procedure MySwapBuffers(oglc:TOGLContextDesk);
270 begin
271      //SwapBuffers(oglc.DC)
272 end;
273 
274 procedure MyglMakeCurrent(oglc:TOGLContextDesk);
275 begin
276     //wglMakeCurrent(oglc.DC, oglc.hrc);
277 end;
278 procedure processpoint(const point:gdbvertex3s);
279 begin
280      //inc(pointcount);
281      //middlepoint:=uzegeometry.VertexAdd(middlepoint,point);
282 end;
283 procedure TOGLStateManager.glcolor3ub(const red, green, blue: GLubyte);
284 begin
285      if (red<>_colour.r)
286      or (green<>_colour.g)
287      or (blue<>_colour.b)then
288                               begin
289                                    _colour.r:=red;
290                                    _colour.g:=green;
291                                    _colour.b:=blue;
292                                    {$IFNDEF DELPHI}gl{$ELSE}dglOpenGL{$ENDIF}.glColor3ubv(@_colour);
293                               end;
294 end;
295 
296 procedure TOGLStateManager.glcolor4ub(const red, green, blue, alpha: GLubyte);
297 begin
298      if (red<>_colour.r)
299      or (green<>_colour.g)
300      or (blue<>_colour.b)
301      or (alpha<>_colour.a)then
302                               begin
303                                    _colour.r:=red;
304                                    _colour.g:=green;
305                                    _colour.b:=blue;
306                                    _colour.a:=alpha;
307                                    {$IFNDEF DELPHI}gl{$ELSE}dglOpenGL{$ENDIF}.glColor4ub(red, green, blue, alpha);
308                               end;
309 end;
310 
311 
312 procedure TOGLStateManager.glColor3ubv(const v: TRGB);
313 begin
314      if (v.r<>_colour.r)
315      or (v.g<>_colour.g)
316      or (v.b<>_colour.b)then
317                               begin
318                                    {$IFNDEF DELPHI}gl{$ELSE}dglOpenGL{$ENDIF}.glColor3ubv(@v);
319                                    _colour:=v;
320                               end;
321 end;
322 
323 procedure TOGLStateManager.startrender;
324 begin
325      middlepoint:=nulvertex;
326      pointcount:=0;
327      primcount:=0;
328      bathcount:=0;
329 end;
330 procedure TOGLStateManager.endrender;
331 begin
332     //sysvar.debug.renderdeb.middlepoint:=middlepoint;
333     {sysvar.debug.renderdeb.pointcount:=pointcount;
334     sysvar.debug.renderdeb.primcount:=primcount;
335     sysvar.debug.renderdeb.bathcount:=bathcount;
336     if pointcount<>0 then
337                           sysvar.debug.renderdeb.middlepoint:=uzegeometry.VertexMulOnSc(middlepoint,1/pointcount);
338     }
339 end;
340 {$IFDEF SINGLEPRECISIONGEOMETRY}
341 procedure TOGLStateManager.glVertex3dv(const v: PGDBVertex);
342 var
343    t:GDBvertex3S;
344 begin
345      t.x:=v.x;
346      t.y:=v.y;
347      t.z:=v.z;
348      glVertex3fv(@t);
349 end;
350 {$ENDIF}
351 procedure TOGLStateManager.myglvertex2iv(const V:GDBPointer);
352 var t:gdbvertex;
353 begin
354      {$IFDEF DEBUGCOUNTGEOMETRY}
355      //processpoint(v^);
356      inc(pointcount);
357      {$ENDIF}
358      if notuseLCS then
359                       glVertex2iV(pointer(v))
360                   else
361                       begin
362                            t:=vertexadd(createvertex(pGDBvertex2DI(v)^.x,pGDBvertex2DI(v)^.y,0),CurrentCamCSOffset);
363                            glVertex3dV(@t);
364                       end;
365 end;
366 procedure TOGLStateManager.myglVertex2i(x, y: GLint);
367 var t:gdbvertex;
368 begin
369      {$IFDEF DEBUGCOUNTGEOMETRY}
370      //processpoint(v^);
371      inc(pointcount);
372      {$ENDIF}
373      if notuseLCS then
374                       glVertex2i(x,y)
375                   else
376                       begin
377                            t:=vertexadd(createvertex(x,y,0),CurrentCamCSOffset);
378                            glVertex3dV(@t);
379                       end;
380 end;
381 procedure TOGLStateManager.myglvertex2dv(const V:GDBPointer);
382 var t:gdbvertex;
383 begin
384      {$IFDEF DEBUGCOUNTGEOMETRY}
385      //processpoint(v^);
386      inc(pointcount);
387      {$ENDIF}
388      if notuseLCS then
389                       glVertex2dV(pointer(v))
390                   else
391                       begin
392                            t:=vertexadd(createvertex(pgdbvertex2d(v)^.x,pgdbvertex2d(v)^.y,0),CurrentCamCSOffset);
393                            glVertex3dV(@t);
394                       end;
395 end;
396 
397 procedure TOGLStateManager.myglVertex3dV;
398 var t:gdbvertex;
399 begin
400      {$IFDEF DEBUGCOUNTGEOMETRY}
401      //processpoint(v^);
402      inc(pointcount);
403      {$ENDIF}
404      if notuseLCS then
405                       glVertex3dV(pointer(v))
406                   else
407                       begin
408                            t:=vertexadd(v^,CurrentCamCSOffset);
409                            glVertex3dV(@t);
410                       end;
411 end;
412 procedure TOGLStateManager.myglVertex3fV;
413 var t:gdbvertex3s;
414 begin
415      {$IFDEF DEBUGCOUNTGEOMETRY}
416      processpoint(v^);
417      inc(pointcount);
418      {$ENDIF}
419      if notuseLCS then
420                       glVertex3fV(pointer(v))
421                   else
422                       begin
423                            t:=vertexadd(v^,CurrentCamCSOffsetS);
424                            glVertex3fV(@t);
425                       end;
426 end;
427 procedure TOGLStateManager.myglNormal3dV(const V:PGDBVertex);{$IFNDEF DELPHI}inline;{$ENDIF}
428 begin
429      glNormal3dV(pointer(v))
430 end;
431 procedure TOGLStateManager.myglVertex2f(const x,y:GLFloat);
432 var t:gdbvertex;
433 begin
434      {$IFDEF DEBUGCOUNTGEOMETRY}
435      inc(pointcount);
436      {$ENDIF}
437      if notuseLCS then
438                       glVertex2f(x,y)
439                   else
440                       begin
441                            t:=vertexadd(createvertex(x,y,0),CurrentCamCSOffset);
442                            glVertex3dv(@t);
443                       end;
444 end;
445 
446 procedure TOGLStateManager.myglVertex2d(const x,y:GDBDouble);
447 var t:gdbvertex;
448 begin
449      {$IFDEF DEBUGCOUNTGEOMETRY}
450      inc(pointcount);
451      {$ENDIF}
452      if notuseLCS then
453                       glVertex2d(x,y)
454                   else
455                       begin
456                            t:=vertexadd(createvertex(x,y,0),CurrentCamCSOffset);
457                            glVertex3dv(@t);
458                       end;
459 end;
460 
461 procedure TOGLStateManager.myglVertex3d;
462 var t:gdbvertex;
463 begin
464      {$IFDEF DEBUGCOUNTGEOMETRY}
465      //processpoint(v);
466      inc(pointcount);
467      {$ENDIF}
468      if notuseLCS then
469                       glVertex3dV(@v)
470                   else
471                       begin
472                            t:=vertexadd(v,CurrentCamCSOffset);
473                            glVertex3dv(@t);
474                       end;
475 end;
476 procedure TOGLStateManager.myglVertex;
477 var t,t1:gdbvertex;
478 begin
479      t1:=createvertex(x,y,z);
480      {$IFDEF DEBUGCOUNTGEOMETRY}
481      //processpoint(t1);
482      inc(pointcount);
483      {$ENDIF}
484      if notuseLCS then
485                       glVertex3dV(@t1)
486                   else
487                       begin
488                            t:=vertexadd(t1,CurrentCamCSOffset);
489                            glVertex3dv(@t);
490                       end;
491 end;
isOpenGLErrornull492 function isOpenGLError:GLenum;
493 //var
494    //s:string;
495 begin
496      result:=glgeterror;
497      if result<>GL_NO_ERROR then
498                       begin
499                            //s:='OPENGL ERROR! '+inttostr(result);
500                            //MessageBox(0,@s[1],0,MB_OK);
501                            {asm
502                               int(3);
503                            end;}
504                       end;
505 end;
506 procedure TOGLStateManager.myglbegin(mode:GLenum);
507 begin
508 //(*
509     //if (mode<>GL_LINES)and(mode<>GL_QUADS) then
510     //                          mode:=mode;
511      if ((mode<>currentmode)or(currentmode=GL_LINE_STRIP)or(currentmode=GL_TRIANGLE_STRIP)or(currentmode=GL_TRIANGLE_FAN)) then
512      begin
513      if currentmode<>MY_EmptyMode then
514                                      begin
515                                           glend;
516                                           //isOpenGLError;
517                                           {$IFDEF DEBUGCOUNTGEOMETRY}inc(bathcount);{$ENDIF}
518                                      end;
519      glbegin(mode);
520      //{$IFDEF DEBUGCOUNTGEOMETRY}inc(bathcount);{$ENDIF}
521      end;
522 
523 
524      {IFDEF DEBUGCOUNTGEOMETRY}inc(primcount);{ENDIF}
525 
526      inc(bcount);
527      currentmode:=mode;
528      {if bcount>1 then
529                      asm
530                               int(3);
531                      end;}
532      pointcount:=0;
533 //*)
534 //    glbegin(mode)
535 end;
536 procedure TOGLStateManager.myglend;
537 begin
538 (*
539      if bcount<1 then
540                      asm
541                               {int(3);}
542                      end;
543      dec(bcount);
544 *)
545 //     glend;
546      pointcount:=0;
547 
548 end;
549 procedure TOGLStateManager.mytotalglend;
550 begin
551      (*if bcount<1 then
552                      asm
553                               {int(3);}
554                      end;*)
555      //dec(bcount);
556 
557      if currentmode<>MY_EmptyMode then
558                                      begin
559                                           glend;
560                                           {$IFDEF DEBUGCOUNTGEOMETRY}inc(bathcount);{$ENDIF}
561                                           currentmode:=MY_EmptyMode;
562                                      end;
563 end;
564 procedure TOGLStateManager.myglEnable(const cap: GLenum);
565 begin
566      mytotalglend;
567      glEnable(cap);
568 end;
569 procedure TOGLStateManager.myglDisable(const cap: GLenum);
570 begin
571      mytotalglend;
572      glDisable(cap);
573      case cap of
574                 GL_LINE_STIPPLE:begin
575                                      _LineStipplefactor:=-1;
576                                      _LineStipplepattern:=0;
577                                 end;
578      end;
579 end;
580 procedure TOGLStateManager.myglPointSize(const size: GLfloat);
581 begin
582      if currentpointsize<>size then
583                      begin
584                           mytotalglend;
585                           glPointSize(size);
586                           currentpointsize:=size;
587                      end;
588 end;
589 procedure TOGLStateManager.myglLineWidth(const width: GLfloat);
590 begin
591      if currentlinewidth<>width then
592                      begin
593                           mytotalglend;
594                           gllinewidth(width);
595                           currentlinewidth:=width;
596                      end;
597 end;
598 procedure TOGLStateManager.myglStencilFunc(const func: GLenum;const  ref: GLint;const  mask: GLuint);
599 begin
600      if
601      (_myglStencilfunc<>func)or
602      (_myglStencilref<>ref)or
603      (_myglStencilmask<>mask)
604      then
605          begin
606               mytotalglend;
607               glStencilFunc(func,ref,mask);
608               _myglStencilfunc:=func;
609               _myglStencilref:=ref;
610               _myglStencilmask:=mask;
611          end;
612 
613 end;
614 procedure TOGLStateManager.myglStencilOp(const fail, zfail, zpass: GLenum);
615 begin
616      if
617      (_myglStencilfail<>fail)or
618      (_myglStencilzfail<>zfail)or
619      (_myglStencilzpass<>zpass)
620      then
621          begin
622               mytotalglend;
623               glStencilOp(fail, zfail, zpass);
624               _myglStencilfail:=fail;
625               _myglStencilzfail:=zfail;
626               _myglStencilzpass:=zpass;
627          end;
628 end;
629 procedure TOGLStateManager.myglLogicOp(const opcode: GLenum);
630 begin
631      if _myglLogicOpCode<>opcode then
632      begin
633      mytotalglend;
634      glLogicOp(opcode);
635      _myglLogicOpCode:=opcode;
636      end;
637 end;
638 procedure TOGLStateManager.myglLineStipple(const factor: GLint; const pattern: GLushort);
639 begin
640      if
641      (_LineStipplefactor<>factor)or
642      (_LineStipplepattern<>pattern)
643      then
644          begin
645               mytotalglend;
646               glLineStipple(factor,pattern);
647               _LineStipplefactor:=factor;
648               _LineStipplepattern:=pattern;
649          end;
650 end;
651 procedure TOGLStateManager.myglPolygonStipple(const ppattern:pointer);
652 begin
653      if
654      (_ppolygonpattern<>ppattern)
655      then
656          begin
657               mytotalglend;
658               glPolygonStipple(ppattern);
659               _ppolygonpattern:=ppattern;
660          end;
661 end;
662 procedure TOGLStateManager.myglPushMatrix;
663 begin
664      mytotalglend;
665      glPushMatrix
666 end;
667 
668 procedure TOGLStateManager.myglPopMatrix;
669 begin
670      mytotalglend;
671      glPopMatrix;
672 end;
673 procedure TOGLStateManager.myglMultMatrixD(const matrix:DMatrix4D);
674 begin
675      glmultmatrixd(@matrix);
676 end;
677 procedure TOGLStateManager.myglMultMatrixF(const matrix:DMatrix4F);
678 begin
679      glmultmatrixf(@matrix);
680 end;
681 procedure TOGLStateManager.myglViewport(const x,y,width,height:GDBInteger);
682 begin
683      glViewport(x,y,width,height);
684 end;
685 procedure TOGLStateManager.myglGetIntegerv(pname: GLenum; params: PGLint);
686 begin
687      glGetIntegerv(pname,params);
688 end;
689 procedure TOGLStateManager.myglLoadMatrixd(const m: PGLdouble);
690 begin
691      glLoadMatrixd(m);
692 end;
693 procedure TOGLStateManager.myglLightfv(light, pname: GLenum; const params: PGLfloat);
694 begin
695      glLightfv(light,pname,params);
696 end;
697 procedure TOGLStateManager.myglMaterialf(face, pname: GLenum; param: GLfloat);
698 begin
699      glMaterialf(face,pname,param);
700 end;
701 procedure TOGLStateManager.myglMaterialfv(face, pname: GLenum; const params: PGLfloat);
702 begin
703      glMaterialfv(face,pname,params);
704 end;
705 procedure TOGLStateManager.myglColorMaterial(face, mode: GLenum);
706 begin
707      glColorMaterial(face,mode);
708 end;
709 procedure TOGLStateManager.myglLightModeli(pname: GLenum; param: GLint);
710 begin
711      glLightModeli(pname,param);
712 end;
713 procedure TOGLStateManager.myglLoadIdentity;
714 begin
715      glLoadIdentity;
716 end;
717 procedure TOGLStateManager.myglOrtho(left, right, bottom, top, zNear, zFar: GLdouble);
718 begin
719      glOrtho(left,right,bottom,top,zNear,zFar);
720 end;
721 procedure TOGLStateManager.myglScalef(x, y, z: GLfloat);
722 begin
723      glScalef(x,y,z);
724 end;
725 procedure TOGLStateManager.myglTranslated(x, y, z: GLdouble);
726 begin
727      glTranslated(x,y,z);
728 end;
729 procedure TOGLStateManager.myglBindTexture(target: GLenum; texture: GLuint);
730 begin
731      glBindTexture(target,texture);
732 end;
733 procedure TOGLStateManager.myglCopyTexSubImage2D(target: GLenum; level, xoffset, yoffset, x, y: GLint; width, height: GLsizei);
734 begin
735      glCopyTexSubImage2D(target,level, xoffset, yoffset, x, y,width, height);
736 end;
737 procedure TOGLStateManager.myglTexCoord2d(s, t: GLdouble);
738 begin
739      glTexCoord2d(s, t);
740 end;
741 procedure TOGLStateManager.myglClearColor(red, green, blue, alpha: GLclampf);
742 begin
743      glClearColor(red, green, blue, alpha);
744 end;
745 procedure TOGLStateManager.myglBlendFunc(sfactor, dfactor: GLenum);
746 begin
747      glBlendFunc(sfactor, dfactor);
748 end;
749 procedure TOGLStateManager.myglHint(target, mode: GLenum);
750 begin
751      glHint(target, mode);
752 end;
753 procedure TOGLStateManager.myglClear(mask: GLbitfield);
754 begin
755      glClear(mask);
756 end;
757 procedure TOGLStateManager.myglAccum(op: GLenum; value: GLfloat);
758 begin
759      glAccum(op,value);
760 end;
761 procedure TOGLStateManager.myglDrawBuffer(mode: GLenum);
762 begin
763      glDrawBuffer(mode);
764 end;
765 procedure TOGLStateManager.myglReadBuffer(mode: GLenum);
766 begin
767      glReadBuffer(mode);
768 end;
769 procedure TOGLStateManager.myglCopyPixels(x, y: GLint; width, height: GLsizei; atype: GLenum);
770 begin
771      glCopyPixels(x, y,width, height,atype);
772 end;
773 procedure TOGLStateManager.myglReadPixels(x, y: GLint; width, height: GLsizei; format, atype: GLenum; pixels: Pointer);
774 begin
775      glReadPixels(x, y, width, height, format, atype, pixels);
776 end;
777 procedure TOGLStateManager.myglRasterPos2i(x, y: GLint);
778 begin
779      glRasterPos2i(x, y);
780 end;
781 procedure TOGLStateManager.myglDrawPixels(width, height: GLsizei; format, atype: GLenum; const pixels: Pointer);
782 begin
783      glDrawPixels(width, height, format, atype, pixels);
784 end;
785 procedure TOGLStateManager.myglDeleteTextures(n: GLsizei; const textures: PGLuint);
786 begin
787      glDeleteTextures(n, textures);
788 end;
789 procedure TOGLStateManager.myglGenTextures(n: GLsizei; textures: PGLuint);
790 begin
791      glGenTextures(n, textures);
792 end;
793 procedure TOGLStateManager.myglTexImage2D(target: GLenum; level: GLInt; internalformat: GLEnum; width, height: GLsizei; border: GLint; format, atype: GLenum; const pixels: Pointer);
794 begin
795      glTexImage2D(target, level, internalformat, width, height, border, format, atype, pixels);
796 end;
797 procedure TOGLStateManager.myglTexParameteri(target: GLenum; pname: GLenum; param: GLint);
798 begin
799      glTexParameteri(target, pname, param);
800 end;
801 procedure TOGLStateManager.myglGetDoublev(pname: GLenum; params: PGLdouble);
802 begin
803      glGetDoublev(pname, params);
804 end;
myglGetStringnull805 function TOGLStateManager.myglGetString(name: GLenum): PAnsiChar;
806 begin
807      result:=glGetString(name);
808 end;
809 procedure TOGLStateManager.myglMatrixMode(const mode: GLenum);
810 begin
811      if _glMatrixMode<>mode then
812      begin
813      mytotalglend;
814      glMatrixMode(mode);
815      _glMatrixMode:=mode;
816      end;
817 end;
818 
819 constructor TOGLStateManager.init;
820 begin
821      currentmode:=MY_EmptyMode;
822      currentpointsize:=-1;
823      currentlinewidth:=-1;
824 
825      _myglStencilfunc:=maxint;
826      _myglStencilref:=-1;
827      _myglStencilmask:={-1}0;
828 
829      _myglStencilfail:=maxint;
830      _myglStencilzfail:=maxint;
831      _myglStencilzpass:=maxint;
832 
833      _myglLogicOpCode:=maxint;
834      _glMatrixMode:=maxint;
835 
836      _LineStipplefactor:=maxint;
837      _LineStipplepattern:=maxword;
838      _colour.r:=255;
839      _colour.g:=255;
840      _colour.b:=255;
841      _ppolygonpattern:=nil;
842 
843 end;
844 
845 procedure SetDCPixelFormat(oglc:TOGLContextDesk);
846 {var
847   nPixelFormat: GDBInteger;
848   pfd: TPixelFormatDescriptor;
849   rez: BOOL;}
850 begin
851 {  programlog.logoutstr('OGLSpecFunc.SetDCPixelformat',lp_IncPos);
852   FillChar(pfd, SizeOf(pfd), 0);
853   with pfd do
854   begin
855     nSize := sizeof(pfd);
856     nVersion := 1;
857     dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
858     iPixelType := PFD_TYPE_RGBA;
859     cColorBits := 32;
860     cDepthBits := 32;
861     cAccumBits := 32;
862     cAuxBuffers:= 8;
863     cStencilBits:= 4;
864     iLayerType := PFD_MAIN_PLANE;
865   end;
866   nPixelFormat := ChoosePixelFormat(oglc.DC, @pfd);
867   DescribePixelFormat(oglc.dc,nPixelFormat,sizeof(PIXELFORMATDESCRIPTOR),pfd);
868   programlog.logoutstr('PixelFormat='+inttostr(nPixelFormat),0);
869   programlog.logoutstr('cColorBits='+inttostr(pfd.cColorBits),0);
870   programlog.logoutstr('cAccumBits='+inttostr(pfd.cAccumBits),0);
871   programlog.logoutstr('cDepthBits='+inttostr(pfd.cDepthBits),0);
872   programlog.logoutstr('cStencilBits='+inttostr(pfd.cStencilBits),0);
873   programlog.logoutstr('cAuxBuffers='+inttostr(pfd.cAuxBuffers),0);
874   programlog.logoutstr('cStencilBits='+inttostr(pfd.cStencilBits),0);
875   rez:=SetPixelFormat(oglc.dc, nPixelFormat, @pfd);
876   if rez then programlog.logoutstr('end;',lp_DecPos)
877          else
878              begin
879                   programlog.logoutstr(pansichar('SetPixelFormat error - '+inttostr(getlasterror)),lp_DecPos);
880              end;}
881 end;
882 Procedure DrawAABB(const BoundingBox:TBoundingBox);
883 begin
884 oglsm.myglbegin(GL_LINES);
885    oglsm.myglVertex(BoundingBox.LBN.x,BoundingBox.LBN.y,BoundingBox.LBN.Z);
886    oglsm.myglVertex(BoundingBox.RTF.x,BoundingBox.LBN.y,BoundingBox.LBN.Z);
887    oglsm.myglVertex(BoundingBox.RTF.x,BoundingBox.LBN.y,BoundingBox.LBN.Z);
888    oglsm.myglVertex(BoundingBox.RTF.x,BoundingBox.RTF.y,BoundingBox.LBN.Z);
889    oglsm.myglVertex(BoundingBox.RTF.x,BoundingBox.RTF.y,BoundingBox.LBN.Z);
890    oglsm.myglVertex(BoundingBox.LBN.x,BoundingBox.RTF.y,BoundingBox.LBN.Z);
891    oglsm.myglVertex(BoundingBox.LBN.x,BoundingBox.RTF.y,BoundingBox.LBN.Z);
892    oglsm.myglVertex(BoundingBox.LBN.x,BoundingBox.LBN.y,BoundingBox.LBN.Z);
893 oglsm.myglend();
894 oglsm.myglbegin(GL_LINES);
895    oglsm.myglVertex(BoundingBox.LBN.x,BoundingBox.LBN.y,BoundingBox.RTF.Z);
896    oglsm.myglVertex(BoundingBox.RTF.x,BoundingBox.LBN.y,BoundingBox.RTF.Z);
897    oglsm.myglVertex(BoundingBox.RTF.x,BoundingBox.LBN.y,BoundingBox.RTF.Z);
898    oglsm.myglVertex(BoundingBox.RTF.x,BoundingBox.RTF.y,BoundingBox.RTF.Z);
899    oglsm.myglVertex(BoundingBox.RTF.x,BoundingBox.RTF.y,BoundingBox.RTF.Z);
900    oglsm.myglVertex(BoundingBox.LBN.x,BoundingBox.RTF.y,BoundingBox.RTF.Z);
901    oglsm.myglVertex(BoundingBox.LBN.x,BoundingBox.RTF.y,BoundingBox.RTF.Z);
902    oglsm.myglVertex(BoundingBox.LBN.x,BoundingBox.LBN.y,BoundingBox.RTF.Z);
903 oglsm.myglend();
904 oglsm.myglbegin(GL_LINES);
905    oglsm.myglVertex(BoundingBox.LBN.x,BoundingBox.LBN.y,BoundingBox.LBN.Z);
906    oglsm.myglVertex(BoundingBox.LBN.x,BoundingBox.LBN.y,BoundingBox.RTF.Z);
907    oglsm.myglVertex(BoundingBox.RTF.x,BoundingBox.LBN.y,BoundingBox.LBN.Z);
908    oglsm.myglVertex(BoundingBox.RTF.x,BoundingBox.LBN.y,BoundingBox.RTF.Z);
909    oglsm.myglVertex(BoundingBox.RTF.x,BoundingBox.RTF.y,BoundingBox.LBN.Z);
910    oglsm.myglVertex(BoundingBox.RTF.x,BoundingBox.RTF.y,BoundingBox.RTF.Z);
911    oglsm.myglVertex(BoundingBox.LBN.x,BoundingBox.RTF.y,BoundingBox.LBN.Z);
912    oglsm.myglVertex(BoundingBox.LBN.x,BoundingBox.RTF.y,BoundingBox.RTF.Z);
913 oglsm.myglend();
914 end;
915 begin
916      bcount:=0;
917      GLRasterizer.init;
918      //oglsm.init;
919      oglsm:=@GLRasterizer;
920 end.
921