Sign in to follow this  

Stream 0 size is too small

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am doing a simple DirectX application that draws a line strip. It seems to initialize, set vertex declaration, put data in the vertex buffer, etc just fine. But then I get an error on the drawing, Stream 0 size is too small:
void drawPrimitives()
{
	lpD3Ddevice->BeginScene();

	lpD3Ddevice->SetVertexDeclaration(vbDecl);
	lpD3Ddevice->SetStreamSource(0, lpVB, 0, sizeof(Vertex));
	lpD3Ddevice->DrawPrimitive(D3DPT_LINESTRIP, 0, 6);

	lpD3Ddevice->EndScene();
}

Here is my full source code:
//header.h
#ifndef __HEADER_INCLUDED__
#define __HEADER_INCLUDED__

#define DEBUG
#define D3D_DEBUG_INFO

#include <windows.h>
#include <d3dx9.h>
#include <d3d9.h>

#include "log.h"

extern LRESULT CALLBACK	WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
extern bool				initD3D(HWND hWnd);
extern bool				initVertexDecl();
extern bool				isDeviceLost();
extern bool				initVertexBuffer();
extern void				drawPrimitives();

struct Vertex
{
	Vertex():position(0,0,0){}
	Vertex(float x, float y, float z):position(x, y, z){}
	D3DXVECTOR3 position;
};

#endif __HEADER_INCLUDED__

#include "header.h"

bool				isWindowOpen;
IDirect3D9*			lpD3D;
IDirect3DDevice9*		lpD3Ddevice;
D3DPRESENT_PARAMETERS		d3dPP;
IDirect3DVertexBuffer9*		pVB;
IDirect3DVertexDeclaration9*	vbDecl;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASS 	wc;
	HWND	 	hWnd;
	MSG		msg;

	msg.message = WM_NULL;
	isWindowOpen = true;

	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
	wc.style = 0;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpszClassName = "Win32 Class";
	wc.lpszMenuName = NULL;
	wc.lpfnWndProc = WndProc;

	if(!RegisterClass(&wc))
	{
		MessageBox(NULL, "Failed to register wndclass!", "Error", MB_OK);
		return 0;
	}

	hWnd = CreateWindow("Win32 Class", "My Window", WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, NULL, NULL, hInstance, NULL);
	if(hWnd == NULL)
	{
		MessageBox(NULL, "Failed to create window!", "Error", MB_OK);
		return 0;
	}

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	initLogFile();

	if(initD3D(hWnd))
	{
		while(msg.message != WM_QUIT && isWindowOpen)
		{
			while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
			if(!isDeviceLost())
				drawPrimitives();
		}
	}
	else
	{
		MessageBox(NULL, "Failed to initialize Direct3D", "Error", MB_OK);
		return 0;
	}
	if(lpVB != NULL)
		lpVB->Release();
	if(lpD3Ddevice != NULL)
		lpD3Ddevice->Release();
	if(lpD3D != NULL)
		lpD3D->Release();

	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_CLOSE:
		isWindowOpen = false;
		DestroyWindow(hWnd);
		break;
	case WM_DESTROY:
		isWindowOpen = false;
		PostQuitMessage(0);
		break;
	default: 
		return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return 0;
}

