Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1006
  • Last Modified:

want to have cube and cyclinder object in ray tracing project with java and opengl


  I want to create a cube and cyclinder object in raytracing project in java but do not know how to start.
Can any one give me a help or advise.Thanks
0
david-lee
Asked:
david-lee
  • 5
  • 4
1 Solution
 
ikeworkCommented:
well, you are allowed to use OpenGL?

if yes, here is a great bunch of tutorials at nehe:
look at the left bar

http://nehe.gamedev.net/

particulary lesson 5 called "3D Shapes":

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=05

if you have a specific question, just ask .. ;)

ike
0
 
ikeworkCommented:
even with java code at the bottom of the page..
0
 
david-leeAuthor Commented:

I do not see anything related to raytracer.  below is a code segment in my program it shows a sphere, now i want to add a cube and a cyclinder, how will i create a cube, anyone give me a hint, help.thanks
double[][] sphere = new double[4][4]; // 4 spheres with radius and center
      static int ns = 1; // number of spheres
      
      double[][] cyclinder = new double[4][4]; //
      static int ncy = 1; // number of spheres


      double[][] lightSrc = new double[3][3]; // 3 light sources
      static int nl = 3; // number of light sources

      static int depth = 5; //
      static int yplane = -HEIGHT / 10; // 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) {
            double[] viewpt = new double[3], raypt = new double[3];
      
            double[] color = new double[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(declare as i,j)
            for (int i = 0; i < nl; i++) {
                  for (int j = 0; j < 3;j++) { // light source positions
                        lightSrc[i][j] = -8 * WIDTH + 40 * WIDTH * Math.random();
                  }
                  
//initialze ncy number of cyclinder
                  
                  for (int k = 0; k < ncy; k++) {
                        cyclinder[k][0] = 10 + WIDTH * Math.random() / 10; // radius
                        for (int l = 1; l < 4; l++) { // center
                              cyclinder[k][l] = -WIDTH / 4 + WIDTH * Math.random() / 2;
                        }
                        
                        
            }

            // starting viewpoint on positive z axis
            viewpt[0] = 0;
            viewpt[1] = 0;
            viewpt[2] = 2*HEIGHT;

            // trace rays against the spheres and a plane
            for (double y = -HEIGHT /4; y < HEIGHT / 4; y++) {
                  for (double x = -WIDTH /4; x < WIDTH /4; 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.glColor3dv(color, 0);
                        drawPoint(x, y);
                  }
                  }
            }
            
            }
      

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

            double[] reflectClr = new double[3], transmitClr = new double[3];
            double[] rpoint = new double[3]; //
            double[] rD = new double[3];
            double[] vD = new double[3];
            double[] n = new double[3];
            double[] p = new double[3];

            for (int i = 0; i < 3; i++) {
                  color[i] = 0;
            }

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

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

            
                  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];
                        }
                        
                  
                        rayTracing(reflectClr, p, rpoint, depth - 1);
                        //rayTracing(transmitClr, p, rpoint, depth - 1);
                        for (int i = 0; i < 3; i++) {
                              color[i] = (color[i] + 0.9 * reflectClr[i]);
                              if (color[i] > 1) //color values are not normalized.
                                    color[i] = 1;
                        }
                  }
            }
      }

      public void phong(double[] color, double[] point, double[] vD, double[] n) {
            double[] s = new double[3]; // light source direction + view direction
            double[] lgtsd = new double[3]; // light source direction
            double[] inormal = new double[3]; // for shadow
            double[] ipoint = new double[3]; // for shadow

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

            
                  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];
                              
                        }
                        normalize(lgtsd);
                        for (int j = 0; j < 3; j++) {
                              s[j] = lgtsd[j] + vD[j]; // for specular term
                        }
                        normalize(s);

                        double 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] = 0.5 * diffuse + 0.7 * specular;
                  }
            //      color[1] = color[2] = color[0];
            }
      }

public void intersect(double[] vpt, double[] rpt, double[] point,
                  double[] normal) {
double t = 0;
            double a, b, c, d, e, f;
            normal[0] = 0;
            normal[1] = 0;
            normal[2] = 0;

            for (int i = 0; i < ns; i++) {
                  a = vpt[0] - sphere[i][1];
                  b = rpt[0] - vpt[0];
                  c = vpt[1] - sphere[i][2];
                  d = rpt[1] - vpt[1];
                  e = vpt[2] - sphere[i][3];
                  f = rpt[2] - vpt[2];

                  double A = b * b + d * d + f * f;
                  double B = 2 * (a * b + c * d + e * f);
                  double C = a * a + c * c + e * e - sphere[i][0] * sphere[i][0];

                  double answers[] = new double[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 {
                              
                              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] = point[0] - sphere[i][1];
                              normal[1] = point[1] - sphere[i][2];
                              normal[2] = point[2] - sphere[i][3];
                        }
                  }
            }
            
            // y = vpt + t(rpt - vpt) = yplane; => t = (yplane - vpt)/(rpt -vpt);

            double tmp = (yplane - vpt[1]) / (rpt[1] - vpt[1]);
            double[] ipoint = new double[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;
                  }
            }
            
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ikeworkCommented:
>> now i want to add a cube and a cyclinder, how will i create a cube

thats shown in the link ..
0
 
david-leeAuthor Commented:
my project is to make a ray tracer with 3 objects: sphere and cube and cyclinder.  the sphere i had already.  sample source code in the link just show to draw 3d object not to trace ray hit the object.thank you for your help
0
 
ikeworkCommented:
ray-hit-tests is different from ray-tracing, you want to render a box and do ray-intersection-tests with it (hit-tests)? opengl is a ray-tracer, you dont make a ray-tracer, you use one ..
0
 
david-leeAuthor Commented:
the question for this program is :Implement a ray tracing system  which include sphrere, cube...
0
 
ikeworkCommented:
and what excactly is meant by "ray tracing system"? draw the objects and then make hit tests with the mouse, sending a ray down the mouse position?
0
 
david-leeAuthor Commented:
create a scene with sphere, cyclinder, then a ray, calculate the the intersection of ray with objects:sphere e.t.c.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now