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.

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 on other sites
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 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 on other sites
maybe it's "D3DCREATE_FPU_PRESERVE" again ...

Share on other sites
Quote:
 Original post by MJPInitializing 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.

1. 1
2. 2
frob
15
3. 3
Rutin
11
4. 4
5. 5

• 13
• 14
• 62
• 14
• 15
• Forum Statistics

• Total Topics
632128
• Total Posts
3004270

×