September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
9
Task/Draw-a-sphere/BASIC/draw-a-sphere-11.basic
Normal file
9
Task/Draw-a-sphere/BASIC/draw-a-sphere-11.basic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
10 LET I=21
|
||||
20 LET J=2
|
||||
30 FOR K=-PI TO PI STEP 0.07
|
||||
40 PLOT 21+I*SIN K,22+21*COS K
|
||||
50 PLOT 21+21*SIN K,22+(I-1)*COS K
|
||||
60 NEXT K
|
||||
70 LET I=I-J
|
||||
80 LET J=J+1
|
||||
90 IF I>0 THEN GOTO 30
|
||||
|
|
@ -1,117 +0,0 @@
|
|||
MODE 8
|
||||
INSTALL @lib$+"D3DLIB"
|
||||
D3DTS_VIEW = 2
|
||||
D3DTS_PROJECTION = 3
|
||||
D3DRS_SPECULARENABLE = 29
|
||||
|
||||
SYS "LoadLibrary", @lib$+"D3DX8BBC.DLL" TO d3dx%
|
||||
IF d3dx%=0 ERROR 100, "Couldn't load D3DX8BBC.DLL"
|
||||
SYS "GetProcAddress", d3dx%, "D3DXCreateSphere" TO `D3DXCreateSphere`
|
||||
SYS "GetProcAddress", d3dx%, "D3DXMatrixLookAtLH" TO `D3DXMatrixLookAtLH`
|
||||
SYS "GetProcAddress", d3dx%, "D3DXMatrixPerspectiveFovLH" TO `D3DXMatrixPerspectiveFovLH`
|
||||
|
||||
DIM eyepos%(2), lookat%(2), up%(2), mat%(3,3)
|
||||
|
||||
DIM D3Dlight8{Type%, Diffuse{r%,g%,b%,a%}, Specular{r%,g%,b%,a%}, \
|
||||
\ Ambient{r%,g%,b%,a%}, Position{x%,y%,z%}, Direction{x%,y%,z%}, \
|
||||
\ Range%, Falloff%, Attenuation0%, Attenuation1%, Attenuation2%, \
|
||||
\ Theta%, Phi%}
|
||||
|
||||
DIM D3Dmaterial8{Diffuse{r%,g%,b%,a%}, Ambient{r%,g%,b%,a%}, \
|
||||
\ Specular{r%,g%,b%,a%}, Emissive{r%,g%,b%,a%}, Power%}
|
||||
|
||||
DIM D3Dbasemesh8{QueryInterface%, Addref%, Release%, \
|
||||
\ DrawSubset%, GetNumFaces%, GetNumVertices%, GetFVF%, \
|
||||
\ GetDeclaration%, GetOptions%, GetDevice%, \
|
||||
\ CloneMeshFVF%, CloneMesh%, GetVertexBuffer%, GetIndexBuffer%, \
|
||||
\ LockVertexBuffer%, UnlockVertexBuffer%, LockIndexBuffer%, \
|
||||
\ UnlockIndexBuffer%, GetAttributeTable%}
|
||||
|
||||
DIM D3Ddevice8{QueryInterface%, AddRef%, Release%, TestCooperativeLevel%, \
|
||||
\ GetAvailableTextureMem%, ResourceManagerDiscardBytes%, GetDirect3D%, \
|
||||
\ GetDeviceCaps%, GetDisplayMode%, GetCreationParameters%, SetCursorProperties%, \
|
||||
\ SetCursorPosition%, ShowCursor%, CreateAdditionalSwapChain%, Reset%, \
|
||||
\ Present%, GetBackBuffer%, GetRasterStatus%, SetGammaRamp%, GetGammaRamp%, \
|
||||
\ CreateTexture%, CreateVolumeTexture%, CreateCubeTexture%, CreateVertexBuffer%, \
|
||||
\ CreateIndexBuffer%, CreateRenderTarget%, CreateDepthStencilSurface%, \
|
||||
\ CreateImageSurface%, CopyRects%, UpdateTexture%, GetFrontBuffer%, \
|
||||
\ SetRenderTarget%, GetRenderTarget%, GetDepthStencilSurface%, BeginScene%, \
|
||||
\ EndScene%, Clear%, SetTransform%, GetTransform%, MultiplyTransform%, \
|
||||
\ SetViewport%, GetViewport%, SetMaterial%, GetMaterial%, SetLight%, GetLight%, \
|
||||
\ LightEnable%, GetLightEnable%, SetClipPlane%, GetClipPlane%, SetRenderState%, \
|
||||
\ GetRenderState%, BeginStateBlock%, EndStateBlock%, ApplyStateBlock%, \
|
||||
\ CaptureStateBlock%, DeleteStateBlock%, CreateStateBlock%, SetClipStatus%, \
|
||||
\ GetClipStatus%, GetTexture%, SetTexture%, GetTextureStageState%, \
|
||||
\ SetTextureStageState%, ValidateDevice%, GetInfo%, SetPaletteEntries%, \
|
||||
\ GetPaletteEntries%, SetCurrentTexturePalette%, GetCurrentTexturePalette%, \
|
||||
\ DrawPrimitive%, DrawIndexedPrimitive%, DrawPrimitiveUP%, \
|
||||
\ DrawIndexedPrimitiveUP%, ProcessVertices%, CreateVertexShader%, \
|
||||
\ SetVertexShader%, GetVertexShader%, DeleteVertexShader%, \
|
||||
\ SetVertexShaderConstant%, GetVertexShaderConstant%, GetVertexShaderDeclaration%, \
|
||||
\ GetVertexShaderFunction%, SetStreamSource%, GetStreamSource%, SetIndices%, \
|
||||
\ GetIndices%, CreatePixelShader%, SetPixelShader%, GetPixelShader%, \
|
||||
\ DeletePixelShader%, SetPixelShaderConstant%, GetPixelShaderConstant%, \
|
||||
\ GetPixelShaderFunction%, DrawRectPatch%, DrawTriPatch%, DeletePatch%}
|
||||
|
||||
pDevice%=FN_initd3d(@hwnd%, 1, 1)
|
||||
IF pDevice%=0 ERROR 100, "Couldn't create Direct3D8 device"
|
||||
!(^D3Ddevice8{}+4) = !pDevice%
|
||||
|
||||
SYS `D3DXCreateSphere`, pDevice%, FN_f4(1), 50, 50, ^meshSphere%, 0
|
||||
IF meshSphere% = 0 ERROR 100, "D3DXCreateSphere failed"
|
||||
!(^D3Dbasemesh8{}+4) = !meshSphere%
|
||||
|
||||
REM. Point-source light:
|
||||
D3Dlight8.Type%=1 : REM. point source
|
||||
D3Dlight8.Diffuse.r% = FN_f4(1)
|
||||
D3Dlight8.Diffuse.g% = FN_f4(1)
|
||||
D3Dlight8.Diffuse.b% = FN_f4(1)
|
||||
D3Dlight8.Specular.r% = FN_f4(1)
|
||||
D3Dlight8.Specular.g% = FN_f4(1)
|
||||
D3Dlight8.Specular.b% = FN_f4(1)
|
||||
D3Dlight8.Position.x% = FN_f4(2)
|
||||
D3Dlight8.Position.y% = FN_f4(1)
|
||||
D3Dlight8.Position.z% = FN_f4(4)
|
||||
D3Dlight8.Range% = FN_f4(10)
|
||||
D3Dlight8.Attenuation0% = FN_f4(1)
|
||||
|
||||
REM. Material:
|
||||
D3Dmaterial8.Diffuse.r% = FN_f4(0.2)
|
||||
D3Dmaterial8.Diffuse.g% = FN_f4(0.6)
|
||||
D3Dmaterial8.Diffuse.b% = FN_f4(1.0)
|
||||
D3Dmaterial8.Specular.r% = FN_f4(0.4)
|
||||
D3Dmaterial8.Specular.g% = FN_f4(0.4)
|
||||
D3Dmaterial8.Specular.b% = FN_f4(0.4)
|
||||
D3Dmaterial8.Power% = FN_f4(100)
|
||||
|
||||
fovy = RAD(30)
|
||||
aspect = 5/4
|
||||
znear = 1
|
||||
zfar = 1000
|
||||
bkgnd% = &7F7F7F
|
||||
eyepos%() = 0, 0, FN_f4(6)
|
||||
lookat%() = 0, 0, 0
|
||||
up%() = 0, FN_f4(1), 0
|
||||
|
||||
SYS D3Ddevice8.Clear%, pDevice%, 0, 0, 3, bkgnd%, FN_f4(1), 0
|
||||
SYS D3Ddevice8.BeginScene%, pDevice%
|
||||
SYS D3Ddevice8.SetLight%, pDevice%, 0, D3Dlight8{}
|
||||
SYS D3Ddevice8.LightEnable%, pDevice%, 0, 1
|
||||
SYS D3Ddevice8.SetMaterial%, pDevice%, D3Dmaterial8{}
|
||||
SYS D3Ddevice8.SetRenderState%, pDevice%, D3DRS_SPECULARENABLE, 1
|
||||
|
||||
SYS `D3DXMatrixLookAtLH`, ^mat%(0,0), ^eyepos%(0), ^lookat%(0), ^up%(0)
|
||||
SYS D3Ddevice8.SetTransform%, pDevice%, D3DTS_VIEW, ^mat%(0,0)
|
||||
|
||||
SYS `D3DXMatrixPerspectiveFovLH`, ^mat%(0,0), FN_f4(fovy), \
|
||||
\ FN_f4(aspect), FN_f4(znear), FN_f4(zfar)
|
||||
SYS D3Ddevice8.SetTransform%, pDevice%, D3DTS_PROJECTION, ^mat%(0,0)
|
||||
|
||||
SYS D3Dbasemesh8.DrawSubset%, meshSphere%, 0
|
||||
SYS D3Ddevice8.EndScene%, pDevice%
|
||||
SYS D3Ddevice8.Present%, pDevice%, 0, 0, 0, 0
|
||||
|
||||
SYS D3Ddevice8.Release%, pDevice%
|
||||
SYS D3Dbasemesh8.Release%, meshSphere%
|
||||
SYS "FreeLibrary", d3dx%
|
||||
END
|
||||
|
|
@ -1,162 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
#define MAXD 8
|
||||
int g[] = { -1, 1, -1, 1 };
|
||||
/* Perlin-like noise */
|
||||
inline void
|
||||
hashed(int *data, int *out, int len) {
|
||||
# define ror(a, d) ((a << (d)) | (a >> (32 - d)))
|
||||
register unsigned int h = 0x12345678, tmp;
|
||||
unsigned int *d = (void*)data;
|
||||
int i = len;
|
||||
|
||||
while (i--) {
|
||||
tmp = *d++;
|
||||
h += ror(h, 15) ^ ror(tmp, 5);
|
||||
}
|
||||
|
||||
h ^= ror(h, 7);
|
||||
h += ror(h, 23);
|
||||
h ^= ror(h, 19);
|
||||
h += ror(h, 11);
|
||||
h ^= ror(h, 13);
|
||||
h += ror(h, 17);
|
||||
# undef ror
|
||||
for (i = len; i--; ) {
|
||||
out[i] = g[h & 3];
|
||||
h >>= 2;
|
||||
}
|
||||
}
|
||||
|
||||
double scale[MAXD], scale_u[MAXD];
|
||||
void noise_init()
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i < MAXD; i++) {
|
||||
scale[i] = 1 / (1 + sqrt(i + 1));
|
||||
scale_u[i] = scale[i] / sqrt(i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
double noise(double *x, int d)
|
||||
{
|
||||
# define sum(s, x) for (s = 0, j = 0; j < d; j++) s += x
|
||||
register int i, j;
|
||||
int n[MAXD], o[MAXD], g[MAXD], tmp;
|
||||
double s, r, t, w, ret, u[MAXD];
|
||||
|
||||
sum(s, x[j]);
|
||||
s *= scale[d];
|
||||
|
||||
for (i = 0; i < d; i++) {
|
||||
o[i] = i;
|
||||
t = x[i] + s;
|
||||
u[i] = t - (n[i] = floor(t));
|
||||
}
|
||||
o[d] = 0;
|
||||
|
||||
for (i = 0; i < d - 1; i++)
|
||||
for (j = i; j < d; j++)
|
||||
if (u[o[i]] < u[o[j]])
|
||||
tmp = o[i], o[i] = o[j], o[j] = tmp;
|
||||
|
||||
ret = w = 0, r = 1;
|
||||
for (s = 0, j = 0; j < d; j++) s += n[j];
|
||||
s *= scale_u[d];
|
||||
|
||||
for (i = 0; i <= d; i++) {
|
||||
for (j = 0; j < d; j++)
|
||||
u[j] = x[j] + s - n[j];
|
||||
|
||||
for (t = (d + 1.) / (2 * d), j = 0; j < d; j++) {
|
||||
t -= u[j] * u[j];
|
||||
if (t <= 0) break;
|
||||
}
|
||||
|
||||
if (t >= 0) {
|
||||
r = 0;
|
||||
hashed(n, g, d);
|
||||
for (j = 0; j < d; j++)
|
||||
if (g[j]) r += (g[j] == 1 ? u[j] : -u[j]);
|
||||
t *= t;
|
||||
ret += r * t * t;
|
||||
}
|
||||
|
||||
if (i < d) {
|
||||
n[o[i]]++;
|
||||
s += scale_u[d];
|
||||
}
|
||||
}
|
||||
return ret * (d * d);
|
||||
}
|
||||
|
||||
double get_noise2(double x, double y)
|
||||
{
|
||||
int i, ws;
|
||||
double r = 0, v[2];
|
||||
|
||||
for (i = 1, ws = 0; i <= 128; i <<= 1) {
|
||||
v[0] = x * i, v[1] = y * i;
|
||||
r += noise(v, 2);
|
||||
ws ++;
|
||||
}
|
||||
r /= ws;
|
||||
return r;
|
||||
}
|
||||
|
||||
double get_noise3(double x, double y, double z)
|
||||
{
|
||||
int i, ws;
|
||||
double r = 0, v[3], w;
|
||||
|
||||
for (i = 1, ws = 0; i <= 32; i <<= 1) {
|
||||
v[0] = x * i, v[1] = y * i, v[2] = z * i;
|
||||
w = 1./sqrt(i);
|
||||
r += noise(v, 3) * w;
|
||||
ws += w;
|
||||
}
|
||||
return r / ws;
|
||||
}
|
||||
|
||||
|
||||
int main(int c, char** v)
|
||||
{
|
||||
unsigned char pix[256 * 256], *p;
|
||||
int i, j;
|
||||
double x, y, z, w;
|
||||
FILE *fp;
|
||||
|
||||
noise_init();
|
||||
|
||||
for (p = pix, i = 0; i < 256 * 256; i++) *p++ = 0;
|
||||
|
||||
for (p = pix, i = 0; i < 256; i++) {
|
||||
y = (i - 128) / 125.;
|
||||
for (j = 0; j < 256; j++, p++) {
|
||||
x = (j - 128) / 125.;
|
||||
*p = (get_noise2(i/256., j/256.) + 1) / 6 * i;
|
||||
|
||||
z = 1- x*x - y*y;
|
||||
if (z < 0) continue;
|
||||
|
||||
z = sqrt(z);
|
||||
|
||||
w = get_noise3(x, y, z);
|
||||
|
||||
w = (w + 1) / 2;
|
||||
w *= (1 + x - y + z) / 3.5;
|
||||
if (w < 0) w = 0;
|
||||
|
||||
*p = w * 255;
|
||||
}
|
||||
}
|
||||
|
||||
fp = fopen("out.pgm", "w+");
|
||||
fprintf(fp, "P5\n256 256\n255\n");
|
||||
fwrite(pix, 1, 256 * 256, fp);
|
||||
fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
29
Task/Draw-a-sphere/Julia/draw-a-sphere.julia
Normal file
29
Task/Draw-a-sphere/Julia/draw-a-sphere.julia
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# v0.6
|
||||
|
||||
function draw_sphere(r, k, ambient, light)
|
||||
shades = ('.', ':', '!', '*', 'o', 'e', '&', '#', '%', '@')
|
||||
for i in floor(Int, -r):ceil(Int, r)
|
||||
x = i + 0.5
|
||||
line = IOBuffer()
|
||||
for j in floor(Int, -2r):ceil(2r)
|
||||
y = j / 2 + 0.5
|
||||
if x ^ 2 + y ^ 2 ≤ r ^ 2
|
||||
v = normalize([x, y, sqrt(r ^ 2 - x ^ 2 - y ^ 2)])
|
||||
b = dot(light, v) ^ k + ambient
|
||||
intensity = ceil(Int, (1 - b) * (length(shades) - 1))
|
||||
if intensity < 1
|
||||
intensity = 1 end
|
||||
if intensity > length(shades)
|
||||
intensity = length(shades) end
|
||||
print(shades[intensity])
|
||||
else
|
||||
print(' ')
|
||||
end
|
||||
end
|
||||
println()
|
||||
end
|
||||
end
|
||||
|
||||
light = normalize([30, 30, -50])
|
||||
draw_sphere(20, 4, 0.1, light)
|
||||
draw_sphere(10, 2, 0.4, light)
|
||||
47
Task/Draw-a-sphere/Kotlin/draw-a-sphere.kotlin
Normal file
47
Task/Draw-a-sphere/Kotlin/draw-a-sphere.kotlin
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// version 1.0.6
|
||||
|
||||
const val shades = ".:!*oe&#%@"
|
||||
val light = doubleArrayOf(30.0, 30.0, -50.0)
|
||||
|
||||
fun normalize(v: DoubleArray) {
|
||||
val len = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])
|
||||
v[0] /= len; v[1] /= len; v[2] /= len
|
||||
}
|
||||
|
||||
fun dot(x: DoubleArray, y: DoubleArray): Double {
|
||||
val d = x[0] * y[0] + x[1] * y[1] + x[2] * y[2]
|
||||
return if (d < 0.0) -d else 0.0
|
||||
}
|
||||
|
||||
fun drawSphere(r: Double, k: Double, ambient: Double) {
|
||||
val vec = DoubleArray(3)
|
||||
var intensity: Int
|
||||
var b : Double
|
||||
var x: Double
|
||||
var y: Double
|
||||
for (i in Math.floor(-r).toInt() .. Math.ceil(r).toInt()) {
|
||||
x = i + 0.5
|
||||
for (j in Math.floor(-2.0 * r).toInt() .. Math.ceil(2.0 * r).toInt()) {
|
||||
y = j / 2.0 + 0.5
|
||||
if (x * x + y * y <= r * r) {
|
||||
vec[0] = x
|
||||
vec[1] = y
|
||||
vec[2] = Math.sqrt(r * r - x * x - y * y)
|
||||
normalize(vec)
|
||||
b = Math.pow(dot(light, vec), k) + ambient
|
||||
intensity = ((1.0 - b) * (shades.length - 1)).toInt()
|
||||
if (intensity < 0) intensity = 0
|
||||
if (intensity >= shades.length - 1) intensity = shades.length - 2
|
||||
print(shades[intensity])
|
||||
}
|
||||
else print(' ')
|
||||
}
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
normalize(light)
|
||||
drawSphere(20.0, 4.0, 0.1)
|
||||
drawSphere(10.0, 2.0, 0.4)
|
||||
}
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
WindowWidth =420
|
||||
WindowHeight =460
|
||||
|
||||
nomainwin
|
||||
|
||||
open "Sphere" for graphics_nsb_nf as #w
|
||||
|
||||
#w "down ; fill lightgray"
|
||||
|
||||
xS =200
|
||||
yS =200
|
||||
for radius =150 to 0 step -1
|
||||
level$ =str$( int( 256 -256 *radius /150))
|
||||
c$ =level$ +" " +level$ +" " +level$
|
||||
#w "color "; c$
|
||||
#w "backcolor "; c$
|
||||
#w "place "; xS; " "; yS
|
||||
xS =xS -0.5
|
||||
yS =yS -0.2
|
||||
#w "circlefilled "; radius
|
||||
next radius
|
||||
|
||||
#w "flush"
|
||||
wait
|
||||
close #w
|
||||
end
|
||||
|
|
@ -6,8 +6,8 @@ my @light = normalize([ 3, 2, -5 ]);
|
|||
my $depth = 255;
|
||||
|
||||
sub MAIN ($outfile = 'sphere-perl6.pgm') {
|
||||
my $out = open( $outfile, :w, :bin ) or die "$!\n";
|
||||
$out.say("P5\n$x $y\n$depth"); # .pgm header
|
||||
spurt $outfile, "P5\n$x $y\n$depth\n"; # .pgm header
|
||||
my $out = open( $outfile, :a, :bin ) or die "$!\n";
|
||||
$out.write( Blob.new(draw_sphere( ($x-1)/2, .9, .2) ) );
|
||||
$out.close;
|
||||
}
|
||||
|
|
|
|||
77
Task/Draw-a-sphere/Phix/draw-a-sphere.phix
Normal file
77
Task/Draw-a-sphere/Phix/draw-a-sphere.phix
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
--
|
||||
-- demo\rosetta\Draw_a_sphere.exw
|
||||
--
|
||||
include pGUI.e
|
||||
|
||||
Ihandle dlg, canvas
|
||||
cdCanvas cddbuffer, cdcanvas
|
||||
|
||||
function dot(sequence x, sequence y)
|
||||
return sum(sq_mul(x,y))
|
||||
end function
|
||||
|
||||
function normalize(sequence v)
|
||||
atom len = sqrt(dot(v, v))
|
||||
if len=0 then return {0,0,0} end if
|
||||
return sq_mul(v,1/len)
|
||||
end function
|
||||
|
||||
procedure drawSphere(integer width, height, atom k, atom amb, sequence direction)
|
||||
integer r = floor((min(width,height)-20)/2)
|
||||
integer cx = floor(width/2)
|
||||
integer cy = floor(height/2)
|
||||
integer lum
|
||||
for x=-r to r do
|
||||
for y=-r to r do
|
||||
integer z = r*r-x*x-y*y
|
||||
if z>=0 then
|
||||
atom s = dot(direction, normalize({x,y,sqrt(z)}))
|
||||
lum = and_bits(#FF,255*(power(iff(s<0?0:s),k)+amb)/(1+amb))
|
||||
lum += lum*#100+lum*#10000
|
||||
cdCanvasPixel(cddbuffer, x+cx, y+cy, lum)
|
||||
end if
|
||||
end for
|
||||
end for
|
||||
end procedure
|
||||
|
||||
function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)
|
||||
integer {width, height} = IupGetIntInt(canvas, "DRAWSIZE")
|
||||
cdCanvasActivate(cddbuffer)
|
||||
cdCanvasClear(cddbuffer)
|
||||
drawSphere(width,height,1.5,0.2,normalize({-30,-30,50}))
|
||||
cdCanvasFlush(cddbuffer)
|
||||
return IUP_DEFAULT
|
||||
end function
|
||||
|
||||
function map_cb(Ihandle ih)
|
||||
cdcanvas = cdCreateCanvas(CD_IUP, ih)
|
||||
cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
|
||||
cdCanvasSetBackground(cddbuffer, CD_BLACK)
|
||||
return IUP_DEFAULT
|
||||
end function
|
||||
|
||||
function esc_close(Ihandle /*ih*/, atom c)
|
||||
if c=K_ESC then return IUP_CLOSE end if
|
||||
return IUP_CONTINUE
|
||||
end function
|
||||
|
||||
procedure main()
|
||||
IupOpen()
|
||||
|
||||
canvas = IupCanvas(NULL)
|
||||
IupSetAttribute(canvas, "RASTERSIZE", "340x340") -- initial size
|
||||
IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
|
||||
|
||||
dlg = IupDialog(canvas)
|
||||
IupSetAttribute(dlg, "TITLE", "Draw a sphere")
|
||||
IupSetCallback(dlg, "K_ANY", Icallback("esc_close"))
|
||||
IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
|
||||
|
||||
IupMap(dlg)
|
||||
IupSetAttribute(canvas, "RASTERSIZE", NULL) -- release the minimum limitation
|
||||
IupShowXY(dlg,IUP_CENTER,IUP_CENTER)
|
||||
IupMainLoop()
|
||||
IupClose()
|
||||
end procedure
|
||||
|
||||
main()
|
||||
|
|
@ -1,251 +0,0 @@
|
|||
; Original by Comtois @ 28/03/06
|
||||
;
|
||||
; Updated/Formated by Fluid Byte @ March.24,2009
|
||||
;
|
||||
; http://www.purebasic.fr/english/viewtopic.php?p=281258#p281258
|
||||
|
||||
Declare CreateSphere(M,P)
|
||||
Declare UpdateMesh()
|
||||
|
||||
#_SIZEVERT = 36
|
||||
#_SIZETRIS = 6
|
||||
#FULLSCREEN = 0
|
||||
|
||||
Structure VECTOR
|
||||
X.f
|
||||
Y.f
|
||||
Z.f
|
||||
EndStructure
|
||||
|
||||
Structure VERTEX
|
||||
X.f
|
||||
Y.f
|
||||
Z.f
|
||||
NX.f
|
||||
NY.f
|
||||
NZ.f
|
||||
Color.l
|
||||
U.f
|
||||
V.f
|
||||
EndStructure
|
||||
|
||||
Structure TRIANGLE
|
||||
V1.w
|
||||
V2.w
|
||||
V3.w
|
||||
EndStructure
|
||||
|
||||
Macro CALC_NORMALS
|
||||
*PtrV\NX = *PtrV\X
|
||||
*PtrV\NY = *PtrV\Y
|
||||
*PtrV\NZ = *PtrV\Z
|
||||
EndMacro
|
||||
|
||||
Global *VBuffer, *IBuffer
|
||||
Global Meridian = 50, Parallele = 50, PasLength = 4, Length
|
||||
|
||||
Define EventID, i, NbSommet, CameraMode, Angle.f, Pas.f = 0.5
|
||||
|
||||
InitEngine3D() : InitSprite() : InitKeyboard()
|
||||
|
||||
Add3DArchive(GetTemporaryDirectory(),#PB_3DArchive_FileSystem)
|
||||
Add3DArchive(#PB_Compiler_Home + "Examples\Sources\Data\",#PB_3DArchive_FileSystem)
|
||||
|
||||
If #FULLSCREEN
|
||||
OpenScreen(800,600,32,"Sphere 3D")
|
||||
Else
|
||||
OpenWindow(0,0,0,800,600,"Sphere 3D",#PB_Window_SystemMenu | 1)
|
||||
OpenWindowedScreen(WindowID(0),0,0,800,600,0,0,0)
|
||||
EndIf
|
||||
|
||||
;-Texture
|
||||
CreateImage(0,128,128)
|
||||
StartDrawing(ImageOutput(0))
|
||||
For i = 0 To 127 Step 4
|
||||
Box(0,i,ImageWidth(0),2,RGB(255,255,255))
|
||||
Box(0,i + 2,ImageWidth(0),2,RGB(0,0,155))
|
||||
Next i
|
||||
StopDrawing()
|
||||
SaveImage(0,GetTemporaryDirectory() + "temp.bmp") : FreeImage(0)
|
||||
|
||||
;-Material
|
||||
CreateMaterial(0,LoadTexture(0,"temp.bmp"))
|
||||
RotateMaterial(0,0.1,#PB_Material_Animated)
|
||||
|
||||
;-Mesh
|
||||
CreateSphere(Meridian,Parallele)
|
||||
|
||||
;-Entity
|
||||
CreateEntity(0,MeshID(0),MaterialID(0))
|
||||
ScaleEntity(0,60,60,60)
|
||||
|
||||
;-Camera
|
||||
CreateCamera(0,0,0,100,100)
|
||||
MoveCamera(0,0,0,-200)
|
||||
CameraLookAt(0,EntityX(0),EntityY(0),EntityZ(0))
|
||||
|
||||
;-Light
|
||||
AmbientColor(RGB(105, 105, 105))
|
||||
CreateLight(0, RGB(255, 255, 55), EntityX(0) + 150, EntityY(0) , EntityZ(0))
|
||||
CreateLight(1, RGB( 55, 255, 255), EntityX(0) - 150, EntityY(0) , EntityZ(0))
|
||||
CreateLight(2, RGB( 55, 55, 255), EntityX(0) , EntityY(0) + 150, EntityZ(0))
|
||||
CreateLight(3, RGB(255, 55, 255), EntityX(0) , EntityY(0) - 150, EntityZ(0))
|
||||
|
||||
; ----------------------------------------------------------------------------------------------------
|
||||
; MAINLOOP
|
||||
; ----------------------------------------------------------------------------------------------------
|
||||
|
||||
Repeat
|
||||
If #FULLSCREEN = 0
|
||||
Repeat
|
||||
EventID = WindowEvent()
|
||||
|
||||
Select EventID
|
||||
Case #PB_Event_CloseWindow : End
|
||||
EndSelect
|
||||
Until EventID = 0
|
||||
EndIf
|
||||
|
||||
Angle + Pas
|
||||
RotateEntity(0, Angle, Angle,Angle)
|
||||
|
||||
If PasLength > 0 : UpdateMesh() : EndIf
|
||||
|
||||
If ExamineKeyboard()
|
||||
If KeyboardReleased(#PB_Key_F1)
|
||||
CameraMode = 1 - CameraMode
|
||||
CameraRenderMode(0, CameraMode)
|
||||
EndIf
|
||||
EndIf
|
||||
|
||||
RenderWorld()
|
||||
FlipBuffers()
|
||||
Until KeyboardPushed(#PB_Key_Escape)
|
||||
|
||||
; ----------------------------------------------------------------------------------------------------
|
||||
; FUNCTIONS
|
||||
; ----------------------------------------------------------------------------------------------------
|
||||
|
||||
Procedure CreateSphere(M,P)
|
||||
; M = Meridian
|
||||
; P = Parallele
|
||||
; The radius is 1. Front to remove it later, it's just for the demo.
|
||||
|
||||
If M < 3 Or P < 2 : ProcedureReturn 0 : EndIf
|
||||
|
||||
Protected Normale.VECTOR, NbSommet, i, j, Theta.f, cTheta.f, sTheta.f
|
||||
Protected Alpha.f, cAlpha.f, sAlpha.f, *PtrV.VERTEX, *PtrF.TRIANGLE, NbTriangle
|
||||
|
||||
NbSommet = 2 + ((M + 1) * P)
|
||||
*VBuffer = AllocateMemory(#_SIZEVERT * Nbsommet)
|
||||
|
||||
For i = 0 To M
|
||||
Theta = i * #PI * 2.0 / M
|
||||
cTheta = Cos(theta)
|
||||
sTheta = Sin(theta)
|
||||
|
||||
For j = 1 To P
|
||||
Alpha = j * #PI / (P + 1)
|
||||
cAlpha = Cos(Alpha)
|
||||
sAlpha = Sin(Alpha)
|
||||
*PtrV = *VBuffer + #_SIZEVERT * ((i * P) + (j - 1))
|
||||
*PtrV\X = sAlpha * cTheta
|
||||
*PtrV\Y = sAlpha * sTheta
|
||||
*PtrV\Z = cAlpha
|
||||
*PtrV\U = Theta / (2.0 * #PI)
|
||||
*PtrV\V = Alpha / #PI
|
||||
CALC_NORMALS
|
||||
Next j
|
||||
Next i
|
||||
|
||||
; Southpole
|
||||
*PtrV = *VBuffer + #_SIZEVERT * ((M + 1) * P)
|
||||
*PtrV\X = 0
|
||||
*PtrV\Y = 0
|
||||
*PtrV\Z = -1
|
||||
*PtrV\U = 0
|
||||
*PtrV\V = 0
|
||||
CALC_NORMALS
|
||||
|
||||
; Northpole
|
||||
*PtrV + #_SIZEVERT
|
||||
*PtrV\X = 0
|
||||
*PtrV\Y = 0
|
||||
*PtrV\Z = 1
|
||||
*PtrV\U = 0
|
||||
*PtrV\V = 0
|
||||
CALC_NORMALS
|
||||
|
||||
; Les facettes
|
||||
NbTriangle = 4 * M * P
|
||||
*IBuffer = AllocateMemory(#_SIZETRIS * NbTriangle)
|
||||
*PtrF = *IBuffer
|
||||
|
||||
For i = 0 To M - 1
|
||||
For j = 1 To P - 1
|
||||
*PtrF\V1 = ((i + 1) * P) + j
|
||||
*PtrF\V2 = ((i + 1) * P) + (j - 1)
|
||||
*PtrF\V3 = (i * P) + (j - 1)
|
||||
*PtrF + #_SIZETRIS
|
||||
*PtrF\V3 = ((i + 1) * P) + j ;Recto
|
||||
*PtrF\V2 = ((i + 1) * P) + (j - 1) ;Recto
|
||||
*PtrF\V1 = (i * P) + (j - 1) ;Recto
|
||||
*PtrF + #_SIZETRIS
|
||||
*PtrF\V1 = i * P + j
|
||||
*PtrF\V2 = ((i + 1) * P) + j
|
||||
*PtrF\V3 = (i * P) + (j - 1)
|
||||
*PtrF + #_SIZETRIS
|
||||
*PtrF\V3 = i * P + j ;Recto
|
||||
*PtrF\V2 = ((i + 1) * P) + j ;Recto
|
||||
*PtrF\V1 = (i * P) + (j - 1) ;Recto
|
||||
*PtrF + #_SIZETRIS
|
||||
Next j
|
||||
Next i
|
||||
|
||||
; The Poles
|
||||
For i = 0 To M - 1
|
||||
*PtrF\V3 = (M + 1) * P + 1
|
||||
*PtrF\V2 = (i + 1) * P
|
||||
*PtrF\V1 = i * P
|
||||
*PtrF + #_SIZETRIS
|
||||
*PtrF\V1 = (M + 1) * P + 1 ;Recto
|
||||
*PtrF\V2 = (i + 1) * P ;Recto
|
||||
*PtrF\V3 = i * P ;Recto
|
||||
*PtrF + #_SIZETRIS
|
||||
Next i
|
||||
|
||||
For i = 0 To M - 1
|
||||
*PtrF\V3 = (M + 1) * P
|
||||
*PtrF\V2 = i * P + (P - 1)
|
||||
*PtrF\V1 = (i + 1) * P + (P - 1)
|
||||
*PtrF + #_SIZETRIS
|
||||
*PtrF\V1 = (M + 1) * P ;Recto
|
||||
*PtrF\V2 = i * P + (P - 1) ;Recto
|
||||
*PtrF\V3 = (i + 1) * P + (P - 1) ;Recto
|
||||
*PtrF + #_SIZETRIS
|
||||
Next i
|
||||
|
||||
If CreateMesh(0,100)
|
||||
Protected Flag = #PB_Mesh_Vertex | #PB_Mesh_Normal | #PB_Mesh_UVCoordinate | #PB_Mesh_Color
|
||||
SetMeshData(0,Flag,*VBuffer,NbSommet)
|
||||
SetMeshData(0,#PB_Mesh_Face,*IBuffer,NbTriangle)
|
||||
ProcedureReturn 1
|
||||
EndIf
|
||||
|
||||
ProcedureReturn 0
|
||||
EndProcedure
|
||||
|
||||
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
|
||||
Procedure UpdateMesh()
|
||||
Protected NbTriangle = 4 * Meridian * Parallele
|
||||
|
||||
Length + PasLength
|
||||
|
||||
If Length >= NbTriangle
|
||||
PasLength = 0
|
||||
Length = Nbtriangle
|
||||
EndIf
|
||||
|
||||
SetMeshData(0,#PB_Mesh_Face,*IBuffer,Length)
|
||||
EndProcedure
|
||||
|
|
@ -12,21 +12,22 @@ drawSphere: procedure; parse arg r, k, ambient /*get the arguments
|
|||
else shading= "·:!°oe@░▒▓" /* ASCII " " */
|
||||
lightSource= '30 30 -50' /*position of light source.*/
|
||||
parse value norm(lightSource) with s1 s2 s3 /*normalize light source. */
|
||||
sLen=length(shading)-1; rr=r*r /*handy─dandy variables. */
|
||||
shadeLen=length(shading) - 1; rr=r**2; r2=r+r /*handy─dandy variables. */
|
||||
|
||||
do i=floor(-r) to ceil(r) ; x= i+.5; xx=x**2; $=
|
||||
do j=floor(-2*r) to ceil(r+r); y=j/2+.5; yy=y**2
|
||||
do i=floor( -r) to ceil( r); x=i + .5; xx=x**2; $=
|
||||
do j=floor(-r2) to ceil(r2); y=j * .5 + .5; yy=y**2
|
||||
if xx+yy<=rr then do /*is point within sphere ? */
|
||||
parse value norm(x y sqrt(rr-xx-yy)) with v1 v2 v3
|
||||
dot=s1*v1 + s2*v2 + s3*v3 /*the dot product of the Vs*/
|
||||
parse value norm(x y sqrt(rr-xx-yy) ) with v1 v2 v3
|
||||
dot=s1*v1 + s2*v2 + s3*v3 /*the dot product of the Vs*/
|
||||
if dot>0 then dot=0 /*if positive, make it zero*/ /*◄■■■■ same as: dot=max(0, dot) */
|
||||
if dot>0 then dot=0 /*if positive, make it zero*/
|
||||
b=abs(dot)**k + ambient /*calculate the brightness.*/
|
||||
if b<=0 then brite=sLen
|
||||
else brite=trunc( max( (1-b) * sLen, 0) )
|
||||
$=($)substr(shading,brite+1,1) /*construct a display line.*/
|
||||
end
|
||||
b=-dot**k + ambient /*calculate the brightness.*/
|
||||
if b<=0 then brite=shadeLen
|
||||
else brite=max( (1-b) * shadeLen, 0) % 1
|
||||
$=($)substr(shading, brite + 1, 1)
|
||||
end /* [↑] build display line.*/
|
||||
else $=$' ' /*append a blank to line. */
|
||||
end /*j*/
|
||||
end /*j*/ /*[↓] strip trailing blanks*/
|
||||
say strip($, 'T') /*show a line of the sphere*/
|
||||
end /*i*/ /* [↑] display the sphere.*/
|
||||
return
|
||||
|
|
|
|||
|
|
@ -1,4 +0,0 @@
|
|||
graphic #g, 300, 300 'create a graphic object
|
||||
#g place(100,100) 'place the drawing pen at 100,100
|
||||
#g circle(75) 'make a circle with radius 75
|
||||
render #g 'show it
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
func normalize (vec) { vec »/» (vec »*« vec -> sum.sqrt) }
|
||||
func dot (x, y) { -(x »*« y -> sum) ^max^ 0 }
|
||||
func dot (x, y) { -(x »*« y -> sum) `max` 0 }
|
||||
|
||||
var x = var y = 255
|
||||
x += 1 if x.is_even # must be odd
|
||||
|
|
@ -15,7 +15,7 @@ func draw_sphere(rad, k, ambient) {
|
|||
if ((var x2 = x*x) + (var y2 = y*y) < r2) {
|
||||
var vector = normalize([x, y, (r2 - x2 - y2).sqrt])
|
||||
var intensity = (dot(light, vector)**k + ambient)
|
||||
var pixel = (0 ^max^ (intensity*depth -> int) ^min^ depth)
|
||||
var pixel = (0 `max` (intensity*depth -> int) `min` depth)
|
||||
pixels << pixel
|
||||
}
|
||||
else {
|
||||
|
|
@ -29,5 +29,5 @@ var outfile = %f'sphere-sidef.pgm'
|
|||
var out = outfile.open('>:raw')
|
||||
|
||||
out.say("P5\n#{x} #{y}\n#{depth}") # .pgm header
|
||||
out.write(draw_sphere((x-1)/2, .9, .2).map{.chr}.join)
|
||||
out.print(draw_sphere((x-1)/2, .9, .2).map{.chr}.join)
|
||||
out.close
|
||||
|
|
|
|||
15
Task/Draw-a-sphere/Zkl/draw-a-sphere-1.zkl
Normal file
15
Task/Draw-a-sphere/Zkl/draw-a-sphere-1.zkl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
img:=PPM(640,480);
|
||||
R:=100; R2:=R*R; //radius, in pixels; radius squared
|
||||
X0:=640/2; Y0:=480/2; //coordinates of center of screen
|
||||
foreach Y in ([-R..R]){ //for all the coordinates near the circle
|
||||
foreach X in ([-R..R]){ // which is under the sphere
|
||||
D2:=X*X + Y*Y;
|
||||
C:=0; //default color is black
|
||||
if(D2<=R2){ //coordinate is inside circle under sphere
|
||||
Z:=(R2-D2).toFloat().sqrt();//height of point on surface of sphere above X,Y
|
||||
C=0x82+Z-(X+Y)/2; //color is proportional; offset X and Y, and
|
||||
} // shift color to upper limit of its range
|
||||
img[X+X0,Y+Y0]=C.shiftLeft(8)+C; //green + blue = cyan
|
||||
}
|
||||
}
|
||||
img.write(File("foo.ppm","wb"));
|
||||
1
Task/Draw-a-sphere/Zkl/draw-a-sphere-2.zkl
Normal file
1
Task/Draw-a-sphere/Zkl/draw-a-sphere-2.zkl
Normal file
|
|
@ -0,0 +1 @@
|
|||
img[X+X0,Y+Y0]=C*140+C;
|
||||
23
Task/Draw-a-sphere/Zkl/draw-a-sphere-3.zkl
Normal file
23
Task/Draw-a-sphere/Zkl/draw-a-sphere-3.zkl
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#<<<
|
||||
cmd:=0'|
|
||||
set term wxt
|
||||
set parametric
|
||||
set urange [0:pi]
|
||||
set vrange [0:2*pi]
|
||||
set xyplane 0
|
||||
set view equal xyz
|
||||
set pm3d hidden3d 100 depthorder
|
||||
set style line 100 lt 7 lw 0.1
|
||||
set palette defined (0 "dark-blue", 1 "light-blue")
|
||||
unset key
|
||||
set samples 24
|
||||
set isosamples 36
|
||||
set title 'sphere (pm3d)' font "Times,20"
|
||||
R = 3
|
||||
splot R*sin(u)*cos(v), R*sin(u)*sin(v), R*cos(u) w pm3d
|
||||
|;
|
||||
#<<<
|
||||
|
||||
gnuplot:=System.popen("gnuplot","w");
|
||||
gnuplot.write(cmd); gnuplot.flush();
|
||||
ask("Hit return to finish"); gnuplot.close();
|
||||
Loading…
Add table
Add a link
Reference in a new issue