Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

PumpkinPieman

Problem rendering a polygon

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

This code seemed to work before, now I can't seem to render the polygon properly. Anyone know what might be wrong?
#include <windows.h>
#include <D3DX8.h>

#pragma comment(lib,"d3d8.lib")
#pragma comment(lib,"d3dx8.lib")

// ERROR DECLARATIONS

void FatalError(const char *error_msg);
void FatalError(HRESULT hr,const char *error_msg);

// WINDOW VARIABLES & DECLARATIONS

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE prevhInst, LPSTR p_cmd_line,int p_show);
HWND WndMain;
HINSTANCE g_inst = NULL;
const int g_width = 640;
const int g_height = 480;
const char AppName[] = "AppName";
bool g_fullscreen = false;

void init_window(void);
void kill_window(void);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wparam, LPARAM lparam);
#define MB(s) MessageBox(hWnd, s, "Info", MB_OK); OutputDebugString( s ); OutputDebugString( "\n" );

// DIRECT 3D VARIABLES & DECLARATIONS


LPDIRECT3D8 D3DInterface = NULL;
IDirect3DDevice8* D3DDevice = NULL;

void init_d3d(void);
void kill_d3d(void);
D3DFORMAT find_16bit_mode(void);
void render(void);

// DIRECT 3D SCENE VARIABLES & DECLARATIONS


void init_scene(void);
void kill_scene(void);
/*
struct vertex{
    FLOAT x, y, z;
    FLOAT tu, tv;
};
#define D3D8T_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)
*/
struct vertex{
    FLOAT x, y, z; // The transformed position for the vertex.

    DWORD color;        // The vertex color.

};
#define D3D8T_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)


struct hexagon{
	FLOAT x, y, z;
};

IDirect3DVertexBuffer8 *D3DVertexBuffer=NULL;

// MAIN APPLICATION START

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE prevhInst, LPSTR p_cmd_line,int p_show)
{
	g_inst = hInst;

	init_window();
	init_d3d();
	init_scene();

	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));
	while(msg.message != WM_QUIT)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			render(); // Do rendering here

			
		}
	}

	kill_scene();
	kill_d3d();
	kill_window();

	return 0;
}

void FatalError(const char *error_msg){

   kill_scene();

   kill_d3d();

   kill_window();

   //Write our error message out to the debugger (if it's active)

	OutputDebugString( error_msg );
   OutputDebugString("\n");
	MessageBox(NULL, error_msg,AppName, MB_OK );

   exit(5);

}

void FatalError(HRESULT hr,const char *error_msg)
{
char buffer[255];

   
   D3DXGetErrorStringA(hr,buffer,250);

   strcat(buffer,"\n");
   strcat(buffer,error_msg);

   kill_scene();

   kill_d3d();

   kill_window();

   //Write our error message out to the debugger (if it's active)

	OutputDebugString( buffer );
   OutputDebugString("\n");
	MessageBox(NULL, buffer,AppName, MB_OK );

   exit(5);

}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wparam, LPARAM lparam)
{
	switch(uMsg){
		case WM_KEYDOWN:  // A key has been pressed, end the app

			return 0;
		case WM_DESTROY:  //This window is being destroyed, tell Windows we're quitting

			MB("Window Destroyed");
			PostQuitMessage(0);
			return 0;
		default: return (DefWindowProc(hWnd,uMsg,wparam,lparam));
	}

   
}

void init_window(void)
{
	//Register Window Class

	WNDCLASSEX wcex = {sizeof(WNDCLASSEX), CS_CLASSDC,
		WindowProc, 0L, 0L, g_inst,
		NULL, NULL, NULL, NULL,
		"Stawski", NULL};
	if(!RegisterClassEx(&wcex))
		FatalError("Initialization of window class failed");

	// Register Window

	WndMain = CreateWindow("Stawski", AppName,
		WS_OVERLAPPEDWINDOW, 0, 0, g_width, g_height,
		NULL, NULL, g_inst, NULL);
	if(WndMain == NULL)
		FatalError("Initialization of window registration failed");

	ShowWindow(WndMain, SW_SHOWNORMAL);
	UpdateWindow(WndMain);
}
void kill_window(void)
{
	UnregisterClass("Stawski", g_inst);
}
void init_d3d(void){
HRESULT hr;
D3DPRESENT_PARAMETERS d3dpp;
D3DDISPLAYMODE display_mode;

	D3DInterface = Direct3DCreate8( D3D_SDK_VERSION );
	if(!D3DInterface ){
		FatalError("Error getting Direct3D");
	}

	hr=D3DInterface->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&display_mode);
	if(FAILED(hr)){
		FatalError(hr,"Error getting display mode\n");
	}

	ZeroMemory(&d3dpp,sizeof(d3dpp));

	d3dpp.SwapEffect     = D3DSWAPEFFECT_DISCARD;
	d3dpp.hDeviceWindow  = WndMain;
	d3dpp.BackBufferCount= 1;

	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

	if(g_fullscreen){
		d3dpp.Windowed          = FALSE;
		d3dpp.BackBufferWidth   = g_width;
		d3dpp.BackBufferHeight  = g_height;
		d3dpp.BackBufferFormat  = find_16bit_mode();
	}else{
		d3dpp.Windowed          = TRUE;
		d3dpp.BackBufferFormat  = display_mode.Format;
	}

	hr=D3DInterface->CreateDevice(D3DADAPTER_DEFAULT,
							D3DDEVTYPE_HAL,
							WndMain,
							D3DCREATE_SOFTWARE_VERTEXPROCESSING,
							&d3dpp,
							&D3DDevice);
	if(FAILED(hr)){
		FatalError(hr,"Error creating device\n");
	}
	
	D3DDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
}

