This commit is contained in:
1 parent
9a59f1fe52
commit
6d3b8e6ac5
8 files changed
+4454
-49
No files matched your search
+159
-1
@@ -14,16 +14,174 @@ function KMRenderMobileCreateTexture(aMinFilter, aMagFilter: TKMFilterType): Car
|
||||
procedure KMRenderMobileDeleteTexture(aTexId: Cardinal);
|
||||
procedure KMRenderMobileUpdateTexture(aTexture: Cardinal; aWidth, aHeight: Word; aMode: TKMTexFormat; const aData: Pointer);
|
||||
procedure KMRenderMobileInvalidateTextureBindingCache;
|
||||
procedure KMRenderMobileApplyOrdinary2DProjection(aScreenX, aScreenY: Word);
|
||||
procedure KMRenderMobileClearCurrentTarget;
|
||||
procedure KMRenderMobileSetActiveTextureUnit0;
|
||||
procedure KMRenderMobileDrawSampledTriangles(
|
||||
const aPositionData, aTexCoordData: Pointer;
|
||||
aStride, aVertexCount: Integer;
|
||||
aColorR, aColorG, aColorB, aColorA: Single);
|
||||
procedure KMRenderMobileActivateSampledProgram;
|
||||
procedure KMRenderMobileInvalidateSampledProgram;
|
||||
procedure KMRenderMobileInitializeSampledProgram;
|
||||
function KMRenderMobileSampledProgramReady: Boolean;
|
||||
procedure KMRenderMobileDrawBrightnessOverlay(
|
||||
aIntensity: Single; aWidth, aHeight: Word);
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
SysUtils, CastleGLUtils, CastleGLES;
|
||||
SysUtils, CastleGLUtils, CastleGLES, CastleRectangles, CastleProjection, CastleRenderContext, CastleVectors,
|
||||
CastleGLShaders, CastleRenderOptions;
|
||||
|
||||
var
|
||||
gTextureBackendReady: Boolean;
|
||||
gTextureBindingKnown: Boolean;
|
||||
gCachedTextureId: Cardinal;
|
||||
gSampledProgram: TGLSLProgram;
|
||||
|
||||
const
|
||||
SampledVertexShader =
|
||||
'attribute vec3 aPosition;' + #10 +
|
||||
'attribute vec2 aTexCoord;' + #10 +
|
||||
'uniform mat4 uProjection;' + #10 +
|
||||
'varying vec2 vTexCoord;' + #10 +
|
||||
'void main() {' + #10 +
|
||||
' gl_Position = uProjection * vec4(aPosition, 1.0);' + #10 +
|
||||
' vTexCoord = aTexCoord;' + #10 +
|
||||
'}';
|
||||
SampledFragmentShader =
|
||||
'uniform sampler2D uTexture;' + #10 +
|
||||
'uniform vec4 uColor;' + #10 +
|
||||
'varying vec2 vTexCoord;' + #10 +
|
||||
'void main() {' + #10 +
|
||||
' gl_FragColor = texture2D(uTexture, vTexCoord) * uColor;' + #10 +
|
||||
'}';
|
||||
|
||||
|
||||
procedure KMRenderMobileApplyOrdinary2DProjection(aScreenX, aScreenY: Word);
|
||||
var
|
||||
Dimensions: TFloatRectangle;
|
||||
begin
|
||||
Dimensions.Left := 0;
|
||||
Dimensions.Width := aScreenX;
|
||||
Dimensions.Bottom := aScreenY;
|
||||
Dimensions.Height := -Single(aScreenY);
|
||||
RenderContext.ProjectionMatrix := OrthoProjectionMatrix(Dimensions, -100, 100);
|
||||
end;
|
||||
|
||||
|
||||
procedure KMRenderMobileClearCurrentTarget;
|
||||
begin
|
||||
RenderContext.Clear([cbColor, cbDepth, cbStencil], TVector4.Zero);
|
||||
end;
|
||||
|
||||
|
||||
procedure KMRenderMobileDrawBrightnessOverlay(
|
||||
aIntensity: Single; aWidth, aHeight: Word);
|
||||
begin
|
||||
DrawRectangle(
|
||||
Rectangle(0, 0, aWidth, aHeight),
|
||||
Vector4(aIntensity, aIntensity, aIntensity, aIntensity),
|
||||
bsDstColor, bdOne, True);
|
||||
RenderContext.BlendingEnable(bsSrcAlpha, bdOneMinusSrcAlpha);
|
||||
end;
|
||||
|
||||
|
||||
procedure KMRenderMobileSetActiveTextureUnit0;
|
||||
begin
|
||||
CastleGLES.glActiveTexture(CastleGLES.GL_TEXTURE0);
|
||||
end;
|
||||
|
||||
|
||||
procedure KMRenderMobileInitializeSampledProgram;
|
||||
var
|
||||
candidate: TGLSLProgram;
|
||||
positionAttribute, texCoordAttribute: TGLSLAttribute;
|
||||
projectionUniform, textureUniform, colorUniform: TGLSLUniform;
|
||||
begin
|
||||
if gSampledProgram <> nil then Exit;
|
||||
|
||||
candidate := TGLSLProgram.Create;
|
||||
try
|
||||
candidate.FragmentShaderRequired := True;
|
||||
candidate.AttachVertexShader(SampledVertexShader);
|
||||
candidate.AttachFragmentShader(SampledFragmentShader);
|
||||
candidate.Link;
|
||||
positionAttribute := candidate.Attribute('aPosition');
|
||||
texCoordAttribute := candidate.Attribute('aTexCoord');
|
||||
projectionUniform := candidate.Uniform('uProjection', umIgnore);
|
||||
textureUniform := candidate.Uniform('uTexture', umIgnore);
|
||||
colorUniform := candidate.Uniform('uColor', umIgnore);
|
||||
if (projectionUniform.Location = -1) or (textureUniform.Location = -1)
|
||||
or (colorUniform.Location = -1) then
|
||||
raise Exception.Create('Sampled shader is missing a required uniform');
|
||||
gSampledProgram := candidate;
|
||||
candidate := nil;
|
||||
finally
|
||||
candidate.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
procedure KMRenderMobileActivateSampledProgram;
|
||||
begin
|
||||
if gSampledProgram = nil then
|
||||
raise Exception.Create('Sampled shader program is not ready');
|
||||
gSampledProgram.Enable;
|
||||
end;
|
||||
|
||||
|
||||
procedure KMRenderMobileInvalidateSampledProgram;
|
||||
begin
|
||||
FreeAndNil(gSampledProgram);
|
||||
end;
|
||||
|
||||
|
||||
function KMRenderMobileSampledProgramReady: Boolean;
|
||||
begin
|
||||
Result := gSampledProgram <> nil;
|
||||
end;
|
||||
|
||||
|
||||
procedure KMRenderMobileDrawSampledTriangles(
|
||||
const aPositionData, aTexCoordData: Pointer;
|
||||
aStride, aVertexCount: Integer;
|
||||
aColorR, aColorG, aColorB, aColorA: Single);
|
||||
var
|
||||
positionAttribute, texCoordAttribute: TGLSLAttribute;
|
||||
projectionUniform, colorUniform: TGLSLUniform;
|
||||
positionEnabled, texCoordEnabled: Boolean;
|
||||
begin
|
||||
if (aPositionData = nil) or (aTexCoordData = nil) or (aStride <= 0) or (aVertexCount <= 0) then
|
||||
raise Exception.Create('Invalid sampled triangle draw input');
|
||||
if gSampledProgram = nil then
|
||||
KMRenderMobileActivateSampledProgram;
|
||||
|
||||
positionAttribute := gSampledProgram.Attribute('aPosition');
|
||||
texCoordAttribute := gSampledProgram.Attribute('aTexCoord');
|
||||
projectionUniform := gSampledProgram.Uniform('uProjection', umIgnore);
|
||||
colorUniform := gSampledProgram.Uniform('uColor', umIgnore);
|
||||
if (projectionUniform.Location = -1) or (colorUniform.Location = -1) then
|
||||
raise Exception.Create('Sampled shader is missing a required draw uniform');
|
||||
|
||||
KMRenderMobileActivateSampledProgram;
|
||||
projectionUniform.SetValue(RenderContext.ProjectionMatrix);
|
||||
colorUniform.SetValue(Vector4(aColorR, aColorG, aColorB, aColorA));
|
||||
RenderContext.BindBuffer[btArray] := 0;
|
||||
positionEnabled := False;
|
||||
texCoordEnabled := False;
|
||||
try
|
||||
positionAttribute.EnableArrayVector3(nil, CastleGLES.GLsizei(aStride), PtrUInt(aPositionData));
|
||||
positionEnabled := True;
|
||||
texCoordAttribute.EnableArrayVector2(nil, CastleGLES.GLsizei(aStride), PtrUInt(aTexCoordData));
|
||||
texCoordEnabled := True;
|
||||
CastleGLES.glDrawArrays(CastleGLES.GL_TRIANGLES, 0, CastleGLES.GLsizei(aVertexCount));
|
||||
finally
|
||||
if texCoordEnabled then texCoordAttribute.DisableArray;
|
||||
if positionEnabled then positionAttribute.DisableArray;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
procedure RequireTextureBackendReady;
|
||||
|
||||
Reference in new issue
Block a user