Why won't this code show me anything in the window?

Started by
8 comments, last by TimmyC 13 years, 12 months ago
Hello, I tried to create a 2D vector in directx 9 but it won't actually show anything on the screen. Anyone have any ideas?

void PongRefresh()
{

	vertexBuffer->Lock(0, 0, (void**)&vertices, NULL);
  D3DXCOLOR vertexColour = D3DXCOLOR(1.0f,106/255,0.0f,1.0f);

  vertices[0].colour = vertexColour;
  vertices[0].x = 800/ 2.0f;
  vertices[0].y = 50.0f;
  vertices[0].z = 0.5f;
  vertices[0].rhw = 1.0f;

  vertices[1].colour = vertexColour;
  vertices[1].x = 800 - (800 / 5.0f);
  vertices[1].y = 600 - (600 / 5.0f);
  vertices[1].z = 0.5f;
  vertices[1].rhw = 1.0f;


  vertices[2].colour = vertexColour;
  vertices[2].x = 800 / 5.0f;
  vertices[2].y = 600 - (600 / 5.0f);
  vertices[2].z = 0.5f;
  vertices[2].rhw = 1.0f;

 //Unlock the vertex buffer
  vertexBuffer->Unlock();
  // Two new lines of code here
d3ddev->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );
}
Advertisement
Are you swapping the back buffers afterwards (if your using double buffer that is, which you probably are). Post the code that calls this function.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Okay here it is in full
main.cpp:
// include the basic windows header files and the Direct3D header files#include "References.h"#include "Pong.h"// include the basic windows header files and the Direct3D header file#include <windows.h>#include <windowsx.h>// global declarationsLPDIRECT3D9 d3d;    // the pointer to our Direct3D interface// function prototypesvoid initD3D(HWND hWnd);    // sets up and initializes Direct3Dvoid render_frame(void);    // renders a single framevoid cleanD3D(void);    // closes Direct3D and releases memory// the WindowProc function prototypeLRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);// the entry point for any Windows programint 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.hbrBackground = (HBRUSH)COLOR_WINDOW;    wc.lpszClassName = L"WindowClass";    RegisterClassEx(&wc);    hWnd = CreateWindowEx(NULL,                          L"WindowClass",                          L"Our First Direct3D Program",                          WS_OVERLAPPEDWINDOW,                          300, 300,                          800, 600,                          NULL,                          NULL,                          hInstance,                          NULL);    ShowWindow(hWnd, nCmdShow);    // set up and initialize Direct3D    initD3D(hWnd);    // enter the main loop:    MSG msg;    while(TRUE)    {        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))        {            TranslateMessage(&msg);            DispatchMessage(&msg);        }        if(msg.message == WM_QUIT)            break;        render_frame();    }    // clean up DirectX and COM    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);    // create the Direct3D interface    D3DPRESENT_PARAMETERS d3dpp;    // create a struct to hold various device information    ZeroMemory(&d3dpp, sizeof(d3dpp));    // clear out the struct for use    d3dpp.Windowed = TRUE;    // program windowed, not fullscreen    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    // discard old frames    d3dpp.hDeviceWindow = hWnd;    // set the window to be used by Direct3D    // create a device class using this information and the info from the d3dpp stuct    d3d->CreateDevice(D3DADAPTER_DEFAULT,                      D3DDEVTYPE_HAL,                      hWnd,                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,                      &d3dpp,                      &d3ddev);	//Create vertex bufferd3ddev->CreateVertexBuffer(sizeof(TLVERTEX) * 4, NULL,    D3DFVF_TLVERTEX, D3DPOOL_MANAGED, &vertexBuffer, NULL);d3ddev->SetStreamSource(0, vertexBuffer, 0, sizeof(TLVERTEX));}// this is the function used to render a single framevoid render_frame(void){    // clear the window to a deep blue    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);    d3ddev->BeginScene();    // begins the 3D scene    // do 3D rendering on the back buffer here	PongRefresh();    d3ddev->EndScene();    // ends the 3D scene    d3ddev->Present(NULL, NULL, NULL, NULL);   // displays the created frame on the screen}// this is the function that cleans up Direct3D and COMvoid cleanD3D(void){    d3ddev->Release();    // close and release the 3D device    d3d->Release();    // close and release Direct3D}

