Help! App wont draw triangle.

Started by
5 comments, last by CPPNick 14 years, 7 months ago
Hello! This is my first Direct3D app. I followed tutorial#2 from MSDN about drawing primitives, and rewrote it so I could learn. I have what I think should be the complete code, and it does clear the screen to blue, but it doesn't draw the triangle. can anyone point out the error? because I tried following MS's tutor#2 example line by line, and couldn't find what the problem is =(

#include "MAIN.H"
#include <d3d10.h>
#include <d3dx10.h>
const char *ClsName = "DXGAME";
const char *WndName = "3D Room";
struct DXVERTEX
{
    D3DXVECTOR3 pos;
	D3DXVECTOR4 color;
	DXVERTEX(){}
	DXVERTEX(D3DXVECTOR3 p, D3DXVECTOR4 c)
	{
		pos = p;
		color = c;
	}
};
ID3D10Device* dxdev = NULL;
IDXGISwapChain* dxsc = NULL;
ID3D10RenderTargetView* dxrtv = NULL;
D3DXMATRIX viewMatrix;
D3DXMATRIX projectionMatrix;
ID3D10Effect* dxfx;
ID3D10EffectTechnique* dxfxtechnique;
ID3D10EffectMatrixVariable *dxmtxview, *dxmtxproject, *dxmtxworld;
ID3D10InputLayout* dxvertexlayout;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MSG Msg;
	HWND hWnd;
	WNDCLASSEX WndClsEx;
	HCURSOR null_cursor;
	HANDLE render_thread;
	DWORD ThreadID;

	//Create thread for rendering
	//render_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)RenderProc, NULL, CREATE_SUSPENDED, &ThreadID);

	//Create invisible cursor
	BYTE andmask[] = {0xFF};
	BYTE xormask[] = {0x00};
	null_cursor = CreateCursor(hInstance, 0, 0, 1, 1, andmask, xormask);
	
	//Register class
	WndClsEx.cbSize        = sizeof(WNDCLASSEX);
	WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
	WndClsEx.lpfnWndProc   = WndProcedure;
	WndClsEx.cbClsExtra    = 0;
	WndClsEx.cbWndExtra    = 0;
	WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	WndClsEx.hCursor       = null_cursor;
	WndClsEx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	WndClsEx.lpszMenuName  = NULL;
	WndClsEx.lpszClassName = ClsName;
	WndClsEx.hInstance     = hInstance;
	WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	RegisterClassEx(&WndClsEx);

	//Create window
	RECT screenrc;
	GetWindowRect(GetDesktopWindow(), &screenrc);

	hWnd = CreateWindowEx(0, ClsName, // 0 = WS_EX_TOPMOST
			  WndName,
			  (WS_VISIBLE | WS_POPUP),// | WS_DLGFRAME),
			  screenrc.right / 2 - 640 / 2,
			  screenrc.bottom / 2 - 480 / 2,
			  640,
			  480,
			  NULL,
			  NULL,
			  hInstance,
			  NULL);

	if(!hWnd)
		return 0;
	
	//
	//set up d3d devices
	
	DXGI_SWAP_CHAIN_DESC dxscd;
	ZeroMemory(&dxscd, sizeof(dxscd));

	dxscd.BufferCount = 1;
	dxscd.BufferDesc.Width = 640;
	dxscd.BufferDesc.Height = 480;
	dxscd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	dxscd.BufferDesc.RefreshRate.Numerator = 60;
	dxscd.BufferDesc.RefreshRate.Denominator = 1;
	dxscd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	dxscd.OutputWindow = hWnd;
	dxscd.SampleDesc.Count = 1;
	dxscd.SampleDesc.Quality = 0;
	dxscd.Windowed = true;
	
	if( FAILED( D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_REFERENCE, NULL, 0,
											D3D10_SDK_VERSION, &dxscd, &dxsc, &dxdev ) ) )
        return false;

	ID3D10Texture2D *dxbb;
	if(FAILED(dxsc->GetBuffer(0, __uuidof( ID3D10Texture2D ), (LPVOID*)&dxbb )))
		return false;
	if(FAILED(dxdev->CreateRenderTargetView(dxbb, NULL, &dxrtv)))
		return false;
	
	dxdev->OMSetRenderTargets(1, &dxrtv, NULL);
	
	D3D10_VIEWPORT dxvp;
	dxvp.Width = 640;
    dxvp.Height = 480;
    dxvp.MinDepth = 0.0f;
    dxvp.MaxDepth = 1.0f;
    dxvp.TopLeftX = 0;
    dxvp.TopLeftY = 0;
    dxdev->RSSetViewports( 1, &dxvp );

	if ( FAILED( D3DX10CreateEffectFromFile("shader.fx", NULL, NULL, "fx_4_0",
											D3D10_SHADER_ENABLE_STRICTNESS,
											0, dxdev, NULL, NULL, &dxfx, NULL, NULL)))
		return false;

	dxfxtechnique = dxfx->GetTechniqueByName("Render");

	D3D10_INPUT_ELEMENT_DESC layout[2];

	layout[0].SemanticName = "POSITION";
	layout[0].SemanticIndex = 0;
	layout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
	layout[0].InputSlot = 0;
	layout[0].AlignedByteOffset = 0;
	layout[0].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA;
	layout[0].InstanceDataStepRate = 0;

	layout[1].SemanticName = "COLOR";
	layout[1].SemanticIndex = 0;
	layout[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
	layout[1].InputSlot = 0;
	layout[1].AlignedByteOffset = 12;
	layout[1].InputSlotClass = D3D10_INPUT_PER_VERTEX_DATA;
	layout[1].InstanceDataStepRate = 0;

	D3D10_PASS_DESC dxpassdesc;

	dxfxtechnique->GetPassByIndex(0)->GetDesc(&dxpassdesc);

	if(FAILED(dxdev->CreateInputLayout(layout, 2, dxpassdesc.pIAInputSignature, dxpassdesc.IAInputSignatureSize, &dxvertexlayout)))
		return false;

	dxdev->IASetInputLayout(dxvertexlayout);
	
	DXVERTEX vertices[3];
	
	vertices[0] = DXVERTEX(D3DXVECTOR3(0.0f, 0.5f, 0.5f),D3DXVECTOR4(1, 0, 0, 1));
	vertices[1] = DXVERTEX(D3DXVECTOR3(0.5f, -0.5f, 0.5f),D3DXVECTOR4(0, 1, 0, 1));
	vertices[2] = DXVERTEX(D3DXVECTOR3(-0.5f, -0.5f, 0.5f),D3DXVECTOR4(0, 0, 1, 1));

	D3D10_BUFFER_DESC dxvbuffdesc;
	ID3D10Buffer *dxvbuffer;

	dxvbuffdesc.Usage = D3D10_USAGE_DEFAULT;
	dxvbuffdesc.ByteWidth = sizeof(DXVERTEX) * 3;
	dxvbuffdesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
	dxvbuffdesc.CPUAccessFlags = 0;
	dxvbuffdesc.MiscFlags = 0;

	D3D10_SUBRESOURCE_DATA dxinitdata;
    dxinitdata.pSysMem = vertices;

	if (FAILED(dxdev->CreateBuffer(&dxvbuffdesc, &dxinitdata, &dxvbuffer)))
		return false;

	UINT stride = sizeof(DXVERTEX);
	UINT offset = 0;

	dxdev->IASetVertexBuffers(0, 1, &dxvbuffer, &stride, &offset);

	//D3D10_RASTERIZER_DESC dxrasterdesc;

	//dxrasterdesc.CullMode = D3D10_CULL_NONE;
	//dxrasterdesc.FillMode = D3D10_FILL_SOLID;
	//dxrasterdesc.FrontCounterClockwise = true;
	//dxrasterdesc.DepthBias = false;
	//dxrasterdesc.DepthBiasClamp = 0;
	//dxrasterdesc.SlopeScaledDepthBias = 0;
	//dxrasterdesc.DepthClipEnable = true;
	//dxrasterdesc.ScissorEnable = false;
	//dxrasterdesc.MultisampleEnable = false;
	//dxrasterdesc.AntialiasedLineEnable = true;

	//ID3D10RasterizerState *dxrasterstate;

	//dxdev->CreateRasterizerState(&dxrasterdesc, &dxrasterstate);
	//dxdev->RSSetState(dxrasterstate);

	dxdev->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	ShowWindow(hWnd, SW_SHOWNORMAL);
	UpdateWindow(hWnd);
	
	//ResumeThread(render_thread);
	do
	{
		if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
		else
		{
			if(GetFocus() != hWnd) continue;
			float ClearColor[4] = { 0.0f, 0.4f, 0.0f, 1.0f };
			dxdev->ClearRenderTargetView(dxrtv, ClearColor);
			
			D3D10_TECHNIQUE_DESC dxtechdesc;
			dxfxtechnique->GetDesc(&dxtechdesc);
			for(UINT p = 0; p < dxtechdesc.Passes; ++p)
			{
				dxfxtechnique->GetPassByIndex(p)->Apply(0);
				dxdev->Draw(3, 0);
			}
			dxsc->Present(0, 0);
		}
	}while(Msg.message != WM_QUIT);	
	return Msg.wParam;
}

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
	switch(Msg)
	{
		case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            EndPaint(hWnd, &ps);
            break;
		case WM_KEYDOWN:
			if(wParam == VK_ESCAPE)	PostQuitMessage(WM_QUIT);
			break;
		default:
			return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    return 0;
}
//DWORD WINAPI RenderProc(LPVOID param)
//{
//	return 0;
//}

