I have been implementing a raytracing engine for the space sim community project but have noticed, what seemed to me, an unwarranted significant performance issue.
My computer is an Athlon XP 2800
I am able to get the engine to iterate over the pixels of a 640x480 display at over 250fps.
When i add in a array look up it slows down to 190fps
when i add in some quite simple vector math to set up a ray for tracing it slows down to 15fps which i believe is excessive.
I believe i have isolated the problem somewhat to the following (which is in a code fragment listed later in the post)
1
| tempPixel.position.copy(tempVec3D_3); |
By commenting out the above statement my frame rate doubles! This seems quite odd as tempPixel.position is a Vec3D object and the copy method (listed further in the post) simply copies the contents of the given vector!
Can anyone give me some pointers as to why that task is so processor intensive?
This is the code in question. Note that the actual ray tracing call has been commented out.
tile.height and tile.width are 480 and 640 respectively.
pixels array is an array of Pixel objects which have attributes useful for ray tracing, i.e. position, intersection point, display co-ordinates etc.
tempVec3D_3 is an instance of the Vec3D class listed further in the post.
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
| for (intTemp1=0;intTemp1<tile.height;++intTemp1) { for (intTemp2=0;intTemp2<tile.width;++intTemp2) { tempPixel=camera.pixels[intTemp3++];
tempVec3D_3.copy(tempVec); tempVec3D_3.scaleAdd(camera.stepUp,intTemp1); tempVec3D_3.scaleAdd(camera.stepRight,intTemp2); tempPixel.position.copy(tempVec3D_3); tempVec3D_3.subtract(camera.position); tempVec3D_3.unit(); } ++intTemp3; } |
i have the following class which represents a 3d vector and contains the vector operations for it.
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
| package org.moogiesoft.JRTRT;
final public class Vec3D { public double x,y,z; public Vec3D (final Vec3D other) { x=other.x; y=other.y; z=other.z; } public Vec3D () { } public Vec3D (final double x,final double y,final double z) { this.x=x; this.y=y; this.z=z; } final public void zeroise() { x=y=z=0.0; } final public String toString() { return "x= "+x+" y= "+y+" z= "+z; }
final public void scale(final double scaleMuliplier) { x=x * scaleMuliplier; y=y * scaleMuliplier; z=z * scaleMuliplier; }
final public void scaleAdd(final Vec3D other,final double scale) { x+=scale*other.x; y+=scale*other.y; z+=scale*other.z; } final public double dot(final Vec3D other ) { return x*other.x+ y*other.y+ z*other.z; } final public void copySubtract(final Vec3D vec, final Vec3D other ) { x=vec.x-other.x; y=vec.y-other.y; z=vec.z-other.z; } final public void subtract(final Vec3D other ) { x-=other.x; y-=other.y; z-=other.z; } final public void copyAdd(final Vec3D vec, final Vec3D other ) { x=vec.x+other.x; y=vec.y+other.y; z=vec.z+other.z; } final public void add(final Vec3D other ) { x+=other.x; y+=other.y; z+=other.z; } final public void unit() { final double val= 1.0/ Math.sqrt(x*x+y*y+z*z); x*=val; y*=val; z*=val;
} final public void copy(final Vec3D other) { x=other.x; y=other.y; z=other.z; } final public double lengthSquared() { return x*x+ y*y+ z*z; } final public void cap() { x=x>1.0?1:x; y=y>1.0?1:y; z=z>1.0?1:z; } final public void crossProduct(final Vec3D other) { final double oldx=x; final double oldy=y; x=y * other.z - z * other.y; y=z * other.x - oldx * other.z; z=x * other.y - oldy * other.x; } final public void multiply(final Vec3D other) { x *= other.x; y *= other.y; z *= other.z; } final public void copyScale(final Vec3D other,final double scale) { x=other.x*scale; y=other.y*scale; z=other.z*scale; }
} |