Created
July 12, 2016 23:12
-
-
Save ltucker/8ed0d44dcd67a4281a4ac52b8ab45b34 to your computer and use it in GitHub Desktop.
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
local platform = require("platform") | |
require("opengl"):import{"*"} | |
require("sprites"):import{ | |
"Image", | |
"init_program", | |
ortho_mat="glOrtho" | |
} | |
function setup() | |
fbsize = platform.screen_size() | |
device_width = fbsize.width | |
device_height = fbsize.height | |
base_width = 240 | |
base_height = 160 | |
width_ratio = device_width / base_width | |
height_ratio = device_height / base_height | |
-- don't take the floor for a non-integer scale | |
-- scale = math.floor(math.min(width_ratio, height_ratio)) | |
scale = math.min(width_ratio, height_ratio) | |
-- size of the device in base pixels | |
base_screen_width = device_width / scale | |
base_screen_height = device_height / scale | |
-- offset of (0,0) on the device in base pixels | |
offset_x = (base_screen_width - base_width) / 2 | |
offset_y = (base_screen_height - base_height) / 2 | |
-- projection to apply to transform base pixels to | |
-- screen space. | |
device_projection = glOrtho( | |
-offset_x, base_screen_width-offset_x, | |
base_screen_height-offset_y, -offset_y, | |
1, -1 | |
) | |
-- record the default framebuffer (not always 0, ie iOS) | |
device_framebuffer = glGetIntegerv(GL_FRAMEBUFFER_BINDING) | |
-- emulated ovesrscaling factor, the next largest scale that | |
-- is larger than the device's framebuffer. | |
over_scale = math.ceil(scale) | |
-- calculate the width and height of the framebuffer texture. | |
-- these might be larger than the requested sizes | |
-- due to power-of-two texture size restrictions. | |
fb_tex_width, fb_tex_height = next_legal_texture_size(base_width, base_height) | |
-- set up the texture | |
fb_texture = glGenTextures(1) | |
glBindTexture(GL_TEXTURE_2D, fb_texture) | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, nil) | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) -- <-- this will be used since it's upscaling | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) | |
-- make the texture the color attachment for the new offscreen framebuffer | |
framebuffer = glGenFramebuffers(1) | |
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer) | |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_texture, 0) | |
-- world -> offscreen framebuffer projection | |
fb_projection = glOrtho(0, fb_tex_width, fb_tex_height, 0, 1, -1) | |
BACKGROUND = Image:from_file("background.png") | |
TEST_IMAGE_1 = Image:from_file("test1.png") | |
TEST_IMAGE_2 = Image:from_file("test2.png") | |
end | |
function update() | |
-- prepare to draw to the offscreen framebuffer | |
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer) | |
glViewport(0,0, fb_tex_width, fb_tex_height) | |
glScissor(0,0,base_width,base_height) | |
-- draw as usual, but draw onto the offscreen framebuffer | |
blit(BACKGROUND.texture, | |
0, 0, BACKGROUND.width, BACKGROUND.height, | |
0, 0, 1, 1, | |
fb_projection | |
) | |
blit(TEST_IMAGE_1.texture, | |
140, 90, TEST_IMAGE_1.width, TEST_IMAGE_1.height, | |
0, 0, 1, 1, | |
fb_projection | |
) | |
blit(TEST_IMAGE_2.texture, | |
10, 10, TEST_IMAGE_2.width, TEST_IMAGE_2.height, | |
0, 0, 1, 1, | |
fb_projection | |
) | |
-- finally, draw the framebuffer to the device by rendering it's texture | |
-- into a quad covering the screen as defined before. | |
glBindFramebuffer(GL_FRAMEBUFFER, device_framebuffer) | |
glViewport(0, 0, device_width, device_height) | |
-- letter-box / pillar-box to restrict drawing to | |
-- the virtual screen area only (optional) | |
glDisable(GL_SCISSOR_TEST) | |
glClearColor(0,0,0,1) | |
glClear(GL_COLOR_BUFFER_BIT) | |
glEnable(GL_SCISSOR_TEST) | |
glScissor( | |
offset_x*scale, | |
offset_y*scale, | |
base_width*scale, | |
base_height*scale | |
) | |
-- instead of blitting the framebuffer use a special | |
-- program to emulate downscaling | |
-- blit(frambuffer, ...) | |
downscale_blit(fb_texture, | |
0, 0, base_width, base_height, | |
0, 0, base_width / fb_tex_width, base_height / fb_tex_height, | |
over_scale, | |
fb_tex_width, | |
fb_tex_height, | |
device_projection | |
) | |
end | |
-- framebuffer scaled blit | |
fb_v_shader_src = | |
[[ | |
precision highp float; | |
uniform mat4 viewProjection; | |
uniform float superScale; | |
attribute vec4 vPosition; | |
attribute vec4 texCoord_in; | |
varying vec2 texCoord; | |
void main() | |
{ | |
texCoord = texCoord_in.xy; | |
gl_Position = viewProjection*vPosition; | |
} | |
]] | |
fb_f_shader_src = | |
[[ | |
precision highp float; | |
varying vec2 texCoord; | |
uniform sampler2D tex; | |
uniform float superScale; | |
uniform float pixelWidth; | |
uniform float pixelHeight; | |
void main() | |
{ | |
// pretend there are this many texels ... | |
float wt = superScale * pixelWidth; | |
float ht = superScale * pixelHeight; | |
// texture minification from section 3.7.7 | |
float u = wt * texCoord[0]; | |
float v = ht * texCoord[1]; | |
float i0 = floor(u - 0.5); | |
float j0 = floor(v - 0.5); | |
float i1 = i0 + 1.0; | |
float j1 = j0 + 1.0; | |
float a = fract(u - 0.5); | |
float b = fract(v - 0.5); | |
vec4 t00 = texture2D(tex, vec2(i0/wt, j0/ht)); | |
vec4 t10 = texture2D(tex, vec2(i1/wt, j0/ht)); | |
vec4 t01 = texture2D(tex, vec2(i0/wt, j1/ht)); | |
vec4 t11 = texture2D(tex, vec2(i1/wt, j1/ht)); | |
gl_FragColor = (1.0 - a)*(1.0-b)*t00 + a*(1.0-b)*t10 + (1.0-a)*b*t01 + a*b*t11; | |
} | |
]] | |
_fb_program, _fb_vshader, _fb_fshader = init_program(fb_v_shader_src, fb_f_shader_src) | |
FB_ARG_VPOSITION = glGetAttribLocation(_fb_program, "vPosition") | |
FB_ARG_TEXCOORD = glGetAttribLocation(_fb_program, "texCoord_in") | |
FB_ARG_TEX = glGetUniformLocation(_fb_program, "tex") | |
FB_ARG_PIXEL_WIDTH = glGetUniformLocation(_fb_program, "pixelWidth") | |
FB_ARG_PIXEL_HEIGHT = glGetUniformLocation(_fb_program, "pixelHeight") | |
FB_ARG_SUPERSCALE = glGetUniformLocation(_fb_program, "superScale") | |
FB_ARG_VIEWPROJECTION = glGetUniformLocation(_fb_program, "viewProjection") | |
--- emulated downscaling blit | |
function downscale_blit(tex, x, y, w, h, tx, ty, tw, th, super_scale, pixel_width, pixel_height, projection) | |
glUseProgram(_fb_program) | |
glActiveTexture(GL_TEXTURE0) | |
glBindTexture(GL_TEXTURE_2D, tex) | |
glUniformMatrix4fv(FB_ARG_VIEWPROJECTION, 1, GL_FALSE, projection) | |
glUniform1f(FB_ARG_PIXEL_WIDTH, pixel_width) | |
glUniform1f(FB_ARG_PIXEL_HEIGHT, pixel_height) | |
glUniform1f(FB_ARG_SUPERSCALE, super_scale) | |
glVertexAttribPointer( | |
FB_ARG_VPOSITION, 2, GL_FLOAT, GL_FALSE, 0, | |
_quad(x,y,w,h) | |
) | |
glEnableVertexAttribArray(FB_ARG_VPOSITION) | |
glVertexAttribPointer(FB_ARG_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, | |
_quad(tx,ty,tw,th) | |
) | |
glEnableVertexAttribArray(FB_ARG_TEXCOORD) | |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4) | |
end | |
--- basic blit | |
blit_v_shader_src = | |
[[ | |
uniform mat4 viewProjection; | |
attribute vec4 vPosition; | |
attribute vec4 texCoord_in; | |
varying vec2 texCoord; | |
void main() | |
{ | |
texCoord = texCoord_in.xy; | |
gl_Position = viewProjection*vPosition; | |
} | |
]] | |
blit_f_shader_src = | |
[[ | |
precision mediump float; | |
varying vec2 texCoord; | |
uniform sampler2D tex; | |
void main() | |
{ | |
gl_FragColor.rgba = texture2D(tex, texCoord); | |
} | |
]] | |
_blit_program, _blit_vshader, _blit_fshader = init_program(blit_v_shader_src, blit_f_shader_src) | |
BLIT_ARG_VPOSITION = glGetAttribLocation(_blit_program, "vPosition") | |
BLIT_ARG_TEXCOORD = glGetAttribLocation(_blit_program, "texCoord_in") | |
BLIT_ARG_TEX = glGetUniformLocation(_blit_program, "tex") | |
BLIT_ARG_VIEWPROJECTION = glGetUniformLocation(_blit_program, "viewProjection") | |
function blit(tex, x, y, w, h, tx, ty, tw, th, projection) | |
glUseProgram(_blit_program) | |
glActiveTexture(GL_TEXTURE0) | |
glBindTexture(GL_TEXTURE_2D, tex) | |
glUniformMatrix4fv(BLIT_ARG_VIEWPROJECTION, 1, GL_FALSE, projection) | |
glVertexAttribPointer( | |
BLIT_ARG_VPOSITION, 2, GL_FLOAT, GL_FALSE, 0, | |
_quad(x,y,w,h) | |
) | |
glEnableVertexAttribArray(BLIT_ARG_VPOSITION) | |
glVertexAttribPointer(BLIT_ARG_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, | |
_quad(tx,ty,tw,th) | |
) | |
glEnableVertexAttribArray(BLIT_ARG_TEXCOORD) | |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4) | |
end | |
function _quad(x,y,w,h) | |
local x2 = x + w | |
local y2 = y + h | |
return GLfloatv(8, {x, y2, x2, y2, x, y, x2, y}) | |
end | |
function next_legal_texture_size(width, height) | |
-- assuming power of 2 ... | |
local ptw = 256 | |
while ptw < width do | |
ptw = ptw * 2 | |
end | |
local pth = 256 | |
while pth < height do | |
pth = pth * 2 | |
end | |
return ptw, pth | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment