Sign in to follow this  

Why don't the walls appear correctly?

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

Hiya guys, Well, today for the first time I attempted to make a level and obviously there is a issue... For some reason one of the squares comes out VERY incorrectly and I don't know why. Please focus on the function DrawMap(). Its not complete so don't say anything about that :) --->
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#define ScreenW 640
#define ScreenH 480
#define CustomFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)

const char *ClsName = "BasicApp";
const char *WndName = "DirectX";

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
VOID InitD3D();
VOID InitGraphics();
VOID InitLights();
VOID Display();
VOID DrawMap();
VOID CleanProgram();

struct CustomV
{

 FLOAT x, y, z;
 D3DVECTOR normals;
 FLOAT u, v;

};

LPDIRECT3D9 D3D;
LPDIRECT3DDEVICE9 D3DDev;
LPDIRECT3DVERTEXBUFFER9 WallB;
LPDIRECT3DTEXTURE9 BrickWallT;
D3DPRESENT_PARAMETERS D3DPP;
D3DXMATRIX ViewM, ProjectionM;
FLOAT SideWays = 0.0f, UpDown = 0.0f;
HINSTANCE hInstance;
HWND hWnd;
DWORD StartingCount;
VOID* VertMemLoc;
CustomV Wall[] =
{

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

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

};
BYTE Map[] = {2, 1, 1, 1, 2,   //0 = Nothing
              2, 0, 0, 0, 2,   //1 = Front/Back Wall
              2, 0, 0, 0, 2,   //2 = Side Wall
              2, 0, 0, 0, 2,   //3 = Diagonal Wall - South East
			  5, 1, 1, 1, 6};  //4 = Diagonal Wall - South West
                               //5 = Diagonal Wall - North East
			                   //6 = Diagonal Wall - North West

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

 MSG Msg;
 WNDCLASSEX WndClsEx;

 // Create the application window
 WndClsEx.cbSize = sizeof(WNDCLASSEX);
 WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
 WndClsEx.lpfnWndProc = WndProc;
 WndClsEx.cbClsExtra = 0;
 WndClsEx.cbWndExtra = 0;
 WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
 WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 WndClsEx.lpszMenuName = NULL;
 WndClsEx.lpszClassName = ClsName;
 WndClsEx.hInstance = hInstance;
 WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

 // Register the application
 RegisterClassEx(&WndClsEx);

 // Create the window object
 hWnd = CreateWindowEx(WS_EX_TOPMOST,
 ClsName,
 WndName,
 WS_POPUP,
 0,
 0,
 ScreenW,
 ScreenH,
 NULL,
 NULL,
 hInstance,
 NULL); 

 // Find out if the window was created
 if( !hWnd ) // If the window was not created,
 return 0; // stop the application 

 // Display the window to the user
 ShowWindow(hWnd, SW_SHOWNORMAL);
 UpdateWindow(hWnd); 
 InitD3D();

 // Decode and treat the messages
 // as long as the application is running
 while(true)
 {

  StartingCount = GetTickCount();

  if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
  {

   if (Msg.message == WM_QUIT) break;

   TranslateMessage(&Msg);
   DispatchMessage(&Msg);

  }

  Display();

  while((GetTickCount() - StartingCount) < 25);

 }

 CleanProgram();

 return Msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{

 switch (Msg) 
 {

  case WM_DESTROY :
  
   PostQuitMessage(WM_QUIT);
  
  break;

  case WM_CLOSE :

   DestroyWindow(hWnd);
 
  break;

  case WM_KEYUP :

   switch(wParam)
   {

    case VK_ESCAPE :

     DestroyWindow(hWnd);
 
    break;

	case VK_UP :

     UpDown + 2.0f;

	break;

	case VK_DOWN :

     UpDown - 2.0f;

	break;

	case VK_LEFT :

     SideWays - 2.0f;

	break;

	case VK_RIGHT :

     SideWays + 2.0f;

	break;

   }  

  break;

 }

 return DefWindowProc(hWnd, Msg, wParam, lParam);

}

VOID InitD3D()
{

 D3D = Direct3DCreate9(D3D_SDK_VERSION);
 
 ZeroMemory(&D3DPP, sizeof(D3DPP));

 D3DPP.Windowed = false;
 D3DPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
 D3DPP.hDeviceWindow = hWnd;
 D3DPP.BackBufferWidth = ScreenW;
 D3DPP.BackBufferHeight = ScreenH;
 D3DPP.BackBufferFormat = D3DFMT_X8R8G8B8;
 D3DPP.EnableAutoDepthStencil = true;
 D3DPP.AutoDepthStencilFormat = D3DFMT_D16;

 D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3DPP, &D3DDev); 

 InitGraphics();
 InitLights();

}

VOID InitGraphics()
{

 D3DDev->CreateVertexBuffer(8 * sizeof(CustomV), NULL, CustomFVF, D3DPOOL_MANAGED, &WallB, NULL);
 WallB->Lock(NULL, NULL, &VertMemLoc, NULL);

 memcpy(VertMemLoc, Wall, sizeof(Wall));

 WallB->Unlock();
 D3DDev->SetRenderState(D3DRS_LIGHTING, false);
 D3DDev->SetRenderState(D3DRS_ZENABLE, true);
 D3DDev->SetRenderState(D3DRS_NORMALIZENORMALS, true);

 D3DXCreateTextureFromFile(D3DDev, "C:\\Brick Wall.jpg", &BrickWallT);

}

VOID InitLights()
{



}

VOID Display()
{

 D3DXMatrixLookAtLH(&ViewM, &D3DXVECTOR3(-5.0f, 5.0f, 10.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
 D3DXMatrixPerspectiveFovLH(&ProjectionM, D3DXToRadian(45), (FLOAT)ScreenW / (FLOAT)ScreenH, 1.0f, 100.0f); 

 D3DDev->Clear(NULL, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 255, 0), 1.0f, NULL);
 D3DDev->BeginScene();
 D3DDev->SetFVF(CustomFVF);
 D3DDev->SetStreamSource(NULL, WallB, NULL, sizeof(CustomV));
 D3DDev->SetTransform(D3DTS_VIEW, &ViewM);
 D3DDev->SetTexture(NULL, BrickWallT);

 DrawMap();

 D3DDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
 D3DDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
 D3DDev->SetTransform(D3DTS_PROJECTION, &ProjectionM);
 D3DDev->EndScene();
 D3DDev->Present(NULL, NULL, NULL, NULL);

}

VOID DrawMap()
{

 INT MapIndex = 0;
 FLOAT OldX = 0, OldY = 0;
 D3DXMATRIX MapPos, MapRotation;

 for(MapIndex = 0; MapIndex < 5; ++MapIndex)
 {  
    
  if(Map[MapIndex] == 1)
  {

   D3DDev->SetTransform(D3DTS_WORLD, &MapPos);
   D3DDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
   D3DDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);

  }

  if(Map[MapIndex] == 2)
  {

   D3DXMatrixRotationY(&MapRotation, D3DXToRadian(270));

   D3DDev->SetTransform(D3DTS_WORLD, &(MapRotation * MapPos));
   D3DDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
   D3DDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);

  }

  if(MapIndex != 5)
  {

   D3DXMatrixTranslation(&MapPos, OldX + 2.0f, OldY, 1.0f);

   OldX += 2;

  }

  else
  {
 
   D3DXMatrixTranslation(&MapPos, OldX, OldY + 2.0f, 1.0f);

   OldY += 2;
 
  }
    
 }

}

VOID CleanProgram()
{

 D3D->Release();
 D3DDev->Release();
 BrickWallT->Release();
 WallB->Release();

}
Any ideas? Thanks.

Share this post


Link to post
Share on other sites
It would help if you need to provide more information. Essentially you've posted dozens of lines of code and said, "Something here is wrong. What is it?"

Which wall? What have you tried that didn't fix the problem?

Share this post


Link to post
Share on other sites
Sorry for not giving you enough information and thanks for your reply.

