• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C++ Ray Tracing! This is so fun!

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

macklin01

Computational Oncologist / Biomathematician / Mode
Joined
Apr 3, 2002
Location
Bloomington, IN
We have to write a ray-tracing program for our final graphics project. I'm having a lot of fun with this project, especially since I'm self-taught in programming and this is a class for graduate CS people. :eek: But this is fun -- no OpenGL, just raw code. ;)

I've made some good progress and will post some pics tonight. So far, I have spheres and triangles with texture mapping implemented, an illumination model that includes ambient, diffuse, and specular lighting, arbitrary camera viewpoints, and shadowing. (It has to read a scene definition file and parse it as the first part of the project.)

Coming soon: reflections and refractions / transparency, some primitive bump mapping, and anti-aliasing. I just couldn't wait to post. ;)

I also have a website for the class at

http://www.ima.umn.edu/~macklin

that I'll be updating soonish. :)

Okay, thanks for listening. :) -- Paul
 
Ive been planning on doing something similar, i just cant find a reference for how to program a graphics library. Do you have a link to a site that has details on the basic functions used to program up a graphics library?
 
Hmm, you can use OpenGL for some of that, but really, a lot of it's on your own when you do a ray tracer. (Don't plan on real-time rendering -- parse a file and save its output.)

I was lucky to some extent -- I had already written a Matrix library that I can use for a lot of the transformations and such. But I had to write my own routines for rotations, transformations, screen buffer, etc.

I'll post a link to references soon, but a lot of it will be on your own. Data structure is really key here. I made a generalized GraphicsObject class, with pointers (initially NULL) to a Triangle object and Sphere object and its own color, transparency data, etc. You determine what kind of object each graphics object is by which pointer is non-NULL. Also, the graphics objects are a linked list. I do similar for LightObjects. For larger scenes, it's best to further structure the data with a heirarchy such as an octree.

Oh, here's a screenshot of what I'm trying to reproduce:

original.jpg


Click here to download the zipped bitmap version, which will have more accurate colors.

Also, see my link above for my website, which has some additional links on programming and 3D references. I'll also post the ISBN / title info on our graphics book, which will have many of the details you want.

Thanks for your interest! -- Paul
 
very interesting. just took a quick look at the link from the first post. saw you talking about tumors, and it reminded me of something a while back. i mentioned something about an equation that modeled tumors, and someone said something. was that you? anyways, i have a final to get to real soon, but i am excited to look at this stuff in the future. teach me your graphical ways o wise one ::esau bows down:: id love to get into some more advanced c++ topics.
 
@esau:
Math Ninja, thanks for all the kind words. :) I'm really not all that great of a programmer, although I'm getting better. For a self-taught mathematician-turned-computational biophysicist, I do alright. ;)

Hmmm, I think we might have discussed it before, either in folding or in another thread. I'm going to be writing up my masters thesis on the tumor simulations very soon. (And I'll have some cooler movies in a few weeks. I'm going to be adding in a necrotic core. :D)

Good luck on your final! (What's it in?)

@Roof Jumper:
Specific sites can be tricky to come by. (I'd try google for raycasting programming or something like that.) Often, you'll find technical results from journals or generalities about it, or you'll find people who just have formulas with little explanation. (Which can make good implementation difficult.) At my site above, go to the useful links and see those. In particular, the official class website has a lot of its lectures in PDF format posted. I'll post my text for you soon. It would be helpful.



@everyone:
I'm home now and have just posted some pics of my most recent output:

NoReflection.jpg


Click here to get it as a zipped bitmap file.
 
That's awesome man! It'll be really nice when you get the reflections working!!!

I've been thinking about possibly trying to undertake a raytracing project, but the work involved seems to be quite a bit. That, and I'm 99% sure it will need the use of Trig, which I don't know :(

Oh well, I'm sure I'll eventually understand the evils of trig, and write the progam :D

JigPu
 
macklin01 said:
@esau:
Hmmm, I think we might have discussed it before, either in folding or in another thread. I'm going to be writing up my masters thesis on the tumor simulations very soon. (And I'll have some cooler movies in a few weeks. I'm going to be adding in a necrotic core. :D)

Good luck on your final! (What's it in?)

is this an equation you are familiar with?:
rho = 1 + (1/5)sin(m*theta)sin(n*phi)
-vary m and n accordingly-

id love to read that thesis when it is done(if possible).

my final was just in a 300-level probability course. i actually took it before at another Univ, but they wouldnt transfer it.:mad: im used to getting the royal screwjob at this point in my life. :( funny thing is i may not pass it.:eek: i biffed up this semester in a bunch of ways. basically i didnt open my book until last week, guess i got a little too cocky. i just covered all the material for the class today though, and i feel i did really well on the final, so we will see.

anyways, this coming tuesday my summer projects will begin. raytracing and OpenGl have just been added to my "c++ : learn this stuff" list.:D

that pic looks awesome!::esau shivers at the thought of trying to code something similar::


on a totally unnrelated not i need some mathematical advice, figure id ask you right now. In the coming fall semester i will be involved in some independent study in conjunction with a course in number theory. the independent study must be related to number theory in some way. here is the course description:
--------------------------------------------------------------------------------
mth 367 Number Theory............3cr
Fundamental theorem of arithmetic, quadratic residues and quadratic reciprocity, number theoretic functions and diophantine equations.
---------------------------------------------------------------------------------
the independent study can be 1cr, 2cr, or 3cr

Some options ive allready considered would be cryptography, factoring primes, finding extremely large primes, etc....

Im trying to get as many ideas as possible, beofre my meeting with the prof that will be leading this study. Any suggestions?
thanks.
 
Last edited:
esau said:
on a totally unnrelated not i need some mathematical advice, figure id ask you right now. In the coming fall semester i will be involved in some independent study in conjunction with a course in number theory. the independent study must be related to number theory in some way. here is the course description:
--------------------------------------------------------------------------------
mth 367 Number Theory............3cr
Fundamental theorem of arithmetic, quadratic residues and quadratic reciprocity, number theoretic functions and diophantine equations.
---------------------------------------------------------------------------------
the independent study can be 1cr, 2cr, or 3cr

Some options ive allready considered would be cryptography, factoring primes, finding extremely large primes, etc....

Im trying to get as many ideas as possible, beofre my meeting with the prof that will be leading this study. Any suggestions?
thanks.
http://www.utm.edu/research/primes/

That site right there has everything you'd ever want on primes :D Links to programs, theroms to find them, and everything in between. If I knew more math (to understand the equations) I would definatly include some of those formulas in my prime program :rolleyes:

JigPu
 
Funny thing is, C++ is really the only thing that has kept my attention recently, in under 5 months(started in january with some basic basic knowledge of C++(think int main(){ cout <<"hello" << endl; return 0; }) I have learned quite abit of C++, and am in the middle of learning opengl and WIN32(Halfway through petzold's book).)

The thing i like about programming is that it is open, there is a realm of possibilities. One can use it for basic Income tax calculation, all the way to complex detail 3d modeling software. The Sky really seems the limit in programming.

@Macklin:

how long have you been teaching yourself programming, what mathematics courses would you reccomend, I was planning to learn Calc I,II, and III in college along with College Physics up to advanced physics, and get a dual major in Mathematics and Computer graphics design(i think this is the right term, basically, i want to learn how to design, program, etc. computer graphics, preferbably from the ground up, so many schools just hand you a book on direct x , instead of having you understand whats under it).


@ all:

sorry if this post contains spelling/grammatical errors, i had a really bad nights sleep, and am dead tired at 9:30 in the morning.
 
@all:
Sorry for my delayed responses ; things have been insanely busy. I'm trying to clean up my parser and catch up on research, so I'm really behind. I promise to write y'all soon! :D (And this discussion has been a LOT of fun! ;) ) -- Paul
 
JigPu said:


lol....nice link....she has a been a long time friend to my bookmarks...:)

what mathematics courses would you reccomend, I was planning to learn Calc I,II, and III in college

well if you plan to major in mathematics then you will go much farther than calc III. you will dip into some diff eq as well, among other things. calcIII = calcI + calcII + 3D + some other stuff thrown in for good measure. good times.:D you will also be looking at linear algebra, abstract algebra and real analysis.



@macklin01
if im not mistaken you just got your blue stars. congrats.:clap:
 
Just a quick second to pop in:

THANKS! I really had no idea it was coming. I got a PM this morning from Thelemac, subject "Feeling Blue?", which mainly made me wonder how on earth he was reading my mind that my programming was a disaster. Quite a nice surprise and pick-me-up. :)

I'll get back to you all soon, I promise. I just figured out why I've had so much trouble cleaning up my parser this morning and last night: GCC (at least the mingw port) has a BUG in the seekg() and tellg() functions for input streams streams!

For example:

fstream IO("file", ios::in);
IO >> szTemp;
IO >> szTemp;
streampos BM = IO.tellg();

IO >> szTemp;
cout << "characters right after bookmark: >>]" << szTemp << "[<<\n";
IO.close();
Io_Open("file", ios::in);
IO.seekg(BM);

IO >> szTemp;
cout << "characters after reopen, bookmark op: >>]" << szTemp << "[<<\n";
IO.close();

The two outputs should match, but at least in my version of GCC / mingw, they don't. So, now I have to do a workaround with FILE operations, instead. (If I have time, I'll submit the bug.)

-- Paul
 
Hello, everyone!

Well, I'm hard at work on it still, and I have a final in fluid mechanics tomorrow morning at 8AM. :eek:!

At any rate, I just finished antialiasing this morning, and my parser is working well. Quads, triangles, and spheres are fully implemented. I have new screenshots at

http://www.ima.umn.edu/~macklin/HW5.html

@JigPu:
Thanks! Well, I'm just about to start on the reflections tonight! Don't worry -- when the time is right, you'll understand all that stuff and be able to do what you want to do. And actually, when you have a specific application in mind, trig and other things can be much easier (and more enjoyable) to learn.

@esau:
Hmm, That almost looks like various modes of vibrations. Perhaps 3d cardiods? I can see where you could make some very interesting, 3d flowers with that set of functions.

id love to read that thesis when it is done(if possible).

Me too! :) Well, once classes are out of the way, I'll have time to get back to it at last! And I'll be happy to post it up, as well as links to animations of the tumor growth. Thanks for asking!!!

Don't feel so bad about the probability class -- that sort of thing happens to the best of us. Hang in there!

As for independent topics, I agree with JigPu. Number theory ties in very nicely with topics in factoring, prime numbers, and cryptography. I suppose I'd stick with prime numbers and examine its links to cryptography. Very applicable, very fun, and it'll help make concrete some of the very abstract concepts you'll see in number theory and modern algebra.

@Roof Jumper:
Wow, you've come a long ways in a short time! Congrats -- it sounds like you're really enjoying it, which is exactly what you need to learn it well. I'm impressed -- petzold's book is quite a beast. (As such, I've barely had any time to learn the win32 API at all. Just enough to get basic bitmap I/O done at this point. )

That's precisely what I love about proramming, too. You can create your own new world. The possibilities are tantalizing!

I've been teaching myself C++ since about 2 summers ago. (Started from a Dummies book, switched halfway through to C++ in Plain English.) Just started OpenGL over the Christmas break, and I plan on some win32 API over the summer.

As for a dual major, I'd recommend Computer Science, with an eye towards graphics. Typically from what I've seen, graphics will be something you'll learn in your last year there as an undergraduate. However, to do it as you want to do, you'll need a solid CS background (data structures, everything) to do the base level graphics programming.

On the math side, to get just enough math to do it well, you'll certainly want Calc I-III, ordinary differential equations, advanced (vector-based) calculus, and some partial differential equations if you get the chance. I'd also recommend that you get some background in real analysis so you get some exposure to Fourier series, which are very handy in signal processing and compression. Prob / Stat would be helpful at times, too, but not required. Linear algebra is a must. (Think of all those rotations / transformations for 3d graphics.)

Physics I and II should be enough on that end. (Get a good grasp of mechanics and maybe EM theory, but particularly mechanics / kinematics / etc.) If you plan to actually major in math, then you'll also need a few more courses, such as in abstract algebra and statistics.

BTW, I totally agree on learning the CS as opposed to just learning some apps / DX / 3d studio max, etc.

@esau:
Thanks for the reminder on linear algebra. I can't believe I almost forgot to mention it, seeing as my whole life is basically numerical linear algebra. :foreheadslap:

if im not mistaken you just got your blue stars. congrats.
Thanks!! No one was more suprised than I! I'm still trying to get used to those nifty blue stars ... (Man, are those mine? Cool :))

@Honest_Bob:
Thanks for the link -- that was a lot of fun! From what I can tell, he's doing something similar. He must have some geometric primitives that his raytracer can handle, and then the lego models are built up of those. Very impressive.

Well, I'm off to work on the reflections now. (Or a basic start to them.) Thanks again for all the fun responses and encouragement!!!

-- Paul
 
Last edited:
Well, I've got reflections working! I'm still working on debugging transparency (I think it's a problem with assumptions for the intersection testing), but here are some pics! 512 x 512 with 3x3 antialiasing, reflections, wrong transparency. :)

FirstReflections.jpg

Click here to download the zipped bitmap.
 
Thanks! It's getting there ...

And I can't believe I forgot this:
Welcome to the forums!!! :)

-- Paul
 
Back