programing a raytracer

Discuss stuff not about Indigo.
User avatar
eman7613
Posts: 597
Joined: Sat Sep 16, 2006 2:52 pm

Post by eman7613 » Mon May 07, 2007 3:51 am

I haven't gotten a chance to take a look at it, have to finish up some of my other pojects.

But amuusing tidbit, my dad worked at boliva for the government so they had acces to computers before the microprocessor. The computer was built and everyone was given a sheet with the syntax comands and that is it, nothing about what they did or paramaters or anything. My dad and one other person were the only two that new how to use it and their conclusion and use of the syntax was completly diffrent. Two amuusing things happend
1: They had to guess the time it would take to finish the program, if it divided by 0 it would go on for ever!
2: They would write the programs and give it to their secretarys to go type it in and run it. One secretary's name was allice. And her instructions were given to her so that half way through typing it the compuuter spit out a sheet saying "Type faster Alice! Type Faster!" and she wigged out. :twisted:
Yes i know, my spelling sucks

User avatar
oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:

Post by oodmb » Tue May 08, 2007 6:59 am

i've fixed the vertex order problem- now flat shading works PERFECTLY!!!!!
unfortunitly smooth shading is still weird. its not order dependant anymore but it is normal dependent. if all the normals are the same it works fine (for these two planes at least). however if all the normals are 0, .9, .9 and one is 0, .8, .9 then the whole thing turns black. however if the same one is 0, .2, .9 the thing is black and one corner is blue.
a shiny monkey is a happy monkey

IanT
Posts: 153
Joined: Fri Aug 25, 2006 3:13 am

Post by IanT » Tue May 08, 2007 7:01 am

Are you making your normals unit-length?

Ian.

User avatar
oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:

Post by oodmb » Tue May 08, 2007 7:05 am

well, i just set it up so that in my poly class before it sets up the normal it scales it by 100 and then normalises it so i dont get any errors from the normal being less than 1


ok, apparently normal smoothing is dependent with the order of vertex declaration. now i'm realy confused
a shiny monkey is a happy monkey

User avatar
oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:

Post by oodmb » Wed May 09, 2007 11:59 am

i just wrote a code for refraction, although it seems to be doing very little to nothing if the ior is at 1.5 at 15 it works although little, i think i might be missing a cosine or something in my code:

Code: Select all

    public static Ray Refract(double n1, double n2, Ray Ir, Ray Nr){
        Vector I=Ir.Direction;
        Vector N=Nr.Direction;
        //Ir=new Ray(Nr.Origin, I);
        I.normalise();
        N.normalise();
        double LN=I.dot(N);
        if (LN<0){
            Nr.Direction.negate();
            N=Nr.Direction;
            LN=I.dot(N);
        }
        double NON2=n1/n2;
        Ray toI=new Ray(Nr.getPointAt(LN), Ir.getPointAt(1));
        double theta=Math.acos(LN);
        double sizeB=NON2*Math.sin(theta);
        Ray returner= new Ray(Nr.Origin, toI.getPointAt(sizeB));
        returner.ior=n2;
        return returner;
    }
a shiny monkey is a happy monkey

Deus
Posts: 336
Joined: Sun Feb 04, 2007 3:47 am

Post by Deus » Wed May 09, 2007 9:06 pm

While we are at it do this in Java

http://betterexplained.com/articles/und ... uare-root/

Well this takes a couple of clock cycles but in C++ but HUNDREDS in Java (because you cannot use the same method)

But you dont do b= a / sqrt(x^2+y^2+z^2) in raytracers so this optimization is not relevant right?

Deus vs Java ignorants 1-0

/ D

IanT
Posts: 153
Joined: Fri Aug 25, 2006 3:13 am

Post by IanT » Wed May 09, 2007 9:53 pm

I knew you'd be back :wink:

Well this takes a couple of clock cycles but in C++ but HUNDREDS in Java (because you cannot use the same method)

You mean the same method can't be used because the raw float bits can't be cast to/from an 'int' in Java? Who says?

Ian.

IanT
Posts: 153
Joined: Fri Aug 25, 2006 3:13 am

Post by IanT » Wed May 09, 2007 10:03 pm

oodmb,

I'll post you some source-code later on once I've finished work.

(Ignore Deus ... his Java knowledge isn't as good as he thinks it is :wink: )

Ian.

User avatar
eman7613
Posts: 597
Joined: Sat Sep 16, 2006 2:52 pm

Post by eman7613 » Thu May 10, 2007 2:03 am

Dues, jsut for sanity sakes. If your going to tell us that java is so much slower, write an app that does something in java, then copy it to c++ (they have very similar syntax and are both capable of recursion and aliasing so should be easy enough), put in a time function, and run it. Bring us back the results with source and well will lisen.

Posting an artical about an algorithms, and then claiming it works in c fast and in java slow dosent make any impression. I mean seriously, ive got a recursive mergesort that can do a list a few million long in under a .1 seconds so i dont see how its that much slower.
Yes i know, my spelling sucks

IanT
Posts: 153
Joined: Fri Aug 25, 2006 3:13 am

Post by IanT » Thu May 10, 2007 2:10 am

Posting an artical about an algorithms, and then claiming it works in c fast and in java slow dosent make any impression

... especially when said algorithm can be done using the same method in Java (Deus just doesn't know how and assumed it couldn't be done)

I'll do it later when I've finished work :wink:

Ian.

User avatar
oodmb
Posts: 271
Joined: Thu Oct 26, 2006 5:39 am
Location: USA
Contact:

Post by oodmb » Thu May 10, 2007 3:07 am

so nobody has a reason my refraction shouldnt work?
a shiny monkey is a happy monkey

Deus
Posts: 336
Joined: Sun Feb 04, 2007 3:47 am

Post by Deus » Thu May 10, 2007 3:07 am

Littlee matrix multiplication ( you dont do that in a raytracer do you? ;) )

First the java code...

Code: Select all

// $Id: matrix.java,v 1.3 2001/05/27 14:52:57 doug Exp $
// http://www.bagley.org/~doug/shootout/
// modified to use a little less memory by Thomas Holenstein

import java.io.*;
import java.util.*;

public class matrix {
    static int SIZE = 30;

    public static void main(String args[]) {
	int n = Integer.parseInt(args[0]);
	int m1[][] = mkmatrix(SIZE, SIZE);
	int m2[][] = mkmatrix(SIZE, SIZE);
	int mm[][] = new int[SIZE][SIZE];
	for (int i=0; i<n; i++) {
	    mmult(SIZE, SIZE, m1, m2, mm);
	}
	System.out.print(mm[0][0]);
	System.out.print(" ");
	System.out.print(mm[2][3]);
	System.out.print(" ");
	System.out.print(mm[3][2]);
	System.out.print(" ");
	System.out.println(mm[4][4]);
    }

    public static int[][] mkmatrix (int rows, int cols) {
	int count = 1;
	int m[][] = new int[rows][cols];
	for (int i=0; i<rows; i++) {
	    for (int j=0; j<cols; j++) {
		m[i][j] = count++;
	    }
	}
	return(m);
    }

    public static void mmult (int rows, int cols, 
	                      int[][] m1, int[][] m2, int[][] m3) {
	for (int i=0; i<rows; i++) {
	    for (int j=0; j<cols; j++) {
		int val = 0;
		for (int k=0; k<cols; k++) {
		    val += m1[i][k] * m2[k][j];
		}
		m3[i][j] = val;
	    }
	}
    }
}
Got any optimizations? then do them.

Here comes the CPP version

Code: Select all

// -*- mode: c++ -*-
// $Id: matrix.g++,v 1.3 2001/06/20 03:20:02 doug Exp $
// http://www.bagley.org/~doug/shootout/

#include <iostream>
#include <stdlib.h>

using namespace std;

#define SIZE 30

int **mkmatrix(int rows, int cols) {
    int i, j, count = 1;
    int **m = (int **) malloc(rows * sizeof(int *));
    for (i=0; i<rows; i++) {
	m[i] = (int *) malloc(cols * sizeof(int));
	for (j=0; j<cols; j++) {
	    m[i][j] = count++;
	}
    }
    return(m);
}

void zeromatrix(int rows, int cols, int **m) {
    int i, j;
    for (i=0; i<rows; i++)
	for (j=0; j<cols; j++)
	    m[i][j] = 0;
}

void freematrix(int rows, int **m) {
    while (--rows > -1) { free(m[rows]); }
    free(m);
}

int **mmult(int rows, int cols, int **m1, int **m2, int **m3) {
    int i, j, k, val;
    for (i=0; i<rows; i++) {
	for (j=0; j<cols; j++) {
	    val = 0;
	    for (k=0; k<cols; k++) {
		val += m1[i][k] * m2[k][j];
	    }
	    m3[i][j] = val;
	}
    }
    return(m3);
}

int main(int argc, char *argv[]) {
    int i, n = ((argc == 2) ? atoi(argv[1]) : 1);
	
    int **m1 = mkmatrix(SIZE, SIZE);
    int **m2 = mkmatrix(SIZE, SIZE);
    int **mm = mkmatrix(SIZE, SIZE);

    for (i=0; i<n; i++) {
	mm = mmult(SIZE, SIZE, m1, m2, mm);
    }
    cout << mm[0][0] << " " << mm[2][3] << " " << mm[3][2] << " " << mm[4][4] << endl;

    freematrix(SIZE, m1);
    freematrix(SIZE, m2);
    freematrix(SIZE, mm);
    return(0);
}

I am good. Its fine. I dont need to optimize ;-)

Then compile

Code: Select all

$ g++ -O2 -finline-functions matrix.cpp
and java

Code: Select all

javac matrix.java
lets execute with timing on a large matrix

Code: Select all

$ time java matrix 100000
270165 1061760 1453695 1856025

real    0m17.674s
user    0m0.031s
sys     0m0.015s
Java 17.6 seconds not bad!

Lets see what my side of the fence looks like

Code: Select all

$ time ./a.exe 100000
270165 1061760 1453695 1856025

real    0m6.019s
user    0m5.983s
sys     0m0.031s

C++ is 3 times faster.

If you want to look for more examples (also where source for information is) I needed to check for myself ;-)

http://bruscy.multicon.pl/pages/przemek ... 0-411.html

Still java is a nice language! But I'd prefer python if I was going to use that kind of language.

Keep on coding guys. Just get fooled by Suns and the fanbois claims. Think about what people use. There HAS to be a reason for coding stuff in C as I said before.

Case closed :)

/ Deus

User avatar
hstokholm
Posts: 97
Joined: Sat Jul 01, 2006 11:56 am
Location: Denmark

Post by hstokholm » Thu May 10, 2007 3:18 am

Don't know if it's going to help but this way of calculating the refracted ray worked for me in my raytracer:
The directions of the reflected and refracted rays are given by the following formulas. For a ray with direction V, and a surface with normal N (the normal is just the direction perpendicular to the surface - pointing directly away from it), the reflected ray direction Rl is given by
c1 = -dot_product( N, V )
Rl = V + (2 * N * c1 )
Note that since V, N, and Rl are vectors with x, y, and z components, the arithmetic on them is performed per-component. The refracted ray direction Rr is given by
n1 = index of refraction of original medium
n2 = index of refraction of new medium
n = n1 / n2
c2 = sqrt( 1 - n2 * (1 - c12) )

Rr = (n * V) + (n * c1 - c2) * N
from this page I previosly linked to http://www.cs.unc.edu/~rademach/xroads- ... ticle.html
Like a jigsaw piece without a puzzle

IanT
Posts: 153
Joined: Fri Aug 25, 2006 3:13 am

Post by IanT » Thu May 10, 2007 3:21 am

Case closed

Lol, you thought the case was closed from that inverse sqrt example, but your extent of Java "expertise" didn't cover the basic knowledge of Float.floatToIntBits(float f) did it? :wink:

(Nor did you stop to think that a function that returns an approximation to a sqrt function would be totally unusable in a raytracer that demands greater accuracy than that required by a game :roll: )

I'll take a look at the latest single-case-of-proof later on (but a first glance suggests an easy way to speed it up...)

Why do you seem to think that a single example is "proof" when comparing programming languages? Especially when any programmer worth their salt knows not to put Java multi-dimensional array code into a performance-critical loop, sigh :roll:

Ian.

IanT
Posts: 153
Joined: Fri Aug 25, 2006 3:13 am

Post by IanT » Thu May 10, 2007 3:22 am

oodmb wrote:so nobody has a reason my refraction shouldnt work?
I think I do but don't have time to sort it out at the moment (but I will later, I promise)...

Ian.

Post Reply
164 posts

Who is online

Users browsing this forum: No registered users and 178 guests