Jump to content
  • Advertisement
Sign in to follow this  
Heurazio

my first try to create a pyramid

This topic is 3758 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

hi, today i want to programm my first 3d object but i've a few problems first of all the code


//needed header files
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>

// define screen resolution and key makros
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 1024
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// get lib

#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

//globals
LPDIRECT3D9 d3d;	// pointer to interface
LPDIRECT3DDEVICE9 d3ddev; // pointer to device class
LPDIRECT3DVERTEXBUFFER9 t_buffer = NULL;	// pointer to vertex buffer

//
int red, green, blue=255;
int *pColorR = &red, *pColorG = &green, *pColorB = &blue;

//functions prototyps

void initD3D(HWND hWnd);	// setup D3x
void cleanD3D(void);
void initGraphics(void);
void renderFrame(void);		// render single frame
void shuffleColors(void);	//shuffle colors

struct CUSTOMVERTEX {FLOAT X, Y, Z; DWORD COLOR;};
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)

LRESULT CALLBACK WindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

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

	ZeroMemory(&wc, sizeof(WNDCLASS));

	wc.cbSize = sizeof(WNDCLASS);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC) WindowProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.lpszClassName = L"WindowClass";

	RegisterClassEx(&wc);

	hWnd = CreateWindowEx(	NULL, L"WindowClass", L"Our Direct3D Programm",
							WS_EX_TOPMOST | WS_POPUP, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
							NULL,NULL, hInstance, NULL);

	initD3D(hWnd);

	MSG msg;

	while(TRUE)
	{
		DWORD starting_point = GetTickCount();

		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT) break;

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		renderFrame();

		if(KEY_DOWN(VK_ESCAPE)) PostMessage(hWnd, WM_DESTROY ,0 ,0);

		while((GetTickCount() - starting_point) < 25);
	}

	cleanD3D();

	return msg.wParam;
}

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);
}

void shuffleColors(void)
{
	//shuffle colors by rotating the values

	if(*pColorB == 255 && *pColorG != 255 && *pColorR == 0)
	{
		*pColorG += 1;
	};
	if(*pColorG == 255 && *pColorB != 0  && *pColorR == 0)
	{
		*pColorB -= 1;
	};
	if(*pColorG == 255 && *pColorR != 255  && *pColorB == 0)
	{
		*pColorR += 1;
	};
	if(*pColorR == 255 && *pColorG != 0 && *pColorB == 0)
	{
		*pColorG -= 1;
	};
	if(*pColorR == 255 && *pColorB != 255 && *pColorG == 0)
	{
		*pColorB += 1;
	};
	if(*pColorB == 255 && *pColorR != 0 && *pColorG == 0)
	{
		*pColorR -= 1;
	};

	return;
};

void initD3D(HWND hWnd)
{
	d3d = Direct3DCreate9(D3D_SDK_VERSION);

	D3DPRESENT_PARAMETERS d3dpp;

	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = FALSE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.hDeviceWindow = hWnd;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferHeight = SCREEN_HEIGHT;
	d3dpp.BackBufferWidth = SCREEN_WIDTH;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

	d3d -> CreateDevice (	D3DADAPTER_DEFAULT,
							D3DDEVTYPE_HAL,
							hWnd,
							D3DCREATE_SOFTWARE_VERTEXPROCESSING,
							&d3dpp,
							&d3ddev);

	initGraphics();

	d3ddev -> SetRenderState (	D3DRS_LIGHTING, FALSE);// turn of 3d light
	d3ddev -> SetRenderState (	D3DRS_ZENABLE, TRUE); // turn on z buffer

	return;
}

