Cannot draw primitive

Started by
6 comments, last by Temrek 16 years, 2 months ago
Hi, I am trying to learn to use DirectX and Direct 3d specifically, using various sources. I am working with c++ and I cannot figure out why this c++ code does not work, all I an trying to do is to draw a square. Instead it simply displays a black background with a slightly greenish tone (As its supposed to be). This should be all relevant parts, thank you for any help.
	while (TRUE)
	{	
		RE.CreatePlane();
		RE.RenderFrame();
       }

void RatEngine::RenderFrame()
{
   dxDev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 26, 0), 1.0f, 0);
	
    dxDev->BeginScene();
    dxDev->SetFVF(CUSTOMFVF);
    // select the vertex buffer to display
    dxDev->SetStreamSource(0, dxVB, 0, sizeof(VERTEXSTRUCT));
	// copy the vertex buffer to the back buffer
     dxDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
    dxDev->EndScene(); 

    dxDev->Present(NULL, NULL, NULL, NULL);

    return;
}

void RatEngine::CreatePlane()
{
	//Define the plane
	    struct VERTEXSTRUCT vsPlane[] =
    {
		{ -3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255),},// 1,0,},
		{ 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0),},// 0,0,},
		{ -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0),},// 1,1,},
		{ 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255),},// 0,1,},
      };
		//Create the vertex buffer
		   dxDev->CreateVertexBuffer(4*sizeof(VERTEXSTRUCT),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &dxVB,
                               NULL);
	VOID* pVoid;

    // lock t_buffer and load the vertices into it
    dxVB->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, vsPlane, sizeof(vsPlane));
    dxVB->Unlock();

    return;
}
This is the top of the ratengine.h file.
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>

#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
//Struct prototypes
struct VERTEXSTRUCT {    FLOAT x, y, z, rhw;   // from the D3DFVF_XYZRHW flag
    DWORD color;    // from the D3DFVF_DIFFUSE flag
    };
struct CURRENTGAMESTATE {bool Menu; bool Game;};
//Definations
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) //| D3DFVF_TEX1)
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// Global Pointers
LPDIRECT3D9 d3d;    // D3D interface pointer
LPDIRECT3DDEVICE9 dxDev;    // device class pointer
LPDIRECT3DVERTEXBUFFER9 dxVB = NULL;    // vertex buffer pointer 

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

]
Advertisement
At first glance your VERTEXSTRUCT initialisation isn't specifying the ::rhw field so it'll just be garbage and thus screw up the rendering. Explicitly set this to 1.0 to verify.

If that isn't it, fire up the debug runtimes and check all of your return codes - might well be that something is failing.

If that still doesn't yield anything, try setting D3DRS_CULLMODE = D3DCULL_NONE to ensure your vertex ordering isn't working against you.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

I tried it all, nothing seems to fail but it does still not work :(

dxDev->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );	dxDev->SetRenderState(D3DRS_LIGHTING, FALSE);        dxDev->SetRenderState(D3DRS_ZENABLE, TRUE);    


	    struct VERTEXSTRUCT vsPlane[] =    {	     { 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(220, 220, 220), },        { 9.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(220, 220, 220), },        { 9.0f, 9.0f, 0.0f, D3DCOLOR_XRGB(220, 220, 220), },        { 9.0f, 9.0f, 0.0f, D3DCOLOR_XRGB(220, 220, 220), },      };