Well, the problem lies in the function DrawMap()(I think). When I say "the wall" I'm referring to a textured square.

I've tried working with the for loop but nothing really changed.

Thanks again.

Share this post


Link to post
Share on other sites
Just for starters, you set the projection matrix AFTER you've rendered some objects, which, in general, is unusual. It may not be the source of your problem but it's definitely not good practice. The proper setup of the view and projection matrices is very basic stuff.

It also appears that you don't check ANY return codes from any DirectX calls. You may have several problems, but there's no way to tell.

I would suggest you start more simply with just a basic view and projection setup, draw one object and get that code to work. Check return codes to ensure your code and resource references are correct.

Then, and only then, add more features to your code.

Your particular problem may just be incorrect vertex data, but without knowing if the code itself is working, it's difficult to say.

Share this post


Link to post
Share on other sites
Thanks for the reply. The projection matrix thing was an accident and as far as error checking goes, I don't check for errors because like I said this is only a test and I know there won't be any D3D errors because all computer resources are available and well the same result wouldn't really come up each time if it was a D3D error.

Is there any way to post a screen shot of what happens?

Thanks.

Share this post


Link to post
Share on other sites
Discovering your own errors and correcting them is going to help you much more than having someone point them out. I don't know where the problem is in your program and I don't see it on a quick inspection.

Specific Suggestion (repeated): Draw one object and get that code to work. Then add more objects.

Always start out simple and don't continue until the code and resources work.

Always check return codes for errors. It takes just a few microseconds in execution time. Even, as you say, you write error-free code, there's a small chance that some day there'll be an error and you can catch it. It's just good programming practice.

Share this post


Link to post
Share on other sites
Quote:
Original post by pcbrainbuster
I don't check for errors because like I said this is only a test and I know there won't be any D3D errors because all computer resources are available and well the same result wouldn't really come up each time if it was a D3D error.
How do you know? If there's an error in there, it'll be the same each time. Also, just because "all computer resources are available", doesn't mean that your graphics card is able to implement absolutely everything you ever need, graphics cards only support some features - for instance a lot of cards can't use a depth buffer and backbuffer of different bit depth. It's possible that would cause depthbuffer creation to fail, which would cause your clear() call to fail, which would have all sorts of weird effects. That is, if it didn't crash your app outright.

Any warnings or errors from the Debug runtimes?

Quote:
Original post by pcbrainbuster
Is there any way to post a screen shot of what happens?
Take a screenshot, upload it to photobucket or some other free image host, and then link to it here (Using HTML <img> tags).

Off topic: There's a fewof problems I can see, but they wouldn't explain what you're seeing:
1. You're not using AdjustWindowRectEx to get the correct window size. As such, your backbuffer will be larger than your client area, which is less efficient (And will cause problems if you ever do picking). However, the efficiency isn't much of an issue, because...
2. This line does nothing but eat up 100% of the CPU for between 15 and 35ms:
while((GetTickCount() - StartingCount) < 25);
If you don't want to render at full speed, then turn on v-sync - that's what it's for. At the very least, use Sleep().
3. You're only processing one window message per frame. You really need to process all queued messages and then render one frame. Otherwise you'll run into all sorts of weird problems if you do something like move the mouse across the window.
4. The VertMemLoc variable is a global, but it has no use whatsoever outside the Lock()..Unlock() calls for your vertex buffer. That should definitely be a local variable. Worst case is you accidently use it outside the Lock()..Unlock() and crash your app - or worse, cause weird problems that will be extremely difficult to track down.
5. You're looking "backwards". You're looking along -Z, when it's much more usual to look along +Z. Your eye is at Z=10, looking at Z=0.
6. In DrawMap(), you set a transform before you've initialised it. That's not very good (And I suppose could cause a crash on a really bad driver)
7. Try disabling culling (SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE)). That at least will tell you if your wall is being backface culled.

And as Buckeye said, start simple - get one wall working, then move on from there.

Share this post


Link to post
Share on other sites

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