bool initD3D(HWND hWnd)
{
	//lpD3D = Direct3DCreate9(DIRECT3D_VERSION);
	lpD3D = Direct3DCreate9(32);
	if(lpD3D == NULL)
	{
		return false;
	}

	D3DCAPS9				devCaps;
	DWORD					behaviorFlags = 0;
	ZeroMemory(&d3dPP, sizeof(D3DPRESENT_PARAMETERS));


	lpD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &devCaps);

	if(devCaps.DevCaps & D3DDEVCAPS_PUREDEVICE) 
	{
		behaviorFlags |= D3DCREATE_PUREDEVICE;
		behaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
	}
	else
	{
		if(devCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
			behaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
		else
			behaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
	}

	if(devCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
		d3dPP.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
	else
		d3dPP.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;

	int numDeviceModes = lpD3D->GetAdapterModeCount(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8) - 1;
	D3DDISPLAYMODE	displayMode;
	
	RECT highestResolution;
	highestResolution.right = 0;
	highestResolution.bottom = 0;
	highestResolution.left = 0;

	if(numDeviceModes > 0)
	{
		writeToLog("Using 32-bit X8R8G8B8 format for backbuffer.");
	
		for(int i = 0; i <= numDeviceModes; i++)
		{
			if(SUCCEEDED(lpD3D->EnumAdapterModes(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8, i, &displayMode)))
			{
				writeToLog("\nAdapter Mode "); writeToLog(i); writeToLog(": ");
				writeToLog("Res: "); writeToLog((int)displayMode.Width); writeToLog("x"); writeToLog((int)displayMode.Height);
				writeToLog(" Refresh Rate: "); writeToLog((int)displayMode.RefreshRate); writeToLog("HZ \n");
				if(displayMode.Width > highestResolution.right)
					highestResolution.right = displayMode.Width;
				if(displayMode.Height > highestResolution.bottom)
					highestResolution.bottom = displayMode.Height;
				if(displayMode.RefreshRate > highestResolution.left)
					highestResolution.left = displayMode.RefreshRate;
			}
		}

		d3dPP.BackBufferFormat = D3DFMT_X8R8G8B8;
	}
	else
	{
		int numDeviceModes = lpD3D->GetAdapterModeCount(D3DADAPTER_DEFAULT, D3DFMT_R5G6B5) - 1;

		D3DDISPLAYMODE	displayMode;
		writeToLog("Using 16-bit R5G6B5 format for backbuffer.");
	
		for(int i = 0; i <= numDeviceModes; i++)
		{
			if(SUCCEEDED(lpD3D->EnumAdapterModes(D3DADAPTER_DEFAULT, D3DFMT_R5G6B5, i, &displayMode)))
			{
				writeToLog("\nAdapter Mode "); writeToLog(i); writeToLog(": ");
				writeToLog("Res: "); writeToLog((int)displayMode.Width); writeToLog("x"); writeToLog((int)displayMode.Height);
				writeToLog(" Refresh Rate: "); writeToLog((int)displayMode.RefreshRate); writeToLog("HZ \n");
				if(displayMode.Width > highestResolution.right)
					highestResolution.right = displayMode.Width;
				if(displayMode.Height > highestResolution.bottom)
					highestResolution.bottom = displayMode.Height;
				if(displayMode.RefreshRate > highestResolution.left)
					highestResolution.left = displayMode.RefreshRate;
			}
		}

		d3dPP.BackBufferFormat = D3DFMT_R5G6B5;
	}

	if(SUCCEEDED(lpD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dPP.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D24S8)))
	{
		d3dPP.AutoDepthStencilFormat = D3DFMT_D24S8;
		d3dPP.EnableAutoDepthStencil = true;
	}
	else if(SUCCEEDED(lpD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dPP.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D15S1)))
	{
		d3dPP.AutoDepthStencilFormat = D3DFMT_D15S1;
		d3dPP.EnableAutoDepthStencil = true;
	}
	else if(SUCCEEDED(lpD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dPP.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16)))
	{
		d3dPP.AutoDepthStencilFormat = D3DFMT_D16;
		d3dPP.EnableAutoDepthStencil = true;
	}
	else
		d3dPP.EnableAutoDepthStencil = false;

	DWORD multiSampleQualLevels;

	if(SUCCEEDED(lpD3D->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dPP.BackBufferFormat, true, D3DMULTISAMPLE_2_SAMPLES, &multiSampleQualLevels))
		&& SUCCEEDED(lpD3D->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dPP.AutoDepthStencilFormat, true, D3DMULTISAMPLE_2_SAMPLES, &multiSampleQualLevels)))
	{
		
		d3dPP.MultiSampleType = D3DMULTISAMPLE_2_SAMPLES;
		d3dPP.MultiSampleQuality = multiSampleQualLevels - 1;
	}
	else
	{
		d3dPP.MultiSampleType = D3DMULTISAMPLE_NONE;
		d3dPP.MultiSampleQuality = 0;
	}

	d3dPP.BackBufferCount = 1;
	d3dPP.BackBufferWidth = highestResolution.right;
	d3dPP.BackBufferHeight = highestResolution.bottom;
	d3dPP.FullScreen_RefreshRateInHz = 0;
	d3dPP.hDeviceWindow = hWnd;
	d3dPP.Windowed = true;
	d3dPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dPP.Flags = NULL;

	writeToLog("\nChosen Res: "); writeToLog((int)highestResolution.right); writeToLog("x"); writeToLog((int)highestResolution.bottom);

	if(FAILED(lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, behaviorFlags, &d3dPP, &lpD3Ddevice)))
		return false;

	if(!initVertexDecl())
		return false;

	if(!initVertexBuffer())
		return false;
	
	lpD3Ddevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, RGB(255.0f,255.0f,255.0f), 1.0f, 0);

	return true;
}

bool isDeviceLost()
{
	if(!isWindowOpen)
		return true;

	HRESULT hr = lpD3Ddevice->TestCooperativeLevel();

	if(hr == D3DERR_DEVICELOST)
	{
		Sleep(20);
		return true;
	}
	else if(hr == D3DERR_DEVICENOTRESET)
	{
		lpD3Ddevice->Reset(&d3dPP);
		return false;
	}
	else if(hr == D3DERR_DRIVERINTERNALERROR)
	{
		MessageBox(NULL, "Internal Driver Error.", "Error", MB_OK);
		PostQuitMessage(0);
		return true;
	}

	return false;
}

void drawPrimitives()
{
	lpD3Ddevice->BeginScene();

	lpD3Ddevice->SetVertexDeclaration(vbDecl);
	lpD3Ddevice->SetStreamSource(0, lpVB, 0, sizeof(Vertex));
	lpD3Ddevice->DrawPrimitive(D3DPT_LINESTRIP, 0, 6);

	lpD3Ddevice->EndScene();
}

bool initVertexDecl()
{
	D3DVERTEXELEMENT9 vbDeclFormat[] = 
	{
		{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		D3DDECL_END()
	};

	if(FAILED(lpD3Ddevice->CreateVertexDeclaration(vbDeclFormat, &vbDecl)))
		return false;
	return true;
}

bool initVertexBuffer()
{
	if(FAILED(lpD3Ddevice->CreateVertexBuffer(6, D3DUSAGE_WRITEONLY, NULL, D3DPOOL_DEFAULT, &lpVB, NULL)))
		return false;

	Vertex* pVBbuffer;
	if(FAILED(lpVB->Lock(0, 0, (void**) &pVBbuffer, 0)))
		return false;

	pVBbuffer[0] = Vertex(0, 5.0f, 0);
	pVBbuffer[1] = Vertex(3.0f, 0, 0);
	pVBbuffer[2] = Vertex(10.0f, 3.0f, 0);
	pVBbuffer[3] = Vertex(7.0f, 3.0f, 0);
	pVBbuffer[4] = Vertex(8.0f, 5.0f, 0);
	pVBbuffer[5] = Vertex(3.0f, 7.0f, 0);

	if(FAILED(lpVB->Unlock()))
		return false;
	return true;
}

Also one more simple question I have while I'm already posting: The output gives me this warning: D3D9 Helper: Enhanced D3DDebugging disabled; Application was not compiled with D3D_DEBUG_INFO Despite the fact that I put, #define D3D_DEBUG_INFO, in my header.h. Thanks, Mikfig

Share this post


Link to post
Share on other sites
Quote:
Original post by mikfig
I am doing a simple DirectX application that draws a line strip. It seems to initialize, set vertex declaration, put data in the vertex buffer, etc just fine. But then I get an error on the drawing, Stream 0 size is too small
Your vertex buffer is only 6 bytes long, and when you lock it you're causing a buffer overflow by writing 6 vertices into it. The first parameter to CreateVertexBuffer() is the length in bytes, not vertices.

Quote:
Original post by mikfig
Also one more simple question I have while I'm already posting:
The output gives me this warning:
D3D9 Helper: Enhanced D3DDebugging disabled; Application was not compiled with D3D_DEBUG_INFO

Despite the fact that I put, #define D3D_DEBUG_INFO, in my header.h.
You need to define it before d3d9.h is included, the best way to do this is to set it in your project settings -> C/C++ -> Preprocessor.

Some other points:
1. You're not using AdjustWindowRect to get the correct window size from the desired client area size, so your window is 800x600, but your client area (Area D3D draws to) will be something like 784x568, which is slightly less performant and can cause rendering artifacts as D3D shrinks the backbuffer to fit the client area.
2. You should check the return value of GetDeviceCaps(), if it fails the caps aren't initialized.
3. D3DDEVCAPS_PUREDEVICE doesn't mean the device can do hardware vertex process,ing you'll want to check if the D3DDEVCAPS_HWTRANSFORMANDLIGHT flag is set before setting D3DCREATE_HARDWARE_VERTEXPROCESSING.
4. Less important for testing, but you should really be checking that the backbuffer format requested is usable with the display mode (CheckDeviceFormat()), and that the backbuffer format is usable with the depthbuffer format (CheckDepthStencilMatch())
5. Your vertex buffer should probably be in D3DPOOL_MANAGED unless it's dynamic, or if it's in the default pool you need to Release() it before Reset()ing the device and then re-create it after.

Share this post


Link to post
Share on other sites
Thanks for the help, I don't have time to try all of them now. But when I changed the first parameter of CreateVertexBuffer to 6 * sizeof(Vertex) it still gives me the same error at:
lpD3Ddevice->DrawPrimitive(D3DPT_LINESTRIP, 0, 6);
and it also still says that:
Application was not compiled with D3D_DEBUG_INFO
even though I put D3D_DEBUG_INFO into the C/C++ -> Preprocessor -> Preprocessor Definitions.

Share this post


Link to post
Share on other sites
From MSDN for IDirect3DDevice9::DrawPrimitive 3rd paramater:

Quote:
Number of primitives to render. The maximum number of primitives allowed is determined by checking the MaxPrimitiveCount member of the D3DCAPS9 structure. PrimitiveCount is the number of primitives as determined by the primitive type. If it is a line list, each primitive has two vertices. If it is a triangle list, each primitive has three vertices.


From this it seems to me that passing in 6 to that call will require 12 vertices in the buffer, so CreateBuffer would need 12 * sizeof(Vertex) passed in.

EDIT: I see now you are using LINESTRIP, so I think it probably needs 7 vertices.

Share this post


Link to post
Share on other sites
Quote:
Original post by mikfig
Thanks for the help, I don't have time to try all of them now. But when I changed the first parameter of CreateVertexBuffer to 6 * sizeof(Vertex) it still gives me the same error at:
lpD3Ddevice->DrawPrimitive(D3DPT_LINESTRIP, 0, 6);
See grekster's reply, the last parameter is the number of primitives, not the number of vertices.

Quote:
Original post by mikfig
and it also still says that:
Application was not compiled with D3D_DEBUG_INFO
even though I put D3D_DEBUG_INFO into the C/C++ -> Preprocessor -> Preprocessor Definitions.
Hmm, have you tried doing a clean build? To be honest, you're not missing much without that flag [smile]

Share this post


Link to post
Share on other sites
Thanks again. I think I'm going to use an index buffer also. I also realized that I forgot all the transformation matrices, the clearing of the backbuffer, and the buffer "flipflopping" with Present.

Share this post


Link to post
Share on other sites
I updated all my code, now it doesn't give the Stream 0 size is too small error. I also organized into more files now. However, it doesn't render anything. I have a feeling it is either camera position, or the fact that I didn't use any colors with my vertices. But I'm not sure.

Here is my updated code:

//header.h
#ifndef __HEADER_INCLUDED__
#define __HEADER_INCLUDED__

#include <windows.h>
#include <d3dx9.h>
#include <d3d9.h>

#include "log.h"

extern LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
extern bool initD3D(HWND hWnd);
extern bool initVertexDecl();
extern bool isDeviceLost();
extern bool initBuffers();
extern bool initTransformations();
extern void drawPrimitives();

struct Vertex
{
Vertex():position(0,0,0){}
Vertex(float x, float y, float z):position(x, y, z){}
D3DXVECTOR3 position;
};

#endif __HEADER_INCLUDED__




//main.cpp
#include "header.h"

bool isWindowOpen;
IDirect3D9* lpD3D;
IDirect3DDevice9* lpD3Ddevice;
D3DPRESENT_PARAMETERS d3dPP;
IDirect3DVertexBuffer9* lpVB;
IDirect3DIndexBuffer9* lpIB;
IDirect3DVertexDeclaration9* vbDecl;
float resWidth;
float resHeight;

extern D3DXMATRIX mWorld;
extern D3DXMATRIX mCam;
extern D3DXMATRIX mProj;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc;
HWND hWnd;
MSG msg;

msg.message = WM_NULL;
isWindowOpen = true;

wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
wc.style = 0;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpszClassName = "Win32 Class";
wc.lpszMenuName = NULL;
wc.lpfnWndProc = WndProc;

if(!RegisterClass(&wc))
{
MessageBox(NULL, "Failed to register wndclass!", "Error", MB_OK);
return 0;
}

RECT winRect;
winRect.right = 1024;
winRect.bottom = 768;
AdjustWindowRect(&winRect, WS_OVERLAPPEDWINDOW, false);
resWidth = winRect.right;
resHeight = winRect.bottom;

hWnd = CreateWindow("Win32 Class", "My Window", WS_OVERLAPPEDWINDOW, 100, 100, winRect.right, winRect.bottom, NULL, NULL, hInstance, NULL);
if(hWnd == NULL)
{
MessageBox(NULL, "Failed to create window!", "Error", MB_OK);
return 0;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

initLogFile();

if(initD3D(hWnd))
{
while(msg.message != WM_QUIT && isWindowOpen)
{
while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(!isDeviceLost())
drawPrimitives();
}
}
else
{
MessageBox(NULL, "Failed to initialize Direct3D", "Error", MB_OK);
return 0;
}

if(vbDecl != NULL)
vbDecl->Release();
if(lpVB != NULL)
lpVB->Release();
if(lpIB != NULL)
lpIB->Release();
if(lpD3Ddevice != NULL)
lpD3Ddevice->Release();
if(lpD3D != NULL)
lpD3D->Release();

return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
isWindowOpen = false;
DestroyWindow(hWnd);
break;
case WM_DESTROY:
isWindowOpen = false;
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}

bool initD3D(HWND hWnd)
{
lpD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(lpD3D == NULL)
{
return false;
}

D3DCAPS9 devCaps;
DWORD behaviorFlags = 0;
ZeroMemory(&d3dPP, sizeof(D3DPRESENT_PARAMETERS));


if(FAILED(lpD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &devCaps)))
return false;

if(devCaps.DevCaps & D3DDEVCAPS_PUREDEVICE)
{
behaviorFlags |= D3DCREATE_PUREDEVICE;
}
if(devCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
behaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
behaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;

if(devCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
d3dPP.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
else
d3dPP.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;

int numDeviceModes = lpD3D->GetAdapterModeCount(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8) - 1;
D3DDISPLAYMODE displayMode;

RECT highestResolution;
highestResolution.right = 0;
highestResolution.bottom = 0;
highestResolution.left = 0;

if(numDeviceModes > 0)
{
writeToLog("Using 32-bit X8R8G8B8 format for backbuffer.");

for(int i = 0; i <= numDeviceModes; i++)
{
if(SUCCEEDED(lpD3D->EnumAdapterModes(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8, i, &displayMode)))
{
writeToLog("\nAdapter Mode "); writeToLog(i); writeToLog(": ");
writeToLog("Res: "); writeToLog((int)displayMode.Width); writeToLog("x"); writeToLog((int)displayMode.Height);
writeToLog(" Refresh Rate: "); writeToLog((int)displayMode.RefreshRate); writeToLog("HZ \n");
if(displayMode.Width > highestResolution.right)
highestResolution.right = displayMode.Width;
if(displayMode.Height > highestResolution.bottom)
highestResolution.bottom = displayMode.Height;
if(displayMode.RefreshRate > highestResolution.left)
highestResolution.left = displayMode.RefreshRate;
}
}

d3dPP.BackBufferFormat = D3DFMT_X8R8G8B8;
}
else
{
int numDeviceModes = lpD3D->GetAdapterModeCount(D3DADAPTER_DEFAULT, D3DFMT_R5G6B5) - 1;

D3DDISPLAYMODE displayMode;
writeToLog("Using 16-bit R5G6B5 format for backbuffer.");

for(int i = 0; i <= numDeviceModes; i++)
{
if(SUCCEEDED(lpD3D->EnumAdapterModes(D3DADAPTER_DEFAULT, D3DFMT_R5G6B5, i, &displayMode)))
{
writeToLog("\nAdapter Mode "); writeToLog(i); writeToLog(": ");
writeToLog("Res: "); writeToLog((int)displayMode.Width); writeToLog("x"); writeToLog((int)displayMode.Height);
writeToLog(" Refresh Rate: "); writeToLog((int)displayMode.RefreshRate); writeToLog("HZ \n");
if(displayMode.Width > highestResolution.right)
highestResolution.right = displayMode.Width;
if(displayMode.Height > highestResolution.bottom)
highestResolution.bottom = displayMode.Height;
if(displayMode.RefreshRate > highestResolution.left)
highestResolution.left = displayMode.RefreshRate;
}
}

d3dPP.BackBufferFormat = D3DFMT_R5G6B5;
}

if(FAILED(lpD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dPP.BackBufferFormat, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, d3dPP.BackBufferFormat)))
{
d3dPP.BackBufferFormat = D3DFMT_UNKNOWN;
d3dPP.EnableAutoDepthStencil = false;
d3dPP.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dPP.MultiSampleQuality = 0;
}
else
{
if(SUCCEEDED(lpD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dPP.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D24S8)))
{
d3dPP.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dPP.EnableAutoDepthStencil = true;
}
else if(SUCCEEDED(lpD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dPP.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D15S1)))
{
d3dPP.AutoDepthStencilFormat = D3DFMT_D15S1;
d3dPP.EnableAutoDepthStencil = true;
}
else if(SUCCEEDED(lpD3D->CheckDeviceFormat(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dPP.BackBufferFormat, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16)))
{
d3dPP.AutoDepthStencilFormat = D3DFMT_D16;
d3dPP.EnableAutoDepthStencil = true;
}
else
d3dPP.EnableAutoDepthStencil = false;

DWORD multiSampleQualLevels;

if(SUCCEEDED(lpD3D->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dPP.BackBufferFormat, true, D3DMULTISAMPLE_2_SAMPLES, &multiSampleQualLevels))
&& SUCCEEDED(lpD3D->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dPP.AutoDepthStencilFormat, true, D3DMULTISAMPLE_2_SAMPLES, &multiSampleQualLevels)))
{
d3dPP.MultiSampleType = D3DMULTISAMPLE_2_SAMPLES;
d3dPP.MultiSampleQuality = multiSampleQualLevels - 1;
}
else
{
d3dPP.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dPP.MultiSampleQuality = 0;
}
}

d3dPP.BackBufferCount = 1;

d3dPP.BackBufferWidth = (UINT) resWidth;
d3dPP.BackBufferHeight = (UINT) resHeight;
d3dPP.FullScreen_RefreshRateInHz = 0;
d3dPP.hDeviceWindow = hWnd;
d3dPP.Windowed = true;
d3dPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dPP.Flags = NULL;

writeToLog("\nChosen Res: "); writeToLog((int)highestResolution.right); writeToLog("x"); writeToLog((int)highestResolution.bottom);

if(FAILED(lpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, behaviorFlags, &d3dPP, &lpD3Ddevice)))
return false;

if(!initVertexDecl())
return false;

if(!initBuffers())
return false;

if(!initTransformations())
return false;

return true;
}

bool isDeviceLost()
{
if(!isWindowOpen)
return true;

HRESULT hr = lpD3Ddevice->TestCooperativeLevel();

if(hr == D3DERR_DEVICELOST)
{
Sleep(20);
return true;
}
else if(hr == D3DERR_DEVICENOTRESET)
{
lpD3Ddevice->Reset(&d3dPP);
lpD3Ddevice->SetTransform(D3DTS_WORLD, &mWorld);
lpD3Ddevice->SetTransform(D3DTS_VIEW, &mCam);
lpD3Ddevice->SetTransform(D3DTS_PROJECTION, &mProj);
return false;
}
else if(hr == D3DERR_DRIVERINTERNALERROR)
{
MessageBox(NULL, "Internal Driver Error.", "Error", MB_OK);
PostQuitMessage(0);
return true;
}

return false;
}




//renderCode.cpp
#include "header.h"

extern bool isWindowOpen;
extern IDirect3D9* lpD3D;
extern IDirect3DDevice9* lpD3Ddevice;
extern D3DPRESENT_PARAMETERS d3dPP;
extern IDirect3DVertexBuffer9* lpVB;
extern IDirect3DIndexBuffer9* lpIB;
extern IDirect3DVertexDeclaration9* vbDecl;
extern float resWidth;
extern float resHeight;

D3DXMATRIX mWorld, mProj, mCam;

void drawPrimitives()
{
lpD3Ddevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, RGB(255.0f,255.0f,255.0f), 1.0f, 0);

lpD3Ddevice->BeginScene();

lpD3Ddevice->SetVertexDeclaration(vbDecl);
lpD3Ddevice->SetStreamSource(0, lpVB, 0, sizeof(Vertex));
lpD3Ddevice->SetIndices(lpIB);
lpD3Ddevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
lpD3Ddevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, 0, 0, 6, 0, 6);

lpD3Ddevice->EndScene();

lpD3Ddevice->Present(NULL, NULL, NULL, NULL);
}

bool initVertexDecl()
{
D3DVERTEXELEMENT9 vbDeclFormat[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
D3DDECL_END()
};

if(FAILED(lpD3Ddevice->CreateVertexDeclaration(vbDeclFormat, &vbDecl)))
return false;
return true;
}

bool initBuffers()
{
if(FAILED(lpD3Ddevice->CreateVertexBuffer(6 * sizeof(Vertex), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &lpVB, NULL)))
return false;

Vertex* pVertexBuffer;
if(FAILED(lpVB->Lock(0, 0, (void**) &pVertexBuffer, 0)))
return false;

pVertexBuffer[0] = Vertex(0, 5.0f, 10.0f);
pVertexBuffer[1] = Vertex(3.0f, 0, 10.0f);
pVertexBuffer[2] = Vertex(10.0f, 3.0f, 10.0f);
pVertexBuffer[3] = Vertex(7.0f, 3.0f, 10.0f);
pVertexBuffer[4] = Vertex(8.0f, 5.0f, 10.0f);
pVertexBuffer[5] = Vertex(3.0f, 7.0f, 10.0f);

if(FAILED(lpVB->Unlock()))
return false;

if(FAILED(lpD3Ddevice->CreateIndexBuffer(12 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &lpIB, NULL)))
return false;

WORD* pIndexBuffer;

if(FAILED(lpIB->Lock(0, 0, (void**) &pIndexBuffer, NULL)))
return false;

pIndexBuffer[0] = 0;
pIndexBuffer[1] = 1;
pIndexBuffer[2] = 1;
pIndexBuffer[3] = 2;
pIndexBuffer[4] = 2;
pIndexBuffer[5] = 3;
pIndexBuffer[6] = 3;
pIndexBuffer[7] = 4;
pIndexBuffer[8] = 4;
pIndexBuffer[9] = 5;
pIndexBuffer[10] = 5;
pIndexBuffer[11] = 6;
pIndexBuffer[12] = 6;

if(FAILED(lpIB->Unlock()))
return false;

return true;
}

bool initTransformations()
{
D3DXMatrixIdentity(&mWorld);
D3DXMatrixLookAtLH(&mCam, &(D3DXVECTOR3(0,0,-5.0f)), &(D3DXVECTOR3(0.0f,0.0f,0.0f)), &(D3DXVECTOR3(0, 1.0f, 0)));
D3DXMatrixPerspectiveFovLH(&mProj, 0.25f/D3DX_PI, resWidth / resHeight, -10.0f, 30.0f);
if(FAILED(lpD3Ddevice->SetTransform(D3DTS_WORLD, &mWorld)))
return false;

if(FAILED(lpD3Ddevice->SetTransform(D3DTS_VIEW, &mCam)))
return false;

if(FAILED(lpD3Ddevice->SetTransform(D3DTS_PROJECTION, &mProj)))
return false;

return true;
}

Share this post


Link to post
Share on other sites
1. You have a buffer overflow here:
pIndexBuffer[12] = 6;
The pIndexBuffer array only has 12 entries in it, indices 0..11.

2. Your FOV is 4.6 degrees? (0.25 / D3DX_PI) The usual FOV is 45 - 90 degrees. You can use D3DXDegToRad(45.0f) instead.

3. I don't think it's valid to have a negative near clip plane (It's certainly very unusual), try putting 1.0f for the near clip plane (It must be non-zero, and preferably as high as possible).

Share this post


Link to post
Share on other sites
Thanks a lot :D. It is rendering something, however I notice that the last two vertices, 4 and 5, are not drawn and I have changed their coordinates around towards the other vertices without noticing any change in the rendering.

Here is my updated code(just the render stuff):

#include "header.h"

extern bool isWindowOpen;
extern IDirect3D9* lpD3D;
extern IDirect3DDevice9* lpD3Ddevice;
extern D3DPRESENT_PARAMETERS d3dPP;
extern IDirect3DVertexBuffer9* lpVB;
extern IDirect3DIndexBuffer9* lpIB;
extern IDirect3DVertexDeclaration9* vbDecl;
extern float resWidth;
extern float resHeight;

D3DXMATRIX mWorld, mProj, mCam;

void drawPrimitives()
{

//I moved the Begin/EndScene, Clear, and Present
//function calls to main.cpp

lpD3Ddevice->SetVertexDeclaration(vbDecl);
lpD3Ddevice->SetStreamSource(0, lpVB, 0, sizeof(Vertex));
lpD3Ddevice->SetIndices(lpIB);
lpD3Ddevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, 0, 0, 6, 0, 6);
}

bool initVertexDecl()
{
D3DVERTEXELEMENT9 vbDeclFormat[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
D3DDECL_END()
};

if(FAILED(lpD3Ddevice->CreateVertexDeclaration(vbDeclFormat, &vbDecl)))
return false;
return true;
}

bool initBuffers()
{
if(FAILED(lpD3Ddevice->CreateVertexBuffer(6 * sizeof(Vertex), D3DUSAGE_WRITEONLY, NULL, D3DPOOL_MANAGED, &lpVB, NULL)))
return false;

Vertex* pVertexBuffer;
if(FAILED(lpVB->Lock(0, 0, (void**) &pVertexBuffer, 0)))
return false;

pVertexBuffer[0] = Vertex(0, 5.0f, 10.0f);
pVertexBuffer[1] = Vertex(3.0f, 0, 10.0f);
pVertexBuffer[2] = Vertex(10.0f, 3.0f, 10.0f);
pVertexBuffer[3] = Vertex(7.0f, 3.0f, 10.0f);
pVertexBuffer[4] = Vertex(8.0f, 5.0f, 10.0f);
pVertexBuffer[5] = Vertex(3.0f, 7.0f, 10.0f);

if(FAILED(lpVB->Unlock()))
return false;

if(FAILED(lpD3Ddevice->CreateIndexBuffer(12 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &lpIB, NULL)))
return false;

WORD* pIndexBuffer;

if(FAILED(lpIB->Lock(0, 0, (void**) &pIndexBuffer, NULL)))
return false;

pIndexBuffer[0] = 0;
pIndexBuffer[1] = 1;
pIndexBuffer[2] = 1;
pIndexBuffer[3] = 2;
pIndexBuffer[4] = 2;
pIndexBuffer[5] = 3;
pIndexBuffer[6] = 3;
pIndexBuffer[7] = 4;
pIndexBuffer[8] = 4;
pIndexBuffer[9] = 5;
pIndexBuffer[10] = 5;
pIndexBuffer[11] = 0;

if(FAILED(lpIB->Unlock()))
return false;

return true;
}

bool initTransformations()
{
D3DXMatrixIdentity(&mWorld);
D3DXMatrixLookAtLH(&mCam, &(D3DXVECTOR3(0,0,-5.0f)), &(D3DXVECTOR3(1.5f,0.9f,0.0f)), &(D3DXVECTOR3(0, 1.0f, 0)));
D3DXMatrixPerspectiveFovLH(&mProj, 0.3f*D3DX_PI, resWidth / resHeight, 1.0f, 30.0f);
if(FAILED(lpD3Ddevice->SetTransform(D3DTS_WORLD, &mWorld)))
return false;

if(FAILED(lpD3Ddevice->SetTransform(D3DTS_VIEW, &mCam)))
return false;

if(FAILED(lpD3Ddevice->SetTransform(D3DTS_PROJECTION, &mProj)))
return false;

lpD3Ddevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

return true;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by mikfig
Thanks a lot :D. It is rendering something, however I notice that the last two vertices, 4 and 5, are not drawn and I have changed their coordinates around towards the other vertices without noticing any change in the rendering.

Here is my updated code(just the render stuff):
*** Source Snippet Removed ***
You're rendering a line strip, but you're duplicating vertices. A line strip is a line that just goes from the first vertex to the last. Your index setup should probably be:

pIndexBuffer[0] = 0;
pIndexBuffer[1] = 1;
pIndexBuffer[2] = 2;
pIndexBuffer[3] = 3;
pIndexBuffer[4] = 4;
pIndexBuffer[5] = 5;


If you want a line going through all your vertices. Otherwise, use D3DPT_LINELIST instead of D3DPT_LINESTRIP.

Share this post


Link to post
Share on other sites
Thanks, it works perfectly now. I thought the documentation said that each primitive was made up of 2 vertices so I duplicated each one, but I guess that is for a line list.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this