Retrogamecoding(.org) > Examples
Simple 3D for EGSL
Bereb:
A simple 3D demo :
--- Code: ---#!/usr/bin/egsl
-----------------------
-- screen set
-----------------------
openwindow(650,450,32,"3D demo - EGSL")
backcolour(255,255,255)
colour(0,0,0)
clearscreen()
---------------------------
-- constants & variables
---------------------------
rad = math.rad
ox, oy = screenwidth()/2, screenheight()/2
coorx, coory = 0, 0
pen = true
heading = 0
--------------------------------
-- main graphic function
--------------------------------
function pos(x,y)
-- draw if pen is down (== true)
if pen then aaline(ox+coorx, oy-coory, ox+x, oy-y) end
-- set new position
coorx, coory = x, y
end
-----------------
-- 3D functions
-----------------
function isopoint(a,b,c)
pos( (a-c)*0.886 , b-(a+c)*0.5 )
end
function cone(r,l)
r = l/2
-- set at point (0,0,0)
pen = false
local h, v
for h = 0, l, 10 do
for v = 0, 360, 20 do
isopoint( (h/2)*cos(rad(v)), h, (h/2)*sin(rad(v)) )
pen = true
end
pen = false
end
for v = 0, 360, 20 do
for h = 0, l, 10 do
isopoint( (h/2)*cos(rad(v)), h, (h/2)*sin(rad(v)) )
pen= true
end
pen = false
end
end
function ellipsoid(r1, r2, r3)
-- set at point (100,-30,100)
local u, v, l, m, n
pen = false
for u = 0, 360, 20 do
for v = 0, 180, 20 do
l = (r1 * cos(rad(u))) * sin(rad(v))
m = r3 * sin(rad(u))
n = (r2 * cos(rad(u))) * cos(rad(v))
isopoint(100 + l, -30 + m, 100 + n)
pen = true
end
pen= false
end
for v = 0, 180, 20 do
for u = 0, 360, 20 do
l = (r1 * cos(rad(u))) * sin(rad(v))
m = r3 * sin(rad(u))
n = r2 * cos(rad(u)) * cos(rad(v))
isopoint(100 + l, -30 + m, 100 + n)
pen = true
end
pen = false
end
end
function cylinder(r, l)
-- set at point (120,-100,-120)
local h, v
pen= false
for h = 0, l, 10 do
for v = 0, 360, 20 do
isopoint( 120+r*cos(rad(v)), -100+h, -120+r*sin(rad(v)) )
pen = true
end
pen = false
end
for v = 0, 360, 20 do
for h = 0, l, 10 do
isopoint( 120+r*cos(rad(v)), -100+h, -120+r*sin(rad(v)) )
pen = true
end
pen= false
end
end
function sphere(r)
-- set at point (-120,-100,120)
local u, v, l, m, n
pen = false
for u = 0, 360, 20 do
for v = 0, 180, 20 do
l = (r * cos(rad(u))) * sin(rad(v))
m = r * sin(rad(u))
n = (r * cos(rad(u))) * cos(rad(v))
isopoint(-120 + l, -100 + m, 120 + n)
pen= true
end
pen = false
end
for v = 0, 180, 20 do
for u = 0, 360, 20 do
l = (r * cos(rad(u))) * sin(rad(v))
m = r * sin(rad(u))
n = (r * cos(rad(u))) * cos(rad(v))
isopoint(-120 + l, -100 + m, 120 + n)
pen= true
end
pen = false
end
end
---------------------------------
-- 3D demo : main program
---------------------------------
sphere(40)
cone(40, 70)
cylinder(30, 90)
ellipsoid(60, 40, 25)
redraw()
inkey()
closewindow()
--- End code ---
Bereb:
A rotating pyramid :
--- Code: ---#!/usr/bin/egsl
openwindow(400,360,0,"Rotating Pyramid - EGSL")
backcolor(100,100,100)
clearscreen()
color(255,255,255)
local ox, oy = screenwidth()/2, screenheight()/2 -- center of screen
local taille = 100
local alpha, beta = 0, 30
local rad = math.rad
local key
local ax, ay, bx, by, cx, cy, dx, dy
repeat
key = getkey()
clearscreen()
-- building pyramid
ax = taille * cos(rad(alpha))
ay = sin(rad(beta)) * taille * sin(rad(alpha))
bx = taille * cos(rad((alpha + 90) % 360))
by = sin(rad(beta)) * taille * sin(rad((alpha + 90) % 360))
cx = taille * cos(rad((alpha + 180) % 360))
cy = sin(rad(beta)) * taille * sin(rad((alpha + 180) % 360))
dx = taille * cos(rad((alpha + 270) % 360))
dy = sin(rad(beta)) * taille * sin(rad((alpha + 270) % 360))
-- drawing pyramid
aaline(ox, oy-120, ox+ax, oy-ay)
aaline(ox, oy-120, ox+bx, oy-by)
aaline(ox, oy-120, ox+cx, oy-cy)
aaline(ox, oy-120, ox+dx, oy-dy)
aaline(ox+ax, oy-ay, ox+bx, oy-by)
aaline(ox+bx, oy-by, ox+cx, oy-cy)
aaline(ox+cx, oy-cy, ox+dx, oy-dy)
aaline(ox+dx, oy-dy, ox+ax, oy-ay)
redraw()
-- rotating pyramid
alpha = alpha+1
until key == 27
closewindow()
--- End code ---
Sorry, I've not translated my whole script ... taille means size ;)
Bereb:
A cone :
--- Code: ---openwindow(400,400,8,"CONE")
backcolour(255,255,255)
colour(0,0,0)
clearscreen()
-- constants
local rad = math.rad
local ox, oy = screenwidth()/2, screenheight()/2
-- variables
local x, y, h, beta = 0, 0, 0, 0, 0
-- main loop
repeat
key = getkey()
clearscreen()
for alpha = 0, 360, 8 do
x = 100 * cos(rad(alpha))
y = sin(rad(alpha)) * sin(rad(beta)) * 80
h = 100 * cos(rad(beta))
aaline(ox, oy, ox+x, oy-(y - h))
end
redraw()
beta = beta + 2
until key == 27
closewindow()
--- End code ---
Faster without anti-aliasing ( i.e. line() instead of aaline() )
Cybermonkey:
Awesome. Who starts coding an ELITE clone? 8)
Peter:
good work. what about textures?
Navigation
[0] Message Index
[#] Next page
Go to full version