Created
June 1, 2020 02:06
-
-
Save ZakBlystone/74648881615ef3166b38b260e7c0dc0c to your computer and use it in GitHub Desktop.
GLua code for drawing a sphere using the mesh library
This file contains hidden or 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
-- Sphere drawing using mesh library (CC0 Public Domain, free to use in whatever you want) | |
-- Created by: Zachary Blystone ( [email protected] ) | |
-- Localized functions | |
local mesh_position = mesh.Position | |
local mesh_normal = mesh.Normal | |
local mesh_color = mesh.Color | |
local mesh_userdata = mesh.UserData | |
local mesh_texcoord = mesh.TexCoord | |
local mesh_advance = mesh.AdvanceVertex | |
local mesh_begin = mesh.Begin | |
local mesh_end = mesh.End | |
local math_cos = math.cos | |
local math_sin = math.sin | |
local math_pi = math.pi | |
local mtVector = FindMetaTable("Vector") | |
local mtColor = FindMetaTable("Color") | |
local colUnpack = mtColor.Unpack | |
local vecUnpack = mtVector.Unpack | |
local vecSet = mtVector.SetUnpacked | |
local __vec = Vector() | |
local __zero = Vector(0,0,0) | |
-- Renders a sphere using the mesh library | |
local function DrawSphere(pos, radius, detail, color) | |
pos = pos or __zero | |
radius = radius or 32 | |
detail = detail or 32 | |
color = color or color_white | |
local px, py, pz = vecUnpack(pos) | |
local r, g, b, a = colUnpack(color) | |
local stepRow = math_pi / detail | |
local stepCol = 2 * stepRow | |
local dcx, dcy = math_cos(stepCol), math_sin(stepCol) | |
local drx, dry = math_cos(stepRow), math_sin(stepRow) | |
local cx, cy = 1, 0 | |
local rx, ry = 1, 0 | |
local u, v = 0, 0 | |
local step = 1/detail | |
local t = nil | |
for i=0, detail-1 do | |
-- Reset column data | |
u, cx, cy = 0, 1, 0 | |
-- Draw a row | |
mesh_begin( MATERIAL_TRIANGLE_STRIP, detail * 2 ) | |
for j=0, detail do | |
-- Next row: cos, sin | |
local nrx = rx * drx - ry * dry | |
local nry = rx * dry + ry * drx | |
-- Bottom normal | |
local nx0 = nry * cx | |
local ny0 = nry * cy | |
local nz0 = nrx | |
-- Top normal | |
local nx1 = ry * cx | |
local ny1 = ry * cy | |
local nz1 = rx | |
-- Bottom position | |
local x0 = px + radius * nx0 | |
local y0 = py + radius * ny0 | |
local z0 = pz + radius * nz0 | |
-- Top position | |
local x1 = px + radius * nx1 | |
local y1 = py + radius * ny1 | |
local z1 = pz + radius * nz1 | |
-- Top of strip | |
vecSet(__vec, x0, y0, z0) mesh_position( __vec ) | |
vecSet(__vec, nx0, ny0, nz0) mesh_normal( __vec ) | |
mesh_userdata(-nz0,0,nx0,0) | |
mesh_color(r, g, b, a) | |
mesh_texcoord(0, u, v+step) | |
mesh_advance() | |
-- Bottom of strip | |
vecSet(__vec, x1, y1, z1) mesh_position( __vec ) | |
vecSet(__vec, nx1, ny1, nz1) mesh_normal( __vec ) | |
mesh_userdata(-nz1,0,nx1,0) | |
mesh_color(r, g, b, a) | |
mesh_texcoord(0, u, v) | |
mesh_advance() | |
-- Advance sin, cos for column | |
t = cx * dcx - cy * dcy | |
cy = cx * dcy + cy * dcx | |
cx = t | |
-- Increment 'u' texture coordinate | |
u = u + step | |
end | |
mesh_end() | |
-- Advance sin, cos for row | |
t = rx * drx - ry * dry | |
ry = rx * dry + ry * drx | |
rx = t | |
-- Increment 'v' texture coordinate | |
v = v + step | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment