# Vertexbuffer doesn't work?

This topic is 3769 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I started learning DirectX and tried to render a simple triangle. But all what happens is that the window turns black. So.. the Code(the whole):
//Main.cpp

#include <windows.h>
#include "Game.h"

//globale Variablen
HINSTANCE DXTestInstance;
HWND MainWindow;
bool GameIsRunning;
Game GameDX;

//Der Handler des Hauptfensters
LRESULT CALLBACK WindowHandler(HWND windowHandle, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_QUIT:
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(windowHandle, message, wParam, lParam);
}

//---------------------------------------------------------------------------------------------
//Die Hauptnethode: Der Einsprungspunkt in das Programm
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int CmdShow)
{
GameIsRunning = true;
//Fenster erstellen
MSG message;
WNDCLASSEX wcx;
DXTestInstance = hInstance;
wcx.cbSize = sizeof(wcx);
wcx.lpszClassName = "DXTest";
wcx.lpfnWndProc = WindowHandler;
wcx.style = CS_VREDRAW |CS_HREDRAW;
wcx.hInstance = hInstance;
wcx.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hIcon = 0;
wcx.hIconSm = 0;

if(!RegisterClassEx(&wcx))
return 0;

MainWindow = CreateWindowEx(0, "DXTest", "DXTest", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if(!MainWindow)
return 0;
ShowWindow(MainWindow, CmdShow);
if(GameDX.initialize() == 0)
return 0;
while(GameIsRunning)
{
if(PeekMessage(&message, NULL, 0, 0, PM_NOREMOVE))
{
if(GetMessage(&message, NULL, 0, 0) == 0)
return 0;
TranslateMessage(&message);
DispatchMessage(&message);
}
else
{
//Hauptschleife
GameDX.Render();
}
}
return 0;
}


//Game.h
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

//Pragmas
//Includes
#include <stdio.h>
#include <stdarg.h>
#include <windows.h>
#include <commctrl.h>
#include <d3dx9.h>
#include <dsound.h>
#include <dinput.h>
#include <iostream>
//Defines
#define TILEVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
//Usings
using namespace std;
//Externs
extern HWND MainWindow;
//Global Vars
//Global Methods
//Structs
struct TileVertex
{
D3DXVECTOR3 Position;
D3DCOLOR Color;
};
//Classes
class Game
{
public:
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 device;
D3DPRESENT_PARAMETERS d3dpp;
LPDIRECT3DVERTEXBUFFER9 vertexbuffer;
TileVertex* vertex;

Game();
~Game();

int initialize();
void CreateVertexBuffer();
void Render();
};
#endif // GAME_H_INCLUDED


#include "Game.h"

Game::Game()
{
}
int Game::initialize()
{
D3DDISPLAYMODE d3ddm;
D3DDEVTYPE devtype;
D3DCAPS9 caps;

d3d = Direct3DCreate9( D3D_SDK_VERSION);
if( !d3d)
return 0;

return 0;

ZeroMemory( &d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.BackBufferFormat = d3ddm.Format;

if( d3d->GetDeviceCaps( D3DADAPTER_DEFAULT,  D3DDEVTYPE_HAL, &caps) < 0)
{
MessageBox( MainWindow, "no HAL Device", "HAL", MB_OK | MB_ICONWARNING | MB_SETFOREGROUND);
devtype = D3DDEVTYPE_REF;
}
else
devtype = D3DDEVTYPE_HAL;

if( d3d->CreateDevice( D3DADAPTER_DEFAULT, devtype, MainWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device) < 0)
return 0;
return 1;
}
void Game::CreateVertexBuffer()
{
device->CreateVertexBuffer(4*sizeof(*vertex), 0, TILEVERTEX, D3DPOOL_MANAGED, &vertexbuffer, NULL);//4 * sizeof(TileVertex)
vertexbuffer->Lock(0, 0, (void**)&vertex, 0);
vertex[0].Position = D3DXVECTOR3(150, 150, 1);
vertex[0].Color = D3DCOLOR_XRGB(255, 0, 0);
vertex[1].Position = D3DXVECTOR3(200, 150, 1);
vertex[1].Color = D3DCOLOR_XRGB(255, 0, 0);
vertex[2].Position = D3DXVECTOR3(150, 200, 1);
vertex[2].Color = D3DCOLOR_XRGB(0, 255, 0);
vertex[3].Position = D3DXVECTOR3(200, 200, 1);
vertex[3].Color = D3DCOLOR_XRGB(0, 255, 0);
vertexbuffer->Unlock();
device->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);
}
void Game::Render()
{
D3DXMATRIX* world = new D3DXMATRIX();
D3DXMATRIX* view = new D3DXMATRIX();
D3DXVECTOR3 Point;
D3DXVECTOR3 LookAtPoint;

Point = D3DXVECTOR3(200, 200, 1);
LookAtPoint = D3DXVECTOR3(200, 200, 0);

device->Clear(0, NULL, D3DCLEAR_TARGET |D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);
device->BeginScene();
D3DXMatrixLookAtLH(view, &Point, &LookAtPoint, &D3DXVECTOR3( 0.0f, 1.0f, 0.0f));
device->SetTransform( D3DTS_VIEW, view);
D3DXMatrixTranslation(world, 0, 0, 0);
device->SetTransform( D3DTS_WORLD, world);
//device->SetRenderState( D3DRS_LIGHTING, false );
device->SetRenderState(D3DRS_AMBIENT, 0xffffff);
device->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_COLOR1);
device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
device->SetFVF(TILEVERTEX);
device->SetStreamSource(0, vertexbuffer, 0, sizeof(TileVertex));
//device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vertex, sizeof(TileVertex));
device->EndScene();
device->Present(NULL, NULL, NULL, NULL);
delete world;
delete view;
}
Game::~Game()
{
if(vertexbuffer)
{
vertexbuffer->Release();
}
delete d3d;
delete device;
delete vertexbuffer;
delete[] vertex;
}
[/source

I hope someone can help me...
... and pls excuse mistakes in this post, I'm from germany^^
edit:
ok, i added source tags, before I tried code tags^^
and thanks, I will test your tipps tommorow =)

[Edited by - Ankou6591 on October 26, 2007 8:50:57 AM]

##### Share on other sites
Shouldn't you disable D3DRS_LIGHTING ?

##### Share on other sites
Oh god, a "black window" problem [smile]

• First, and most importantly - install the debug runtimes. In recent SDKs, it's in the DirectX control pannel in the start menu -> DirectX SDK -> Utilities. In older SDKs, it's in the DirectX control pannel in the Windows control pannel. In either case, go to the D3D tab and select "Use Debug Version of Direct3D9".
That'll then spit out any problems to the debug output.
• if( d3d->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps) < 0)
is NOT the correct way to check for failure. You should really use the SUCCEEDED and FAILED macros for this instead. While it will probably work, it's not The Right Way.
• You should really be using CheckDeviceFormat() and CheckDepthStencilMatch(). I assume it works just now, but I bet it won't work at all if you change your display to 32-bit. You're assuming that your current backbuffer will work with a 16-bit depth buffer, which is often not the case if the backbuffer isn't 16-bit.
• Does it work if you use DrawPrimitiveUP instead of a vertex buffer? That takes the vertex buffer code out of the equation.
• Have you tried disabling culling? That removes a vertex winding order bug from the equation.
• Have you tried clearing your backbuffer to a colour other than black (say bright green)? It may be that a triangle is rendered, but it's black so you can't see it on the black background.
• Have you tried using transformed vertices (D3DFVF_XYZRHW)? That removes any transform problem.
• You're not clearing the Z-buffer (this is probably the problem). You should use D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER instead of D3DCLEAR_TARGET if you have a Z-buffer.

Also, a little off topic - it's generally better to use [ source ] tags for longer chunks of code. It reduces the vertical page width, and also does syntax highlighting. You can click the Edit button next to your first post if you want to change it.

##### Share on other sites
I sympathize with your plight. As my friend Russ Fish says "the hard part about graphics is that there are so many different ways to code up a black screen".

As Evil Steve pointed out, you have some basics to master w.r.t. HRESULTs and COM method calls. Take a look at Chapter 1. Introduction from my book for methods of dealing with the profusion of HRESULTs and COM interface pointers that you're going to encounter while usinng Direct3D.

