   Unexpected drawing glitch
  I am getting the strangest error I have ever come across when it comes to IT...I have a program that draws rotated triangles to form a polygon. When I compile the program it draws it fine 75% of the time or so. But without changing anything the other 25% of the time the drawing comes out all screwy...Here are pics of what I get:Drawing as expectedRandom buggy drawingAnd here is all the code for it://include the basic windows header files and the Direct3D header file#include <windows.h>#include <windowsx.h>#include <d3d9.h>#include <d3dx9.h>//include the Direct3D Library files#pragma comment (lib, "d3d9.lib")#pragma comment (lib, "d3dx9.lib")#define SCREEN_WIDTH 1280#define SCREEN_HEIGHT 800#define MazeSpacing 5.0#define DefaultBackgroundColour D3DCOLOR_XRGB(255, 255, 255)#define MazeColour D3DCOLOR_XRGB(0, 0, 0)//player counter settings#define PolygonRadius 1.0#define NumPolygonSides 20#define PolygonColour D3DCOLOR_XRGB(0, 0, 255)#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)#define Pi 3.14159LPDIRECT3D9 d3d;LPDIRECT3DDEVICE9 d3ddev;LPDIRECT3DVERTEXBUFFER9 LineVertBuffer = NULL;LPDIRECT3DVERTEXBUFFER9 LinePolygonVertBuffer = NULL;LPDIRECT3DVERTEXBUFFER9 PolygonTriVertBuffer = NULL;struct CUSTOMVERTEX {FLOAT X, Y, Z; DWORD COLOR;};LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);void initD3D(HWND hWnd);void cleanD3D(void);void render_frame(void);void InitGraphics(void);int WINAPI WinMain(HINSTANCE hInstance,    HINSTANCE hPrevInstance,    LPSTR lpCmdLine,    int nCmdShow){    HWND hWnd;    WNDCLASSEX wc;    ZeroMemory(&wc, sizeof(WNDCLASSEX));    wc.cbSize = sizeof(WNDCLASSEX);    wc.style = CS_HREDRAW | CS_VREDRAW;    wc.lpfnWndProc = WindowProc;    wc.hInstance = hInstance;    wc.hCursor = LoadCursor(NULL, IDC_ARROW);    wc.lpszClassName = L"WindowClass";    RegisterClassEx(&wc);    hWnd = CreateWindowEx(NULL, L"WindowClass", L"Mazes", WS_OVERLAPPEDWINDOW, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);    ShowWindow(hWnd, nCmdShow);    initD3D(hWnd);InitGraphics();    MSG msg;    while(TRUE)    {   while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))   {  TranslateMessage(&msg);  DispatchMessage(&msg);   }   if(msg.message == WM_QUIT)  break;   render_frame();    }    cleanD3D();    return msg.wParam;}// this is the main message handler for the programLRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    switch(message)    {   case WM_DESTROY:  { PostQuitMessage(0); return 0;  } break;    }    return DefWindowProc (hWnd, message, wParam, lParam);}// this function initializes and prepares Direct3D for usevoid initD3D(HWND hWnd){    d3d = Direct3DCreate9(D3D_SDK_VERSION);    D3DPRESENT_PARAMETERS d3dpp;    ZeroMemory(&d3dpp, sizeof(d3dpp));    d3dpp.Windowed = TRUE;    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    d3dpp.hDeviceWindow = hWnd;    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;    d3dpp.BackBufferWidth = SCREEN_WIDTH;    d3dpp.BackBufferHeight = SCREEN_HEIGHT;    d3dpp.EnableAutoDepthStencil = TRUE;    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;    d3d->CreateDevice(D3DADAPTER_DEFAULT,  D3DDEVTYPE_HAL,  hWnd,  D3DCREATE_SOFTWARE_VERTEXPROCESSING,  &d3dpp,  &d3ddev);    d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);    // turn off the 3D lighting    d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);    // turn off culling    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);    // turn on the z-buffer}void cleanD3D(void){LineVertBuffer->Release();LinePolygonVertBuffer->Release();PolygonTriVertBuffer->Release();d3ddev->Release();    d3d->Release();}void render_frame(void){    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, DefaultBackgroundColour, 1.0f, 0);    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, DefaultBackgroundColour, 1.0f, 0);    d3ddev->BeginScene();    d3ddev->SetFVF(CUSTOMFVF);    // set the view transform    D3DXMATRIX matView;    // the view transform matrix    D3DXMatrixLookAtLH(&matView,    &D3DXVECTOR3 (0.0f, 0.0f, 100.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    // set the projection transform    D3DXMATRIX matProjection;    // the projection transform matrix    D3DXMatrixPerspectiveFovLH(&matProjection, D3DXToRadian(45),    // the horizontal field of view (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio 1.0f,   // the near view-plane 100.0f);    // the far view-plane    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection/*//test drawing of primitive black lined3ddev->SetStreamSource(0, LineVertBuffer, 0, sizeof(CUSTOMVERTEX));D3DXMATRIX matTranslate;D3DXMatrixTranslation(&matTranslate, 0.0, 0.0, 0.0f);d3ddev->SetTransform(D3DTS_WORLD, &(matTranslate));d3ddev->DrawPrimitive(D3DPT_LINELIST, 0, 1);*//*//test drawing of primitive black polygond3ddev->SetStreamSource(0, LinePolygonVertBuffer, 0, sizeof(CUSTOMVERTEX));D3DXMATRIX matTranslate;D3DXMatrixTranslation(&matTranslate, 0.0, 0.0, 0.0f);d3ddev->SetTransform(D3DTS_WORLD, &(matTranslate));d3ddev->DrawPrimitive(D3DPT_LINESTRIP, 0, NumPolygonSides);*//*//test drawing of primitive polygon triangled3ddev->SetStreamSource(0, PolygonTriVertBuffer, 0, sizeof(CUSTOMVERTEX));D3DXMATRIX matTranslate;D3DXMatrixTranslation(&matTranslate, 0.0, 0.0, 0.0f);d3ddev->SetTransform(D3DTS_WORLD, &(matTranslate));d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 1);*///test drawing of solid polygon using primitive polygon triangled3ddev->SetStreamSource(0, PolygonTriVertBuffer, 0, sizeof(CUSTOMVERTEX));D3DXMATRIX matTranslate;D3DXMatrixTranslation(&matTranslate, 0.0, 0.0, 0.0f);D3DXMATRIX matRotateZ;for(int i=0; i<NumPolygonSides; i++){D3DXMatrixRotationZ(&matRotateZ, i*2*Pi/NumPolygonSides);d3ddev->SetTransform(D3DTS_WORLD, &(matTranslate * matRotateZ));d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, NumPolygonSides);}    d3ddev->EndScene();d3ddev->Present(NULL, NULL, NULL, NULL);}void InitGraphics(void){////LINE PRIMITIVE//CUSTOMVERTEX LineVertices[] =    {   { 0.0f, 0.0f, 0.0f, MazeColour},   { MazeSpacing, 0.0f, 0.0f, MazeColour}    };d3ddev->CreateVertexBuffer(2*sizeof(CUSTOMVERTEX),0,CUSTOMFVF,D3DPOOL_MANAGED,&LineVertBuffer,NULL);VOID* LinepVoid;    // a void pointer    // lock v_buffer and load the vertices into it    LineVertBuffer->Lock(0, 0, (void**)&LinepVoid, 0);    memcpy(LinepVoid, LineVertices, sizeof(LineVertices));    LineVertBuffer->Unlock();////LINE POLYGON PRIMITIVE//float PolygonCentre[] = {0.0, 0.0, 0.0};CUSTOMVERTEX LinePolygonVertices[NumPolygonSides + 1];for(int i=0; i<NumPolygonSides+1; i++){LinePolygonVertices.X = PolygonCentre[0] + PolygonRadius * sin(i*(2*Pi/NumPolygonSides));LinePolygonVertices.Y = PolygonCentre[1] + PolygonRadius * cos(i*(2*Pi/NumPolygonSides));LinePolygonVertices.Z = PolygonCentre[2] + PolygonRadius;LinePolygonVertices.COLOR = PolygonColour;}d3ddev->CreateVertexBuffer((NumPolygonSides + 1)*sizeof(CUSTOMVERTEX),0,CUSTOMFVF,D3DPOOL_MANAGED,&LinePolygonVertBuffer,NULL);VOID* LinePolygonpVoid;    // a void pointer    // lock v_buffer and load the vertices into it    LinePolygonVertBuffer->Lock(0, 0, (void**)&LinePolygonpVoid, 0);memcpy(LinePolygonpVoid, LinePolygonVertices, sizeof(LinePolygonVertices));LinePolygonVertBuffer->Unlock();////POLYGON TRIANGLE PRIMITIVE//CUSTOMVERTEX PolygonTriVertices[] =    {   { 0.0f, 0.0f, 0.0f, PolygonColour},   { 0.0f, PolygonRadius, 0.0f, PolygonColour},{ PolygonRadius * sin(2*Pi/NumPolygonSides), PolygonRadius * cos(2*Pi/NumPolygonSides), 0.0f, PolygonColour}    };d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),0,CUSTOMFVF,D3DPOOL_MANAGED,&PolygonTriVertBuffer,NULL);VOID* PolygonTripVoid;    // a void pointer// lock v_buffer and load the vertices into it    PolygonTriVertBuffer->Lock(0, 0, (void**)&PolygonTripVoid, 0);memcpy(PolygonTripVoid, PolygonTriVertices, sizeof(PolygonTriVertices));PolygonTriVertBuffer->Unlock();}Why on earth is this happening? D:[Edited by - jpetrie on June 2, 2010 1:47:41 PM]
  Those images are tiny and you need to use HTML img tags for your pictures.   ;  OK, fixed the images.   ;  Problem fixed thanks to a clever person on IRC called Zao, who told my I was probably clobbering something! :)Indeed I was. I was calling DrawPrimitive() for each triangle but asking it to draw a TRIANGLESTRIP of X triangles, when I only wanted it to draw 1.Thank you!---P.S. I realise now that there are much better ways to draw a polygon like for example using a TRIANGLEFAN.   ;  In the future, do not mark threads 'resolved' in this forum. Thanks.   