| 1 |
{%MainUnit castleglutils.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: DrawPrimitive2D and friends. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
{ Primitive to draw using DrawPrimitive2D. |
| 23 |
The modes correspond to OpenGL drawing modes, see |
| 24 |
https://www.opengl.org/wiki/Primitive |
| 25 |
https://www.opengl.org/sdk/docs/man2/xhtml/glBegin.xml } |
| 26 |
TPrimitiveMode = ( |
| 27 |
pmPoints, |
| 28 |
pmLineStrip, |
| 29 |
pmLineLoop, |
| 30 |
pmLines, |
| 31 |
pmTriangleStrip, |
| 32 |
pmTriangleFan, |
| 33 |
pmTriangles |
| 34 |
); |
| 35 |
|
| 36 |
TLineStipple = record |
| 37 |
{ Makes pattern scaled to be longer. |
| 38 |
See glLineStipple parameter. } |
| 39 |
Factor: Integer; |
| 40 |
|
| 41 |
{ The line pattern, each bit in 16-bit integer says if the line is visible. |
| 42 |
See glLineStipple parameter. } |
| 43 |
Pattern: Word; |
| 44 |
end; |
| 45 |
|
| 46 |
const |
| 47 |
DefaultCircleSlices = 32; |
| 48 |
|
| 49 |
{ Draw a rectangle filled with a color. |
| 50 |
|
| 51 |
Like with DrawPrimitive2D: |
| 52 |
Blending is automatically used if Color alpha < 1. |
| 53 |
ForceBlending forces the usage of blending. When it is @false, |
| 54 |
we use blending only if Color[3] (alpha) < 1. } |
| 55 |
procedure DrawRectangle(const R: TRectangle; const Color: TCastleColor; |
| 56 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 57 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 58 |
const ForceBlending: boolean = false); overload; |
| 59 |
procedure DrawRectangle(const R: TFloatRectangle; const Color: TCastleColor; |
| 60 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 61 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 62 |
const ForceBlending: boolean = false); overload; |
| 63 |
|
| 64 |
{ Draw a rectangle frame. |
| 65 |
|
| 66 |
Like with DrawPrimitive2D: |
| 67 |
Blending is automatically used if Color alpha < 1. |
| 68 |
ForceBlending forces the usage of blending. When it is @false, |
| 69 |
we use blending only if Color[3] (alpha) < 1. } |
| 70 |
procedure DrawRectangleOutline(const R: TRectangle; const Color: TCastleColor; |
| 71 |
const LineWidth: Single = 1; |
| 72 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 73 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 74 |
const ForceBlending: boolean = false); overload; |
| 75 |
procedure DrawRectangleOutline(const R: TFloatRectangle; const Color: TCastleColor; |
| 76 |
const LineWidth: Single = 1; |
| 77 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 78 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 79 |
const ForceBlending: boolean = false); overload; |
| 80 |
|
| 81 |
{ Draw a circle. It may be scaled differently in X than Y, thus resembling an ellipse. |
| 82 |
|
| 83 |
Like with DrawPrimitive2D: |
| 84 |
Blending is automatically used if Color alpha < 1. |
| 85 |
ForceBlending forces the usage of blending. When it is @false, |
| 86 |
we use blending only if Color[3] (alpha) < 1. } |
| 87 |
procedure DrawCircle(const Middle: TVector2Integer; |
| 88 |
const HorizontalRadius, VerticalRadius: Integer; |
| 89 |
const Color: TCastleColor; |
| 90 |
const CircleSlices: Cardinal = DefaultCircleSlices; |
| 91 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 92 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 93 |
const ForceBlending: boolean = false); overload; |
| 94 |
procedure DrawCircle(const Middle: TVector2; |
| 95 |
const HorizontalRadius, VerticalRadius: Single; |
| 96 |
const Color: TCastleColor; |
| 97 |
const CircleSlices: Cardinal = DefaultCircleSlices; |
| 98 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 99 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 100 |
const ForceBlending: boolean = false); overload; |
| 101 |
|
| 102 |
{ Draw a circle outline. It may be scaled differently in X than Y, thus resembling an ellipse. |
| 103 |
|
| 104 |
Like with DrawPrimitive2D: |
| 105 |
Blending is automatically used if Color alpha < 1. |
| 106 |
ForceBlending forces the usage of blending. When it is @false, |
| 107 |
we use blending only if Color[3] (alpha) < 1. } |
| 108 |
procedure DrawCircleOutline(const Middle: TVector2Integer; |
| 109 |
const HorizontalRadius, VerticalRadius: Integer; |
| 110 |
const Color: TCastleColor; |
| 111 |
const LineWidth: Single = 1; |
| 112 |
const CircleSlices: Cardinal = DefaultCircleSlices; |
| 113 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 114 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 115 |
const ForceBlending: boolean = false); overload; |
| 116 |
procedure DrawCircleOutline(const Middle: TVector2; |
| 117 |
const HorizontalRadius, VerticalRadius: Single; |
| 118 |
const Color: TCastleColor; |
| 119 |
const LineWidth: Single = 1; |
| 120 |
const CircleSlices: Cardinal = DefaultCircleSlices; |
| 121 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 122 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 123 |
const ForceBlending: boolean = false); overload; |
| 124 |
|
| 125 |
{ Draw a simple 2D primitive with a given color. |
| 126 |
This can be used to draw a series of points, lines or triangles, |
| 127 |
depending on the @code(Mode) parameter. |
| 128 |
|
| 129 |
Blending is automatically used if Color alpha < 1. |
| 130 |
ForceBlending forces the usage of blending. When it is @false, |
| 131 |
we use blending only if Color[3] (alpha) < 1. |
| 132 |
|
| 133 |
@param(LineWidth Line width, only used when Mode indicates lines. |
| 134 |
|
| 135 |
Note that @bold(values other than 1 are not guaranteed to be supported.) |
| 136 |
While it's a fast method to have thick lines, it is implementation-limited in case of |
| 137 |
@orderedList( |
| 138 |
@item(desktop OpenGL with anti-aliasing, see |
| 139 |
https://www.opengl.org/sdk/docs/man/html/glLineWidth.xhtml ) |
| 140 |
@item(mobile OpenGLES, with or without anti-aliasing, see |
| 141 |
https://www.khronos.org/opengles/sdk/docs/man/xhtml/glLineWidth.xml ) |
| 142 |
)) |
| 143 |
|
| 144 |
@param(PointSize Point size, only used when Mode indicates points. |
| 145 |
|
| 146 |
Note that @bold(values other than 1 are not guaranteed to be supported.) |
| 147 |
Mobile OpenGLES does not support setting point size like this. |
| 148 |
And desktop OpenGL limits point size in case anti-aliasing is used, |
| 149 |
see https://www.opengl.org/sdk/docs/man2/xhtml/glPointSize.xml .) |
| 150 |
} |
| 151 |
procedure DrawPrimitive2D(const Mode: TPrimitiveMode; |
| 152 |
const Points: array of TVector2SmallInt; |
| 153 |
const Color: TCastleColor; |
| 154 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 155 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 156 |
const ForceBlending: boolean = false; |
| 157 |
const LineWidth: Single = 1; |
| 158 |
const PointSize: Cardinal = 1); overload; |
| 159 |
procedure DrawPrimitive2D(const Mode: TPrimitiveMode; |
| 160 |
const Points: array of TVector2; |
| 161 |
const Color: TCastleColor; |
| 162 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 163 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 164 |
const ForceBlending: boolean = false; |
| 165 |
const LineWidth: Single = 1; |
| 166 |
const PointSize: Cardinal = 1); overload; |
| 167 |
|
| 168 |
{ Draw primitive, using a stipple (pattern) for line rendering. |
| 169 |
Note that the stipple is ignored on mobile (OpenGLES). } |
| 170 |
procedure DrawPrimitive2D(const Mode: TPrimitiveMode; |
| 171 |
const LineStipple: TLineStipple; |
| 172 |
const Points: array of TVector2; |
| 173 |
const Color: TCastleColor; |
| 174 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 175 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 176 |
const ForceBlending: boolean = false; |
| 177 |
const LineWidth: Single = 1; |
| 178 |
const PointSize: Cardinal = 1); overload; |
| 179 |
|
| 180 |
{ Draw a rectangle that modulates colors underneath, |
| 181 |
making nice animation to AFadeColor while FadeIntensity changes from 1.0 |
| 182 |
down to 0.0. |
| 183 |
|
| 184 |
The GLFadeRectangleLight version makes a flash to AFadeColor, |
| 185 |
then goes back to normal. It works reasonably with any color (light or dark). |
| 186 |
|
| 187 |
The GLFadeRectangleDark version makes additional flash to blackness |
| 188 |
in the middle (so it goes from no modulation, to AFadeColor, |
| 189 |
to pure black, and then back to normal). |
| 190 |
So the "Dark" version is a little more impressive when you're |
| 191 |
flashing with a dark color. |
| 192 |
|
| 193 |
These are nice as a screen effect, to flash some color (e.g. flash |
| 194 |
red color when the player is hurt). |
| 195 |
|
| 196 |
Only RGB portion of AFadeColor is used. } |
| 197 |
procedure GLFadeRectangleDark(const X1, Y1, X2, Y2: Integer; |
| 198 |
const AFadeColor: TVector3; |
| 199 |
const FadeIntensity: Single); overload; |
| 200 |
procedure GLFadeRectangleDark(const Rect: TRectangle; |
| 201 |
const AFadeColor: TCastleColor; |
| 202 |
const FadeIntensity: Single); overload; |
| 203 |
procedure GLFadeRectangleDark(const Rect: TFloatRectangle; |
| 204 |
const AFadeColor: TCastleColor; |
| 205 |
const FadeIntensity: Single); overload; |
| 206 |
procedure GLFadeRectangleLight(const Rect: TRectangle; |
| 207 |
const AFadeColor: TCastleColor; |
| 208 |
const FadeIntensity: Single); overload; |
| 209 |
procedure GLFadeRectangleLight(const Rect: TFloatRectangle; |
| 210 |
const AFadeColor: TCastleColor; |
| 211 |
const FadeIntensity: Single); overload; |
| 212 |
procedure GLFadeRectangle(const X1, Y1, X2, Y2: Integer; |
| 213 |
const AFadeColor: TVector3; |
| 214 |
const FadeIntensity: Single); overload; deprecated 'use GLFadeRectangleDark, or TCastleFlashEffect with Dark = true'; |
| 215 |
procedure GLFadeRectangle(const Rect: TRectangle; |
| 216 |
const AFadeColor: TCastleColor; |
| 217 |
const FadeIntensity: Single); overload; deprecated 'use GLFadeRectangleDark, or TCastleFlashEffect with Dark = true'; |
| 218 |
|
| 219 |
{ Draw a rectangle with blending. |
| 220 |
@deprecated Deprecated, use DrawRectangle instead. } |
| 221 |
procedure GLBlendRectangle(const X1, Y1, X2, Y2: Integer; |
| 222 |
const SourceFactor: TBlendingSourceFactor; |
| 223 |
const DestinationFactor: TBlendingDestinationFactor; |
| 224 |
const Color: TVector4); overload; deprecated 'use DrawRectangle'; |
| 225 |
procedure GLBlendRectangle(const Rect: TRectangle; |
| 226 |
const Color: TVector4); overload; deprecated 'use DrawRectangle'; |
| 227 |
|
| 228 |
{ Call OpenGL @url(https://registry.khronos.org/OpenGL-Refpages/gl4/html/glBlendFunc.xhtml glBlendFunc). |
| 229 |
Accepts parameters as enumerated types TBlendingSourceFactor, TBlendingDestinationFactor. |
| 230 |
Secures from using source or destination factor unsupported on current OpenGL version: |
| 231 |
makes a warnings and falls back on a sensible default (so it will always call |
| 232 |
glBlendFunc anyway). } |
| 233 |
procedure GLBlendFunction( |
| 234 |
SourceFactor: TBlendingSourceFactor; |
| 235 |
DestinationFactor: TBlendingDestinationFactor); deprecated 'use RenderContext.BlendingEnable'; |
| 236 |
|
| 237 |
{$endif read_interface} |
| 238 |
|
| 239 |
{$ifdef read_implementation} |
| 240 |
|
| 241 |
{ DrawPrimitive2D ---------------------------------------------------------------- } |
| 242 |
|
| 243 |
const |
| 244 |
{$ifdef USE_DGL} |
| 245 |
GL_CONSTANT_COLOR = GL_CONSTANT_COLOR_ARB; |
| 246 |
{$endif} |
| 247 |
|
| 248 |
BlendingSourceFactorToGL: array [TBlendingSourceFactor] of TGLEnum = ( |
| 249 |
GL_SRC_ALPHA, |
| 250 |
GL_ONE_MINUS_SRC_ALPHA, |
| 251 |
GL_ZERO, |
| 252 |
GL_ONE, |
| 253 |
|
| 254 |
GL_DST_COLOR, |
| 255 |
GL_SRC_COLOR, |
| 256 |
GL_DST_ALPHA, |
| 257 |
GL_ONE_MINUS_DST_COLOR, |
| 258 |
GL_ONE_MINUS_SRC_COLOR, |
| 259 |
GL_ONE_MINUS_DST_ALPHA, |
| 260 |
|
| 261 |
GL_SRC_ALPHA_SATURATE, |
| 262 |
|
| 263 |
GL_CONSTANT_COLOR, |
| 264 |
GL_ONE_MINUS_CONSTANT_COLOR, |
| 265 |
GL_CONSTANT_ALPHA, |
| 266 |
GL_ONE_MINUS_CONSTANT_ALPHA |
| 267 |
); |
| 268 |
BlendingDestinationFactorToGL: array [TBlendingDestinationFactor] of TGLEnum = ( |
| 269 |
GL_SRC_ALPHA, |
| 270 |
GL_ONE_MINUS_SRC_ALPHA, |
| 271 |
GL_ZERO, |
| 272 |
GL_ONE, |
| 273 |
|
| 274 |
GL_DST_COLOR, |
| 275 |
GL_SRC_COLOR, |
| 276 |
GL_DST_ALPHA, |
| 277 |
GL_ONE_MINUS_DST_COLOR, |
| 278 |
GL_ONE_MINUS_SRC_COLOR, |
| 279 |
GL_ONE_MINUS_DST_ALPHA, |
| 280 |
|
| 281 |
// GL_SRC_ALPHA_SATURATE, // not supported as destination factor |
| 282 |
|
| 283 |
GL_CONSTANT_COLOR, |
| 284 |
GL_ONE_MINUS_CONSTANT_COLOR, |
| 285 |
GL_CONSTANT_ALPHA, |
| 286 |
GL_ONE_MINUS_CONSTANT_ALPHA |
| 287 |
); |
| 288 |
PrimitiveModeToGL: array [TPrimitiveMode] of TGLEnum = ( |
| 289 |
GL_POINTS, |
| 290 |
GL_LINE_STRIP, |
| 291 |
GL_LINE_LOOP, |
| 292 |
GL_LINES, |
| 293 |
GL_TRIANGLE_STRIP, |
| 294 |
GL_TRIANGLE_FAN, |
| 295 |
GL_TRIANGLES |
| 296 |
); |
| 297 |
|
| 298 |
type |
| 299 |
{ OpenGL resources for DrawPrimitive2D. |
| 300 |
Shared across OpenGL contexts, must be freed before last OpenGL context is freed. } |
| 301 |
TPrimitive2DResources = class |
| 302 |
{ Used only in shader pipeline } |
| 303 |
Prog: TGLSLProgram; |
| 304 |
UniformViewportSize, UniformColor: TGLSLUniform; |
| 305 |
AttribVertex: TGLSLAttribute; |
| 306 |
|
| 307 |
Vbo: TGLuint; |
| 308 |
Vao: TVertexArrayObject; |
| 309 |
PointPtr: Pointer; |
| 310 |
PointPtrAllocated: Integer; |
| 311 |
destructor Destroy; override; |
| 312 |
end; |
| 313 |
|
| 314 |
destructor TPrimitive2DResources.Destroy; |
| 315 |
begin |
| 316 |
glFreeBuffer(Vbo); |
| 317 |
FreeAndNil(Vao); |
| 318 |
FreeAndNil(Prog); |
| 319 |
FreeMem(PointPtr); // not tied to OpenGL, but freed together anyway |
| 320 |
PointPtr := nil; |
| 321 |
PointPtrAllocated := 0; |
| 322 |
inherited; |
| 323 |
end; |
| 324 |
|
| 325 |
var |
| 326 |
Primitive2DRes: TPrimitive2DResources; |
| 327 |
|
| 328 |
procedure DrawPrimitive2D(const Mode: TPrimitiveMode; |
| 329 |
const Points: array of TVector2SmallInt; const Color: TCastleColor; |
| 330 |
const BlendingSourceFactor: TBlendingSourceFactor; |
| 331 |
const BlendingDestinationFactor: TBlendingDestinationFactor; |
| 332 |
const ForceBlending: boolean; |
| 333 |
const LineWidth: Single; |
| 334 |
const PointSize: Cardinal); |
| 335 |
var |
| 336 |
PointsSingle: array of TVector2; |
| 337 |
I: Integer; |
| 338 |
begin |
| 339 |
SetLength(PointsSingle, Length(Points)); |
| 340 |
for I := 0 to Length(Points) - 1 do |
| 341 |
PointsSingle[I] := Vector2( |
| 342 |
Points[I].X, |
| 343 |
Points[I].Y); |
| 344 |
DrawPrimitive2D(Mode, PointsSingle, Color, |
| 345 |
BlendingSourceFactor, BlendingDestinationFactor, ForceBlending, |
| 346 |
LineWidth, PointSize); |
| 347 |
end; |
| 348 |
|
| 349 |
procedure DrawPrimitive2D(const Mode: TPrimitiveMode; |
| 350 |
const Points: array of TVector2; const Color: TCastleColor; |
| 351 |
const BlendingSourceFactor: TBlendingSourceFactor; |
| 352 |
const BlendingDestinationFactor: TBlendingDestinationFactor; |
| 353 |
const ForceBlending: boolean; |
| 354 |
const LineWidth: Single; |
| 355 |
const PointSize: Cardinal); |
| 356 |
var |
| 357 |
Blending: boolean; |
| 358 |
I, RequiredPointPtrSize: Integer; |
| 359 |
TargetPointPtr: PVector2; |
| 360 |
begin |
| 361 |
TDrawableImage.BatchingFlush; |
| 362 |
|
| 363 |
FrameProfiler.Start(fmDrawPrimitive2D); |
| 364 |
|
| 365 |
if Primitive2DRes = nil then |
| 366 |
Primitive2DRes := TPrimitive2DResources.Create; |
| 367 |
|
| 368 |
if not GLFeatures.EnableFixedFunction then |
| 369 |
begin |
| 370 |
if Primitive2DRes.Prog = nil then |
| 371 |
begin |
| 372 |
Primitive2DRes.Prog := TGLSLProgram.Create; |
| 373 |
Primitive2DRes.Prog.AttachVertexShader({$I primitive_2.vs.inc}); |
| 374 |
Primitive2DRes.Prog.AttachFragmentShader({$I primitive_2.fs.inc}); |
| 375 |
Primitive2DRes.Prog.Link; |
| 376 |
|
| 377 |
Primitive2DRes.UniformViewportSize := Primitive2DRes.Prog.Uniform('viewport_size'); |
| 378 |
Primitive2DRes.UniformColor := Primitive2DRes.Prog.Uniform('color'); |
| 379 |
Primitive2DRes.AttribVertex := Primitive2DRes.Prog.Attribute('vertex'); |
| 380 |
end; |
| 381 |
|
| 382 |
if Primitive2DRes.Vbo = 0 then |
| 383 |
begin |
| 384 |
Assert(GLFeatures.VertexBufferObject); |
| 385 |
glGenBuffers(1, @Primitive2DRes.Vbo); |
| 386 |
end; |
| 387 |
|
| 388 |
if Primitive2DRes.Vao = nil then |
| 389 |
Primitive2DRes.Vao := TVertexArrayObject.Create(nil); |
| 390 |
end; |
| 391 |
|
| 392 |
{ apply LineWidth, PointSize. |
| 393 |
Their setters avoid doing anything when they already have the requested |
| 394 |
values, so we can just assign them here not worrying about performance. } |
| 395 |
RenderContext.LineWidth := LineWidth; |
| 396 |
RenderContext.PointSize := PointSize; |
| 397 |
|
| 398 |
Blending := ForceBlending or (Color.W < 1); |
| 399 |
if Blending then |
| 400 |
RenderContext.BlendingEnable(BlendingSourceFactor, BlendingDestinationFactor); |
| 401 |
|
| 402 |
{ make Primitive2DRes.PointPtr have necessary size } |
| 403 |
RequiredPointPtrSize := SizeOf(TVector2) * (High(Points) + 1); |
| 404 |
if Primitive2DRes.PointPtrAllocated < RequiredPointPtrSize then |
| 405 |
begin |
| 406 |
{ FreeMem + GetMem, not ReallocMem, |
| 407 |
because we don't care about preserving previous memory contents } |
| 408 |
if Primitive2DRes.PointPtr <> nil then |
| 409 |
FreeMem(Primitive2DRes.PointPtr); |
| 410 |
Primitive2DRes.PointPtr := GetMem(RequiredPointPtrSize); |
| 411 |
Primitive2DRes.PointPtrAllocated := RequiredPointPtrSize; |
| 412 |
end; |
| 413 |
|
| 414 |
{ copy Points to Primitive2DRes.PointPtr } |
| 415 |
TargetPointPtr := Primitive2DRes.PointPtr; |
| 416 |
for I := 0 to High(Points) do |
| 417 |
begin |
| 418 |
Move(Points[I], TargetPointPtr^, SizeOf(TVector2)); |
| 419 |
Inc(TargetPointPtr); |
| 420 |
end; |
| 421 |
|
| 422 |
if not GLFeatures.EnableFixedFunction then |
| 423 |
begin |
| 424 |
RenderContext.CurrentProgram := Primitive2DRes.Prog; |
| 425 |
RenderContext.CurrentVao := Primitive2DRes.Vao; |
| 426 |
|
| 427 |
RenderContext.BindBuffer[btArray] := Primitive2DRes.Vbo; |
| 428 |
{ load only RequiredPointPtrSize, not Primitive2DRes.PointPtrAllocated which may be larger } |
| 429 |
Assert(Primitive2DRes.PointPtrAllocated >= RequiredPointPtrSize); |
| 430 |
glBufferData(GL_ARRAY_BUFFER, RequiredPointPtrSize, Primitive2DRes.PointPtr, GL_DYNAMIC_DRAW); |
| 431 |
|
| 432 |
Primitive2DRes.AttribVertex.EnableArrayVector2(Primitive2DRes.Vao, SizeOf(TVector2), 0); |
| 433 |
|
| 434 |
{ We do not warn when InternalClearColorsByDraw, because then indeed we'll have viewport |
| 435 |
with non-zero left/bottom, and we DrawRectangle is used with Viewport as argument. |
| 436 |
It works OK in this case. } |
| 437 |
if (not RenderContext.InternalClearColorsByDraw) and |
| 438 |
( (RenderContext.Viewport.Left <> 0) or |
| 439 |
(RenderContext.Viewport.Bottom <> 0) |
| 440 |
) then |
| 441 |
WritelnWarning('Drawing DrawPrimitive2D (DrawRectangle etc.) when viewport (left,bottom) is not (0,0) is undefined now'); |
| 442 |
|
| 443 |
Primitive2DRes.UniformViewportSize.SetValue(Vector2( |
| 444 |
RenderContext.Viewport.Width, |
| 445 |
RenderContext.Viewport.Height |
| 446 |
)); |
| 447 |
|
| 448 |
Primitive2DRes.UniformColor.SetValue(Color); |
| 449 |
end else |
| 450 |
begin |
| 451 |
{$ifndef OpenGLES} |
| 452 |
Assert(RenderContext.CurrentProgram = nil); // when EnableFixedFunction, this should always stay nil |
| 453 |
glLoadIdentity(); |
| 454 |
glColorv(Color); |
| 455 |
|
| 456 |
glEnableClientState(GL_VERTEX_ARRAY); |
| 457 |
glVertexPointer(2, GL_FLOAT, SizeOf(TVector2), Primitive2DRes.PointPtr); |
| 458 |
{$endif} |
| 459 |
end; |
| 460 |
|
| 461 |
glDrawArrays(PrimitiveModeToGL[Mode], 0, High(Points) + 1); |
| 462 |
|
| 463 |
if not GLFeatures.EnableFixedFunction then |
| 464 |
begin |
| 465 |
// Primitive2DRes.Prog.Disable; // no need to disable, keep it enabled to save speed |
| 466 |
{ attribute arrays are enabled independent from GLSL program, so we need |
| 467 |
to disable them separately } |
| 468 |
Primitive2DRes.AttribVertex.DisableArray; |
| 469 |
|
| 470 |
RenderContext.BindBuffer[btArray] := 0; |
| 471 |
end else |
| 472 |
begin |
| 473 |
{$ifndef OpenGLES} |
| 474 |
glDisableClientState(GL_VERTEX_ARRAY); |
| 475 |
{$endif} |
| 476 |
end; |
| 477 |
|
| 478 |
if Blending then |
| 479 |
RenderContext.BlendingDisable; |
| 480 |
|
| 481 |
FrameProfiler.Stop(fmDrawPrimitive2D); |
| 482 |
end; |
| 483 |
|
| 484 |
procedure DrawPrimitive2D(const Mode: TPrimitiveMode; |
| 485 |
const LineStipple: TLineStipple; |
| 486 |
const Points: array of TVector2; |
| 487 |
const Color: TCastleColor; |
| 488 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 489 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 490 |
const ForceBlending: boolean = false; |
| 491 |
const LineWidth: Single = 1; |
| 492 |
const PointSize: Cardinal = 1); |
| 493 |
begin |
| 494 |
{$ifndef OpenGLES} |
| 495 |
glLineStipple(1, LineStipple.Pattern); |
| 496 |
glEnable(GL_LINE_STIPPLE); |
| 497 |
{$endif} |
| 498 |
|
| 499 |
DrawPrimitive2D(Mode, Points, Color, |
| 500 |
BlendingSourceFactor, BlendingDestinationFactor, ForceBlending, |
| 501 |
LineWidth, PointSize); |
| 502 |
|
| 503 |
{$ifndef OpenGLES} |
| 504 |
glDisable(GL_LINE_STIPPLE); |
| 505 |
{$endif} |
| 506 |
end; |
| 507 |
|
| 508 |
{ DrawRectangle ---------------------------------------------------------------- } |
| 509 |
|
| 510 |
procedure DrawRectangle(const R: TFloatRectangle; const Color: TCastleColor; |
| 511 |
const BlendingSourceFactor: TBlendingSourceFactor; |
| 512 |
const BlendingDestinationFactor: TBlendingDestinationFactor; |
| 513 |
const ForceBlending: boolean); |
| 514 |
var |
| 515 |
RectanglePoint: array [0..3] of TVector2; |
| 516 |
begin |
| 517 |
RectanglePoint[0] := Vector2(R.Left , R.Bottom); |
| 518 |
RectanglePoint[1] := Vector2(R.Left + R.Width, R.Bottom); |
| 519 |
RectanglePoint[2] := Vector2(R.Left + R.Width, R.Bottom + R.Height); |
| 520 |
RectanglePoint[3] := Vector2(R.Left , R.Bottom + R.Height); |
| 521 |
|
| 522 |
DrawPrimitive2D(pmTriangleFan, RectanglePoint, |
| 523 |
Color, BlendingSourceFactor, BlendingDestinationFactor, ForceBlending); |
| 524 |
end; |
| 525 |
|
| 526 |
procedure DrawRectangle(const R: TRectangle; const Color: TCastleColor; |
| 527 |
const BlendingSourceFactor: TBlendingSourceFactor; |
| 528 |
const BlendingDestinationFactor: TBlendingDestinationFactor; |
| 529 |
const ForceBlending: boolean); |
| 530 |
begin |
| 531 |
DrawRectangle(FloatRectangle(R), Color, |
| 532 |
BlendingSourceFactor, BlendingDestinationFactor, ForceBlending); |
| 533 |
end; |
| 534 |
|
| 535 |
procedure DrawRectangleOutline(const R: TFloatRectangle; const Color: TCastleColor; |
| 536 |
const LineWidth: Single; |
| 537 |
const BlendingSourceFactor: TBlendingSourceFactor; |
| 538 |
const BlendingDestinationFactor: TBlendingDestinationFactor; |
| 539 |
const ForceBlending: boolean); |
| 540 |
var |
| 541 |
RectanglePoint: array [0..3] of TVector2; |
| 542 |
begin |
| 543 |
RectanglePoint[0] := Vector2(R.Left , R.Bottom); |
| 544 |
RectanglePoint[1] := Vector2(R.Left + R.Width, R.Bottom); |
| 545 |
RectanglePoint[2] := Vector2(R.Left + R.Width, R.Bottom + R.Height); |
| 546 |
RectanglePoint[3] := Vector2(R.Left , R.Bottom + R.Height); |
| 547 |
|
| 548 |
DrawPrimitive2D(pmLineLoop, RectanglePoint, |
| 549 |
Color, BlendingSourceFactor, BlendingDestinationFactor, ForceBlending, LineWidth); |
| 550 |
end; |
| 551 |
|
| 552 |
procedure DrawRectangleOutline(const R: TRectangle; const Color: TCastleColor; |
| 553 |
const LineWidth: Single; |
| 554 |
const BlendingSourceFactor: TBlendingSourceFactor; |
| 555 |
const BlendingDestinationFactor: TBlendingDestinationFactor; |
| 556 |
const ForceBlending: boolean); |
| 557 |
begin |
| 558 |
DrawRectangleOutline(FloatRectangle(R), Color, LineWidth, |
| 559 |
BlendingSourceFactor, BlendingDestinationFactor, ForceBlending); |
| 560 |
end; |
| 561 |
|
| 562 |
{ DrawCircle ----------------------------------------------------------------- } |
| 563 |
|
| 564 |
procedure DrawCircle(const Middle: TVector2; |
| 565 |
const HorizontalRadius, VerticalRadius: Single; |
| 566 |
const Color: TCastleColor; |
| 567 |
const CircleSlices: Cardinal = DefaultCircleSlices; |
| 568 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 569 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 570 |
const ForceBlending: boolean = false); |
| 571 |
var |
| 572 |
CirclePoint: array of TVector2; |
| 573 |
PreviousSin, PreviousCos, NextSin, NextCos: Single; |
| 574 |
I: Integer; |
| 575 |
begin |
| 576 |
SetLength(CirclePoint, CircleSlices * 3); |
| 577 |
SinCos(0, PreviousSin, PreviousCos); |
| 578 |
for I := 1 to CircleSlices do |
| 579 |
begin |
| 580 |
if I = CircleSlices then |
| 581 |
{ do not depend that SinCos(2 * Pi, ...) is precisely equal to SinCos(0, ...) } |
| 582 |
SinCos(0, NextSin, NextCos) |
| 583 |
else |
| 584 |
SinCos(2 * Pi * I / CircleSlices, NextSin, NextCos); |
| 585 |
CirclePoint[(I - 1) * 3 ] := Vector2( |
| 586 |
Middle.X, |
| 587 |
Middle.Y); |
| 588 |
CirclePoint[(I - 1) * 3 + 1] := Vector2( |
| 589 |
Middle.X + PreviousSin * HorizontalRadius, |
| 590 |
Middle.Y + PreviousCos * VerticalRadius); |
| 591 |
CirclePoint[(I - 1) * 3 + 2] := Vector2( |
| 592 |
Middle.X + NextSin * HorizontalRadius, |
| 593 |
Middle.Y + NextCos * VerticalRadius); |
| 594 |
PreviousSin := NextSin; |
| 595 |
PreviousCos := NextCos; |
| 596 |
end; |
| 597 |
|
| 598 |
DrawPrimitive2D(pmTriangleFan, CirclePoint, |
| 599 |
Color, BlendingSourceFactor, BlendingDestinationFactor, ForceBlending); |
| 600 |
end; |
| 601 |
|
| 602 |
procedure DrawCircle(const Middle: TVector2Integer; |
| 603 |
const HorizontalRadius, VerticalRadius: Integer; |
| 604 |
const Color: TCastleColor; |
| 605 |
const CircleSlices: Cardinal = DefaultCircleSlices; |
| 606 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 607 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 608 |
const ForceBlending: boolean = false); |
| 609 |
begin |
| 610 |
DrawCircle(Vector2(Middle), HorizontalRadius, VerticalRadius, |
| 611 |
Color, CircleSlices, |
| 612 |
BlendingSourceFactor, BlendingDestinationFactor, ForceBlending); |
| 613 |
end; |
| 614 |
|
| 615 |
procedure DrawCircleOutline(const Middle: TVector2; |
| 616 |
const HorizontalRadius, VerticalRadius: Single; |
| 617 |
const Color: TCastleColor; |
| 618 |
const LineWidth: Single = 1; |
| 619 |
const CircleSlices: Cardinal = DefaultCircleSlices; |
| 620 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 621 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 622 |
const ForceBlending: boolean = false); |
| 623 |
var |
| 624 |
CirclePoint: array of TVector2; |
| 625 |
S, C: Single; |
| 626 |
I: Integer; |
| 627 |
begin |
| 628 |
SetLength(CirclePoint, CircleSlices); |
| 629 |
for I := 0 to CircleSlices - 1 do |
| 630 |
begin |
| 631 |
SinCos(2 * Pi * I / (CircleSlices - 1), S, C); |
| 632 |
CirclePoint[I] := Vector2( |
| 633 |
Middle.X + S * HorizontalRadius, |
| 634 |
Middle.Y + C * VerticalRadius); |
| 635 |
end; |
| 636 |
|
| 637 |
DrawPrimitive2D(pmLineLoop, CirclePoint, |
| 638 |
Color, BlendingSourceFactor, BlendingDestinationFactor, ForceBlending, LineWidth); |
| 639 |
end; |
| 640 |
|
| 641 |
procedure DrawCircleOutline(const Middle: TVector2Integer; |
| 642 |
const HorizontalRadius, VerticalRadius: Integer; |
| 643 |
const Color: TCastleColor; |
| 644 |
const LineWidth: Single = 1; |
| 645 |
const CircleSlices: Cardinal = DefaultCircleSlices; |
| 646 |
const BlendingSourceFactor: TBlendingSourceFactor = bsSrcAlpha; |
| 647 |
const BlendingDestinationFactor: TBlendingDestinationFactor = bdOneMinusSrcAlpha; |
| 648 |
const ForceBlending: boolean = false); |
| 649 |
begin |
| 650 |
DrawCircleOutline(Vector2(Middle), HorizontalRadius, VerticalRadius, |
| 651 |
Color, LineWidth, CircleSlices, |
| 652 |
BlendingSourceFactor, BlendingDestinationFactor, ForceBlending); |
| 653 |
end; |
| 654 |
|
| 655 |
{ GLFadeRectangle ------------------------------------------------------------ } |
| 656 |
|
| 657 |
procedure GLFadeRectangleDark(const X1, Y1, X2, Y2: Integer; |
| 658 |
const AFadeColor: TVector3; const FadeIntensity: Single); |
| 659 |
begin |
| 660 |
GLFadeRectangleDark(Rectangle(X1, Y1, X2 - X1, Y2 - Y1), |
| 661 |
Vector4(AFadeColor, 1.0), FadeIntensity); |
| 662 |
end; |
| 663 |
|
| 664 |
procedure GLFadeRectangleDark(const Rect: TRectangle; |
| 665 |
const AFadeColor: TCastleColor; const FadeIntensity: Single); |
| 666 |
var |
| 667 |
Color: TCastleColor; |
| 668 |
begin |
| 669 |
if FadeIntensity > 0 then |
| 670 |
begin |
| 671 |
Color := FadeDarkColor(AFadeColor, FadeIntensity); |
| 672 |
DrawRectangle(Rect, Color, bsZero, bdSrcColor, true); |
| 673 |
end; |
| 674 |
end; |
| 675 |
|
| 676 |
procedure GLFadeRectangleDark(const Rect: TFloatRectangle; |
| 677 |
const AFadeColor: TCastleColor; const FadeIntensity: Single); |
| 678 |
begin |
| 679 |
GLFadeRectangleDark(Rect.Round, AFadeColor, FadeIntensity); |
| 680 |
end; |
| 681 |
|
| 682 |
procedure GLFadeRectangle(const X1, Y1, X2, Y2: Integer; |
| 683 |
const AFadeColor: TVector3; const FadeIntensity: Single); |
| 684 |
begin |
| 685 |
{$warnings off} // using deprecated in deprecated |
| 686 |
GLFadeRectangle(Rectangle(X1, Y1, X2 - X1, Y2 - Y1), |
| 687 |
Vector4(AFadeColor, 1.0), FadeIntensity); |
| 688 |
{$warnings on} |
| 689 |
end; |
| 690 |
|
| 691 |
procedure GLFadeRectangle(const Rect: TRectangle; |
| 692 |
const AFadeColor: TCastleColor; const FadeIntensity: Single); |
| 693 |
var |
| 694 |
Color: TCastleColor; |
| 695 |
begin |
| 696 |
if FadeIntensity > 0 then |
| 697 |
begin |
| 698 |
Color := FadeDarkColor(AFadeColor, FadeIntensity); |
| 699 |
DrawRectangle(Rect, Color, bsZero, bdSrcColor, true); |
| 700 |
end; |
| 701 |
end; |
| 702 |
|
| 703 |
procedure GLFadeRectangleLight(const Rect: TRectangle; |
| 704 |
const AFadeColor: TCastleColor; const FadeIntensity: Single); |
| 705 |
var |
| 706 |
Color: TCastleColor; |
| 707 |
begin |
| 708 |
if FadeIntensity > 0 then |
| 709 |
begin |
| 710 |
Color := FadeColor(AFadeColor, FadeIntensity); |
| 711 |
DrawRectangle(Rect, Color, bsSrcAlpha, bdOneMinusSrcAlpha, true); |
| 712 |
end; |
| 713 |
end; |
| 714 |
|
| 715 |
procedure GLFadeRectangleLight(const Rect: TFloatRectangle; |
| 716 |
const AFadeColor: TCastleColor; const FadeIntensity: Single); |
| 717 |
begin |
| 718 |
GLFadeRectangleLight(Rect.Round, AFadeColor, FadeIntensity); |
| 719 |
end; |
| 720 |
|
| 721 |
{ GLBlendRectangle ----------------------------------------------------------- } |
| 722 |
|
| 723 |
procedure GLBlendRectangle(const X1, Y1, X2, Y2: Integer; |
| 724 |
const SourceFactor: TBlendingSourceFactor; |
| 725 |
const DestinationFactor: TBlendingDestinationFactor; |
| 726 |
const Color: TVector4); |
| 727 |
begin |
| 728 |
DrawRectangle(Rectangle(X1, Y1, X2 - X1, Y2 - Y1), Color, |
| 729 |
SourceFactor, DestinationFactor, true); |
| 730 |
end; |
| 731 |
|
| 732 |
procedure GLBlendRectangle(const Rect: TRectangle; |
| 733 |
const Color: TVector4); |
| 734 |
begin |
| 735 |
DrawRectangle(Rect, Color, bsOne, bdSrcAlpha, true); |
| 736 |
end; |
| 737 |
|
| 738 |
{ GLBlendFunction ------------------------------------------------------------ } |
| 739 |
|
| 740 |
procedure GLBlendFunction( |
| 741 |
SourceFactor: TBlendingSourceFactor; |
| 742 |
DestinationFactor: TBlendingDestinationFactor); |
| 743 |
begin |
| 744 |
{$ifndef OpenGLES} |
| 745 |
if not GLFeatures.Version_1_4 then |
| 746 |
begin |
| 747 |
if SourceFactor in [bsSrcColor, bsOneMinusSrcColor] then |
| 748 |
begin |
| 749 |
WritelnWarning('Blending', 'Blending factor "%s" as "source" requires OpenGL 1.4', [ |
| 750 |
BlendingSourceFactorNames[SourceFactor] |
| 751 |
]); |
| 752 |
SourceFactor := bsSrcAlpha; |
| 753 |
end; |
| 754 |
|
| 755 |
if DestinationFactor in [bdDstColor, bdOneMinusDstColor] then |
| 756 |
begin |
| 757 |
WritelnWarning('Blending', 'Blending factor "%s" as "destination" requires OpenGL 1.4', [ |
| 758 |
BlendingDestinationFactorNames[DestinationFactor] |
| 759 |
]); |
| 760 |
DestinationFactor := bdOneMinusSrcAlpha; |
| 761 |
end; |
| 762 |
end; |
| 763 |
{$endif} |
| 764 |
|
| 765 |
glBlendFunc( |
| 766 |
BlendingSourceFactorToGL[SourceFactor], |
| 767 |
BlendingDestinationFactorToGL[DestinationFactor]); |
| 768 |
end; |
| 769 |
|
| 770 |
{$endif read_implementation} |