thanks for any help!
Advertisement
Have you turned on Direct3D Debugging from the Direct3D control panel? That usually gives you hints at what has gone wrong if there is any misuse of the API.
For D3D10 you want to pass the D3D10_CREATE_DEVICE_DEBUG to D3D10CreateDeviceAndSwapChain() in the flags to enable the debug runtimes.

http://msdn.microsoft.com/en-us/library/ee416707%28VS.85%29.aspx

http://msdn.microsoft.com/en-us/library/ee415373%28VS.85%29.aspx
Quote:Original post by CPPNick
This is my first Direct3D app.


can't someone tell just by glancing over my code what the prob is?

I tried the debug flag, but where does the output go? I see no errors anywhere.

Guess your all just a bunch of copy pasters eh :p
By just glancing at what you said if the screen is blue but no triangle.. It means your backbuffer is presenting, but i would guess something in HWND or viewport may not be drawing it where you can see it.

But i too am also only up to Chapter 2, So don't trust my opinion.
Tis true what you suggest...
before I started directx, I actually made a full on software renderer with dynamic lighting and everything. I had a gun up front like an FPS game, and you could walk around a room with several objects, including a mesh of a head. It was pretty quick by the time I finally threw in the towel. Annnnnywho, I agree you may be right, but after experiencing the entire development of my own little software rendering engine, Direct3D still looks like a bunch of glyphs to me..

Thx for input!
OK. it was the fx file......Guess Im the copy Paster =(
lol...I was looking at two different tutorials, and the second one seemed to be a copy of MSDN's with a better explanation, but the guy must have changed something...
I used MS's fx file, and now its ok. Tomorrow......Transformations!!

This topic is closed to new replies.

Advertisement