void renderFrame(void)
{
	d3ddev -> Clear (	0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB (0, 0, 0), 1.0f, 0);
	d3ddev -> Clear (	0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB (0, 0, 0), 1.0f, 0);

	d3ddev -> BeginScene();

	d3ddev -> SetFVF (CUSTOMFVF);

	D3DXMATRIX matView;	//view matrix
	D3DXMatrixLookAtLH(	&matView,
						&D3DXVECTOR3	(0.0f, 8.0f, 25.0f),	//cam position
						&D3DXVECTOR3	(0.0f, 0.0f, 0.0f),		//look at
						&D3DXVECTOR3	(0.0f, 1.0f, 0.0f));		//upset of camaera
	d3ddev -> SetTransform(D3DTS_VIEW, &matView);	// set transformation to matrix

	D3DXMATRIX matProjection;
	D3DXMatrixPerspectiveFovLH (	&matProjection,
									D3DXToRadian(45),
									(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT,
									1.0f,
									100.0f);
	d3ddev -> SetTransform(	D3DTS_PROJECTION, &matProjection);

	static float index = 0.0f; index += 0.03f;

	D3DXMATRIX matRotationY;
	D3DXMatrixRotationY ( &matRotationY, index);
	d3ddev -> SetTransform ( D3DTS_WORLD, &(matRotationY));

	d3ddev -> SetStreamSource ( 0, t_buffer, 0, sizeof(CUSTOMVERTEX));

	//draw primitives

	d3ddev -> DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
	d3ddev -> DrawPrimitive(D3DPT_TRIANGLELIST, 3, 1);
	d3ddev -> DrawPrimitive(D3DPT_TRIANGLELIST, 6, 1);
	d3ddev -> DrawPrimitive(D3DPT_TRIANGLELIST, 9, 1);

	//end d3d
	d3ddev -> Present(NULL,NULL,NULL,NULL);
	
	return;
}

void cleanD3D(void)
{
	t_buffer -> Release();
	d3ddev -> Release();
	d3d -> Release();

	return;
}

void initGraphics(void)
{
	shuffleColors();

	//create vertics

	struct CUSTOMVERTEX t_vert[] =
	{//		X		Y		Z
		//base
		{	0.0f,	-10.0f,	10.0f,	D3DCOLOR_XRGB(*pColorR, *pColorG, *pColorB),},
		{	10.0f,	-10.0f,	-10.0f, D3DCOLOR_XRGB(*pColorR, *pColorG, *pColorB),},
		{	-10.0f,	-10.0f,	-10.0f, D3DCOLOR_XRGB(*pColorR, *pColorG, *pColorB),},

		//triangle 1
		{	0.0f,	10.0f,	0.0f,	D3DCOLOR_XRGB(*pColorR, *pColorG, *pColorB),},
		{	10.0f,	-10.0f,	-10.0f,	D3DCOLOR_XRGB(*pColorR, *pColorG, *pColorB),},
		{	-10.0f, -10.0f, -10.0f, D3DCOLOR_XRGB(*pColorR, *pColorG, *pColorB),},

		//triangle 2
		{	0.0f,	10.0f,	0.0f,	D3DCOLOR_XRGB(*pColorR, *pColorG, *pColorB),},	
		{	0.0f,	-10.0f, 10.0f,	D3DCOLOR_XRGB(*pColorR, *pColorG, *pColorB),},
		{	10.0f,	-10.0f,	-10.0f, D3DCOLOR_XRGB(*pColorR, *pColorG, *pColorB),},

		//triangle 3
		{	0.0f,	10.0f,	0.0f,	D3DCOLOR_XRGB(*pColorR, *pColorG, *pColorB),},
		{	0.0f,	-10.0f,	10.0f,	D3DCOLOR_XRGB(*pColorR, *pColorG, *pColorB),},
		{	-10.0f,	-10.0f,	-10.0f,	D3DCOLOR_XRGB(*pColorR, *pColorG, *pColorB),},

	};

	d3ddev -> CreateVertexBuffer (	12*sizeof(CUSTOMVERTEX),
									0,
									CUSTOMFVF,
									D3DPOOL_MANAGED,
									&t_buffer,
									NULL);
	VOID *pVoid;

	t_buffer -> Lock ( 0, 0, (void**)&pVoid, 0);
	memcpy ( pVoid, t_vert, sizeof(t_vert));
	t_buffer -> Unlock();

	return;
}





i'm realy sorry if it isent in a box or else but i dont know the bbc code ... so back to basic, the problem is ... yeah sry coudn't descripe have no error message or so else ... only the fucking debugger whos crabing on this
	d3ddev -> CreateVertexBuffer (	12*sizeof(CUSTOMVERTEX),
									0,
									CUSTOMFVF,
									D3DPOOL_MANAGED,
									&t_buffer,
									NULL);


dose somone of you know why ? thanks [Edited by - Heurazio on June 12, 2008 4:22:35 AM]

Share this post


Link to post
Share on other sites
Advertisement
i hope so ... :-D sry dont know realy...

but could this be helpfull for you ?

german:

Unbehandelte Ausnahme bei 0x00ab24c7 in d3dobjects.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x00000000.

hope corect english:

untreaded exeption at 0x00ab24c7 in d3dobjects.exe: 0xC0000005:
access violation when reading at position 0x00000000.

regards

Share this post


Link to post
Share on other sites
Looks like you are trying to access in a void memory area...Direct3D9 does not gives you any message? Have you checked all pointers?

Share this post


Link to post
Share on other sites
It shouldn't be NULL, when the debugger stop with that access violation message, just hover your cursor over d3ddev and see what value it have, if it's 0, check what's going on before and after the CreateDevice call, and if it return any error message with an HRESULT variable.

Share this post


Link to post
Share on other sites
so ok, i've checkt it, now its working but i have no idea why ... i've only ritten a cuple of code new

ok its working but coudn't see anything of the triangle

regards

Share this post


Link to post
Share on other sites
I can't help with the problem, but the tag you're looking for is the source tag. It's like so: [ source]my codes[/source ] but without the spaces before the ']'s. And it's technically not BBC code. Gamedev.net uses a homebrew thing which is mostly HTML (links, bullets, etc.), but has some additional stuff in square brackets (smileys, source and code tags).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!