DrawSubset() causing mad crazy texture flickering

Started by
2 comments, last by UltimateWalrus 15 years, 3 months ago
OK, so I have a little engine that can draw cubes and ramps using IDIRECT3DVERTEXBUFFER9's. The vertex format I'm using has XYZ and UV coordinates. Whenever I want to draw one of them, I just do:

              d3ddev->SetTexture(0, Tex_Cube1);
              d3ddev->SetStreamSource(0, Shape_cube, 0, sizeof(VERTEXUV));
              d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 12);
Free Image Hosting at www.ImageShack.us Works fine! Then I tried to render meshes... I tried both of these:

D3DXCreateSphere(d3ddev, 0.5f, 10,10, &Mesh_sphere1, NULL);
//or:
D3DXCreateTeapot(d3ddev, &Mesh_sphere1, NULL);
When when I try to render the mesh, all the textures flicker and go crazy. All I did was call Mesh_sphere1->DrawSubset(0); . Free Image Hosting at www.ImageShack.us What's going on?? Anybody have any ideas? Any help would be much, much, much appreciated :]
Advertisement
Do you get any error messages from the debug runtimes?

If that doesn't help, please post some more code. If there's lots of it, surround it with [ source ] and [ /source ] tags (without the spaces).
Here is my Direct3D initialization routine:

int Init_Direct3D(HWND hwnd, int width, int height, bool fullscreen){    //initialize Direct3D    d3d = Direct3DCreate9(D3D_SDK_VERSION);    if (d3d == NULL)    {        MessageBox(hwnd, L"Error initializing Direct3D", L"Error", MB_OK);        return 0;    }    //set Direct3D presentation parameters    D3DDISPLAYMODE dm;    d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &dm);    D3DPRESENT_PARAMETERS d3dpp;     ZeroMemory(&d3dpp, sizeof(d3dpp));    d3dpp.Windowed = (!fullscreen);    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    d3dpp.EnableAutoDepthStencil = TRUE;    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;    d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;    d3dpp.BackBufferFormat = dm.Format;    d3dpp.BackBufferCount = 1;    d3dpp.BackBufferWidth = width;    d3dpp.BackBufferHeight = height;    d3dpp.hDeviceWindow = hwnd;    //Only needed for dialog boxes    d3dpp.Flags=D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;    //create Direct3D device    d3d->CreateDevice(        D3DADAPTER_DEFAULT,         D3DDEVTYPE_HAL,         hwnd,        D3DCREATE_SOFTWARE_VERTEXPROCESSING,        &d3dpp,         &d3ddev);    if (d3ddev == NULL)    {        MessageBox(hwnd, L"Error creating Direct3D device", L"Error", MB_OK);        return 0;    }    //clear the backbuffer to black    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);    //create pointer to the back buffer    d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);    D3DXCreateSprite(d3ddev, &sprite_handler);    //turn dynamic lighting and z-buffering off    d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);    d3ddev->SetRenderState(D3DRS_ZENABLE, FALSE);    //set the Direct3D stream to use the custom vertex    d3ddev->SetFVF(D3DFVF_XYZ | D3DFVF_TEX1);    return 1;}


Here's the game initialization:


int Game_Init(HWND hwnd){  //Position the camera:  Camera position, camera lookat, and up direction  SetCameraUp(0,0,-5,  0,0,0,  0,1,0);  float ratio = (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT;  SetPerspective(45.0f, ratio, 0.1f, 10000.0f);   //Set up the projection matrix (may wish to tweak this)  //turn dynamic lighting off, z-buffering on  d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);  d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);  //set the Direct3D stream to use the custom VERTEXUV  d3ddev->SetFVF(D3DFVF_XYZ | D3DFVF_TEX1);  //Load resources.  LoadResources();  //Set up the level.  CreateGrid(10, 10, 10);  //Timer functions  //---------------  timeBeginPeriod(1);    return 1;}



LoadResources() leads to the code that creates my mesh, which I do like so:

D3DXCreateTeapot(d3ddev, &Mesh_sphere1, NULL);




Then, here's my draw routine:


   //DRAW ROUTINE    //------------    if(render_scene)    {      ClearScene(D3DCOLOR_ARGB(0,0,0,0));      if(d3ddev->BeginScene() == D3D_OK)      {        //Draw the level.        DrawGrid();        DrawPlayers();        //Draw a sprite in the corner.          sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);        //Print the framerate.        char fps_string[256];        sprintf(fps_string, "FPS: %d", fps_framerate);        DrawFont(&Font1, sprite_handler, fps_string, 320, 10);        sprite_handler->End();        fps_framecount++;        d3ddev->EndScene();      }      d3ddev->Present(0,0,0,0);    }



Inside of DrawPlayers(), here's the one line I've been using to draw the mesh.
I've also tried creating a material and setting d3ddev's material to that, but I see no difference.

Mesh_sphere1->DrawSubset(0);



Maybe that will be helpful. Thanks for the reply! Sorry, I am still somewhat of a beginner at this D3D Mesh-drawing stuff.

I will check on that debug runtime thing.
OK, this is really bizarre.

I enabled the DirectX debug runtime thingy (by changing options in the DirectX control panel and switching to d3dx9d.lib, as was specified in the link you gave me.).

What happens now is drastically different than before... Basically, the framerate grinds to a halt, and all I get is this dark blue teapot in the corner. No cubes are displayed.

Free Image Hosting at www.ImageShack.us

This happens with both Debug and Release configurations --- which leads me to believe that it was the change to the Direct3D Control Panel that caused this change in my program's behavior.

I've worked out my code so that I can render the player as a cube. Basically, ALL of these problems are being caused by switching between these two drawing routines:

d3ddev->SetTexture(0, Tex_Player1);d3ddev->SetStreamSource(0, Shape_cube, 0, sizeof(VERTEXUV));d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 12);


And:

Mesh_sphere1->DrawSubset(0);


The former code works perfectly --- runs at a steady 100fps and does exactly what it's supposed to, displaying all cubes as well as the player cube.

But when I comment out the former code and use the latter code, it just freaks out. The "debug" mode is not giving me any error messages... should it be?

This topic is closed to new replies.

Advertisement