struct VERTEXSTRUCT {FLOAT X, Y, Z; DWORD COLOR;};
You're still screwing up the RHW:
struct VERTEXSTRUCT vsPlane[] ={   { 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(220, 220, 220), },   { 9.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(220, 220, 220), },   { 9.0f, 9.0f, 0.0f, D3DCOLOR_XRGB(220, 220, 220), },   { 9.0f, 9.0f, 0.0f, D3DCOLOR_XRGB(220, 220, 220), },};
For the first vertex,that sets X to 3, Y to 3, Z to 0, rhw to 14474460, and leaves the colour undefined. This is why I never fill in structs like that (It's a horrible thing to do in C++ anyway).

You probably want:
struct VERTEXSTRUCT vsPlane[] ={   { 3.0f, 3.0f, 0.0f, 0.5f, D3DCOLOR_XRGB(220, 220, 220), },   { 9.0f, 3.0f, 0.0f, 0.5f, D3DCOLOR_XRGB(220, 220, 220), },   { 9.0f, 9.0f, 0.0f, 0.5f, D3DCOLOR_XRGB(220, 220, 220), },   { 9.0f, 9.0f, 0.0f, 0.5f, D3DCOLOR_XRGB(220, 220, 220), },};
(Note that using values of exactly 0 or 1 for RHW can cause weird bugs sometimes.

In addition, make sure you enable the Debug Runtimes.

EDIT: Wait, what? Are you using transformed vertices or not?
Oops, I accidently opened an older version.

struct VERTEXSTRUCT { FLOAT x, y, z, rhw; DWORD color;};

is how it looks, and I am afrain defining rhw as 0.5f for all vertexes did nothing :(


Also, I tried enabling the dubbing as per the link (did not know you could do that!). The output windows then lists the following repeatedly:

Direct3D9: (ERROR) : [0] : Address 00F27CE6
Direct3D9: (ERROR) : [1] : Address 00FFBDD8
Direct3D9: (ERROR) : [2] : Address 00F76EAF
Direct3D9: (ERROR) : [3] : Address 00F6B054
Direct3D9: (ERROR) : [4] : Address 00F6ADE6
Direct3D9: (ERROR) : [5] : Address 00F6AA33
Direct3D9: (ERROR) : [6] : Address 00F55692
Direct3D9: (ERROR) : [7] : Address 00411B53
Direct3D9: (ERROR) : [8] : Address 00411DA8
Direct3D9: (ERROR) : [9] : Address 00413168
Direct3D9: (ERROR) : [10] : Address 00412ECF
Direct3D9: (ERROR) : [11] : Address 7C816FD7
Direct3D9: (ERROR) : [12] : Address 00000000
Direct3D9: (ERROR) : [13] : Address 00000000
Direct3D9: (ERROR) : [14] : Address 00000000
Direct3D9: (ERROR) : [15] : Address 00000000

I tried looking up the errors but several of them made no sense.
Those errors are just telling you that there's memory leaks in your application (Scroll up and you'll see a message like "D3D9 (WARN): Memory leaks detected. Dumping...").

Can we see your complete code (Paste it in [ source ] tags please)?
Well ok, but keep in mind that I have not really put that much effort into doing things properly as its justa learning exercise. (Set dimensions should go into the contructor, for instance)
// include the basic windows header files and the Direct3D header file#include "stdafx.h"#include "RatEngine.h"#include "WndMsgHandler.h"// the WindowProc function prototypeLRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);// Entry Pointint WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,                   LPSTR lpCmdLine,                   int nCmdShow){	//The Game engine class	RatEngine RE;	RE.SetDimensions(1024, 768);	//The windows message handler	WndMsgHandler WMH;	HWND hWd; //The main app window	hWd = RE.InitWindow(hInstance);	RE.InitDirectX();	//The main game loop	while (TRUE)     {			RE.CreatePlane();		RE.RenderFrame();		RE.KeyChecker();		int nSignal = WMH.IntereptMessage(); //Store any windows message signals		//If IntereptMessage tells the app to quit, do so	if (nSignal == 1)	{			RE.DestroyEngine();		break;		}			}	return 0;}LRESULT 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);}


RatEngine.h

#include <windows.h>#include <windowsx.h>#define D3D_DEBUG_INFO#include <d3d9.h>#include <d3dx9.h>#pragma comment (lib, "d3d9.lib")#pragma comment (lib, "d3dx9.lib")//Struct prototypesstruct VERTEXSTRUCT {FLOAT x, y, z, rhw; DWORD color;};struct CURRENTGAMESTATE {bool Menu; bool Game;};//Definations#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)// Global PointersLPDIRECT3D9 d3d;    // D3D interface pointerLPDIRECT3DDEVICE9 dxDev;    // device class pointerLPDIRECT3DVERTEXBUFFER9 dxVB = NULL;    // vertex buffer pointer // the WindowProc function prototypeLRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);class RatEngine{HWND hWd; //The window to be createdpublic:	void SetDimensions(int nX, int nY);	HWND InitWindow(HINSTANCE hInstance);	int ScreenX; int ScreenY;	void InitDirectX();	void RenderFrame();	void DestroyEngine();	void KeyChecker();	void CreatePlane();};//This function handles all the intialization code for the main game windowHWND RatEngine::InitWindow(HINSTANCE hInstance){	    WNDCLASSEX wStr; //The window struct	    ZeroMemory(&wStr, sizeof(WNDCLASSEX)); //Set the struct to null	//Enter struct settings    wStr.cbSize = sizeof(WNDCLASSEX);    wStr.style = CS_HREDRAW | CS_VREDRAW;    wStr.lpfnWndProc = (WNDPROC)WindowProc;    wStr.hInstance = hInstance;    wStr.hCursor = LoadCursor(NULL, IDC_ARROW);    wStr.lpszClassName = L"WindowClass";		RegisterClassEx(&wStr); //Leave the window to windows	//Create the window    hWd = CreateWindowEx(NULL, L"WindowClass", L"RatsFTW",		WS_EX_TOPMOST | WS_POPUP, 0, 0, ScreenX, ScreenY,        NULL, NULL, hInstance, NULL);	//Display it!    ShowWindow(hWd, NULL);	return hWd;}//This function handles all the intialization code for DirectXvoid RatEngine::InitDirectX(){   	//Create the device	d3d = Direct3DCreate9(D3D_SDK_VERSION);	 D3DPRESENT_PARAMETERS dxStr; //Create the Direct3d data struct    ZeroMemory(&dxStr, sizeof(dxStr)); //Clear the data struct	//Enter d3d settings	dxStr.Windowed = FALSE;    dxStr.SwapEffect = D3DSWAPEFFECT_DISCARD;    dxStr.hDeviceWindow = hWd;    dxStr.BackBufferFormat = D3DFMT_X8R8G8B8;    dxStr.BackBufferWidth = ScreenX;    dxStr.BackBufferHeight = ScreenY;    dxStr.EnableAutoDepthStencil = TRUE;    dxStr.AutoDepthStencilFormat = D3DFMT_D16;	 //Create the device, put in new information and send the struct    d3d->CreateDevice(D3DADAPTER_DEFAULT,                      D3DDEVTYPE_HAL,                      hWd,                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,                      &dxStr,                      &dxDev);	dxDev->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );	dxDev->SetRenderState(D3DRS_LIGHTING, FALSE);        dxDev->SetRenderState(D3DRS_ZENABLE, TRUE);        return;}//This function renders a single framevoid RatEngine::RenderFrame(){   dxDev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0);   dxDev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);	    dxDev->BeginScene();	    // select which vertex format we are using    dxDev->SetFVF(CUSTOMFVF);    // select the vertex buffer to display    dxDev->SetStreamSource(0, dxVB, 0, sizeof(VERTEXSTRUCT));	// copy the vertex buffer to the back buffer     dxDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);    dxDev->EndScene();     dxDev->Present(NULL, NULL, NULL, NULL);    return;}void RatEngine::DestroyEngine(){   dxVB->Release();    // Destroy vertex buffer   dxDev->Release();    // Destroy Device   d3d->Release();    // Destroy Interface pointer    return;}//This function checks for any keys that are pressedvoid RatEngine::KeyChecker(){//Tell the app ti shutdow´nf if a key is pressedif(KEY_DOWN(VK_ESCAPE))	{    PostMessage(hWd, WM_DESTROY, 0, 0);	}}//Create the basic plane the game takes place onvoid RatEngine::CreatePlane(){	//Define the plane	    struct VERTEXSTRUCT vsPlane[] =		{   { 3.0f, 3.0f, 0.0f, 0.5f, D3DCOLOR_XRGB(220, 220, 220), },   { 9.0f, 3.0f, 0.0f, 0.5f, D3DCOLOR_XRGB(220, 220, 220), },   { 9.0f, 9.0f, 0.0f, 0.5f, D3DCOLOR_XRGB(220, 220, 220), },   { 9.0f, 9.0f, 0.0f, 0.5f, D3DCOLOR_XRGB(220, 220, 220), },      };		//Create the vertex buffer		   dxDev->CreateVertexBuffer(4*sizeof(VERTEXSTRUCT),                               0,                               CUSTOMFVF,                               D3DPOOL_MANAGED,                               &dxVB,                               NULL);	VOID* pVoid;    // lock t_buffer and load the vertices into it    dxVB->Lock(0, 0, (void**)&pVoid, 0);    memcpy(pVoid, vsPlane, sizeof(vsPlane));    dxVB->Unlock();	return;}//Set the screen resolutionvoid RatEngine::SetDimensions(int nX, int nY){	ScreenX = nX;	ScreenY = nY;}


WndMsgHandler.h

#include <windows.h>#include <windowsx.h>class WndMsgHandler{	MSG msg; //This one stores the windows messages.public: 	int IntereptMessage();};////This function takes a message and interpets it, and returns an INT value that signals//to the main .cpp file what to do.int WndMsgHandler::IntereptMessage(){	if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))        {			            if (msg.message == WM_QUIT)			{                //Tell the main .cpp file to quit.				return 1;			}            TranslateMessage(&msg);            DispatchMessage(&msg);        }	//Tell the main .cpp file to do nothing.	return 0;}


[/source]
I am suspecting that the void pointer is the criminal, am I wrong?

This topic is closed to new replies.

Advertisement