Created
June 8, 2020 23:33
-
-
Save lhog/ae457103568dde100c511c977cbfdd0d 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
function widget:GetInfo() | |
return { | |
name = "skin", | |
desc = "skin", | |
author = "ivand", | |
license = "GNU GPL, v2 or later", | |
layer = 0, | |
enabled = true -- loaded by default? | |
} | |
end | |
local shader | |
local viewMatLoc, projMatLoc | |
local vao | |
local shaderSrc = { | |
vs = | |
[[ | |
#version 410 core | |
#extension GL_ARB_explicit_attrib_location : enable | |
// defines | |
#define VA_TYPE VA_TYPE_L | |
// globals | |
// uniforms | |
uniform mat4 u_movi_mat; | |
uniform mat4 u_proj_mat; | |
// VS input attributes | |
layout(location = 0) in vec4 a_vertex_xyzw; | |
layout(location = 1) in vec3 a_normal_xyz; | |
layout(location = 2) in vec2 a_texcoor_stuv; | |
layout(location = 3) in vec4 a_color0_rgba; | |
layout(location = 4) in vec4 a_color1_rgba; | |
// VS output attributes | |
out vec4 v_vertex_xyzw; | |
out vec3 v_normal_xyz; | |
out vec2 v_texcoor_stuv; | |
out vec4 v_color0_rgba; | |
out vec4 v_color1_rgba; | |
void main() { | |
gl_Position = u_proj_mat * u_movi_mat * vec4(a_vertex_xyzw); | |
v_vertex_xyzw = a_vertex_xyzw; | |
v_normal_xyz = a_normal_xyz; | |
v_texcoor_stuv = a_texcoor_stuv; | |
v_texcoor_stuv.y = 1.0 - v_texcoor_stuv.y; | |
v_color0_rgba = a_color0_rgba; | |
v_color1_rgba = a_color1_rgba; | |
} | |
]], | |
fs = | |
[[ | |
#version 410 core | |
#extension GL_ARB_explicit_attrib_location : enable | |
// defines | |
#define VA_TYPE VA_TYPE_L | |
// globals | |
// uniforms | |
uniform sampler2D u_tex0; | |
uniform vec4 u_alpha_test_ctrl = vec4(0.0, 0.0, 0.0, 1.0); | |
uniform vec3 u_gamma_exponents = vec3(1.0, 1.0, 1.0); | |
uniform float u_tex_loaded = 1.0; | |
// FS input attributes | |
in vec4 v_vertex_xyzw; | |
in vec3 v_normal_xyz; | |
in vec2 v_texcoor_stuv; | |
in vec4 v_color0_rgba; | |
in vec4 v_color1_rgba; | |
// FS output attributes | |
layout(location = 0) out vec4 f_color_rgba; | |
void main() { | |
f_color_rgba = mix(vec4(1.0), texture(u_tex0, v_texcoor_stuv.st), u_tex_loaded); | |
f_color_rgba *= v_color0_rgba * (1.0 / 255.0); | |
float alpha_test_gt = float(f_color_rgba.a > u_alpha_test_ctrl.x) * u_alpha_test_ctrl.y; | |
float alpha_test_lt = float(f_color_rgba.a < u_alpha_test_ctrl.x) * u_alpha_test_ctrl.z; | |
if ((alpha_test_gt + alpha_test_lt + u_alpha_test_ctrl.w) == 0.0) | |
discard; | |
f_color_rgba.rgb = pow(f_color_rgba.rgb, u_gamma_exponents); | |
} | |
]] | |
} | |
local function CreateStretchedPlaneVAO(lrRatio, tbRatio) | |
--[[ | |
Plane 4 x 4 vertices | |
* * * * | |
* * * * | |
* * * * | |
* * * * | |
draw with quads for simplicity | |
every quad requires 2 triangles ==> 6 indices | |
9 quads in the plane | |
]]-- | |
local vao = gl.CreateVertexArray(4 * 4, 9 * 6, false) | |
local verts = { | |
p = { | |
-1.0, -1.0, 0, 1, -0.5, -1.0, 0, 1, 0.5, -1.0, 0, 1, 1.0, -1.0, 0, 1, | |
-1.0, -0.5, 0, 1, -0.5, -0.5, 0, 1, 0.5, -0.5, 0, 1, 1.0, -0.5, 0, 1, | |
-1.0, 0.5, 0, 1, -0.5, 0.5, 0, 1, 0.5, 0.5, 0, 1, 1.0, 0.5, 0, 1, | |
-1.0, 1.0, 0, 1, -0.5, 1.0, 0, 1, 0.5, 1.0, 0, 1, 1.0, 1.0, 0, 1, | |
}, --each point is defined by 4 coordinates (xyzw) | |
c0 = { | |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
}, | |
uv = { | |
0, 0, 0, 0, lrRatio, 0, 0, 0, 1 - lrRatio, 0, 0, 0, 1, 0, 0, 0, | |
0, tbRatio, 0, 0, lrRatio, tbRatio, 0, 0, 1 - lrRatio, tbRatio, 0, 0, 1, tbRatio, 0, 0, | |
0, 1 - tbRatio, 0, 0, lrRatio, 1 - tbRatio, 0, 0, 1 - lrRatio, 1 - tbRatio, 0, 0, 1, 1 - tbRatio, 0, 0, | |
0, 1, 0, 0, lrRatio, 1, 0, 0, 1 - lrRatio, 1, 0, 0, 1, 1, 0, 0, | |
}, --uv coordinates. 4 tuple, with trailing two == 0 (only first two are used) | |
i = { | |
0, 1, 4, 1, 5, 4, 1, 2, 5, 2, 6, 5, 2, 3, 6, 3, 7, 6, | |
4, 5, 8, 5, 9, 8, 5, 6, 9, 6, 10, 9, 6, 7, 10, 7, 11, 10, | |
8, 9, 12, 9, 13, 12, 9, 10, 13, 10, 14, 13, 10, 11, 14, 11, 15, 14, | |
}, --indices forming triangles of quads | |
} | |
for i = 0, 15 do | |
verts.p[4*i + 1] = verts.p[4*i + 1] * 0.5 | |
verts.p[4*i + 2] = verts.p[4*i + 2] * 0.5 | |
end | |
-- 0, 0 relative indices to start of vertex and index arrays. Non-zero indices allow for partial VAO update | |
gl.UpdateVertexArray(vao, 0, 0, verts) | |
return vao | |
end | |
function widget:DrawScreen() | |
if not shader then | |
return | |
end | |
--Spring.Echo("BLABLABLA") | |
--TODO do it once per ... | |
gl.Texture(0, "LuaUI/Widgets/uvtemplate002-lg.jpg") | |
gl.UseShader(shader) | |
gl.UniformMatrix(viewMatLoc, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) | |
gl.UniformMatrix(projMatLoc, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) | |
if vao then | |
--Spring.Echo("BLABLABLA") | |
--render as triangles | |
gl.RenderVertexArray(vao, GL.TRIANGLES) --skip optional arguments for simplicity | |
end | |
gl.UseShader(0) | |
gl.Texture(0, false) | |
end | |
function widget:Initialize() | |
-- VA_TYPE_T (attributes: p(xyzw), st) | |
--local shaderSrc = gl.GetDefaultShaderSources("VA_TYPE_TC") | |
shader = gl.CreateShader({ | |
vertex = shaderSrc.vs, | |
fragment = shaderSrc.fs, | |
uniformInt = { | |
u_tex0 = 0, | |
} | |
}) | |
viewMatLoc = gl.GetUniformLocation(shader, "u_movi_mat") | |
projMatLoc = gl.GetUniformLocation(shader, "u_proj_mat") | |
--[[ | |
gl.UseShader(shader) | |
gl.UniformMatrix(viewMatLoc, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) | |
gl.UniformMatrix(projMatLoc, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1) | |
gl.UseShader(0) | |
]]-- | |
vao = CreateStretchedPlaneVAO(0.1, 0.1) | |
Spring.Echo("shader", shader, "vao", vao) | |
end | |
function widget:Finalize() | |
if shader then gl.DeleteShader(shader) end | |
if vao then gl.DeleteVertexArray(vao) end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment