Advertisement
Advertisement
| 05.01.2008 at 07:37AM PDT, ID: 23368634 | Points: 500 |
|
[x]
Attachment Details
|
||
| Microsoft |
| Apple |
| Internet |
| Gamers |
| Digital Living |
| Virus & Spyware |
| Hardware |
| Software |
| ITPro |
| Developer |
| Storage |
| OS |
| Database |
| Security |
| Programming |
| Web Development |
| Networking |
| Other |
| Community Support |
| 05.01.2008 at 08:07AM PDT, ID: 21479311 |
| 05.01.2008 at 08:09AM PDT, ID: 21479324 |
| 05.01.2008 at 09:22AM PDT, ID: 21480072 |
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);
}
}
|
| 05.01.2008 at 09:23AM PDT, ID: 21480075 |
| 05.01.2008 at 09:26AM PDT, ID: 21480107 |
| 05.01.2008 at 11:55AM PDT, ID: 21481477 |
| 05.01.2008 at 11:58AM PDT, ID: 21481506 |
| 05.01.2008 at 12:30PM PDT, ID: 21481819 |
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;
}
}
}
}
}
|
| 05.01.2008 at 12:56PM PDT, ID: 21482084 |
| 05.01.2008 at 12:59PM PDT, ID: 21482114 |
| 05.01.2008 at 01:02PM PDT, ID: 21482153 |
| 05.01.2008 at 01:04PM PDT, ID: 21482166 |
| 05.01.2008 at 01:08PM PDT, ID: 21482227 |
| 05.03.2008 at 12:06AM PDT, ID: 21491579 |