Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 18 Aug 2005
Offline Last Active Mar 30 2016 05:19 PM

Posts I've Made

In Topic: Killing Room

06 March 2016 - 10:06 AM

Looks cool!

In Topic: Translate 2D mouse position to place an object in 3D space

24 February 2016 - 09:38 AM

Read: http://mathworld.wolfram.com/Tangent.html -- look for the big triangle. Remember SOHCAHTOA.


tan(theta), where theta = pi/8, gives you the rise over run. When the camera is 1 unit away from the origin, the run is 1. The rise goes on to give you half of the image plane width.

That helps because I didn't know what the tangent was for.


I've analyzed the code and simplified it. Below is the final modified version. Let's look at it line by line.


First useless line.

const float pi = 4.0f*atanf(1.0f);

All this does is give us the value of PI. math.h has a #define you can use so this is unnecessary. As you'll see in the finial code, many things were unnecessary.


Next line.

const float aspect = (float)(m_renderbufferWidth) / (float)(m_renderbufferHeight);

This give us the aspect ratio of the screen. Not sure why this is needed for the x coordinate and not the y. I think it's magic.


Next Two lines.

const float fx = 2.0f * ((float)(x) / (float)(m_renderbufferWidth - 1)) - 1.0f;
const float fy = 2.0f * ((float)(y) / (float)(m_renderbufferHeight - 1)) - 1.0f;

This converts screen coordinates from a top/left orientation to a center screen coordinates but there's a little more to this then that.

1) Subtracting the width and height by one. The reason you do this is because resolution sizes are usually all even numbers. For example, a resolution of 1280x720 there is no center pixel or a pixel at 0. The other reason this could be is that you need a value of base 0, as if this was an array and w/h represents the total number of elements.

2) x/y divied by w/h. This gives you a normalize value of the point on your screen. In other words, a value from 0 to 1.

3) 2.0f *; not entirely sure what this is doing and why it is needed.

4) The last - 1.0f flips the sign.


Second useless line

const float y_fov = pi/4; // pi/4 radians = 45 degrees

This takes PI and divides it by 4 to give us 45 degrees in radians. This is unnecessary.


Modified line.

const float tangent = tan(y_fov / 2.0f);

This divides the 45 degrees by 2 to give use 22.5 degrees in radians. This can be simplified by plopping a number in tan.


I know what the rest is doing, I just don't understand why it works.

*    desc:  Convert 2d screen coordinates to 3D perspective space
void Convert2Dto3D( float & destX, float & destY, float x, float y, float width, float height )
    const float aspect = width / height;

    // Convert upper left 0,0 to center screen 0,0 coordinates
    const float fx = (2.0f * (x / (width - 1))) - 1.0f;
    const float fy = (2.0f * (y / (height - 1))) - 1.0f;

    // (pi/4 radians = 45 degrees / 2) or ((pi/4) / 2))
    const float tangent = tan( 0.392699082 );

    // Project x,y to a z plane of 1
    destX = aspect * tangent* fx;
    destY = -tangent * fy;

}   // Convert2Dto3D

In Topic: Translate 2D mouse position to place an object in 3D space

20 February 2016 - 05:41 PM

done! You have your up vote.


I wish I could look at this code and know what it's doing. If anyone can explain it, please do.

In Topic: Translate 2D mouse position to place an object in 3D space

19 February 2016 - 10:48 AM

Thank you guys for your help!


Taby, thank you for posting the code. It works GREAT!


All I had to do was multiply the final X and Y values by a positive z order.


I couldn't be more happy! There's no way I would have been able to write that code. Taby, you are Awesome!

In Topic: OpenGL Projection Matrix Clarifications

19 December 2015 - 03:40 PM

(Why does everyone say that the camera is at 0,0,0 then?!)


Saying the camera is at 0,0,0 is a bit misleading. In computer graphics, everything is a vector and vector math requires that the origin be 0. It's no more complicated then that. The camera, which is nothing more then a matrix, is the translation and rotation from 0,0,0.