_TITLE "Demo of the Versatile _PUTIMAGE by harixxx"
'loadTexture "picture1.jpg", texture&(), imgWidth, imgHeight
loadTexture "qb64icon.bmp", texture&(), imgWidth, imgHeight
DIM grid
(imgWidth
, imgWidth
) AS point2d
DIM vec2d
(points
) AS point2d
, vec3d
(points
) AS point3d
READ vec3d
(i
).x
, vec3d
(i
).y
, vec3d
(i
).z
DIM cubeside
(faces
) AS face4points
READ cubeside
(i
).a
, cubeside
(i
).b
, cubeside
(i
).c
, cubeside
(i
).d
'points data
'3d points for cube (8 points x,y,z)
data 100,100,100, -100,100,100, -100,100,-100, 100,100,-100 data 100,-100,100, -100,-100,100, -100,-100,-100, 100,-100,-100 '4 points face connection for cube (6 sides a,b,c,d)
data 1,3,2,4, 1,8,4,5, 8,6,7,5, 6,3,7,2, 3,8,7,4, 1,6,5,2
'update rotation
zoom
= (zoom
+ 1) MOD 360 '3d to 2d rotation
x = vec3d(i).x
y = vec3d(i).y
z = vec3d(i).z
rot3dto2d x, y, z, xr, yr, zr, nx, ny, centerx, centery, sinT(zoom)
vec2d(i).x = nx
vec2d(i).y = ny
'3d cube setup
x1 = vec2d(cubeside(i).a).x: y1 = vec2d(cubeside(i).a).y
x2 = vec2d(cubeside(i).b).x: y2 = vec2d(cubeside(i).b).y
x3 = vec2d(cubeside(i).c).x: y3 = vec2d(cubeside(i).c).y
x4 = vec2d(cubeside(i).d).x: y4 = vec2d(cubeside(i).d).y
'find the middle of convex 3d vectors
IF convex3d
(x1
, y1
, x2
, y2
, x3
, y3
) >= 0 THEN sx1 = (x2 - x3) / imgWidth
sy1 = (y2 - y3) / imgHeight
sx2 = (x4 - x1) / imgWidth
sy2 = (y4 - y1) / imgHeight
'grids setup
'grid steps size
tx1 = sx1 * x + vec2d(cubeside(i).c).x
ty1 = sy1 * x + vec2d(cubeside(i).c).y
tx2 = (sx2 * x + vec2d(cubeside(i).a).x - tx1) / imgWidth
ty2 = (sy2 * x + vec2d(cubeside(i).a).y - ty1) / imgHeight
grid(x, y).x = tx2 * y + tx1
grid(x, y).y = ty2 * y + ty1
'draw 3d cube
FOR y
= 0 TO imgHeight
- 1 FOR x
= 0 TO imgWidth
- 1 x1 = grid(x, y).x: y1 = grid(x, y).y
x2 = grid(x, y + 1).x: y2 = grid(x, y + 1).y
x3 = grid(x + 1, y + 1).x: y3 = grid(x + 1, y + 1).y
x4 = grid(x + 1, y).x: y4 = grid(x + 1, y).y
'draw 1 quadrangle polygon by connecting 2 triangles polygon
triangle x1, y1, x2, y2, x3, y3, texture&(x, y)
triangle x1, y1, x4, y4, x3, y3, texture&(x, y)
'================= sines & cosines table
sinT
= SIN(3.141593 / 180 * n
)cosT
= COS(3.141593 / 180 * n
)
'======================== convex 3d
'routines to find middle of convex 3d vectors
'(http://en.wikipedia.org/wiki/Convex_function)
FUNCTION convex3d
(x1
, y1
, x2
, y2
, x3
, y3
) convex3d = (x1 - x2) * (y3 - y2) - (x3 - x2) * (y1 - y2)
'======================== 3d to 2d rotation
'thank's to Sami Kyostila 12-24-1997 for 3d to 2d rotation
'but i don't know who the first creator for this code :D
SUB rot3dto2d
(x
, y
, z
, rx
, ry
, rz
, nx
, ny
, cx
, cy
, sz
) x1 = x: y1 = y: z1 = z
xx = x1
yy = y1 * cosT(rx) + z1 * sinT(rx)
zz = z1 * cosT(rx) - y1 * sinT(rx)
y1 = yy
x1 = xx * cosT(ry) - zz * sinT(ry)
z1 = xx * sinT(ry) + zz * cosT(ry)
zz = z1
xx = x1 * cosT(rz) - y1 * sinT(rz)
yy = x1 * sinT(rz) + y1 * cosT(rz)
fc = zz / 500 + 1
nx = xx / fc * sz * 1.2 + cx
ny = yy / fc * sz * 1.2 + cy
'============================= make texture from file
'make REDIM array as long before calling this routine
'because array updated and contain of 32bits color
'example: 'REDIM texture&(0, 0)' or 'REDIM texture(0, 0) as long'
SUB loadTexture
(imageFile$
, textureName&
(), imgWidth
, imgHeight
) REDIM textureName&
(imgWidth
, imgHeight
) textureName&
(x
, y
) = POINT(x
, y
)
'=============================== solid triangle
'/------------ my solid triangle v1.1b
SUB triangle
(x1%
, y1%
, x2%
, y2%
, x3%
, y3%
, c&
) px1 = x1%: px2 = x2%: px3 = x3%
py1 = y1%: py2 = y2%: py3 = y3%
'(thank's to relminator for 'sort y' correction
' i see your comment on the mono&disco module)
'sort y
'alpha
A = px1: B = px1: C = px2
'delta
dA = (px1 - px3) / (py1 - py3)
dB = (px1 - px2) / (py1 - py2)
dC = (px2 - px3) / (py2 - py3)
'wow! your triangle shading close to mine
'(thank's to wiki for trigonometry
' http://en.wikipedia.org/wiki/Triangle#Using_trigonometry)
'triangle shading
xx1% = A
IF y%
< py2
THEN xx2%
= B: B
= B
+ dB
ELSE xx2%
= C: C
= C
+ dC
LINE (xx1%
, y%
)-(xx2%
, y%
), c&
A = A + dA