Important alert: (current site time 7/16/2013 3:08:07 AM EDT)
 

winzip icon

Realtime simple raytracer (updated 30 April 2002)

Email
Submitted on: 3/1/2002 7:02:12 AM
By: Almar Joling  
Level: Intermediate
User Rating: By 51 Users
Compatibility: VB 6.0
Views: 26134
author picture
(About the author)
 
     Updated by Bill Soo... instead of 25 fps, it runs at 60 fps now! Which is an astonishing framerate for VB Again some pretty unique code from me. This program is a _realtime_ raytracer (not raycaster!). I ported it from C++ to VB, and it still runs pretty fast. It also shows you how to use SetDibbits to change all the bits in a picturebox with just one call. You can zoom in and out, add more spheres by changing a few variables/constants in the code. Light-positions can be set by the scroll bars and changes will be visible directly! Note: Please compile this program, or you will receive ~1-2 FPS from the IDE. I would appreciate some votes, people, so that I can still think my (unique) work is appreciated =-) Update: I've moved some math functions inline, and improved the code wherever it was possible. 10fps more now! Do not forget to compile the program though... Original source is also included. I'd really appreciate if you vote for this code guys.

 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Realtime simple raytracer (updated 30 April 2002)
'**************************************
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Function SetDIBits Lib "gdi32" (ByVal hdc As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 11 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
3/1/2002 7:45:17 AMNigel

Very Impressive!
(If this comment was disrespectful, please report it.)

 
3/1/2002 10:21:13 AMRobert Rayment

Nice maths & imaging 5/5 I get about 10 fps for exe on my 400MHz m/c.
(If this comment was disrespectful, please report it.)

 
3/1/2002 10:52:07 AMKaoS

Pretty cool code!
I get 15fps on 900MHz PIII.
I like it...more stuff like that!

THX
(If this comment was disrespectful, please report it.)

 
3/1/2002 12:23:53 PMgonchuki

poor performance for a 900 Mhz!!! i get 21fps on my 672Mhz AMD Duron... and 28fps with all tricky switches on... still have to see if i can optimize the code.

Good Work!
(If this comment was disrespectful, please report it.)

 
3/1/2002 12:57:35 PMFeuchtersoft

Neato. I don't get a single frame per second, although it's cool, nonetheless.
(If this comment was disrespectful, please report it.)

 
3/1/2002 1:34:48 PMAlmar Joling

Feuchtersoft: Did you compile it? If not, you really should. running the program from VB is _very_ slow

I'm aware that there is a difference between Intel/AMD machines. I'm not sure what causes it, but I guess it depends on some optimizations VB adds.

If manage to optimize the code more (besides moving the math inline), I'd like to hear it =-)

Almar
(If this comment was disrespectful, please report it.)


 
3/1/2002 1:55:47 PM...tHe_cLeanER... / jB

LOL, i get 3fps on my Athlon 1.2ghz!
(If this comment was disrespectful, please report it.)

 
3/1/2002 2:44:09 PMAlmar Joling

Did you compile it???? If not, then... well something is really taking up clock cycles on your PC.

(If this comment was disrespectful, please report it.)

 
3/1/2002 2:44:58 PMJason Hensley

I get 42fps on my AthlonXP and Radeon DDR VIVO video card.
(If this comment was disrespectful, please report it.)

 
3/1/2002 2:45:41 PMJason Hensley

I'm getting 46fps now
(If this comment was disrespectful, please report it.)

 
3/1/2002 5:26:36 PMeXQue

Very nice program got 4 with my 1.2 Ghz

(If this comment was disrespectful, please report it.)

 
3/1/2002 5:59:30 PMXip3000

89 FPS in my ATHLON 1400 and GeForce256 DDR
(If this comment was disrespectful, please report it.)

 
3/1/2002 6:14:22 PMDragonfire Software

Cool code. I get around 29 FPS on a 1.4 Ghz P4, 256 MB RAM (If that matters any...) Keep up the good work!

http://dfs.vze.com/
(If this comment was disrespectful, please report it.)

 
3/1/2002 11:00:19 PMXNetman

Nice little example with a repectable 36fps on my machine. You can possibly speed up the maths a little by creating a look up table for the trig functions rather than calculating them each time. It would also be nice if you could move the light position without pausing the animation. Anyway 4 globes from me you'll get 5 when you provide support for other simple primitives e.g. cubes, pyramids.
(If this comment was disrespectful, please report it.)

 
3/2/2002 1:08:21 AMFeuchtersoft

Ah, I just compiled it. 33 fps. Ha! I should probably read instructions next time.
(If this comment was disrespectful, please report it.)

 
3/2/2002 12:55:59 PMHans J. Reich

You get a five - very nice, and I like to see graphics stuff as a break from all the winamp players and such.
(If this comment was disrespectful, please report it.)

 
3/2/2002 1:50:18 PMCarles P.V.

Simply great!

Good work.
(If this comment was disrespectful, please report it.)

 
3/2/2002 2:20:00 PMJeremy Pettit

25 fps on Duron 850mhz running Win NT Server 4 and Direct X 6. Not bad performance considering these factors.
(If this comment was disrespectful, please report it.)

 
3/2/2002 2:20:11 PMM. Highlander

i got 0 fps
oh wait you have to run it first ;-)
COOOOOOOOOOOOOOOOOOOOOOOOOOOOOOL!
(If this comment was disrespectful, please report it.)

 
3/2/2002 2:46:34 PMEdin Omeragic

15 fps on celeren 500 MHz
i must give this friend to try this
on pentum I 133 Mhz
(If this comment was disrespectful, please report it.)

 
3/2/2002 3:43:06 PMChris Pietschmann

I got 3fps in the IDE, but 23fps compiled, on my 900mhz Athlon. Very nice. Could you send me, or post a text on how to convert xyz to xy coordinates, and lighting and viewpoint, and such. I will try to figure it out from your code, but it would be easier if there was a little tutorial on the math that I could read.

Very Nice!
(If this comment was disrespectful, please report it.)

 
3/2/2002 10:45:15 PMJamie Lindgren

Really cool. In the IDE I got a mind boggling 3 FPS >< but when compiled I got 29 (on my 1.5 gigz comp)
(If this comment was disrespectful, please report it.)

 
3/4/2002 10:53:02 PMgonchuki

someone said would try on a pentium 133... i tested it on my AMD K5-100 result: 4 FPS

very good... i get the same in IDE on my main pc...
(If this comment was disrespectful, please report it.)

 
3/5/2002 1:06:36 AMhavok

Nice program, u need votes. So u got a 5 from me.

Good job Almar, your coding seems to amaze me, doing stuff that other people don't even think of.

[havok]
(If this comment was disrespectful, please report it.)

 
3/7/2002 8:57:57 PMSchitzn

I get an error relating to an undefined function/sub: Could someone email me and tell me why I can't seem to find the sub/funct:
"GenerateRayDirectionTable"

I have the DirectX 8 reference listed without error? Ideas?
(If this comment was disrespectful, please report it.)

 
3/8/2002 2:40:50 AMAlmar Joling

I first used the math functions contained in DirectX, but they were slower than a simple VB version of them... I forgot to remove the reference. About your problem, the sub is really there! I'm always running with Full compile, never seen the error before. I guess it would't compile without it either, =-). The sub is in mdlMain, almost on the top I believe...

-Almar
(If this comment was disrespectful, please report it.)


 
3/9/2002 3:22:41 PMxmetrix

53fps on athlon 2000xp 1.67@1.8ghz 4gb ddr, radeon 8500 64mb ddrAGP, scsi hd, winXP pro directx8a
(If this comment was disrespectful, please report it.)

 
3/14/2002 9:04:16 AMShlomo

very nice 8 fps on my PII 350MHz
but it takes 100% cpu usage.


(If this comment was disrespectful, please report it.)

 
3/16/2002 6:47:19 AMAlmar Joling

Yeah, it uses a simple do...loop, with just one DoEvents. It doesn't give Windows much time to breathe =-). raytracers are known for their required power, that's why we still do not see any large real-time raytraced animations yet.
(If this comment was disrespectful, please report it.)

 
3/25/2002 11:26:20 PMKevin m F

cool, but you need a fast comp to run it
(If this comment was disrespectful, please report it.)

 
3/26/2002 6:52:27 AMAlmar Joling

Yeah...realtime rendered scenes won't exist for the next years anyway. It requires so much calculations. That's why raytracing a simple (3D) scene on a post-stamp format can still take 5min on a 500Mhz..

(If this comment was disrespectful, please report it.)

 
3/26/2002 4:58:58 PMAlmar Joling

I do like to know why Tim Sørensen has give me a vote of 1. Didn't it work? You're the only one that has voted so low, so please I'd like to hear why so I can modify the code as needed
(If this comment was disrespectful, please report it.)

 
3/30/2002 6:33:55 AMrenekersten

hey very nice! i get about 10 fps on my p2 400 mhz. you get five stars from me
(If this comment was disrespectful, please report it.)

 
3/31/2002 2:05:34 AMCarlos Ruvalcaba

a Really nice one, 2 fps IDE, 4 fps Compiled, in my Pentium 200Mhz, 5 Globes from me!
(If this comment was disrespectful, please report it.)

 
4/17/2002 5:05:06 PMBill Soo

I got 15fps on my 500mhz. I added ALL the compiler optimizations and got that up to 19fps. I then got rid of SetDibits and used direct memory writes. That improved the speed to 22fps. Finally I replaced the GetFPS function with a simple timer control. That brought the speed to 24fps.

Total improvement...60%.

(If this comment was disrespectful, please report it.)

 
4/17/2002 11:43:03 PMAdriano Paladini

47fps on my AMD K6-III 450mhz with GEFORCE 2 MX-400 (32MB).
(If this comment was disrespectful, please report it.)

 
4/18/2002 1:40:09 AMAlmar Joling

Bill: I would love to see that code :). Although I think the timer control's precision (which is bad!) is increasing the FPS. Although the timr control does run in it's own thread..hmm

(If this comment was disrespectful, please report it.)

 
4/18/2002 8:05:36 AMOzan Yasin Dogan

Intel PIII-450mhz Leadtek Geforce2Mx Running at 210Mhz, 11 fps
Nice code but still slow..

Graphics --> C++
(If this comment was disrespectful, please report it.)

 
4/18/2002 12:50:47 PMBill Soo

The timer control has poor resolution (~55ms) but that doesn't matter much since you are sampling a fairly large time interval (1000 ms). The big time savings is due to the fact that the timer control only fires every second, thus it only has to update the caption once per second, instead of 22 times per second.
(If this comment was disrespectful, please report it.)

 
4/19/2002 4:38:39 PMBill Soo

I replaced the frmMain.picRay.Refresh statement with the InvalidateRect API call to which I passed a small rect structure that was just the part of the picturebox containing the spheres. This forced a repaint of only a section of the picturebox, rather than the whole thing, resulting in a frame rate of 33fps.
(If this comment was disrespectful, please report it.)

 
4/19/2002 5:40:20 PMBill Soo

The rotate and shadesphere functions are pretty fast. Where the time seems to be consumed is in the TraceScene function (which calls shadesphere true, but shadesphere is a small fraction of the total time). So I took a look at this code and made some small optimizations that gained a fps. Then I got rid of the MySphere and ClosestSphere variables and referred directly to Spheres. This gave me a huge increase from 33 fps to 49fps!

So total optimizations went from 15fps (original code) to 49fps....
(If this comment was disrespectful, please report it.)

 
4/20/2002 2:49:52 AMAlmar Joling

I believe it's faster than the original C++ version then =oD. Good work!!
(If this comment was disrespectful, please report it.)

 
4/20/2002 2:50:51 AMAlmar Joling

btw, if you email me the updated version, I'll update this submission. -Almar
(If this comment was disrespectful, please report it.)

 
4/28/2002 8:20:02 PMHurroTisRobbo

Top stuff! 5 globes!
(If this comment was disrespectful, please report it.)

 
4/29/2002 6:34:27 PMJuan Martín Díaz

Is this submition updated with Bill´s code?

(If this comment was disrespectful, please report it.)

 
4/30/2002 2:13:06 PMElan

I pulled 40fp/s nice.
(If this comment was disrespectful, please report it.)

 
5/9/2002 8:11:49 AMExeat

My Pentium I 166mghz got a suprising 11 FPS - I honestly expected to get a lot less, well done.


(If this comment was disrespectful, please report it.)

 
5/20/2002 5:40:29 AMLuke

Very nice! 62fps on a Intel 82810 Graphics Controller!! 800Mhz Athlon, 256Mb RAM.
(If this comment was disrespectful, please report it.)

 
8/16/2002 11:16:30 AMthri-p

Getting an average of 145fps. I'm running on XP, Athlon 1.2ghz, 640mb SDRAM, Leadfast Geforce2 Ultra. Nice codes. 5* :)
(If this comment was disrespectful, please report it.)

 
10/7/2002 2:37:26 AMNelson Oliveira

very nice 49fps on a celeron 466
5 globes for u
(If this comment was disrespectful, please report it.)

 
10/7/2002 8:18:27 AMTimon Albers

Why do all of you got so few fps? I got about 220fps. -> Athlon 1800+
(If this comment was disrespectful, please report it.)

 
10/7/2002 11:56:49 AM

Very impressive code. Ran icredibly fast here at 150-250 fps on a Athlon XP 1700+, 512 Mb ddr mem and a Radeon 8500
(If this comment was disrespectful, please report it.)

 
10/7/2002 12:45:54 PMChristopher Rouse

It worked V slow in the VB IDE I got about 8-12fps, but when it was compiled it was to fast about 200fps on my Athlon XP 2000+ and 512mb of ddr mem and a geforce 3 ti 200.
(If this comment was disrespectful, please report it.)

 
10/7/2002 3:59:12 PMAlmar Joling

I'm wondering why there is suddenly so much interest for this code :P. A link somewhere?

-Almar
(If this comment was disrespectful, please report it.)

 
10/7/2002 6:43:38 PMjohn sheridan

I think your code won code of the day, so its in all the mailing list emails that go out everyday. But thats wierd cuz this is like a year old...
btw, this code is amazing - i got 15 fps in the IDE and 135 FPS! when its compiled
5 globes from me
(If this comment was disrespectful, please report it.)

 
11/24/2002 10:07:06 AMPeter Wilson

Hmmm? If this is really ray-tracing (of which I have seen no real evidence thus far), then let's see some reflections!
(If this comment was disrespectful, please report it.)

 
11/24/2002 10:09:54 AMAlmar Joling

Peter Wilson: There is another version of my raytracer which has shadows, reflections and stuff..

(If this comment was disrespectful, please report it.)

 
3/17/2003 1:18:38 AM

Impressive work, I'm averaging 230fps on my AXP 1800
(If this comment was disrespectful, please report it.)

 
11/3/2003 8:29:40 PMJames J. Kelly Jr.

HOLY COW!
I get like 57 FPS on my PIII 550Mhz!
Thats a way better preformance than
Direct3D or OpenGL ever gave me on
ANYTHING!
(If this comment was disrespectful, please report it.)

 
6/29/2004 12:46:03 AMBurbble

Hey, I know this code was posted a while ago, just found it while browsing for something. It really is impressive -- I get around 220FPS with an AMD Athlon XP 2000+

Dunno why other people are getting such low FPS though...
(If this comment was disrespectful, please report it.)

 
11/2/2008 11:41:35 AMAlexander Overvoorde

Very good! I'm getting an fps of 400 on my Quad Core 2.4 Ghz here :D
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.