Then, um... well, maybe you should just read the rest of the chapters.

Take some time to study that pipeline diagram. Print it out on multiple sheets of paper and tape them together and put it on the wall right next to your computer. Stare at it a lot until you understand what's going on when. The chapters of my book follow the order in which you need to understand them to program the entire pipeline, starting with a simple program and adding more and more rendering features through the pipeline from top to bottom in that diagram.

Oh, and the quickest way to diagnose "why am I getting a black screen?" is to change your background color to white (via IDirect3DDevice9::Clear) and see if you get black geometry on your white screen.

##### Share on other sites
ok, thanks, but it still doesn't work...
I edited the new Source in my first Post.
Quote:
 First, and most importantly - install the debug runtimes. In recent SDKs, it's in the DirectX control pannel in the start menu -> DirectX SDK -> Utilities. In older SDKs, it's in the DirectX control pannel in the Windows control pannel. In either case, go to the D3D tab and select "Use Debug Version of Direct3D9".That'll then spit out any problems to the debug output.

done, but nothing happened... Maybe because there is more than one version Version of DirectX on my PC. I changed it in the Windows control panel, I don't have DirectX in the start menu, I erased it(i erased nearly everything in start->programme(german^^)) and I can't find it in the DirectX SDK Installation Folder.
But, i don't even know what should happen, so maybe the debugger works and I just can't see it...
Quote:
 Shouldn't you disable D3DRS_LIGHTING ?

nothing happened =(
Quote:
 You should really be using CheckDeviceFormat() and CheckDepthStencilMatch(). I assume it works just now, but I bet it won't work at all if you change your display to 32-bit. You're assuming that your current backbuffer will work with a 16-bit depth buffer, which is often not the case if the backbuffer isn't 16-bit.

I don't know how to use it. The Problem is, I can't find what RType is?(except of a cool SNES Game..).
Quote:
 Does it work if you use DrawPrimitiveUP instead of a vertex buffer? That takes the vertex buffer code out of the equation.

if I used it right... no
Quote:
 Have you tried disabling culling? That removes a vertex winding order bug from the equation.

nothing happened
Quote:
 Have you tried clearing your backbuffer to a colour other than black (say bright green)? It may be that a triangle is rendered, but it's black so you can't see it on the black background.

It's blue now, but there's nothing
Quote:
 Have you tried using transformed vertices (D3DFVF_XYZRHW)? That removes any transform problem.

nothing happened...
Quote:
 You're not clearing the Z-buffer (this is probably the problem). You should use D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER instead of D3DCLEAR_TARGET if you have a Z-buffer.

nothing happened x(
@legalize: ok, thx, I didn't read it yet, but I will^^

So, now ... mhm, it turned to a blue window problem^^

and, yes I know, my english is pretty bad

##### Share on other sites
Couple of things that look odd to me....

I dont see a call to 'new' your vertex array anywhere...missing a 'vertex = new TileVertex[4];'?

The way you've placed your triangles in relation to the camera's position looks to me like you wouldnt see the them. Your camera's position is the same as one of your vertices, with all the other vertices on a plane perpendicular to your lookat vector.

You have set the World and View matrices but there is no Projection specified.

##### Share on other sites
I would make simple shader to knock out all possible problems with lighting and stuff. you can also try to render with transformed vertices. that would knock out possible problems with camera and position. I dont know if you have already done some shader programming, but its quite easy. take a look at SDK samples. here you have simple shader, if that helps:

float4x4 view;float4x4 projection;float4 basicVs(float4 pos :POSITION) :POSITION{	float4x4 projView = mul(view, projection);	return mul(pos, projView);}float4 basicPs(float4 pos :POSITION) : COLOR0{    return float4(1,1,1,1);}technique test{    pass p0    {        VertexShader = compile vs_2_0 basicVs();        PixelShader = compile ps_2_0 basicPs();            }}

if you want to render with transformed vertices, just set FVF to position only, and make triangle like (100,100,0), (200,100,0), (100,200,0) and set VertexShader in that .fx file to null. then you have to see something!

##### Share on other sites
thanks to all
I still don't know why the code didn't work, but now it does, after I coded it the third time new...