Archived

This topic is now archived and is closed to further replies.

Ray Tracing Up-side-down

This topic is 5016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

http://www.2tothex.com/raytracing/basic.html I''ve been searching and searching for 1,5 hours now. I can''t seem to find any not differing points in my sourcecode and his. But still, the pixels are up-side-down. It is nothing with my rendering system, since other rendering is fine. When I flip the y (in my ray-directions), it works like it should. Has anyone experienced this, and knows the answer? -- You''re Welcome, Rick Wong - sitting in his chair doing the most time-consuming thing..

Share this post


Link to post
Share on other sites
Hmm.. I''m using SDL.

    for(int y = 0; y < height; y++ )
{
for(int x = 0; x < width; x++ )
{
vDir = directionTable[x+y*width];

Raytrace( vStart, vDir, screen, color );

Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
*(Uint16 *)p = color;
}
}


--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..

Share this post


Link to post
Share on other sites
cVector* GenerateDirectionTable( int width, int height )
{
cVector *direction = new cVector[width*height];

for(int y=0; y<height; y++)
{
for(int x=0; x<width; x++)
{
cVector &currDirection = direction[x+y*width];
currDirection.mX = x - width/2;
currDirection.mY = y - height/2; // if I negate this, it works

currDirection.mZ = 256;
currDirection.Normalize();
}
}

return direction;
}


--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..

Share this post


Link to post
Share on other sites
I can provide more code, here is my cVector class:

#include "cVector.h"


/*
Reading from class
*/

cVector cVector::operator+ ( cVector Vec ) const
{
cVector tVec( mX + Vec.mX,
mY + Vec.mY,
mZ + Vec.mZ );
return tVec;
}
cVector cVector::operator- ( cVector Vec ) const
{
cVector tVec( mX - Vec.mX,
mY - Vec.mY,
mZ - Vec.mZ );
return tVec;
}
cVector cVector::operator* ( cVector Vec ) const
{
cVector tVec( mX * Vec.mX,
mY * Vec.mY,
mZ * Vec.mZ );
return tVec;
}
cVector cVector::operator/ ( cVector Vec ) const
{
cVector tVec( mX / Vec.mX,
mY / Vec.mY,
mZ / Vec.mZ );
return tVec;
}


cVector cVector::operator- () const
{
cVector tVec( -mX,
-mY,
-mZ );
return tVec;
}


/*
Writing to class
*/

cVector cVector::operator= ( cVector Vec )
{
cVector tVec( (mX = Vec.mX),
(mY = Vec.mY),
(mZ = Vec.mZ) );
return tVec;
}
cVector cVector::operator+= ( cVector Vec )
{
cVector tVec( (mX += Vec.mX),
(mY += Vec.mY),
(mZ += Vec.mZ) );
return tVec;
}
cVector cVector::operator-= ( cVector Vec )
{
cVector tVec( (mX -= Vec.mX),
(mY -= Vec.mY),
(mZ -= Vec.mZ) );
return tVec;
}
cVector cVector::operator*= ( cVector Vec )
{
cVector tVec( (mX *= Vec.mX),
(mY *= Vec.mY),
(mZ *= Vec.mZ) );
return tVec;
}
cVector cVector::operator/= ( cVector Vec )
{
cVector tVec( (mX /= Vec.mX),
(mY /= Vec.mY),
(mZ /= Vec.mZ) );
return tVec;
}


/*
Comparing to class (Read only too)
*/

bool cVector::operator== ( cVector Vec ) const
{
if( mX == Vec.mX )
if( mY == Vec.mY )
if( mZ == Vec.mZ )
return true;
return false;
}
bool cVector::operator!= ( cVector Vec ) const
{
if( mX == Vec.mX )
if( mY == Vec.mY )
if( mZ == Vec.mZ )
return false; // NOTE: false if everything is equal

return true; // NOTE: true if something is not equal

}
bool cVector::operator> (cVector Vec ) const
{
if( mX > Vec.mX )
if( mY > Vec.mY )
if( mZ > Vec.mZ )
return true;
return false;
}
bool cVector::operator>= (cVector Vec ) const
{
if( mX >= Vec.mX )
if( mY >= Vec.mY )
if( mZ >= Vec.mZ )
return true;
return false;
}
bool cVector::operator< (cVector Vec ) const
{
if( mX < Vec.mX )
if( mY < Vec.mY )
if( mZ < Vec.mZ )
return true;
return false;
}
bool cVector::operator<= (cVector Vec ) const
{
if( mX <= Vec.mX )
if( mY <= Vec.mY )
if( mZ <= Vec.mZ )
return true;
return false;
}



/*
Maths and Physics
*/

float cVector::DotProduct( cVector Vec ) const
{
return (mX*Vec.mX) + (mY*Vec.mY) + (mZ*Vec.mZ);
}
float cVector::CrossProduct( cVector Vec ) const
{
return ( (mY*Vec.mZ - mZ*Vec.mY) +
(mZ*Vec.mX - mX*Vec.mZ) +
(mX*Vec.mY - mY*Vec.mX) );
}
float cVector::Magnitude() const
{
return (float)sqrt((mX*mX) + (mY*mY) + (mZ*mZ));
}
cVector cVector::Normalize()
{
float M = this->Magnitude();
if(M == 0.0f) M = 1.0f;

mX = mX / M;
mY = mY / M;
mZ = mZ / M;

return *this;
}


I REALLY REALLY need this solved, been looking more than 6 hours..

--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..

Share this post


Link to post
Share on other sites
possibly you''re just working on a pc, while he worked on a mac, and there, the coordinates are by default, upside-down. i don''t know, but it could be :D




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net

Share this post


Link to post
Share on other sites
If you use a right-handed coordinate system like in opengl, the y axis will be pointing up in world space. Then when finding the direction to send rays in you probably begin with negative and then go to positive values which mean you will end up using positive y as up-direction, and hence your image will be up side down.

Share this post


Link to post
Share on other sites
i mean, on a mac, the main menu bar is on top, while on windows, the task bar is on the bottom, too..

i guess mac has 0,0 at left-top, while windows has it at left-bottom




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net

Share this post


Link to post
Share on other sites
quote:
Original post by davepermen
i guess mac has 0,0 at left-top, while windows has it at left-bottom


Nope - SDL behaves the same on all platforms. (Otherwise it wouldn''t be very cross-platform, now would it?)

Share this post


Link to post
Share on other sites
quote:
Original post by superpig
quote:
Original post by davepermen
i guess mac has 0,0 at left-top, while windows has it at left-bottom


Nope - SDL behaves the same on all platforms. (Otherwise it wouldn''t be very cross-platform, now would it?)


sure, but what if sdl defines the origin at a common place, but the mac-os itself has it different? the original tutorial doesn''t state its sdl if i remember correctly. or has it changed recently?




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net

Share this post


Link to post
Share on other sites
IIRC, the Mac endians are reversed from PC. Dunno if it has any relevance.

And having programmed on a Mac before, yes, the coordinate system is EXACTLY the same as on a PC.

1 2 3 4..
1
2
3
4
..

Share this post


Link to post
Share on other sites
I''ve used the same web page when making my own ray tracer. As others have said windows takes the top-left pixel to be the origin. With the positive X axis moving right, and the positive Y axis moving down. However, the coordinate system that the raytracer uses has its origin in the center of the picture, and importantly a positive Y axis moving up as other posters have mentioned. (Left or right handedness isn''t an issue here since both have positive Y as up.) To see this take a look at your ray direction code and have a think about where the ray for the central pixel goes:-

currDirection.mX = x - width/2;
currDirection.mY = y - height/2;

Substitue width/2, height/2 for the central pixel:-

currDirection.mX = width/2 - width/2;
currDirection.mY = height/2 - height/2;

currDirection.mX = 0;
currDirection.mY = 0;

Now take pixel 0,0 top-left of your window, but above and left of the images origin.

currDirection.mX = x - width/2;
currDirection.mY = y - height/2;

Substitue 0, 0:-

currDirection.mX = 0 - width/2;
currDirection.mY = 0 - height/2;

currDirection.mX = -width/2;
currDirection.mY = -height/2;

So we''ve placed this ray in the negative quadrent of the image, below and left of the images origin. Not what we expected. Hence, as you''ve found, the Y axis needs to be reversed between the image coordinates and the screen coordinates:-

currDirection.mX = x - width/2;
currDirection.mY = height/2 - y;

Which explains that you do need to reverse the Y axis, but not why the original website does not. Maybe its a bug with his code, or a difference with mac screen coordinates?

Share this post


Link to post
Share on other sites
It''s not a bug in his code, since I downloaded his and compiled FINE. 100% complete ABSOLUTELY fine!!


But now, I don''t care anymore. Because I don''t want to track the bug anymore, since it''s obvious that there is none.

I''m using Lab-Rats:
currDirection.mX = x - width/2;
currDirection.mY = y - height/2;


Thanks all! Now it''s time to work on the techniques!!

--
You''re Welcome,
Rick Wong

- sitting in his chair doing the most time-consuming thing..

Share this post


Link to post
Share on other sites