void kill_d3d(void){

   if(D3DDevice){
      D3DDevice->Release();
      D3DDevice=NULL;
   }

   if(D3DInterface){
      D3DInterface->Release();
      D3DInterface=NULL;
   }

}

D3DFORMAT find_16bit_mode(void){
HRESULT hr;

   hr=D3DInterface->CheckDeviceType(D3DADAPTER_DEFAULT,
                             D3DDEVTYPE_HAL,
                             D3DFMT_R5G6B5,
                             D3DFMT_R5G6B5,
                             FALSE);
   if(SUCCEEDED(hr)){
      return D3DFMT_R5G6B5;
   }

   hr=D3DInterface->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,D3DFMT_X1R5G5B5,D3DFMT_X1R5G5B5,FALSE);
   if(SUCCEEDED(hr)){
      return D3DFMT_X1R5G5B5;
   }

   FatalError("Couldn't find a decent mode\n");

   return (D3DFORMAT)NULL;
}

void init_scene(void)
{
HRESULT hr;
unsigned char *vb_vertices;
D3DXMATRIX view_matrix;
D3DXMATRIX matProj;

vertex D3D_vertices[] ={
   {  0.0f,  1.0f, 0.0f, 0xFFFFFFFF }, // x, y, z, color

   {  1.0f, -1.0f, 0.0f, 0xFFFFFFFF },
   { -1.0f, -1.0f, 0.0f, 0xFFFFFFFF }

};
   D3DDevice->SetRenderState(D3DRS_LIGHTING,FALSE);

   hr=D3DDevice->CreateVertexBuffer(sizeof(D3D_vertices),
                                       D3DUSAGE_WRITEONLY,
                                       D3D8T_CUSTOMVERTEX,
                                       D3DPOOL_MANAGED,
                                       &D3DVertexBuffer);
   if(FAILED(hr)){
      FatalError("Error creating triangle vertex buffer");
   }

   hr=D3DVertexBuffer->Lock(0,
                       0,
                       &vb_vertices,
                       0);
   if(FAILED(hr)){
      FatalError("Error Locking triangle buffer");
   }
   
   //vb_vertices now points to our vertices inside the Vertex buffer, so

   //to fill in our VB, we copy to vb_vertices.

   memcpy(vb_vertices, D3D_vertices, sizeof(D3D_vertices) );

   //Unlock so Direct3D knows we're done and can do any behind-the-scenes magic required

   D3DVertexBuffer->Unlock();

   D3DXMatrixLookAtLH(&view_matrix,&D3DXVECTOR3( 0.0f, 0.0f,-8.0f ),
                                   &D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
                                   &D3DXVECTOR3( 0.0f, 1.0f, 0.0f ));

   D3DDevice->SetTransform(D3DTS_VIEW,&view_matrix);


   D3DXMatrixPerspectiveFovLH(&matProj, //Result Matrix

                              D3DX_PI/4,//Field of View, in radians. (PI/4) is typical

                              ((float)g_width / (float)g_height),     //Aspect ratio

                              1.0f,     //Near view plane

                              100.0f ); // Far view plane


   D3DDevice->SetTransform( D3DTS_PROJECTION, &matProj );

}
void kill_scene(void)
{
   if(D3DVertexBuffer){
      D3DVertexBuffer->Release();
      D3DVertexBuffer=NULL;
   }

}
void render(void)
{
	D3DXMATRIX matWorld;

	//D3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER , D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );

	D3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0 );
	if(SUCCEEDED(D3DDevice->BeginScene()))
	{
		D3DDevice->SetVertexShader(D3D8T_CUSTOMVERTEX);
		D3DDevice->SetStreamSource(0, D3DVertexBuffer, sizeof(vertex));


		D3DDevice->SetTransform(D3DTS_WORLD, &matWorld);

		D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);

		D3DDevice->EndScene();
	}
	D3DDevice->Present(NULL, NULL, NULL, NULL);
}

Pumpkin Pieman - [Blogger]

Go to war again, blood is freedom's stain, But don't you pray for my soul anymore. EDIT* Nevermind I figured it out. [edited by - PumpkinPieman on April 3, 2004 9:22:11 AM]

Share this post


Link to post
Share on other sites
Advertisement

  • 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!