-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Include dimensions in information we store about the image * Refactor image drawing to Draw module * Formatting * Wire up resize mode to image * Add very basic tiling logic * Image mode * Get basic tiling working * Formatting
Showing
6 changed files
with
141 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Image.re | ||
* | ||
* Core logic for rendering images to the screen | ||
*/ | ||
|
||
open Reglm; | ||
open Reglfw.Glfw; | ||
|
||
open Revery_Core; | ||
open Revery_Shaders; | ||
module Geometry = Revery_Geometry; | ||
|
||
let identityMatrix = Mat4.create(); | ||
|
||
let drawImage = | ||
( | ||
~imagePath: string, | ||
~transform: Mat4.t=identityMatrix, | ||
~width: float, | ||
~height: float, | ||
~opacity=1.0, | ||
~tint=Colors.white, | ||
~resizeMode=ImageResizeMode.Stretch, | ||
(), | ||
) => { | ||
let textureShader = Assets.textureShader(); | ||
let imgInfo: ImageRenderer.t = ImageRenderer.getTexture(imagePath); | ||
|
||
switch (imgInfo.hasLoaded) { | ||
| false => () | ||
| true => | ||
let ctx = RenderPass.getContext(); | ||
CompiledShader.use(textureShader.compiledShader); | ||
let m = ctx.projection; | ||
|
||
let world = transform; | ||
|
||
CompiledShader.setUniformMatrix4fv(textureShader.uniformWorld, world); | ||
CompiledShader.setUniformMatrix4fv(textureShader.uniformProjection, m); | ||
|
||
CompiledShader.setUniform4fv( | ||
textureShader.uniformColor, | ||
Vec4.create(tint.r, tint.g, tint.b, opacity *. tint.a), | ||
); | ||
|
||
glBindTexture(GL_TEXTURE_2D, imgInfo.texture); | ||
|
||
switch (resizeMode) { | ||
| Stretch => | ||
let quad = | ||
Assets.quad(~minX=0., ~minY=0., ~maxX=width, ~maxY=height, ()); | ||
Geometry.draw(quad, textureShader.compiledShader); | ||
| Repeat => | ||
let x = ref(0); | ||
let y = ref(0); | ||
|
||
let xDiv = int_of_float(ceil(width /. float_of_int(imgInfo.width))); | ||
let yDiv = int_of_float(ceil(height /. float_of_int(imgInfo.height))); | ||
|
||
let localTransform = Mat4.create(); | ||
|
||
/* | ||
TODO: | ||
Implement this via geometry batching rather than additional draw calls | ||
*/ | ||
while (y^ < yDiv) { | ||
while (x^ < xDiv) { | ||
let xPos = float_of_int(x^ * imgInfo.width); | ||
let yPos = float_of_int(y^ * imgInfo.height); | ||
let v = Vec3.create(xPos, yPos, 0.); | ||
|
||
Mat4.fromTranslation(localTransform, v); | ||
Mat4.multiply(localTransform, world, localTransform); | ||
CompiledShader.setUniformMatrix4fv( | ||
textureShader.uniformWorld, | ||
localTransform, | ||
); | ||
|
||
let quad = | ||
Assets.quad( | ||
~minX=0., | ||
~minY=0., | ||
~maxX=float_of_int(imgInfo.width), | ||
~maxY=float_of_int(imgInfo.height), | ||
(), | ||
); | ||
Geometry.draw(quad, textureShader.compiledShader); | ||
|
||
incr(x); | ||
}; | ||
incr(y); | ||
}; | ||
}; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
type t = | ||
| Stretch | ||
| Repeat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,35 @@ | ||
open Reglm; | ||
open Reglfw.Glfw; | ||
|
||
open Revery_Core; | ||
open Revery_Draw; | ||
|
||
module Shaders = Revery_Shaders; | ||
module Geometry = Revery_Geometry; | ||
module Layout = Layout; | ||
module LayoutTypes = Layout.LayoutTypes; | ||
|
||
open Node; | ||
open ViewNode; | ||
|
||
class imageNode (imagePath: string) = { | ||
as _this; | ||
inherit (class node)() as _super; | ||
val _resizeMode: ref(ImageResizeMode.t) = ref(ImageResizeMode.Stretch); | ||
pub! draw = (parentContext: NodeDrawContext.t) => { | ||
/* Draw background first */ | ||
_super#draw(parentContext); | ||
let textureShader = Assets.textureShader(); | ||
let texture = ImageRenderer.getTexture(imagePath); | ||
|
||
let ctx = RenderPass.getContext(); | ||
Shaders.CompiledShader.use(textureShader.compiledShader); | ||
let m = ctx.projection; | ||
|
||
let dimensions = _this#measurements(); | ||
let width = float_of_int(dimensions.width); | ||
let height = float_of_int(dimensions.height); | ||
let quad = Assets.quad(~minX=0., ~minY=0., ~maxX=width, ~maxY=height, ()); | ||
|
||
let opacity = _super#getStyle().opacity *. parentContext.opacity; | ||
|
||
let world = _this#getWorldTransform(); | ||
|
||
Shaders.CompiledShader.setUniformMatrix4fv( | ||
textureShader.uniformWorld, | ||
world, | ||
let style = _this#getStyle(); | ||
|
||
Image.drawImage( | ||
~imagePath, | ||
~transform=world, | ||
~width=float_of_int(dimensions.width), | ||
~height=float_of_int(dimensions.height), | ||
~resizeMode=_resizeMode^, | ||
~tint=Colors.white, | ||
~opacity=style.opacity, | ||
(), | ||
); | ||
Shaders.CompiledShader.setUniformMatrix4fv( | ||
textureShader.uniformProjection, | ||
m, | ||
); | ||
|
||
Shaders.CompiledShader.setUniform4fv( | ||
textureShader.uniformColor, | ||
Vec4.create(1.0, 1.0, 1.0, opacity), | ||
); | ||
|
||
glBindTexture(GL_TEXTURE_2D, texture); | ||
Geometry.draw(quad, textureShader.compiledShader); | ||
}; | ||
pub setResizeMode = (mode: ImageResizeMode.t) => { | ||
_resizeMode := mode; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters