Im currently trying to make a very simple raytracer. Now ive got this big problem. As shown in the image below the image output is incorrect, however this only happens when I use view plane dimensions that are not the same. (100 x 100 works but 800 x 600 gives the strange output). This has been bugging me for days and I wonder if anyone can spot where ive gone wrong.
Ill include the camera ray spawn code and the render code, minus the intersection code as I know this is correct. Ill also include the tga file creator as that might be the problem.
Thanks for your help
-Alex
render code (inc ray camera spawn):
public void spawnCameraRays(int imageDimX, int imageDimY){
_raySpawnTable = new Vector3f[imageDimX][imageDimY];
_cameraPos = new Vector3f();
//Vars to hold half screen width and height
float halfWidth = (float)imageDimX / 2;
float halfHeight = (float)imageDimY / 2;
//loop through every pixel and generate ray directions
for(int y = 0; y < imageDimY; y++){
for(int x = 0; x < imageDimX; x++){
_raySpawnTable[x][y] = new Vector3f(x - halfWidth, y - halfHeight, 255);
_raySpawnTable[x][y].normalize();
}
}
//Assign member variables the dimensions for memory clean up
_cleanX = imageDimX;
_cleanY = imageDimY;
}
public void cameraRayClean(){
//Drop hint to GC to clean memory
for(int y = 0; y < _cleanY; y++){
for(int x = 0; x < _cleanX; x++)
_raySpawnTable[x][y] = null;
}
_raySpawnTable = null;
}
public boolean traceScene(primitiveObjects.p_sphere obj, viewPlane vPlane, int vpX, int vpY){ // Change when Scene Inc
//Camera Ray;
ray cameraRay = new ray();
cameraRay.setOrigin(new Vector3f(0.0f, 0.0f, -600.0f));
//Loop through all pixels in view plane and trace the cameraRay, based on raySpawnTable;
if (_raySpawnTable == null || vPlane == null)
return false;
for (int iterX = 0; iterX < vpX; iterX++){
int rgb = 0; //Loop Iterator for 3 rgb colours
for(int iterY = 0; iterY < vpY; iterY++, rgb+=3){
cameraRay.setDirection(_raySpawnTable[iterX][iterY]);
rayTraceResult result;
result = obj.calcIntersection(cameraRay);
if(result.getHit() == true){
vPlane._viewPlane[iterX][rgb + 0] = obj._r;
vPlane._viewPlane[iterX][rgb + 1] = obj._g;
vPlane._viewPlane[iterX][rgb + 2] = obj._b;
//System.out.print("111");
}
else{
vPlane._viewPlane[iterX][rgb + 0] = 111;
vPlane._viewPlane[iterX][rgb + 1] = 111; //RGB Black (More Elegant?)
vPlane._viewPlane[iterX][rgb + 2] = 111;
//System.out.print("000");
}
}
//System.out.println("");
}
return true;
}
//Variables
private Vector3f _raySpawnTable[][];
private Vector3f _cameraPos;
Tga out put code:
public boolean writeTGA(String fileName, viewPlane vPlane){
char tgaHeader[] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char header[] = new char [6];
char bits = 24;
int colourMode = 3;
header[0] = (char)(vPlane.getVPlaneX() % 256);
header[1] = (char)(vPlane.getVPlaneX() / 256);
header[2] = (char)(vPlane.getVPlaneY() % 256);
header[3] = (char)(vPlane.getVPlaneY() / 256);
header[4] = bits;
header[5] = 0;
try{
tgaOutFile = new File(fileName);
tgaOutFile.createNewFile();
}
catch(IOException e){
//EXCEPTION HERE
System.out.println("Error Creating File");
}
try{
tgaOutStream = new FileOutputStream(fileName);
}
catch(FileNotFoundException e){
System.out.println("");
System.out.println("Error Opening File");
//EXCEPTION HERE
}
try{
for(int i = 0; i < 12; i++)
tgaOutStream.write(tgaHeader[i]);
for(int i = 0; i < 6; i++)
tgaOutStream.write(header[i]);
//Switch RGB -> BGR
for (int iterX = 0; iterX < vPlane.getVPlaneX(); iterX++){
for(int iterY = 0; iterY < (vPlane.getVPlaneY() * 3); iterY+=colourMode){
char tempColour = vPlane._viewPlane[iterX][iterY];
vPlane._viewPlane[iterX][iterY] = vPlane._viewPlane[iterX][iterY + 2];
vPlane._viewPlane[iterX][iterY + 2] = tempColour;
}
}
//Write Data Image To File
for (int iterX = 0; iterX < vPlane.getVPlaneX(); iterX++){
for(int iterY = 0; iterY < vPlane.getVPlaneY() * 3; iterY++){
tgaOutStream.write(vPlane._viewPlane[iterX][iterY]);
}
}
tgaOutStream.close(); //Give Resource Back to VM
}
catch(IOException e){
//EXCEPTION HERE
System.out.println("Error Closing or writing File");
}
return true;
}
private FileOutputStream tgaOutStream;
private File tgaOutFile;
the strange output as unequal resolutions:
http://img.photobuck...ytraceError.jpg
Thanks Again!