Pong.h:
#include "References.h"void PongRefresh(); //Pongconst DWORD D3DFVF_TLVERTEX = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1;IDirect3DVertexBuffer9* vertexBuffer;LPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class//Custom vertexstruct TLVERTEX{    float x;    float y;    float z;    float rhw;    D3DXCOLOR colour;    float u;    float v;};	TLVERTEX* vertices;void PongRefresh(){	vertexBuffer->Lock(0, 0, (void**)&vertices, NULL);  D3DXCOLOR vertexColour = D3DXCOLOR(1.0f,106/255,0.0f,1.0f);  vertices[0].colour = vertexColour;  vertices[0].x = 800/ 2.0f;  vertices[0].y = 50.0f;  vertices[0].z = 0.5f;  vertices[0].rhw = 1.0f;  vertices[1].colour = vertexColour;  vertices[1].x = 800 - (800 / 5.0f);  vertices[1].y = 600 - (600 / 5.0f);  vertices[1].z = 0.5f;  vertices[1].rhw = 1.0f;  vertices[2].colour = vertexColour;  vertices[2].x = 800 / 5.0f;  vertices[2].y = 600 - (600 / 5.0f);  vertices[2].z = 0.5f;  vertices[2].rhw = 1.0f; //Unlock the vertex buffer  vertexBuffer->Unlock();  // Two new lines of code hered3ddev->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );}

References.h:
//Reference files used in the project.#include <windows.h>#include <windowsx.h>#include <C:\Program Files (x86)\Microsoft DirectX SDK (February 2010)\Include\d3d9.h>#include <C:\Program Files (x86)\Microsoft DirectX SDK (February 2010)\Include\d3dx9.h>#include <d3d9.h>// include the Direct3D Library file#pragma comment (lib, "d3d9.lib")
Maybe I'm asking a stupid thing, but where/how do you setup your projection matrix?
Quote:Original post by szecs
Maybe I'm asking a stupid thing, but where/how do you setup your projection matrix?


There is none.
In your vertex structure, you should be storing your colour as a DWORD (or D3DCOLOR - same thing), not a D3DXCOLOR (consists of four floats). Not sure what the rules on locking vertex buffers are, but it might pay to setstreamsource after the unlock and before drawing your primitive. I forget if you need to call SetFVF or not with VBs, but it's something else to try (right before drawprimitive).
I'm not about d3d, but no projection matrix means identity matrix (or something similar in scale) so, the screen is mapped to a -1...1 or 0...1 space (or something similar), so your triangle is way too huge to be in this range. Try a smaller triangle, with coordinates ranging from 0 to 1.0.

I hope that helps.
Thanks I got it working by defining using SETFVF and using a DWORD color. However only 3 vertices will ever render. I.e. if I want to make a square
  vertices[0].colour = vertexColour;  vertices[0].x = 120.0f;  vertices[0].y = 50.0f;  vertices[0].z = 0.0f;  vertices[0].rhw = 1.0f;    vertices[1].colour = vertexColour;  vertices[1].x = 520.0f;  vertices[1].y = 400.0f;  vertices[1].z = 0.0f;  vertices[1].rhw = 1.0f;    vertices[2].colour = vertexColour;  vertices[2].x = 120.0f;  vertices[2].y = 400.0f;  vertices[2].z = 0.0f;  vertices[2].rhw = 1.0f;  vertices[3].colour = vertexColour;  vertices[3].x = 520.0f;  vertices[3].y = 50.0f;  vertices[3].z = 0.0f;  vertices[3].rhw = 1.0f; 

It needs 4 vertices but it will never render vertices[3] or even vertices[4], vertices[5], or any vertices[>2] for the matter meaning I only ever get a right angled triangle of 3 vertices rendering. Is there some setting I am missing?
d3ddev->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2 );

change the 2 into 3,4 or whatever.
Quote:Original post by koopertrooper
Thanks I got it working by defining using SETFVF and using a DWORD color. However only 3 vertices will ever render. I.e. if I want to make a square
  vertices[0].colour = vertexColour;  vertices[0].x = 120.0f;  vertices[0].y = 50.0f;  vertices[0].z = 0.0f;  vertices[0].rhw = 1.0f;    vertices[1].colour = vertexColour;  vertices[1].x = 520.0f;  vertices[1].y = 400.0f;  vertices[1].z = 0.0f;  vertices[1].rhw = 1.0f;    vertices[2].colour = vertexColour;  vertices[2].x = 120.0f;  vertices[2].y = 400.0f;  vertices[2].z = 0.0f;  vertices[2].rhw = 1.0f;  vertices[3].colour = vertexColour;  vertices[3].x = 520.0f;  vertices[3].y = 50.0f;  vertices[3].z = 0.0f;  vertices[3].rhw = 1.0f; 

It needs 4 vertices but it will never render vertices[3] or even vertices[4], vertices[5], or any vertices[>2] for the matter meaning I only ever get a right angled triangle of 3 vertices rendering. Is there some setting I am missing?

If you want to render a square, you should probably specify D3DPT_TRIANGLEFAN as your primitive type, and reorder your vertex array to define the corner points of your quad clockwise (or counter-clockwise, depending on what backface culling you use) starting from (say) the top-left.

Edit: more importantly, what Kasya said.

This topic is closed to new replies.

Advertisement