| 1 |
{%MainUnit castleinternalglutils.pas} |
| 2 |
{ |
| 3 |
Copyright 2016-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Part of CastleGLUtils unit: simple helpers for OpenGL commands. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
{ Comfortable wrappers for OpenGL glGet* that return a single value. |
| 22 |
|
| 23 |
Guarantee that result is zero in case of OpenGL error. |
| 24 |
(Otherwise, OpenGL could leave them undefined and only set glGetError.) |
| 25 |
@groupBegin } |
| 26 |
function glGetFloat(pname: TGLEnum): TGLfloat; |
| 27 |
function glGetInteger(pname: TGLEnum): TGLint; |
| 28 |
function glGetBoolean(pname: TGLEnum): TGLboolean; |
| 29 |
{ @groupEnd } |
| 30 |
|
| 31 |
(*------------------------------------------------------------------------------ |
| 32 |
Comfortable wrappers around many OpenGL functions. |
| 33 |
Overloaded for our vector types. |
| 34 |
|
| 35 |
Note that functions here simply call appropriate OpenGL functions. |
| 36 |
Long time ago we tried using tricks to speed this up (eliminate |
| 37 |
function call overhead), by importing these functions from so/dll |
| 38 |
under different names, like |
| 39 |
|
| 40 |
procedure glVertexv(const V: TVector3); |
| 41 |
{$ifdef OPENGL_CDECL} cdecl; {$endif} |
| 42 |
{$ifdef OPENGL_STDCALL} stdcall; {$endif} |
| 43 |
overload; external OpenGLDLL name 'glVertex3fv'; |
| 44 |
|
| 45 |
But this is problematic: it assumes that TVector3 will be passed |
| 46 |
by reference. Which actually is not guaranteed by a compiler (FPC sometimes |
| 47 |
doesn't). In newer FPC versions, we could use "constref" for this. |
| 48 |
Or we could just declare these functions as "inline". |
| 49 |
However, speeding these functions is just not needed at all anymore |
| 50 |
(especially with new VBO renderer that passes everything through arrays). |
| 51 |
|
| 52 |
TODO: Some of these functions should be treated as internal. |
| 53 |
A subset of these internal functions are only available in fixed-function pipeline. |
| 54 |
The rest of these functions are just deprecated -- because they |
| 55 |
are only for fixed-function pipeline, in immediate mode (and all modern code |
| 56 |
should use VBO). |
| 57 |
*) |
| 58 |
|
| 59 |
{$ifndef OpenGLES} |
| 60 |
|
| 61 |
// Do not mark glColorv as "deprecated" for now. These have valid (although internal) usage. |
| 62 |
{ } |
| 63 |
procedure glColorv(const v: TVector3Byte); overload; //deprecated 'instead of this, use drawing routines that take Color from parameters or properties, like TCastleAbstractFont.Print(X,Y,Color,...) or TDrawableImage.Color'; |
| 64 |
{ } |
| 65 |
procedure glColorv(const v: TVector4Byte); overload; //deprecated 'instead of this, use drawing routines that take Color from parameters or properties, like TCastleAbstractFont.Print(X,Y,Color,...) or TDrawableImage.Color'; |
| 66 |
{ } |
| 67 |
procedure glColorv(const v: TVector3); overload; //deprecated 'instead of this, use drawing routines that take Color from parameters or properties, like TCastleAbstractFont.Print(X,Y,Color,...) or TDrawableImage.Color'; |
| 68 |
{ } |
| 69 |
procedure glColorv(const v: TVector4); overload; //deprecated 'instead of this, use drawing routines that take Color from parameters or properties, like TCastleAbstractFont.Print(X,Y,Color,...) or TDrawableImage.Color'; |
| 70 |
|
| 71 |
{ } |
| 72 |
procedure CastleGlClipPlane(plane: TGLenum; const V: TVector4Double); |
| 73 |
|
| 74 |
procedure glNormalv(const v: TVector3); overload; |
| 75 |
|
| 76 |
procedure glMaterialv(face, pname: TGLEnum; const params: TVector4); overload; |
| 77 |
|
| 78 |
procedure glVertexv(const v: TVector2); overload; |
| 79 |
procedure glVertexv(const v: TVector3); overload; |
| 80 |
procedure glVertexv(const v: TVector4); overload; |
| 81 |
|
| 82 |
procedure glTexCoordv(const v: TVector2); overload; |
| 83 |
procedure glTexCoordv(const v: TVector3); overload; |
| 84 |
procedure glTexCoordv(const v: TVector4); overload; |
| 85 |
|
| 86 |
procedure glTexGenv(coord, pname: TGLenum; const params: TVector4); overload; |
| 87 |
|
| 88 |
procedure glLightv(light, pname: TGLEnum; const params: TVector4); overload; |
| 89 |
procedure glLightv(light, pname: TGLEnum; const params: TVector3); overload; |
| 90 |
|
| 91 |
procedure glLightModelv(pname: TGLenum; const params: TVector4); overload; |
| 92 |
|
| 93 |
procedure glFogv(pname: TGLEnum; const params: TVector4); overload; |
| 94 |
|
| 95 |
// Do not mark as "deprecated" for now. These have valid (although internal) usage. |
| 96 |
{ } |
| 97 |
procedure glMultMatrix(const m: TMatrix4); overload; //deprecated 'use other methods to transform things, e.g. TCastleUserInterface position / anchors, or TCastleTransform for TCastleScene, or TTransformNode in X3D'; |
| 98 |
{ } |
| 99 |
procedure glLoadMatrix(const m: TMatrix4); overload; //deprecated 'use other methods to transform things, e.g. TCastleUserInterface position / anchors, or TCastleTransform for TCastleScene, or TTransformNode in X3D'; |
| 100 |
|
| 101 |
{ } |
| 102 |
procedure glTexEnvv(target, pname: TGLEnum; const params: TVector4); overload; |
| 103 |
|
| 104 |
{$endif} |
| 105 |
|
| 106 |
type |
| 107 |
TGLIndex = {$ifdef GLIndexesShort} UInt16 {$else} UInt32 {$endif}; |
| 108 |
TGLIndexList = {$ifdef GLIndexesShort} TUInt16List {$else} TUInt32List {$endif}; |
| 109 |
const |
| 110 |
GLIndexConst = {$ifdef GLIndexesShort} GL_UNSIGNED_SHORT {$else} GL_UNSIGNED_INT {$endif}; |
| 111 |
|
| 112 |
var |
| 113 |
{ Current color, set by glColorv and used for TCastleAbstractFont font printing |
| 114 |
(in case you use deprecated TCastleAbstractFont.Print overloads without |
| 115 |
explicit colors). |
| 116 |
|
| 117 |
@deprecated Instead of this, use drawing routines that take |
| 118 |
Color from parameters or properties, like TCastleAbstractFont.Print(X,Y,Color,...) |
| 119 |
or TDrawableImage.Color. } |
| 120 |
CurrentColor: TCastleColor |
| 121 |
deprecated 'instead of this, use drawing routines that take Color from parameters or properties, like TCastleAbstractFont.Print(X,Y,Color,...) or TDrawableImage.Color'; |
| 122 |
|
| 123 |
{ Enable or disable given OpenGL flag. } |
| 124 |
procedure GLSetEnabled(Value: TGLenum; IsEnabled: boolean); |
| 125 |
|
| 126 |
{ If Buffer <> 0 then it does glDeleteBuffers and sets Buffer to 0. } |
| 127 |
procedure glFreeBuffer(var Buffer: TGLuint); |
| 128 |
|
| 129 |
{ If Vao <> 0 then it does glDeleteVertexArrays and sets Vao to 0. } |
| 130 |
procedure glFreeVertexArray(var Vao: TGLuint); |
| 131 |
|
| 132 |
{ Global position for drawing 2D stuff. |
| 133 |
@deprecated Do this use this. |
| 134 |
Instead of this, use drawing routines that take position as parameters, |
| 135 |
like TDrawableImage.Draw(X,Y) or TCastleAbstractFont.Print(X,Y,...). } |
| 136 |
var |
| 137 |
WindowPos: TVector2Integer |
| 138 |
deprecated 'use drawing routines that take position as parameters, like TDrawableImage.Draw(X,Y) or TCastleAbstractFont.Print(X,Y,...)'; |
| 139 |
|
| 140 |
type |
| 141 |
TEnableTextureTarget = (etNone, et2D, etCubeMap, et3D); |
| 142 |
|
| 143 |
{ Enable exactly one (or none, for Target=etNone) OpenGL texture target |
| 144 |
for fixed-function pipeline. |
| 145 |
Always use this instead of manually calling @code(glDisable(GL_TEXTURE_2D)), |
| 146 |
@code(glEnable(GL_TEXTURE_2D)) and such. This makes sure to have at most |
| 147 |
one texture target enabled, and disable others. |
| 148 |
|
| 149 |
Remember that this state is different for every texture unit in OpenGL, |
| 150 |
in case you use multi-texturing. |
| 151 |
|
| 152 |
Remember that not all texture targets are guaranteed to be supported by OpenGL. |
| 153 |
Target=etNone and Target=et2D are always supported. |
| 154 |
For the rest, check appropriate GLFeatures property (before even |
| 155 |
creating a texture with such type). If you pass an unsupported target type |
| 156 |
to this procedure, it will be ignored (all targets will be disabled, |
| 157 |
like for Target=etNone). |
| 158 |
|
| 159 |
Note that this is only for fixed-function OpenGL pipeline. |
| 160 |
Shader pipeline completely ignores the enabled state of texture units. } |
| 161 |
function GLEnableTexture(const Target: TEnableTextureTarget): boolean; |
| 162 |
|
| 163 |
function BoolToGL(const Value: boolean): TGLboolean; |
| 164 |
|
| 165 |
{$endif read_interface} |
| 166 |
|
| 167 |
{$ifdef read_implementation} |
| 168 |
|
| 169 |
{ glGetXxx ---------------------------------------------------------------------} |
| 170 |
|
| 171 |
function glGetFloat(pname: TGLEnum): TGLfloat; |
| 172 |
begin |
| 173 |
FillChar(result, SizeOf(result), 0); |
| 174 |
glGetFloatv(pname, @result) |
| 175 |
end; |
| 176 |
|
| 177 |
function glGetInteger(pname: TGLEnum): TGLint; |
| 178 |
begin |
| 179 |
{ Just for test, to somewhat simulate hardware with lower |
| 180 |
GL_MAX_TEXTURE_UNITS_ARB, |
| 181 |
if pname = GL_MAX_TEXTURE_UNITS_ARB then |
| 182 |
begin |
| 183 |
Result := 2; |
| 184 |
Exit; |
| 185 |
end;} |
| 186 |
|
| 187 |
FillChar(result, SizeOf(result), 0); |
| 188 |
glGetIntegerv(pname, @result); |
| 189 |
end; |
| 190 |
|
| 191 |
function glGetBoolean(pname: TGLEnum): TGLboolean; |
| 192 |
begin |
| 193 |
FillChar(result, SizeOf(result), 0); |
| 194 |
glGetBooleanv(pname, @result) |
| 195 |
end; |
| 196 |
|
| 197 |
{ ---------------------------------------------------------------------------- } |
| 198 |
|
| 199 |
{$ifndef OpenGLES} |
| 200 |
|
| 201 |
procedure glColorv(const v: TVector3Byte); |
| 202 |
begin |
| 203 |
glColor3ubv(@v); |
| 204 |
{$warnings off} // consciously using deprecated in deprecated |
| 205 |
CurrentColor := Vector4(Vector3(V), 1); |
| 206 |
{$warnings on} |
| 207 |
end; |
| 208 |
|
| 209 |
procedure glColorv(const v: TVector4Byte); |
| 210 |
begin |
| 211 |
glColor4ubv(@v); |
| 212 |
{$warnings off} // consciously using deprecated in deprecated |
| 213 |
CurrentColor := Vector4(V); |
| 214 |
{$warnings on} |
| 215 |
end; |
| 216 |
|
| 217 |
procedure glColorv(const v: TVector3); |
| 218 |
begin |
| 219 |
glColor3fv(@v); |
| 220 |
{$warnings off} // consciously using deprecated in deprecated |
| 221 |
CurrentColor := Vector4(V, 1); |
| 222 |
{$warnings on} |
| 223 |
end; |
| 224 |
|
| 225 |
procedure glColorv(const v: TVector4); |
| 226 |
begin |
| 227 |
glColor4fv(@v); |
| 228 |
{$warnings off} // consciously using deprecated in deprecated |
| 229 |
CurrentColor := V; |
| 230 |
{$warnings on} |
| 231 |
end; |
| 232 |
|
| 233 |
procedure CastleGlClipPlane(plane: TGLenum; const V: TVector4Double); |
| 234 |
begin |
| 235 |
glClipPlane(plane, @V); |
| 236 |
end; |
| 237 |
|
| 238 |
procedure glNormalv(const v: TVector3); begin glNormal3fv(@v); end; |
| 239 |
|
| 240 |
procedure glMaterialv(face, pname: TGLEnum; const params: TVector4); begin glMaterialfv(face, pname, @params); end; |
| 241 |
|
| 242 |
procedure glVertexv(const v: TVector2); begin glVertex2fv(@v); end; |
| 243 |
procedure glVertexv(const v: TVector3); begin glVertex3fv(@v); end; |
| 244 |
procedure glVertexv(const v: TVector4); begin glVertex4fv(@v); end; |
| 245 |
|
| 246 |
procedure glTexCoordv(const v: TVector2); begin glTexCoord2fv(@v); end; |
| 247 |
procedure glTexCoordv(const v: TVector3); begin glTexCoord3fv(@v); end; |
| 248 |
procedure glTexCoordv(const v: TVector4); begin glTexCoord4fv(@v); end; |
| 249 |
|
| 250 |
procedure glTexGenv(coord, pname: TGLenum; const params: TVector4); begin glTexGenfv(coord, pname, @params); end; |
| 251 |
|
| 252 |
procedure glLightv(light, pname: TGLEnum; const params: TVector4); begin glLightfv(light, pname, @params); end; |
| 253 |
procedure glLightv(light, pname: TGLEnum; const params: TVector3); begin glLightfv(light, pname, @params); end; |
| 254 |
|
| 255 |
procedure glLightModelv(pname: TGLenum; const params: TVector4); overload; begin glLightModelfv(pname, @params); end; |
| 256 |
|
| 257 |
procedure glFogv(pname: TGLEnum; const params: TVector4); begin glFogfv(pname, @params); end; |
| 258 |
|
| 259 |
procedure glMultMatrix(const m: TMatrix4); begin glMultMatrixf(@m) end; |
| 260 |
procedure glLoadMatrix(const m: TMatrix4); begin glLoadMatrixf(@m) end; |
| 261 |
|
| 262 |
procedure glTexEnvv(target, pname: TGLEnum; const params: TVector4); begin glTexEnvfv(target, pname, @params); end; |
| 263 |
|
| 264 |
{$endif} |
| 265 |
|
| 266 |
{ Various helpers ------------------------------------------------------------ } |
| 267 |
|
| 268 |
procedure GLSetEnabled(Value: TGLenum; IsEnabled: boolean); |
| 269 |
begin |
| 270 |
if IsEnabled then glEnable(Value) else glDisable(Value); |
| 271 |
end; |
| 272 |
|
| 273 |
procedure glFreeBuffer(var Buffer: TGLuint); |
| 274 |
begin |
| 275 |
if Buffer <> 0 then |
| 276 |
begin |
| 277 |
glDeleteBuffers(1, @Buffer); |
| 278 |
Buffer := 0; |
| 279 |
end; |
| 280 |
end; |
| 281 |
|
| 282 |
procedure glFreeVertexArray(var Vao: TGLuint); |
| 283 |
begin |
| 284 |
if Vao <> 0 then |
| 285 |
begin |
| 286 |
glDeleteVertexArrays(1, @Vao); |
| 287 |
Vao := 0; |
| 288 |
end; |
| 289 |
end; |
| 290 |
|
| 291 |
function BoolToGL(const Value: boolean): TGLboolean; |
| 292 |
begin |
| 293 |
if Value then |
| 294 |
Result := GL_TRUE |
| 295 |
else |
| 296 |
Result := GL_FALSE; |
| 297 |
end; |
| 298 |
|
| 299 |
function GLEnableTexture(const Target: TEnableTextureTarget): boolean; |
| 300 |
begin |
| 301 |
Result := true; |
| 302 |
|
| 303 |
if GLFeatures.EnableFixedFunction then |
| 304 |
begin |
| 305 |
{$ifndef OpenGLES} |
| 306 |
|
| 307 |
case Target of |
| 308 |
etNone: begin |
| 309 |
glDisable(GL_TEXTURE_2D); |
| 310 |
if GLFeatures.TextureCubeMap then glDisable(GL_TEXTURE_CUBE_MAP); |
| 311 |
if GLFeatures.Texture3D then glDisable(GL_TEXTURE_3D); |
| 312 |
end; |
| 313 |
et2D: begin |
| 314 |
glEnable(GL_TEXTURE_2D); |
| 315 |
if GLFeatures.TextureCubeMap then glDisable(GL_TEXTURE_CUBE_MAP); |
| 316 |
if GLFeatures.Texture3D then glDisable(GL_TEXTURE_3D); |
| 317 |
end; |
| 318 |
etCubeMap: begin |
| 319 |
glDisable(GL_TEXTURE_2D); |
| 320 |
if GLFeatures.TextureCubeMap then glEnable(GL_TEXTURE_CUBE_MAP) else Result := false; |
| 321 |
if GLFeatures.Texture3D then glDisable(GL_TEXTURE_3D); |
| 322 |
end; |
| 323 |
et3D: begin |
| 324 |
glDisable(GL_TEXTURE_2D); |
| 325 |
if GLFeatures.TextureCubeMap then glDisable(GL_TEXTURE_CUBE_MAP); |
| 326 |
if GLFeatures.Texture3D then glEnable(GL_TEXTURE_3D) else Result := false; |
| 327 |
end; |
| 328 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 329 |
else raise EInternalError.Create('GLEnableTexture:Target?'); |
| 330 |
{$endif} |
| 331 |
end; |
| 332 |
|
| 333 |
{$endif} |
| 334 |
end; |
| 335 |
end; |
| 336 |
|
| 337 |
{$endif read_implementation} |