Advertisement

05.01.2008 at 07:37AM PDT, ID: 23368634 | Points: 500
[x]
Attachment Details

adaptive supersampling

Tags: opengl
hi everyone
I am trying adding super sampling to my code for a ray tracer and having so much problems. can anyone help how I should do. I attached what I did so far

Thanks
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: dminh01
Question Asked On: 05.01.2008
Participating Experts: 2
Points: 500
Views: 0
Translate:
Loading Advertisement...
05.01.2008 at 08:07AM PDT, ID: 21479311

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.01.2008 at 08:09AM PDT, ID: 21479324

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.01.2008 at 09:22AM PDT, ID: 21480072

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.01.2008 at 09:23AM PDT, ID: 21480075

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.01.2008 at 09:26AM PDT, ID: 21480107

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.01.2008 at 11:55AM PDT, ID: 21481477

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.01.2008 at 11:58AM PDT, ID: 21481506

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.01.2008 at 12:30PM PDT, ID: 21481819

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.01.2008 at 12:56PM PDT, ID: 21482084

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.01.2008 at 12:59PM PDT, ID: 21482114

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.01.2008 at 01:02PM PDT, ID: 21482153

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.01.2008 at 01:04PM PDT, ID: 21482166

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.01.2008 at 01:05PM PDT, ID: 21482187

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.01.2008 at 01:08PM PDT, ID: 21482227

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.03.2008 at 12:06AM PDT, ID: 21491579

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.01.2008 at 08:07AM PDT, ID: 21479311

Rank: Sage

looks like you forgot to make the attachment
 
05.01.2008 at 08:09AM PDT, ID: 21479324

Rank: Master

hi dminh01,

basically you have to render the image in a much higher solution than the current display mode. then you pick some of those high-resolution-pixels and combine them to one pixel in the final resolution

those might help:

http://en.wikipedia.org/wiki/Supersampling
http://www.neoseeker.com/Hardware/faqs/kb/10,72.html

ike
 
05.01.2008 at 09:22AM PDT, ID: 21480072
I will try it in 1hour as I am picking up a friend at the airport now. Thanks for any helps. By the way this is the code
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
import javax.media.opengl.*;
 
public class adaptive_raytracing extends J3_11_rayTracing {
 
    public void display(GLAutoDrawable glDrawable) {
        float[] viewpt = new float[3];
        float[] raypt = new float[3], raypt1 = new float[3], raypt2 = new float[3], raypt3 = new float[3], raypt4 = new float[3];
        // initial ray: viewpt -> raypt
 
        float[] color = new float[3]; // traced color
 
        float[] icolor = new float[3], icolor1 = new float[3], icolor2 = new float[3], icolor3 = new float[3], icolor4 = new float[3]; // traced color
 
 
        // initialize 'ns' number of spheres
        for (int i = 0; i < ns; i++) {
            sphere[i][0] = 10 + WIDTH * Math.random() / 10; // radius
 
            for (int j = 1; j < 4; j++) { // center
 
                sphere[i][j] = -WIDTH / 4 + WIDTH * Math.random() / 2;
            }
        }
 
        // initialize 'nl' light source locations
        for (int i = 0; i < nl; i++) {
            for (int j = 0; j < 3; j++) { // light source positions
 
                lightSrc[i][j] = (float) (- 2 * WIDTH + 4 * WIDTH * Math.random());
            }
        }
 
        // starting viewpoint on positive z axis
        viewpt[0] = 0;
        viewpt[1] = 0;
        viewpt[2] = (float) (1.5 * HEIGHT);
 
        boolean done = false;
 
        // trace rays against the spheres and a plane
        for (double y = -HEIGHT / 2; y < HEIGHT / 2; y++) {
            for (double x = -WIDTH / 2; x < WIDTH / 2; x++) {
 
                // ray from viewpoint to a pixel on the screen
                // tracing the ray (viewpt to raypt) for depth bounces
 
                raypt[0] = (float) x;
                raypt[1] = (float) y;
                raypt[2] = 0;
                rayTracing(icolor, viewpt, raypt, depth);
                gl.glColor3fv(icolor, 0);
 
 
                // adaptive ray tracing
                for (int k = 0; k < 5; k++) {
 
                    double selfx = x, selfy = y;
                    double leftx = x - 0.5, lefty = y;
                    double rightx = x + 0.5, righty = y;
                    double upx = x, upy = y - 0.5;
                    double downx = x, downy = y + 0.5;
 
                    // for the middle value
                    raypt[0] = (float) selfx;
                    raypt[1] = (float) selfy;
                    raypt[2] = 0;
                    rayTracing(icolor, viewpt, raypt, depth);
 
                    // for the leftvalue
                    raypt1[0] = (float) leftx;
                    raypt1[1] = (float) lefty;
                    raypt1[2] = 0;
                    rayTracing(icolor1, viewpt, raypt1, depth);
 
                    // for the value right
                    raypt2[0] = (float) rightx;
                    raypt2[1] = (float) righty;
                    raypt2[2] = 0;
                    rayTracing(icolor2, viewpt, raypt2, depth);
 
                    // for the up value
                    raypt3[0] = (float) upx;
                    raypt3[1] = (float) upy;
                    raypt3[2] = 0;
                    rayTracing(icolor3, viewpt, raypt3, depth);
 
                    // for the down value
                    raypt4[0] = (float) downx;
                    raypt4[1] = (float) downy;
                    raypt4[2] = 0;
                    rayTracing(icolor4, viewpt, raypt4, depth);
 
                    float[] temp = getColorAverage(icolor, icolor1, icolor2, icolor3, icolor4);
 
 
                    if (temp[0] < 0.1 || temp[1] < 0.1 || temp[2] < 0.1) {
 
                        for (int i = 0; i < 3; i++) {
                            color[i] = temp[i];
                        }
                        gl.glColor3fv(color, 0);
                        gl.glViewport(0, 0, WIDTH, HEIGHT);
                        drawPoint(x, y);
                        done = true;
 
                    } else {
 
                        x = x / 2;
                        y = y / 2;
                        done = false;
                    }
 
                }
 
            }
        }
    }
 
    public float[] getColorAverage(float[] c, float[] c1, float[] c2, float[] c3, float[] c4) {
        float[] averageA = new float[3];
 
        for (int i = 0; i < 3; i++) {
            averageA[i] = (((c[i] - c1[i]) + (c[i] - c2[i]) + (c[i] - c3[i]) + (c[i] - c4[i])) / 4);
        }
 
        return averageA;
    }
 
    public static void main(String[] args) {
        adaptive_raytracing f = new adaptive_raytracing();
 
        f.setTitle("Adaptive ray tracing by Duc Tran");
        f.setSize(WIDTH, HEIGHT);
        f.setVisible(true);
    }
}
Open in New Window
 
05.01.2008 at 09:23AM PDT, ID: 21480075


import javax.media.opengl.*;



public class J3_11_rayTracing extends J3_10_Lights {



    double[][] sphere = new double[4][4]; // 4 spheres with radius and center



    static int ns = 3; // number of spheres



    float[][] lightSrc = new float[3][3]; // 3 light sources



    static int nl = 3; // number of light sources



    static int depth = 5; // depth of ray tracing recursion



    static int yplane = -HEIGHT / 8; // a reflective plane





    public void reshape(GLAutoDrawable glDrawable, int x, int y, int w, int h) {



        gl.glMatrixMode(GL.GL_PROJECTION);

        gl.glLoadIdentity();



        gl.glOrtho(-WIDTH / 2, WIDTH / 2, -HEIGHT / 2, HEIGHT / 2, -4 * HEIGHT,

                4 * HEIGHT);



        gl.glDisable(GL.GL_LIGHTING); // calculate color by ray tracing



    }



    public void display(GLAutoDrawable glDrawable) {

        float[] viewpt = new float[3], raypt = new float[3];

        // initial ray: viewpt -> raypt



        float[] color = new float[3]; // traced color



        // initialize 'ns' number of spheres

        for (int i = 0; i < ns; i++) {

            sphere[i][0] = 10 + WIDTH * Math.random() / 10; // radius



            for (int j = 1; j < 4; j++) { // center



                sphere[i][j] = -WIDTH / 4 + WIDTH * Math.random() / 2;

            }

        }



        // initialize 'nl' light source locations

        for (int i = 0; i < nl; i++) {

            for (int j = 0; j < 3; j++) { // light source positions



                lightSrc[i][j] = (float) (-8 * WIDTH + 40 * WIDTH * Math.random());

            }

        }



        // starting viewpoint on positive z axis

        viewpt[0] = 0;

        viewpt[1] = 0;

        viewpt[2] = (float) (1.5 * HEIGHT);



        // trace rays against the spheres and a plane

        for (float y = -HEIGHT / 2; y < HEIGHT / 2; y++) {

            for (float x = -WIDTH / 2; x < WIDTH / 2; x++) {



                // ray from viewpoint to a pixel on the screen

                raypt[0] = x;

                raypt[1] = y;

                raypt[2] = 0;



                // tracing the ray (viewpt to raypt) for depth bounces

                rayTracing(color, viewpt, raypt, depth);

                gl.glColor3fv(color, 0);

                drawPoint(x, y);

            }

        }

    }



    // recursive rayTracing from vpt to rpt for depth bounces, finding final color

    public void rayTracing(float[] color, float[] vpt, float[] rpt, int depth) {



        float[] reflectClr = new float[3], transmitClr = new float[3];

        float[] rpoint = new float[3]; // a point on ray direction



        float[] rD = new float[3]; // ray direction



        float[] vD = new float[3]; // view direction



        float[] n = new float[3]; // normal



        float[] p = new float[3]; // intersection point



        for (int i = 0; i < 3; i++) {

            color[i] = 0;

        }



        if (depth != 0) {// calculate color



            // find intersection of ray from vpt to rpt

            // with the closest object or the background

            intersect(vpt, rpt, p, n); // intersect at p with normal n



            // calculate lighting of the intersection point

            if (n[0] * n[0] + n[1] * n[1] + n[2] * n[2] > 0.001) {



                // view direction vector for lighting and reflection

                for (int i = 0; i < 3; i++) {

                    vD[i] = vpt[i] - rpt[i];

                }



                normalize(n);

                normalize(vD);



                // calculate color using Phong shading

                phong(color, p, vD, n);



                // reflected ray

                reflect(vD, n, rD);



                for (int i = 0; i < 3; i++) {

                    // a point on the reflected ray starting from p

                    rpoint[i] = rD[i] + p[i];

                }



                // recursion to find a bounce at lower level

                rayTracing(reflectClr, p, rpoint, depth - 1);



                for (int i = 0; i < 3; i++) {

                    color[i] = (float) (color[i] + 0.9 * reflectClr[i]);

                   

                    if (color[i] > 1) //color values are not normalized.

                    {

                        color[i] = 1;

                    }

                }

            }

        }

    }



    public void phong(float[] color, float[] point, float[] vD, float[] n) {

        float[] s = new float[3]; // light source direction + view direction



        float[] lgtsd = new float[3]; // light source direction



        float[] inormal = new float[3]; // for shadow



        float[] ipoint = new float[3]; // for shadow



        for (int i = 0; i < nl; i++) {



            // if intersect objects between light source, point in shadow

            intersect(point, lightSrc[i], ipoint, inormal);



            if (inormal[0] * inormal[0] + inormal[1] * inormal[1] + inormal[2] * inormal[2] < 0.001) { // point not in shadow



                for (int j = 0; j < 3; j++) {

                    lgtsd[j] = lightSrc[i][j] - point[j];

                // light source direction

                }

                normalize(lgtsd);

                for (int j = 0; j < 3; j++) {

                    s[j] = lgtsd[j] + vD[j]; // for specular term



                }

                normalize(s);



                float diffuse = dotprod(lgtsd, n);

                double specular = Math.pow(dotprod(s, n), 100);



                if (diffuse < 0) {

                    diffuse = 0;

                }

                if (specular < 0) {

                    specular = 0;

                }

                // 3 color channels correspond to 3 light sources

                color[i] = (float) (0.5 * diffuse + 0.7 * specular);

            }



        }

    }



    public void intersect(float[] vpt, float[] rpt, float[] point,

            float[] normal) {



        // calculate intersection of ray with the closest sphere

        // Ray equation:

        // x/y/z = vpt + t*(rpt - vpt);

        // Sphere equation:

        // (x-cx)^2 + (y-cy)^2 + (z-cz)^2 = r^2;

        // We can solve quadratic formula for t and find the intersection

        // t has to be > 0 to intersect with an object

        float t = 0;

        float a, b, c, d, e, f; // temp for solving the intersection



        normal[0] = 0;

        normal[1] = 0;

        normal[2] = 0;



        for (int i = 0; i < ns; i++) {

            a = (float) (vpt[0] - sphere[i][1]);

            b = rpt[0] - vpt[0];

            c = (float) (vpt[1] - sphere[i][2]);

            d = rpt[1] - vpt[1];

            e = (float) (vpt[2] - sphere[i][3]);

            f = rpt[2] - vpt[2];



            float A = b * b + d * d + f * f;

            float B = 2 * (a * b + c * d + e * f);

            float C = (float) (a * a + c * c + e * e - sphere[i][0] * sphere[i][0]);



            float answers[] = new float[2];



            if (quadraticFormula(A, B, C, answers)) {// intersection



                if (answers[0] < answers[1]) {

                    t = answers[0];

                } else {

                    t = answers[1];

                }

                if (t < 0.001) {

                    t = 0;

                    break;

                } else {

                    // return point and normal

                    point[0] = vpt[0] + t * (rpt[0] - vpt[0]);

                    point[1] = vpt[1] + t * (rpt[1] - vpt[1]);

                    point[2] = vpt[2] + t * (rpt[2] - vpt[2]);

                    normal[0] = (float) (point[0] - sphere[i][1]);

                    normal[1] = (float) (point[1] - sphere[i][2]);

                    normal[2] = (float) (point[2] - sphere[i][3]);

                }

            }

        }



        // calculate ray intersect with plane y = yplane

        // y = vpt + t(rpt - vpt) = yplane; => t = (yplane - vpt)/(rpt -vpt);



        float tmp = (yplane - vpt[1]) / (rpt[1] - vpt[1]);

        float[] ipoint = new float[3]; // for shadow



        if ((tmp > 0.001) && (tmp < t || t == 0)) {

            t = tmp;

            ipoint[0] = vpt[0] + t * (rpt[0] - vpt[0]);

            ipoint[1] = yplane;

            ipoint[2] = vpt[2] + t * (rpt[2] - vpt[2]);

            // if x&z in the rectangle, intersect with plane

            if ((ipoint[0] > -HEIGHT / 2) && (ipoint[0] < HEIGHT / 2) && (ipoint[2] > -HEIGHT / 2) && (ipoint[2] < HEIGHT / 2) && t > 0) {

                // plane normal

                point[0] = ipoint[0];

                point[1] = ipoint[1];

                point[2] = ipoint[2];

                normal[0] = 0;

                normal[1] = 1;

                normal[2] = 0;

            }

        }





        // calculate ray intersect with zplane = yplane*8

        // z = vpt + t(rpt - vpt) = zplane; => t = (zplane - vpt)/(rpt -vpt);

        tmp = (yplane * 8 - vpt[2]) / (rpt[2] - vpt[2]);

        if ((tmp > 0.001) && (tmp < t || t == 0)) {

            t = tmp;

            ipoint[0] = vpt[0] + t * (rpt[0] - vpt[0]);

            ipoint[2] = yplane * 8;

            ipoint[1] = vpt[1] + t * (rpt[1] - vpt[1]);

            // if x&z in the rectangle, intersect with plane

            if ((ipoint[0] > -HEIGHT / 2) && (ipoint[0] < HEIGHT / 2) && (ipoint[1] > -HEIGHT / 2) && (ipoint[1] < HEIGHT / 2) && t > 0) {

                // plane normal

                point[0] = ipoint[0];

                point[1] = ipoint[1];

                point[2] = ipoint[2];

                normal[0] = 0;

                normal[1] = 0;

                normal[2] = 1;

            }

        }

    }



    public static void main(String[] args) {

        J3_11_rayTracing f = new J3_11_rayTracing();



        f.setTitle("rayTracing");

        f.setSize(WIDTH, HEIGHT);

        f.setVisible(true);

    }

}

 
05.01.2008 at 09:26AM PDT, ID: 21480107
I kind of understand the concept but when I tried to program this in Java it does not seems to work well. Any helps are so apreciate
 
05.01.2008 at 11:55AM PDT, ID: 21481477

Rank: Master

you dont need opengl if you write a ray-tracer, you can take any 2d-graphics tool to render the points on the screen. opengl is a raytracer itsself.

depending on your sample-count per pixel you need to render your scene more than once with a slightly change of the camera-position. render to a *framebuffer* in each cycle and add the resulting colours there for each point. at the end divide each pixels value in the framebuffer by the count of samples. then draw the framebuffer to the screen.

btw. you would improve your code(and its readability)  if you use structures instead of arrays. float[3] can be writte as a class point3D or someting.
 
05.01.2008 at 11:58AM PDT, ID: 21481506

Rank: Master

