View previous topic :: View next topic 
Author 
Message 
coltson n00b
Joined: 15 Oct 2005 Posts: 51

Posted: Wed Aug 07, 2013 11:10 pm Post subject: Operator overloading not resulting in the expected value 


I have this code: Code:  point intersection = viewRay.origin + t * viewRay.dir; 
Here operator overloading is used in two cases, in the multiplication between t and viewRay.dir and between viewRay.origin and the result of t * viewRay.dir
viewRay.dir is an structure of the kind Vector, which it is three floats Code:  struct Vector {
float x,y,z;
}; 
t is a float and viewRay.origin it is an structure of the kind Vector, which also it is three floats Code:  struct point {
float x,y,z;
}; 
So if viewRay.origin has it three floats with the values of 300,300 ,1000, if viewRay.dir value it is 0,0,1 and t value it is 926,4, I should have:
(300,300,1000) + 926,4 * (0,0,1) = (300,300,1000) + (0,0,926,4) = (300,300,73,5595).
However this is not the value that I am getting. Here is the output of the program for viewRay.origin being (300,300,1000): Quote:  t value it is: 926.44
viewRay.dir.x value it is: 0 viewRay.dir.y value it is: 0 viewRay.dir.z value it is: 1
viewRay.origin.x value it is: 300 viewRay.origin.x value it is: 300 viewRay.origin.z value it is: 1000
300 300 73.5595
300 300 2000 
These lines are generated by the first line that I posted in this topic followed by these: Code: 
point intersection = viewRay.origin + t * viewRay.dir; //already posted this
cout << "t value it is: " << t << endl;
cout << " viewRay.dir.x value it is: " << viewRay.dir.x << " viewRay.dir.y value it is: " << viewRay.dir.y << " viewRay.dir.z value it is: " << viewRay.dir.z << endl;
cout << " viewRay.origin.x value it is: " << viewRay.origin.x << " viewRay.origin.x value it is: " << viewRay.origin.y << " viewRay.origin.z value it is: " << viewRay.origin.z << endl;
cout << viewRay.origin.x + t*viewRay.dir.x << ' ' << viewRay.origin.y + t*viewRay.dir.y << ' ' << viewRay.origin.z + t*viewRay.dir.z << endl;
cout << intersection.x << ' ' << intersection.y << ' ' << intersection.z << endl; 
So the value being calculated by using operator overloading is . The first two values, 300,300 are correct but the last one is wrong. This 2000 is the default value of t, so I thought the t value was wrong, but it is not, as can be seen by the second line of the output
In fifth I calculate the values "manually", without operator overloading and the values calculated are corrects.
So I think the error might be in the operator overloading for the multiplication. Here is the code Code:  inline Vector operator * (float t, const Vector &v1) {
Vector v2 = {v1.x * t, v1.y * t, v1.z*t};
return v2;
} 
I can't see anything wrong there....... So anyone has an idea?
Thanks for any help. 

Back to top 


MustrumR n00b
Joined: 15 Nov 2011 Posts: 63 Location: Right here

Posted: Thu Aug 08, 2013 8:50 am Post subject: 


Please post all the code. 

Back to top 


steveL Advocate
Joined: 13 Sep 2006 Posts: 3785 Location: The Peanut Gallery

Posted: Thu Aug 08, 2013 6:08 pm Post subject: Re: Operator overloading not resulting in the expected value 


coltson wrote:  Code:  point intersection = viewRay.origin + t * viewRay.dir; 
Here operator overloading is used in two cases, in the multiplication between t and viewRay.dir and between viewRay.origin and the result of t * viewRay.dir
viewRay.dir is an structure of the kind Vector, which it is three floats Code:  struct Vector {
float x,y,z;
}; 
t is a float and viewRay.origin it is an structure of the kind Vector, which also it is three floats Code:  struct point {
float x,y,z;
}; 

It's not though: it's a struct point, same as intersection. 

Back to top 


coltson n00b
Joined: 15 Oct 2005 Posts: 51

Posted: Sat Aug 10, 2013 1:30 am Post subject: 


MustrumR wrote:  Please post all the code. 
The code seems to be too big to post it all here at once (more than 600 lines divided into multiple files) How you suggest I shall proceed in this case?
Quote:  It's not though: it's a struct point, same as intersection. 
True, my mistake 

Back to top 


Hu Watchman
Joined: 06 Mar 2007 Posts: 9393

Posted: Sat Aug 10, 2013 1:31 am Post subject: 


Concatenate it into a single file, upload it to a pastebin, and link to the pastebin from here. 

Back to top 


coltson n00b
Joined: 15 Oct 2005 Posts: 51

Posted: Mon Aug 12, 2013 5:07 am Post subject: 


I did it: here is the link: http://codepad.org/64AYV50l
The lines that I mentioned here are in the first 50 lines and between the 538 and the 545 one
To run the program you also need a txt file with this content in the same dir.
Quote:  Image
Width=640;
Height=480;
bpp=32;
Material
{
Diffuse=1.0,1.0,0.0;
Reflection=0.5;
}
Material
{
Diffuse=0.0,1.0,1.0;
Reflection=0.5;
}
Material
{
Diffuse=1.0,0.0,1.0;
Reflection=0.5;
}
///////////////////////////////////////
// List of spheres //
///////////////////////////////////////
Sphere
{
Center=233.0,290.0,0.0;
Radius_Squared=10000;
Size=100.0;
MaterialId=0.0;
}
Sphere
{
Center=407.0,290.0,0.0;
Radius_Squared=10000;
Size=100.0;
MaterialId=1.0;
}
Sphere
{
Center=320.0,140.0,0.0;
Radius_Squared=10000;
Size=100.0;
MaterialId=2.0;
}
///////////////////////////////////////
// List of lights //
///////////////////////////////////////
Light
{
Position=0.0,240.0,100.0;
Intensity=1.0,1.0,1.0;
}
Light
{
Position=640.0,240.0,10000.0;
Intensity=0.6,0.7,1.0;
}



Back to top 


Genone Retired Dev
Joined: 14 Mar 2003 Posts: 9014 Location: beyond the rim

Posted: Mon Aug 12, 2013 6:57 am Post subject: 


Code:  inline point operator + (const point &p1, const Vector &v1 ) {
point p2 = {p1.x + v1.x, p1.y + v1.y, p1.z + p1.z};
return p2;
}

Typo in the variable name on the zaddition. 

Back to top 


coltson n00b
Joined: 15 Oct 2005 Posts: 51

Posted: Thu Aug 15, 2013 5:07 am Post subject: 


thanks 

Back to top 




You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum

