Jump to content
  • Advertisement
Sign in to follow this  
d h k

OpenGL Weird overall problems when using D3D9 in plugin... [SOLVED]

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

Okay, this is going to be a little hard to explain but please bear with me, I'm pretty clueless right now. I'm an OpenGL user but for this project I'm forced to deal with Direct3d, so consider me a newbie... First of all, I'm looking at the D3D9 tutorials here. When I compile the code from the lessons there in a fresh project with MSVC++ 2008 Express Edition, everything's working fine. However, I need to use d3d9 in a slightly different environment now, as a C++ plugin (DLL) running in another game-maker styled application. That host application is using d3d9 to render and it's technically possible to get the d3d device and render own stuff but for some reason I get very weird problems that I can't seem to solve. First of all, z buffering doesn't work (although this probably has to do with the host application and I'm working this one it myself at the moment, it's a known problem). But textures from .x files don't show up correctly (it is loading the textures but the u-/v-coordinates seem wrong, totally stretched or something - I know it's not the model's fault because it works fine as a standard d3d9 application and in the viewer) and lighting doesn't want to work either (at all), without textures its pure black with textures it seems fullbright. That I don't understand. It doesn't seem like the fact that this is a plugin could cause these problems, could it? Here's some code:
void Init ( IDirect3DDevice9 *d3ddev )
{
	// INIT GRAPHICS

	LPD3DXBUFFER bufShipMaterial;

    D3DXLoadMeshFromX("airplane 2.x",    // load this file
                      D3DXMESH_SYSTEMMEM,    // load the mesh into system memory
                      d3ddev,    // the Direct3D Device
                      NULL,    // we aren't using adjacency
                      &bufShipMaterial,    // put the materials here
                      NULL,    // we aren't using effect instances
                      &numMaterials,    // the number of materials in this model
                      &meshSpaceship);    // put the mesh here

    // retrieve the pointer to the buffer containing the material information
    D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufShipMaterial->GetBufferPointer();

    // create a new material buffer and texture for each material in the mesh
    material = new D3DMATERIAL9[numMaterials];
    texture = new LPDIRECT3DTEXTURE9[numMaterials];

    for(DWORD i = 0; i < numMaterials; i++)    // for each material...
    {
        material = tempMaterials.MatD3D;    // get the material info
        material.Ambient = material.Diffuse;    // make ambient the same as diffuse
        // if there is a texture to load, load it
        if(FAILED(D3DXCreateTextureFromFileA(d3ddev,
                                             tempMaterials.pTextureFilename,
                                             &texture)))
        texture = NULL;    // if there is no texture, set the texture to NULL
      }


	// INIT LIGHT

	D3DLIGHT9 light;    // create the light struct

    ZeroMemory(&light, sizeof(light));    // clear out the struct for use
    light.Type = D3DLIGHT_DIRECTIONAL;    // make the light type 'directional light'
    light.Diffuse.r = 0.5f;    // .5 red
    light.Diffuse.g = 0.5f;    // .5 green
    light.Diffuse.b = 0.5f;    // .5 blue
    light.Diffuse.a = 1.0f;    // full alpha (we'll get to that soon)

    D3DVECTOR vecDirection = {-1.0f, -0.3f, -1.0f};    // the direction of the light
    light.Direction = vecDirection;    // set the direction

    d3ddev->SetLight(0, &light);    // send the light struct properties to light #0
    d3ddev->LightEnable(0, TRUE);    // turn on light #0


	// INIT MISC

	d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);    // turn on the 3D lighting
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE );
    d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));    // ambient light
}

void Render ( IDirect3DDevice9 *d3ddev )
{
	d3ddev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,128),1.0f,0);
	d3ddev->Clear(0,NULL,D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);

    d3ddev->BeginScene();

    // SET UP THE TRANSFORMS

    D3DXMATRIX matView;    // the view transform matrix
    D3DXMatrixLookAtLH(&matView,
    &D3DXVECTOR3 (0.0f, 8.0f, 16.0f),    // the camera position
    &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),    // the look-at position
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction
    d3ddev->SetTransform(D3DTS_VIEW, &matView);    // set the view transform to matView

    D3DXMATRIX matProjection;    // the projection transform matrix
    D3DXMatrixPerspectiveFovLH(&matProjection,
                               D3DXToRadian(60),    // the horizontal field of view
                               SCREEN_WIDTH / SCREEN_HEIGHT,    // the aspect ratio
                               1.0f,    // the near view-plane
                               100.0f);    // the far view-plane
    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);    // set the projection

    static float index = 0.0f; index+=0.03f;    // an ever-increasing float value
    D3DXMATRIX matRotateY;    // a matrix to store the rotation for each triangle
    D3DXMatrixRotationY(&matRotateY, index);    // the rotation matrix
    d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY));    // set the world transform

      // draw the spaceship
    for(DWORD i = 0; i < numMaterials; i++)    // loop through each subset
    {
        d3ddev->SetMaterial(&material);    // set the material for the subset
        if(texture != NULL)    // if the subset has a texture (if texture is not NULL)
            d3ddev->SetTexture(0, texture);    // ...then set the texture

        meshSpaceship->DrawSubset(i);    // draw the subset
    }

    d3ddev->EndScene(); 

    d3ddev->Present(NULL, NULL, NULL, NULL);
}





Init ( ) and Render ( ) are called at appropriate times. Meaning, the very first frame that the game runs, first Init ( ) and then Render ( ) get called, then every following frame only Render ( ). I realize that this is really, really ugly code (and I hate nothing more than that) but this is not the point right now, I'm working on a proof-of-concept right now, cleaning up comes later. The thing is, in the plugin I only have access to the d3d_device every frame when rendering and not much else (meaning, I don't have access to the CreateDevice ( ) call for example and I don't know what exactly it looks like). Of course you probably won't be able to help me much as I can't talk about or show the host application etc., I'm just hoping that maybe some d3d guru could maybe have a hunch as to what *could* possibly be wrong, maybe there's some common mistake that could explain the texture or the lighting trouble. Please let me know, if you need even more code or if it's just impossible to judge for you guys what's wrong, that's no problem. Thanks ahead of time. [Edited by - d h k on July 25, 2009 9:14:44 AM]

Share this post


Link to post
Share on other sites
Advertisement
I really dislike that particular site for tutorials, since it teaches some pretty bad habits, and things that are just plain wrong. Having said that, I don't really know of any better alternatives...

First, do the Debug runtimes have anything relevant to say?

For the texture problem - are the textures a power of 2 in width and height? D3DXCreateTextureFromFile() will resize textures if they're not a power of two, which could cause artifacts.

For the Z-buffer problem - how do you set up your present parameters when you create the device?

It may be worth creating a state block, and applying it every time you render; that way you can be sure the device is in the correct state for rendering each time.

For lighting; do you know if the model has normals in it? You can check if meshSpaceship->GetFVF() includes the D3DFVF_NORMAL flag.

Why are you loading the mesh into system memory? I was under the impression that you couldn't render meshes loaded into system memory, and you should be using D3DXMESH_MANAGED instead.

Also, you can clear your z-buffer and backbuffer in one go with:
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,128), 1.0f, 0);

EDIT: Also, do these problems exist if you put the above code into the main app rather than a plugin (If that's possible, and I don't think it should make any difference)?

Share this post


Link to post
Share on other sites
Thanks for the quick answer!

Quote:
Original post by Evil Steve
First, do the Debug runtimes have anything relevant to say?


That's interesting but how would I use those when I don't run the project from VC but rather just create the DLL, cut it, overwrite the older version in the host application's directory and then run that host app (not mine, don't have the source). I wouldn't have the VC console for it to write stuff to or can they also output to a textfile for example?

Quote:
Original post by Evil Steve
For the texture problem - are the textures a power of 2 in width and height? D3DXCreateTextureFromFile() will resize textures if they're not a power of two, which could cause artifacts.


Yep, the model is the one from the textured mesh lesson on that site, it uses two textures and they're 128x128 and 256x256.

Quote:
Original post by Evil Steve
For the Z-buffer problem - how do you set up your present parameters when you create the device?


The host application allows a plugin to retrieve the present parameters and make changes. I do it like this:


void OnInitGFX ( const char* driverID, void *data )
{
D3DPRESENT_PARAMETERS *d3dpp = (D3DPRESENT_PARAMETERS *) data;

d3dpp->EnableAutoDepthStencil = TRUE;
d3dpp->AutoDepthStencilFormat = D3DFMT_D16;
}




The above function gets called correctly by the host application on init, the driverID really is D3D9 and in that case data is a pointer to the present parameters. I then try to enable the z buffer as I read you should. Using D3DFMT_D32 doesn't help either.

Quote:
Original post by Evil Steve
For lighting; do you know if the model has normals in it? You can check if meshSpaceship->GetFVF() includes the D3DFVF_NORMAL flag.


I'm sure it has normals because the D3D .x viewer shows them and, as I said, in a normal d3d9 application it all works with lighting and the same code.

Quote:
Original post by Evil Steve
Why are you loading the mesh into system memory? I was under the impression that you couldn't render meshes loaded into system memory, and you should be using D3DXMESH_MANAGED instead.


Did it because the lesson said so, unfortunately changing yielded no difference.


Quote:
Original post by Evil Steve
Also, you can clear your z-buffer and backbuffer in one go with:
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,128), 1.0f, 0);


Changed that, thanks! :)

Quote:
Original post by Evil Steve
EDIT: Also, do these problems exist if you put the above code into the main app rather than a plugin (If that's possible, and I don't think it should make any difference)?


What do you mean with main app in this case, the host app that runs my plugin? It's not mine and I don't have the source. If I put the code into a new standalone simple D3D9 win32 app it works perfectly fine.

Thanks so far!!

Share this post


Link to post
Share on other sites
Quote:
Original post by d h k
That's interesting but how would I use those when I don't run the project from VC but rather just create the DLL, cut it, overwrite the older version in the host application's directory and then run that host app (not mine, don't have the source). I wouldn't have the VC console for it to write stuff to or can they also output to a textfile for example?


You can simply attach the debugger to the running process. (Debug => Attach). I can't say if it works for the debug output of DirectX, but I'm guessing it does.

Share this post


Link to post
Share on other sites
Quote:
Original post by d h k
That's interesting but how would I use those when I don't run the project from VC but rather just create the DLL, cut it, overwrite the older version in the host application's directory and then run that host app (not mine, don't have the source). I wouldn't have the VC console for it to write stuff to or can they also output to a textfile for example?
You can run DebugView to capture system-wide debug output, which will show what they're reporting.

Quote:
Original post by d h k
Yep, the model is the one from the textured mesh lesson on that site, it uses two textures and they're 128x128 and 256x256.
Hmm, I'm not really sure what the problem would be then - any chance of seeing a screenshot?

Quote:
Original post by d h k
The host application allows a plugin to retrieve the present parameters and make changes. I do it like this:

[snip]

The above function gets called correctly by the host application on init, the driverID really is D3D9 and in that case data is a pointer to the present parameters. I then try to enable the z buffer as I read you should. Using D3DFMT_D32 doesn't help either.
Very few cards support D3DFMT_D32 actually, D3DFMT_D24X8 is a lot more common. Quite a few cards also require that the Z-buffer bit depth matches the backbuffer bit depth, so if you have a 32-bit backbuffer, you'll probably want a 32-bit Z-buffer (Or 24-bit with 8 bits unused). The debug runtimes will let you know if this is a problem, or you can call IDirect3D9::CheckDeviceFormat and IDirect3D9::CheckDepthStencilMatch to see if the format is valid.

Quote:
Original post by d h k
Quote:
Original post by Evil Steve
Why are you loading the mesh into system memory? I was under the impression that you couldn't render meshes loaded into system memory, and you should be using D3DXMESH_MANAGED instead.


Did it because the lesson said so, unfortunately changing yielded no difference.
Ah, it's quite possible that I'm wrong and system memory meshes work fine [smile]

Quote:
Original post by d h k
Quote:
Original post by Evil Steve
EDIT: Also, do these problems exist if you put the above code into the main app rather than a plugin (If that's possible, and I don't think it should make any difference)?


What do you mean with main app in this case, the host app that runs my plugin? It's not mine and I don't have the source. If I put the code into a new standalone simple D3D9 win32 app it works perfectly fine.
Yeah, unfortunately I meant in the host app. If it works fine in another "clean" app, then I'd guess it's some state that the host app is setting, or some change to the present parameters that it makes (Although that's less likely to affect anything).

Share this post


Link to post
Share on other sites
Okay, running the debug libraries and debug view now:

I have two memory leaks (not surprising), gonna fix those now but I guess they can't be the cause for these problems, right?

It gives me two "D3DX: Invalid file name for the textures" too, which is weird because as you can see, they do shine through. Any ideas?


Left is what I get, right is the same model in a standalone d3d9 app...

Changed both the backbuffer and the zbuffer to 24x8, no changes though. I'm running an NVidia GTX 260 by the way.

Quote:
Original post by Evil Steve
Yeah, unfortunately I meant in the host app. If it works fine in another "clean" app, then I'd guess it's some state that the host app is setting, or some change to the present parameters that it makes (Although that's less likely to affect anything).


That's exactly what I think. Any ideas as to what setting it is that could be causing all this?

Thangs again, great support!

Share this post


Link to post
Share on other sites
Also calling "d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);" changed nothing unfortunately.

Share this post


Link to post
Share on other sites
Bumping this one final time, if no more feedback comes I guess there's nothing you can do to help me with this (I understand that of course).

I think the textures might be wrong in the sense that they only seem to use the top portion of the texture ie. the body of the plane is red because the front propeller is etc. I'm not sure and I don't have any proof but it looks like that.

Now, what in the world could cause this? What does the host app do to make this happen in plugin code. I'm totally out of ideas.

Share this post


Link to post
Share on other sites
My guess would be you're having a pathing problem. The paths to textures in .x files are generally written as relative paths, from the location of the actual .x file. If you are loading with that path, but your working directory is a different path (the host program's path, for instance), that could cause you to not find the textures.

imo, the textures aren't being stretched, rather you are getting some kind of default material color or something from the x file.

I'd suggest checking to see what your current working directory is from the plugin code, and seeing how that compares to the path you are getting from tempMaterials.pTextureFileName. You may need to just do some path manipulation on that filename.

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!