Skip to content

Instantly share code, notes, and snippets.

@VictorQueiroz
Last active November 4, 2020 16:39
Show Gist options
  • Save VictorQueiroz/b263a94e8b23985dadcf6842403336fc to your computer and use it in GitHub Desktop.
Save VictorQueiroz/b263a94e8b23985dadcf6842403336fc to your computer and use it in GitHub Desktop.
Convert framebuffer normalized coordinates into pixel screen position
/**
See: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap26.html#vertexpostproc-viewport
The viewport transformation is determined by the selected viewport’s width and height in pixels, px and py, respectively, and its center (ox, oy) (also in pixels), as well as its depth range min and max determining a depth range scale value pz and a depth range bias value oz (defined below). The vertex’s framebuffer coordinates (xf, yf, zf) are given by
xf = (px / 2) xd + ox
yf = (py / 2) yd + oy
zf = pz × zd + oz
The viewport parameters shown in the above equations are found from these values as
ox = x + width / 2
oy = y + height / 2
oz = minDepth
px = width
py = height
pz = maxDepth - minDepth.
*/
class FramebufferCoordinates {
public constructor(
private readonly width: number,
private readonly height: number,
private readonly minDepth: number,
private readonly maxDepth: number
) {}
public ox() {
return this.width/2;
}
public oy() {
return this.height/2;
}
public xf(xd: number) {
return (this.width/2) * xd + this.ox();
}
public yf(yd: number) {
return (this.height/2) * yd + this.oy();
}
public pz() {
return this.maxDepth - this.minDepth;
}
public zf(zd: number) {
return this.pz() * zd + this.minDepth;
}
}
var coords = new FramebufferCoordinates(800,600,0.0,1.0)
const vertices = [
[coords.xf(0.0),coords.yf(1.0)],
[coords.xf(1.0),coords.yf(1.0)],
[coords.xf(-1.0),coords.yf(1.0)],
[coords.zf(-1.0),coords.zf(1.0)]
]
console.log(vertices)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment