Question

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

Asked by: david-lee


  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

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-04-25 at 07:01:04ID24354953
Topic

OpenGL Graphics & Game Programming

Participating Experts
1
Points
500
Comments
9

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Java using MS OLAP cubes
    Hi, I am using Java together with SQL Server and MS Analysis Service, before we used Visual Basic. In Visual Basic I used the Dso.Server COM object to proces all the cubes and the dimensions from the OLAP cubes, BUT how can I proces my cubes/dimensions in the Java language ....
  2. 3D Cube
    Hi, I made a small program that draws a 3D cube. I am very pleased with the result. But now I want to rotate the Cube like a globe. Could you take a look at the code and tell me if it is easy to implement? You can download the project here: http://home.quicknet.nl/qn/prive/...
  3. Java 3D cube
    Hi all, How can I create a simple cube in Java and Html interface.
  4. 3D 'rotating' and 'translating' cube in Java
    Anyone have a good tutorial on how to create a Java applet 3D 'rotating' and 'translating' cube (or any other shape)? ps. It has to strictly be coded in Java (not Java3D or anyother language for that matter) and use only standard Java libraries e.g. java.applet.* or java.awt....
  5. 3d rotating cube in c++
    How can i make a 3D cube in C++ and also rotate it .. using Turbo C++ 3.0 No VC++ or OpenGL or DirectX please.. I want to make it in simple C++

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: ikeworkPosted on 2009-04-25 at 07:29:50ID: 24232300

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

 

by: ikeworkPosted on 2009-04-25 at 07:31:24ID: 24232306

even with java code at the bottom of the page..

 

by: david-leePosted on 2009-04-25 at 15:29:41ID: 24233749


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;
                  }
            }
            

 

by: ikeworkPosted on 2009-04-26 at 00:33:07ID: 24234982

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

thats shown in the link ..

 

by: david-leePosted on 2009-04-26 at 10:09:54ID: 24236762

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

 

by: ikeworkPosted on 2009-04-26 at 13:27:33ID: 24237472

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 ..

 

by: david-leePosted on 2009-04-26 at 14:36:07ID: 24237719

the question for this program is :Implement a ray tracing system  which include sphrere, cube...

 

by: ikeworkPosted on 2009-04-26 at 23:26:10ID: 24239236

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?

 

by: david-leePosted on 2009-04-27 at 14:43:08ID: 24245957

create a scene with sphere, cyclinder, then a ray, calculate the the intersection of ray with objects:sphere e.t.c.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...