News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Build a basic 3D Render Engine

Started by stevmjon, May 03, 2022, 10:25:50 PM

Previous topic - Next topic

kevin

#30

  Damn didn't know this post would push Steve''s off the page..


  Read about the latest Ray Tracer release / pictures / demo source etc  BACK HERE
   


 Added to the PlayBASIC Gallery


stevmjon

thanks kev, i am glad you like this and posted it on playbasic gallery.

it is fun making an editor, getting all the buttons to work, and also getting multiple views of the scene drawn to a quad display, with the ability to select objects and use tools.
it was a learning curve to set up. if i was to do this again, i would make a more efficient set-up code wise, now i know what is involved.

to get the selection of objects to work, in both the orthographic view and the perspective view, i converted the 2D screen coordinates to 3D coordinates, then shot a ray into the scene either in a straight line (orthographic) or from the camera to the 'screen' along the ray (perspective). also, the zoom level is different between the orthographic view and perspective view (i found interesting). in other words the selection routine is a mini ray tracer, lol.

well, i am gonna grow this a bit more and post when done.

   stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

#32
 Steve,

   It's fantastic but..  yes there's a but..  it's hard coded to rely upon the screen size.  Making it unusable on computers what don't have desk top space for the window, such as my laptop..  

   So in order to run it , I have to scale the window roughly within the limits of my systems desk top size..  This works, but all the SetMouse then break the interface as their absolutes.    So every time I click on anything the mouse is re-positioned away from where It was..  

PlayBASIC Code: [Select]
   //  Scale 


OpenScreen 1600,960,32,1 ; open screen in 'window' mode

ScaleWindowToDeskTop()



Print "Hello World"
Sync
waitkey





linkdll "user32.dll"
SetWindowPos(hwnd,hWndInsertAfter,x,y,cx,cy,wFlags) alias "SetWindowPos" As integer
Endlinkdll



Function ScaleWindowToDeskTop()

// Get the Desk top width/height
dtw=GetDesktopWidth()
dth=GetDesktopHeight()

// Get the PB screens size
w=GetScreenWidth()
h=GetScreenHeight()

// Get the PB screens window Handle
hwnd=GetScreenHandle()

; Stretch the window to the size users display size
SetWindowPos(hwnd,hWndInsertAfter,0,0,dtw,dth,wFlags)

; Resize PB's GFX viewport to screens (the window) new client area
StretchGFXscreen


Endfunction




    I'm putting together a video this morning, but it's pretty long....



   



stevmjon

#33
hey kev

don't worry, i take this as positive criticism. i actually like it when you look at code and give advice to improve it. how else am i going to learn, lol.

i didn't realise i was going to code this much into the editor, so left it in gosubs. i wanted to challenge myself to see how much i could use only gosubs, with minimal functions.
i was going to do this just for only the ray tracing, but ended up using them everywhere. i actually do the opposite of this code, and use mainly functions.

i will definitely implement the screen size check.
i left it this size because i thought your computer was 1680 * 1080. so i thought i left room around the current window.
also, i don't normally exit something using a gosub jump, because in the past you told me it was not recommended to do that. or was it to not exit a nested for - next loop?

here are some tips for using the editor:
> click & hold the small square buttons then move mouse. when you hold down the button it will turn blue, then when release the button it will go default.
    this works for the 'widget' buttons on top right on each window, and also for the color options in the surface editor panel.
> tool buttons that are longer either activate immediately (momentarily turn blue) or you click them and they remain blue so you can then click in the window to activate/use the tool (eg. mirror)
    NOTE: usually to use a tool successfully you need to have an object(s) selected, else there is nothing for the tool to work on.
> select is to click on the object, and there are 2 ways to de-select. 1) hold shift & click object (this allows single object at a time) 2) click anywhere around the windows and this de-selects all of them.
    also check out the 'view' tab, that has select all & de-select all buttons.

* not sure why in the main scene, when you clicked on 'camera' you could only move backwards ???
   it should work forwards / backwards / left / right with the direction of the mouse movement...

anyway, i really need to write the code again to make it more 'functional'. it is long winded at the moment (result of my gosubs only challenge).

i will implement the changes you recommended, inside my current spaghetti code, but may re-write this soon in the future.
to tell you the truth, it is getting to be a pain to add things and also bug fix... i spent half the day coding, then the other half bug fixing to get it to work properly, lol.

   can't wait to post new demo, stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

stevmjon

a quick question, when you click on a 'widget' button, these are any small square buttons, i made it so the mouse pointer turns off, and stays off while moving mouse, then when you release the mouse button it turns mouse pointer back on.
do you think this is a good idea? should i just leave the mouse pointer on?

i made it this way because i thought it looks better than seeing the mouse pointer jitter around while holding down 'widget' button and moving mouse.
i did have the screen position in desktop saved in a variable, so the mouse pointer should appear where it was when it turns back on.

in the video, when you added code so the screen can be re-sized, was the screen re-opened in a smaller size, or was the 1600*900 screen scaled down to fit inside the desktop?
if the screen was scaled down could that account for the mouse pointer jumping in the editor that you had, or the camera movement in the main scene jumping in position so fast?

the reason i mention this is i have added a check for the desktop size (thanks for that advice), and it will open a smaller screen to fit inside the desktop, and all 'widget' buttons with pointer turned off, seem to return to their correct positions, even when i make a much smaller window for testing. the code for the mouse re-position was already there, so no modifying the code was necessary. it is working when i re-size the screen window to any size on my computer. that's why i didn't understand why it wasn't working on your laptop.
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

#35
 I don't think it needs a re-write..  but could be more compartmentalized.     Ideally if you can run the GUI separate from the application.  


Quote

a quick question, when you click on a 'widget' button, these are any small square buttons, i made it so the mouse pointer turns off, and stays off while moving mouse, then when you release the mouse button it turns mouse pointer back on.
do you think this is a good idea? should i just leave the mouse pointer on?


  I'm not a huge fan of it..  if the pathway gets missing when the mouse is off or something else breaks in while you've turn the system mouse off.. this can lead to annoying issues for a user.     I know it can look nicer tho.


Quote


in the video, when you added code so the screen can be re-sized, was the screen re-opened in a smaller size, or was the 1600*900 screen scaled down to fit inside the desktop?
if the screen was scaled down could that account for the mouse pointer jumping in the editor that you had, or the camera movement in the main scene jumping in position so fast?

  That setwindowpos function is just a way we can scale a window to size, regardless of what it's internal surface size is.  It's the same really as if we dragged the window corners in manually.  

  But.. from memory get PB computes mouse coords to the internal surfaces to a ratio of the actual window size.  Which I'm pretty sure occurs when reading the position, but i've got my doubts when forcing the mouse position.   Which would break the positioning.   Could prolly account for it by just getting the windows client area / against it's actual size then scaling for the time being..  

   
    Example Code:  GetWindowRect & GetCleintRect 

    If client area is the 'pb screen' inside the window, while the WINDOW rect is the windows absolute size on the display.  There some padding applied for themes I guess..     Read more about    Get Client Rect in the Windows SDK


   

stevmjon

#36
i am working on an updated version of the ray tracer editor. thought i would keep a list here. most are bug fixes, some changes too.

completed:
> check desktop size :  if smaller than my 'recommended' screen size, it will open a window to fit inside the desktop, allowing a small gap around it for taskbar and window title bar. buttons etc will adjust themselves to fit into the screen. you can also manually change code to open a much larger screen size too. it will adjust to fit all buttons into their sections.

> widget buttons  :  the behaviour of the mouse movement when using widgets is now changed, so it will allow smooth adjustment even if you drag the screen around to a new location.

> added shortcut keys  :  the editor now has shortcut keys for some operations, instead of needing to use the widget buttons (thanks kev for this suggestion). i will add some to the main scene also (listed in help screen).

> help/info screen  :  this has been modified with tabs for easy selection of the info you need. also fits into a smaller size to allow for smaller screens.

> surface editor  :  moved the surface editor panel to allow for small screen sizes. this will display inside the view window of the editor without interfering with widget buttons.

> save files into a readable text format, with different data in their own heading. this allows for any changes in the future to load the file correctly (another handy tip from kev).

> add creating spheres with the button press, rather than needing to clone existing ones (in case you clear the scene and no spheres are in it).

> fixed: a bug is if you change the reflection amount of an objects surface, this reflection in other objects will adjust making this object look semi-transparent, depending on your setting amount.

> fixed: have found a bug where it leaves some 'fireflies' (speckles) in the render on some angles. checking for a fix.


i will edit this post as i complete the above list.

  stevmjon

************************
*** Demo Below _ v0.21 ***
************************
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

 Steve,

    I only had a quick play last night and it feels really good..   there's a couple of little niggles with the dialogs overlapping but nothing big..   will have another pick through later. 

    All in All it just worked straight outta the box..  The controls like object moving/camera feel pretty comfortable.



  Big thumbs up..   
 

stevmjon

cool kev.

let me know what needs to be adjusted and i can modify the code and release an update version.
checkout the save file too, and see if it is more like what should be done. it has headings and data is all readable text. you can even modify the code in notepad. i have even done this, lol.

  stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

 I will..  :) 

Pretty busy atm..  but two things that came to mind was when adjust the colours from object editor it's possible to move the mouse (while invisible) and for those clicks to register on other gadgets..

  Another thing that i remember happening was  if i used the colour select dialog, I move the mouse and then was in state where the system mouse was turned off couldn't see how to escape the situation.    I'll try and recreate on video.. I don't recall exactly what I was doing .. 



stevmjon

i have changed the way the click and drag mouse works on the widget buttons. it should work without any issues.
the mouse does turn off (because when left 'on' the mouse looks wierd), but as soon as you release the mouse button the pointer turns on again.
also, while the mouse is off, it keeps the mouse inside the screen window.

i was also thinking of allowing the mouse scroll wheel to adjust colors. you mentioned this earlier in the video, but i forgot to add this.
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

stevmjon

#41
NEW START...

well, i have began to write this code again as it is like spaghetti code, plus it didn't help everything being in gosubs (challenge i gave myself).
my progress has been pretty good, and i have now made good old functions again making sure to write stuff once in a function i can multi-access.
this is making code smaller again, and a lot easier to grow. i have also made more types to make more objects.

Progress:
> screen check  :  checks desktop size and adjusts screen accordingly
> scene objects added
> perspective grid  :  changed this routine, which draws faster, and it also follows the camera around
> camera  :  allow multiple cameras in the scene
> lights  :  allow multiple lights in the scene
> shortcuts (main scene)  :  added shortcut keys to control camera
> main scene buttons  :  buttons and menus are in main scene and are now images and treated as objects (types) . all text on screen has been removed.
> mouse-over hints  :  if you hover the mouse over a button then a hint text will appear in a box at the bottom of the screen. this explains what the button does.
> active button hints  :  shows hints on how to use current tool selected
> render engine  :  rays and vectors are now objects. i also fixed a few bugs i found. but... as a result of adding functions, the render takes a few seconds longer.
> editor buttons  :  buttons and menus are in editor and are now images and treated as objects (types) . most text on screen has been removed, as some panels change/update text.
> editor windows  :  all different views are working in the editor, and are all drawn to an FX image using ImageViewPort command (limits draw zone).
> tools  :  editor tools are now added and updated
> update help screen  :  added scrollable text in case window is too small to fit all text in window (use mouse wheel)
> shaded mode for objects

i will update above list regularly. it keeps me motivated too. i am having fun.

  stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin


Awesome..  I'm just glad people are enjoying themselves... making things of this caliber  !


stevmjon

#43
time for another update to v0.37 , as this is a re-write of the code to take it from gosubs to functions.

this should run smoothly, and i have added a few new features in the editor that show on screen a 'preview' of what will happen if a tool button is clicked.
so if you hover over create > ball it will show a preview on the screen in red to show where the ball will appear if the tool button is selected.
also if you hover over the multiply > mirror x/y/z tools it will show a preview in red of where the mirror border is and also show the new objects in their mirrored position (as long as you have at least 1 object selected).

another feature is 2 wireframe modes to choose from (wireframe & color wireframe), as well as the previous 2 shaded modes to choose from already included.
i added the new wireframe mode in case the user doesn't want to use shaded mode but wants to see what color the objects are.
also the light can be selected and moved around too.

i will keep updating this to add more features, but i wanted to at least post this updated version which should be stable.
i am looking forward to adding planes & boxes, as this will allow much better objects to be created (coming soon).

EDIT: just fixed a minor bug where the light can be moved when it is not selected, while trying to move another object

  enjoy, stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

stevmjon

just updated the demo in the above post to fix a minor bug.

updated to "v0.37 Demo 2".
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.