Jump to content
  • Advertisement
Sign in to follow this  
michaelth

OpenGL function"cos" problem with direct3d

This topic is 3690 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

I write some rendering code with 2 api(Direct3d and Opengl) for my game.And the logic is independent with the rendering.When it runs at "opengl mode",everything is OK.But it runs at "Direct3d mode",problem comes.I check everywhere for the bug.Since this game's geometry calculation refering functions like "sin","cos",so I check the these functions's calculating result on 2 conditions(after/before "direct3d's init"),the 2 results are different.But I don't even know the reseaon.The test code fragment is as follows: ///////////////////////code fragment////////////////////////////// float RAD=3.1415926535f/180.0f; float ca=cos(RAD*90.0f); cout<<ca<<"\n";//output the first result before InitD3d // Initialize Direct3D InitD3D()..... ca=cos(RAD*90.0f); cout<<ca<<"\n";";//output the second result after InitD3d ///////////////////////output////////////////////////////// 1.2168e-008 -4.37114-008

Share this post


Link to post
Share on other sites
Advertisement
Initializing D3D will set the FPU mode so that only single precision floating-point is used. The regular versions of cos/sin/tan use doubles, which would account for the small difference in your results. Either use the single-precision versions of those functions (cosf/sinf/tanf), or create your D3D device with the D3DCREATE_FPU_PRESERVE option (this will reduce performance).

Share this post


Link to post
Share on other sites
And actually, michaelth, the second value is what I get on my machine. So, in fact, if InitD3D() does anything, it corrects the earlier problem. [smile]

What results do you get from the two calls when you comment out the InitD3D() call? that may tell you something.

EDIT: Ignore the above.
MJP is exactly correct. This code:


double dRAD=3.1415926535/180.0;
double dca=cos(RAD*90.0);
float RAD=3.1415926535f/180.0f;
float ca=cos(RAD*90.0f);

gives exactly the results you're seeing.

Share this post


Link to post
Share on other sites
Quote:
Original post by MJP
Initializing D3D will set the FPU mode so that only single precision floating-point is used. The regular versions of cos/sin/tan use doubles, which would account for the small difference in your results. Either use the single-precision versions of those functions (cosf/sinf/tanf), or create your D3D device with the D3DCREATE_FPU_PRESERVE option (this will reduce performance).


Thanks for your reply.This sounds the right answer.I try the"D3DCREATE_FPU_PRESERVE" option,and the game running result at "Direct3d mode" is finally consistant with "Opengl mode".Maybe I need to get more detailed koownlege about D3D.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!