>> Any helps are so apreciate

can you ask a specific question? where excactly do you encounter problems?
 
05.01.2008 at 12:30PM PDT, ID: 21481819
Basically I wrote the code and dont know why it take forever to render without showing anything up
This is attached algorithm that i used for adaptive super sampling. anyone please tell me what problem this is

thanks
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
    public void display(GLAutoDrawable glDrawable) {
        float[] viewpt = new float[3];
 
        float[] raypt = new float[3], raypt1 = new float[3], raypt2 = new float[3], raypt3 = new float[3], raypt4 = new float[3];
        // initial ray: viewpt -> raypt
 
        float[] color = new float[3]; // traced color
 
        float[] icolor = new float[3], icolor1 = new float[3], icolor2 = new float[3], icolor3 = new float[3], icolor4 = new float[3]; // traced color
 
        float[] avgColor = new float[3];
        float[] diffColor = new float[3];
 
 
        for (int l = 0; l < 3; l++) {
            icolor[l] = 0;
            icolor1[l] = 0;
            icolor2[l] = 0;
            icolor3[l] = 0;
            icolor4[l] = 0;
            avgColor[l] = 0;
            diffColor[l] = 0;
        }
 
        // initialize 'ns' number of spheres
        for (int i = 0; i < ns; i++) {
            sphere[i][0] = 10 + WIDTH * Math.random() / 10; // radius
 
            for (int j = 1; j < 4; j++) { // center
 
                sphere[i][j] = -WIDTH / 4 + WIDTH * Math.random() / 2;
            }
        }
 
        // initialize 'nl' light source locations
        for (int i = 0; i < nl; i++) {
            for (int j = 0; j < 3; j++) { // light source positions
 
                lightSrc[i][j] = (float) (-8 * WIDTH + 40 * WIDTH * Math.random());
            }
        }
 
        // starting viewpoint on positive z axis
        viewpt[0] = 0;
        viewpt[1] = 0;
        viewpt[2] = (float) (1.5 * HEIGHT);
        boolean done = false;
        int count = 0;
        
        // trace rays against the spheres and a plane
        for (double y = -HEIGHT+10 / 2; y < HEIGHT / 2; y++) {
            for (double x = -WIDTH+10 / 2; x < WIDTH / 2; x++) {
 
                // adaptive ray tracing
                while (!done || count<2) {
 
                    // value of the middle points and 4 corner points
                    double selfx = x, selfy = y;
 
                    double upleftx = x - 0.5, uplefty = y + 0.5;
                    double uprightx = x + 0.5, uprighty = y + 0.5;
                    double downleftx = x - 0.5, downlefty = y - 0.5;
                    double downrightx = x + 0.5, downrighty = y - 0.5;
 
                    // for the middle value
                    raypt[0] = (float) selfx;
                    raypt[1] = (float) selfy;
                    raypt[2] = 0;
                    rayTracing(icolor, viewpt, raypt, depth);
 
 
                    // for the leftvalue
                    raypt1[0] = (float) upleftx;
                    raypt1[1] = (float) uplefty;
                    raypt1[2] = 0;
                    rayTracing(icolor1, viewpt, raypt1, depth);
 
 
                    // for the value right
                    raypt2[0] = (float) uprightx;
                    raypt2[1] = (float) uprighty;
                    raypt2[2] = 0;
                    rayTracing(icolor2, viewpt, raypt2, depth);
 
 
                    // for the up value
                    raypt3[0] = (float) downleftx;
                    raypt3[1] = (float) downlefty;
                    raypt3[2] = 0;
                    rayTracing(icolor3, viewpt, raypt3, depth);
 
 
                    // for the down value
                    raypt4[0] = (float) downrightx;
                    raypt4[1] = (float) downrighty;
                    raypt4[2] = 0;
                    rayTracing(icolor4, viewpt, raypt4, depth);
 
                    avgColor = getColorAverage(icolor1, icolor2, icolor3, icolor4);
 
                    for (int j = 0; j < 3; j++) {
 
                        diffColor[j] = Math.abs(avgColor[j] - icolor[j]);
                    }
                    if (diffColor[0] < 0.0001 || diffColor[1] < 0.0001 || diffColor[2] < 0.0001) {
 
                        for (int i = 0; i < 3; i++) {
                            color[i] = color[i] + diffColor[i];
                        }
                        done = true;
                        x = selfx;
                        y = selfy;
 
                    } else {
                        done = false;
                        count++;
                        x = selfx / 2;
                        y = selfy / 2;
                    }
 
                }
 
                gl.glColor3fv(color, 0);
                gl.glViewport(0, 0, WIDTH, HEIGHT);
                drawPoint(x, y);
            }
        }
 
 
    }
 
    public float[] getColorAverage(float[] c1, float[] c2, float[] c3, float[] c4) {
 
        float[] averageA = new float[3];
 
        for (int i = 0; i < 3; i++) {
            averageA[i] = (c1[i] + c2[i] + c3[i] + c4[i]) / 4;
            System.out.println("Average Color       : " + averageA[i]);
        }
 
        return averageA;
    }
 
    // recursive rayTracing from vpt to rpt for depth bounces, finding final color
    public void rayTracing(float[] color, float[] vpt, float[] rpt, int depth) {
 
        float[] reflectClr = new float[3], transmitClr = new float[3];
        float[] rpoint = new float[3]; // a point on ray direction
 
        float[] rD = new float[3]; // ray direction
 
        float[] vD = new float[3]; // view direction
 
        float[] n = new float[3]; // normal
 
        float[] p = new float[3]; // intersection point
 
 
        for (int i = 0; i < 3; i++) {
            color[i] = 0;
        }
 
        if (depth != 0) {// calculate color
 
            // find intersection of ray from vpt to rpt
            // with the closest object or the background
            intersect(vpt, rpt, p, n); // intersect at p with normal n
 
            // calculate lighting of the intersection point
            if (n[0] * n[0] + n[1] * n[1] + n[2] * n[2] > 0.001) {
 
                // view direction vector for lighting and reflection
                for (int i = 0; i < 3; i++) {
                    vD[i] = vpt[i] - rpt[i];
                }
 
                normalize(n);
                normalize(vD);
 
                // calculate color using Phong shading
                phong(color, p, vD, n);
 
                // reflected ray
                reflect(vD, n, rD);
 
                for (int i = 0; i < 3; i++) {
                    // a point on the reflected ray starting from p
                    rpoint[i] = rD[i] + p[i];
                }
 
                // recursion to find a bounce at lower level
                rayTracing(reflectClr, p, rpoint, depth - 1);
 
                for (int i = 0; i < 3; i++) {
                    color[i] = (float) (color[i] + 0.9 * reflectClr[i]);
 
                    if (color[i] > 1) //color values are not normalized. 
                    {
                        color[i] = 1;
                    }
                }
            }
        }
    }
Open in New Window
 
05.01.2008 at 12:56PM PDT, ID: 21482084

Rank: Master

>> anyone please tell me what problem this is

mmh .. did you read my post? please answer the question what problems you have excactly. otherwise its hard to help you.
if you show us code, please explain what you expect the code to do and what it is doing wron. only showing some code without context is not helpful..
 
05.01.2008 at 12:59PM PDT, ID: 21482114

Rank: Master

i just realised you have 2 questions open for the same topic. thats against the rules of ee.
and its not easy to follow anyway.

please choose one of them to close and one to keep open. otherwise its hard to follow for us. im gonna make a community-support request for it
 
05.01.2008 at 01:02PM PDT, ID: 21482153

Rank: Master

fyi, here is the community support request:

http://www.experts-exchange.com/Community_Support/General/Q_23369846.html

it might help the moderators if you tell them there which question to close
 
05.01.2008 at 01:04PM PDT, ID: 21482166
sorry about that. my code is implementing the adaptive supersampling which is as follows:
get a middle point
compare the color of that middle point with the 4 points around it.
if the color is not much different then we will use the average color of those 4 points for the middle point
else keep dividing these points to smaller distance and compare the 5 new points again.


basically the rayTracing will give me the color of the point then i use that color for the purpose above.
I think i implemented it correctly but when running the code it only show black screen.
 
05.01.2008 at 01:05PM PDT, ID: 21482187
thanks for your posting. I will try to delete one
 
05.01.2008 at 01:08PM PDT, ID: 21482227

Rank: Master

>> I think i implemented it correctly but when running the code it only show black screen.

then try to debug it. set breakpoints and go into the code to find out where the problem is..
 
05.03.2008 at 12:06AM PDT, ID: 21491579

Rank: Master

can you dump out x, y and its color before you draw the points?
 
 
20080236-EE-VQP-29 / EE_QW_EXPERT_20070906