Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
3D how to
#1
I would like to put together a write up that explains how the 3D display works as I use it. Through an example program, I thought I would describe in detail what it does. After I put together a simple program, I think a lot of things are understandable. I left out all the unnecessary stuff. I will show more detailed writing and explanations later.

Code: (Select All)
'create texture
shadows = 100
DIM texture(shadows - 1)
text_size = 100
FOR at = 0 TO shadows - 1
    temp = _NEWIMAGE(text_size, text_size, 32)
    _DEST temp
    grey = 255 - 252 / (shadows - 1) * at
    COLOR _RGB(grey, grey, grey)
    CIRCLE (text_size / 2, text_size / 2), text_size * .45
    PAINT (text_size / 2, text_size / 2)
    texture(at) = _COPYIMAGE(temp, 33)
    _FREEIMAGE temp
NEXT at

'create 3D points in a spherical shape
points_c = 3000
space_size = 1000
DIM points(points_c - 1, 2)
FOR ap = 0 TO points_c - 1
    DO
        points(ap, 0) = space_size * RND
        points(ap, 1) = space_size * RND
        points(ap, 2) = space_size * RND
    LOOP WHILE SQR((points(ap, 0) - space_size / 2) ^ 2 + (points(ap, 1) - space_size / 2) ^ 2 + (points(ap, 2) - space_size / 2) ^ 2) > space_size / 2
NEXT ap

'create spectator
DIM SHARED sp(6)
sp(0) = space_size / 2 'X to center space
sp(1) = space_size / 2 'Y to center space
sp(2) = space_size / 2 'Z to center space
sp(3) = 0 'looking in the direction of the observer XZ
sp(4) = 0 'looking in the direction of the observer YZ
sp(5) = 1 'multiplier X-Y see
sp(6) = 1 'multiplier Z see

'create screen
scr = _NEWIMAGE(1000, 1000 / _DESKTOPWIDTH * _DESKTOPHEIGHT, 32)
SCREEN scr
_MOUSEHIDE
_FULLSCREEN
_DEST scr
_DISPLAYORDER _HARDWARE , _SOFTWARE
PRINT "turn with the mouse, move with the mouse buttons, adjust the light with the mouse wheel!"

DO
    _LIMIT 50

    'draw points
    FOR ap = 0 TO points_c - 1
        x = points(ap, 0)
        y = points(ap, 1)
        z = points(ap, 2)
        rotate_to_maptriangle x, y, z 'position of points from the point of view of the observer

        actual_shadow = INT(ABS(z) * (.3 + brightness)) 'distance proportional texture
        IF actual_shadow > shadows - 1 THEN actual_shadow = shadows - 1
        IF actual_shadow < 0 THEN actual_shadow = 0

        ps = 2 'point size on the screen
        _MAPTRIANGLE (0, 0)-(text_size - 1, 0)-(0, text_size - 1), texture(actual_shadow) TO(x - ps, y - ps, z)-(x + ps, y - ps, z)-(x - ps, y + ps, z)
        _MAPTRIANGLE (text_size - 1, text_size - 1)-(text_size - 1, 0)-(0, text_size - 1), texture(actual_shadow) TO(x + ps, y + ps, z)-(x + ps, y - ps, z)-(x - ps, y + ps, z)

    NEXT ap
    _DISPLAY


    'mouse input axis movement and mousewheel
    mousex = mousex * .6
    mousey = mousey * .6
    mw = 0
    WHILE _MOUSEINPUT: mousex = mousex + _MOUSEMOVEMENTX: mousey = mousey + _MOUSEMOVEMENTY: mw = mw + _MOUSEWHEEL: WEND 'movement data read

    'control spectator
    mouse_sens = .001 'mouse rotating sensitive
    sp(3) = sp(3) - mousex * mouse_sens
    sp(4) = sp(4) + mousey * mouse_sens
    IF ABS(sp(4)) > _PI / 2 THEN sp(4) = _PI / 2 * SGN(sp(4))
    vec_x = (SIN(sp(3)) * (COS(sp(4) + _PI)))
    vec_y = (COS(sp(3)) * (COS(sp(4) + _PI)))
    vec_z = -SIN(sp(4) + _PI)
    speed = 2 'moving speed
    moving = ABS(_MOUSEBUTTON(1) OR _KEYDOWN(ASC("w"))) * speed - ABS(_MOUSEBUTTON(2) OR _KEYDOWN(ASC("s"))) * speed
    sp(0) = sp(0) + vec_x * moving
    sp(1) = sp(1) + vec_y * moving
    sp(2) = sp(2) + vec_z * moving

    'control brightness
    brightness = brightness + mw / 50


LOOP UNTIL _KEYDOWN(27)

SUB rotate_to_maptriangle (x, y, z)
    x2 = x - sp(0)
    y2 = y - sp(1)
    z2 = z - sp(2)
    rotate_2d x2, y2, sp(3)
    rotate_2d y2, z2, sp(4) + _PI / 2
    x = x2 * sp(5)
    y = y2 * sp(5)
    z = z2 * sp(6)
END SUB

SUB rotate_2d (x, y, ang)
    x1 = x * COS(ang) - y * SIN(ang)
    y1 = x * SIN(ang) + y * COS(ang)
    x = x1: y = y1
END SUB
Reply
#2
This is amazing, thank you.

(I need to learn what _MapTriangle is all about)

Edit: I see there is a nice explanation about it here: https://qb64sourcecode.com/task19.html
Reply
#3
(09-16-2022, 10:49 PM)james2464 Wrote: This is amazing, thank you.

(I need to learn what _MapTriangle is all about)

Edit:  I see there is a nice explanation about it here:  https://qb64sourcecode.com/task19.html

Yeah, _MAPTRIANGLE can be strange at first, especially the 3D aspect.

Thanks for pointing out the lesson I created. Here is the updated lesson on the new tutorial site:

https://www.qb64tutorial.com/lesson17

I go into even more depth in the new lessons.

Just a sneak peek. The new tutorial site is almost finished. Maybe two more weeks. The asset file is NOT up to date so don't download that yet.
Reply
#4
An amazing example of 3D using _MapTriangle!

It is fine to navigate out of sphere and watch at it from there.
Reply
#5
Hey I must return to school!
Thanks James2464 to point to the new lessons of Gamecoding made by TerryRitchie
Thanks to my teacher for developing more lessons with more details and tips. I have followed his course at the time of QB64 SDL maybe 0.95
Reply




Users browsing this thread: 1 Guest(s)