even with java code at the bottom of the page..
Main Topics
Browse All Topics
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.
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.
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.
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.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
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.
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_PROJ
gl.glLoadIdentity();
gl.glOrtho(-WIDTH / 2, WIDTH / 2, -HEIGHT / 2, HEIGHT / 2, -4 * HEIGHT,
4 * HEIGHT);
gl.glDisable(GL.GL_LIGHTIN
}
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,an
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;
}
}
Business Accounts
Answer for Membership
by: ikeworkPosted on 2009-04-25 at 07:29:50ID: 24232300
well, you are allowed to use OpenGL?
ta/lessons /lesson.as p?lesson=0 5
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/da
if you have a specific question, just ask .. ;)
ike