Archived

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

Rendering incorrectly

This topic is 5109 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 just started learning how to do meshes, and I finally have a class that loads an x file, but when it renders, its kind of transparent. I say kind of, because the colors are the, but (the model is a duck (looks like a rubber duck)) if you look at it from behind, you can see the beak through the back of the head. Does anybody know why this is happening. I know I''m supposed to post my code, but its 9 different files and 1077 lines long. If anybody has "The Zen of Direct3D Game Programming", my code is alot like it. The beginning is too close to the end...

Share this post


Link to post
Share on other sites
Here's my device creation code:
//--- CREATES THE DEVICE ---//
m_pDirect3D = Direct3DCreate8(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS PresentParams;
ZeroMemory(&PresentParams,sizeof(D3DPRESENT_PARAMETERS));

PresentParams.BackBufferFormat = Format;
PresentParams.BackBufferWidth = Size.m_iWidth;
PresentParams.BackBufferHeight = Size.m_iHeight;
PresentParams.hDeviceWindow = hWnd;
PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;

PresentParams.EnableAutoDepthStencil = TRUE;
PresentParams.AutoDepthStencilFormat = D3DFMT_D16;

PresentParams.MultiSampleType = D3DMULTISAMPLE_NONE;

PresentParams.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;

PresentParams.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

m_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&PresentParams,&m_pDevice);

m_pDevice->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&m_pBackBuffer);

Right after that I have these:

g_Direct3D.GetDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
g_Direct3D.GetDevice()->SetRenderState(D3DRS_LIGHTING, FALSE);
g_Direct3D.GetDevice()->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);


I can post more code, but I don't know what to post.

Also, I'm not using any SetTextureStageState calls, might that fix it. (I didn't think so, because it did the same thing with a plain triangle (multicolored))


[edited by - Programmer16 on December 16, 2003 3:30:25 AM]

Share this post


Link to post
Share on other sites
Normals define which direction the triangle is pointing. I''m pretty sure that x files always use normals, or at least mine did without my defining it. I don''t know, it just sounds to me like you have flipped normals, or the triangles are defined the wrong direction such that the default normals are the wrong way. You just might want to play around with it - may be the problem, might not be.

Share this post


Link to post
Share on other sites
I''m guessing its still the zbuffer that isn''t working. Make sure when you set your perspective matrix (D3DXMatrixPerspectiveFOVLH, or whatever) that the zbuffer values are good. More specifically, make sure the nearz value in that function is not 0, or near to zero. Try nearz 1, farz 30000.

Share this post


Link to post
Share on other sites
// Create the viewport
D3DVIEWPORT8 Viewport;

if(Position != NULL)
{
Viewport.X = Position->x;
Viewport.Y = Position->y;
}
else
{
Viewport.X = 0;
Viewport.Y = 0;
}
Viewport.Width = m_iSize.m_iWidth;
Viewport.Height = m_iSize.m_iHeight;
Viewport.MinZ = 0.0f;
Viewport.MaxZ = 1.0f;

m_pDevice->SetViewport(&Viewport);

//Create the projection matrix
D3DXMATRIX ProjectionMatrix;
ZeroMemory(&ProjectionMatrix,sizeof(D3DXMATRIX));

float ScreenAspect = ((float)m_iSize.m_iWidth/(float)m_iSize.m_iHeight);
float FOV = D3DX_PI / 4;

D3DXMatrixPerspectiveFovLH(&ProjectionMatrix,FOV,ScreenAspect,1.0f,1000.0f);

m_pDevice->SetTransform(D3DTS_PROJECTION,&ProjectionMatrix);
// Create the view matrix
D3DXMATRIX ViewMatrix;

D3DXMatrixLookAtLH(&ViewMatrix,&D3DXVECTOR3(0.0f,3.0f,-50.0f),&D3DXVECTOR3(0.0f,0.0f,0.0f),&D3DXVECTOR3(0.0f,1.0f,0.0f));
m_pDevice->SetTransform(D3DTS_VIEW,&ViewMatrix);
// Clear
m_pDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,Color,1.0f,0);

Its not just this either. I had the same problem with a pyramid I built.

Share this post


Link to post
Share on other sites
Here is my code:
My CDirect3D8 class

void CDirect3D8::Create(int width,int height,HWND hWnd,D3DFORMAT Format)
{
m_iSize.iSet(width,height);

m_pDirect3D = Direct3DCreate8(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS PresentParams;
ZeroMemory(&PresentParams,sizeof(D3DPRESENT_PARAMETERS));

PresentParams.BackBufferFormat = Format;
PresentParams.BackBufferWidth = width;
PresentParams.BackBufferHeight = height;
PresentParams.hDeviceWindow = hWnd;
PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;

m_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&PresentParams,&m_pDevice);

m_pDevice->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&m_pBackBuffer);

D3DXCreateSprite(m_pDevice,&m_pSprite);
}

void CDirect3D8::Create(CObjSize Size,HWND hWnd,D3DFORMAT Format)
{
m_iSize.iSet(Size.m_iWidth,Size.m_iHeight);

m_pDirect3D = Direct3DCreate8(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS PresentParams;
ZeroMemory(&PresentParams,sizeof(D3DPRESENT_PARAMETERS));

PresentParams.BackBufferFormat = Format;
PresentParams.BackBufferWidth = Size.m_iWidth;
PresentParams.BackBufferHeight = Size.m_iHeight;
PresentParams.hDeviceWindow = hWnd;
PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;

PresentParams.EnableAutoDepthStencil = TRUE;
PresentParams.AutoDepthStencilFormat = D3DFMT_D16;

PresentParams.MultiSampleType = D3DMULTISAMPLE_NONE;

PresentParams.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;

PresentParams.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

m_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&PresentParams,&m_pDevice);

m_pDevice->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&m_pBackBuffer);

D3DXCreateSprite(m_pDevice,&m_pSprite);

m_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
m_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
m_pDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
}

void CDirect3D8::Destroy()
{
SafeRelease(m_pSprite);
SafeRelease(m_pBackBuffer);
SafeRelease(m_pDevice);
SafeRelease(m_pDirect3D);
}

void CDirect3D8::CreateViewport(D3DXVECTOR2* Position)
{
D3DVIEWPORT8 Viewport;

if(Position != NULL)
{
Viewport.X = Position->x;
Viewport.Y = Position->y;
}
else
{
Viewport.X = 0;
Viewport.Y = 0;
}
Viewport.Width = m_iSize.m_iWidth;
Viewport.Height = m_iSize.m_iHeight;
Viewport.MinZ = 0.0f;
Viewport.MaxZ = 1.0f;

m_pDevice->SetViewport(&Viewport);
}

void CDirect3D8::SetProjectionMatrix()
{
D3DXMATRIX ProjectionMatrix;
ZeroMemory(&ProjectionMatrix,sizeof(D3DXMATRIX));

float ScreenAspect = ((float)m_iSize.m_iWidth/(float)m_iSize.m_iHeight);
float FOV = D3DX_PI / 4;

D3DXMatrixPerspectiveFovLH(&ProjectionMatrix,FOV,ScreenAspect,1.0f,1000.0f);

m_pDevice->SetTransform(D3DTS_PROJECTION,&ProjectionMatrix);
}

void CDirect3D8::SetViewMatrix()
{
D3DXMATRIX ViewMatrix;

D3DXMatrixLookAtLH(&ViewMatrix,&D3DXVECTOR3(0.0f,3.0f,-50.0f),&D3DXVECTOR3(0.0f,0.0f,0.0f),&D3DXVECTOR3(0.0f,1.0f,0.0f));
m_pDevice->SetTransform(D3DTS_VIEW,&ViewMatrix);
}

void CDirect3D8::Clear(D3DCOLOR Color)
{
m_pDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,Color,1.0f,0);
}

void CDirect3D8::Present()
{
m_pDevice->Present(NULL,NULL,NULL,NULL);
}

void CDirect3D8::Draw(CTexture Texture,RECT* SrcRect,D3DXVECTOR2* Position,D3DCOLOR Color)
{
m_pSprite->Draw(Texture.GetTexture(),SrcRect,NULL,NULL,0,Position,Color);
}

void CDirect3D8::BeginScene()
{
if(!m_bSceneOn)
{
m_pDevice->BeginScene();
m_bSceneOn = true;
}
}

void CDirect3D8::Begin()
{
if(m_bSceneOn)
{
if(!m_bSpriteOn)
{
m_pSprite->Begin();
m_bSpriteOn = true;
}
}
}

void CDirect3D8::End()
{
if(m_bSpriteOn)
{
m_pSprite->End();
m_bSpriteOn = false;
}
}

void CDirect3D8::EndScene()
{
if(m_bSceneOn)
{
m_pDevice->EndScene();
m_bSceneOn = false;
}
}

IDirect3DDevice8* CDirect3D8::GetDevice()
{
return m_pDevice;
}

IDirect3DSurface8* CDirect3D8::GetBackBuffer()
{
return m_pBackBuffer;
}

ID3DXSprite* CDirect3D8::GetSprite()
{
return m_pSprite;
}

Here is my CTexture,CMaterial, and CMesh classes

///////////////////////////////////////////////////////////////////////////

// CTexture definitions

///////////////////////////////////////////////////////////////////////////

IDirect3DDevice8* CTexture::m_pDevice = NULL;
D3DCOLOR CTexture::m_Transparent = D3DCOLOR_ARGB(0,255,0,255);

void CTexture::Create(char* filename,int width,int height)
{
m_iSize.m_iWidth = width;
m_iSize.m_iHeight = height;
if(width == 0 || height == 0)
D3DXCreateTextureFromFileEx(m_pDevice,filename,D3DX_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,0,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,D3DX_FILTER_NONE,D3DX_FILTER_NONE,m_Transparent,NULL,NULL,&m_pTexture);
else
D3DXCreateTextureFromFileEx(m_pDevice,filename,width,height,D3DX_DEFAULT,0,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,D3DX_FILTER_NONE,D3DX_FILTER_NONE,m_Transparent,NULL,NULL,&m_pTexture);
}

void CTexture::Destroy()
{
SafeRelease(m_pTexture);
}

void CTexture::Load(char* filename)
{
strcpy(m_sFilename,filename);
Create(filename,m_iSize.m_iWidth,m_iSize.m_iHeight);
}

void CTexture::SetDevice(IDirect3DDevice8* pDevice)
{
m_pDevice = pDevice;
}

void CTexture::SetTransparent(D3DCOLOR Color)
{
m_Transparent = Color;
}

IDirect3DTexture8* CTexture::GetTexture()
{
return m_pTexture;
}

char* CTexture::GetFilename()
{
return m_sFilename;
}

CObjSize CTexture::GetSize()
{
return m_iSize;
}

D3DCOLOR CTexture::GetTransparent()
{
return m_Transparent;
}

void CTexture::SetTexture()
{
m_pDevice->SetTexture(0,m_pTexture);
}
///////////////////////////////////////////////////////////////////////////

// CMaterial definitions

///////////////////////////////////////////////////////////////////////////

IDirect3DDevice8* CMaterial::m_pDevice = NULL;

void CMaterial::Create()
{
ZeroMemory(&m_Material,sizeof(D3DMATERIAL8));

m_Material.Diffuse.r = 1.0f;
m_Material.Diffuse.g = 1.0f;
m_Material.Diffuse.b = 1.0f;
}

void CMaterial::SetDevice(IDirect3DDevice8* pDevice)
{
m_pDevice = pDevice;
}

void CMaterial::SetDiffuse( float r, float g, float b )
{
m_Material.Diffuse.r = r;
m_Material.Diffuse.g = g;
m_Material.Diffuse.b = b;
}

void CMaterial::SetAmbient( float r, float g, float b )
{
m_Material.Ambient.r = r;
m_Material.Ambient.g = g;
m_Material.Ambient.b = b;
}

void CMaterial::SetSpecular( float r, float g, float b, float Power )
{
m_Material.Specular.r = r;
m_Material.Specular.g = g;
m_Material.Specular.b = b;
}

void CMaterial::SetEmissive( float r, float g, float b )
{
m_Material.Emissive.r = r;
m_Material.Emissive.g = g;
m_Material.Emissive.b = b;
}

D3DMATERIAL8 CMaterial::GetMaterial()
{
return m_Material;
}
///////////////////////////////////////////////////////////////////////////

// CMesh definitions

///////////////////////////////////////////////////////////////////////////

IDirect3DDevice8* CMesh::m_pDevice = NULL;

void CMesh::Create()
{
m_pMesh = NULL;
m_NumMats = 0;

m_pTextures = NULL;
m_pMaterials = NULL;
}

void CMesh::Destroy()
{
SafeRelease(m_pMesh);
SafeReleaseExB(m_pTextures,m_NumMats);
}

void CMesh::SetDevice(IDirect3DDevice8* pDevice)
{
m_pDevice = pDevice;
}

void CMesh::LoadXFile(char* filename)
{
ID3DXBuffer* pMaterialBuffer = NULL;

D3DXLoadMeshFromX(filename,D3DXMESH_MANAGED,m_pDevice,0,&pMaterialBuffer,(DWORD*)&m_NumMats,&m_pMesh);

m_pTextures = new CTexture[m_NumMats];
m_pMaterials = new CMaterial[m_NumMats];

D3DXMATERIAL* pMaterials = (D3DXMATERIAL*)pMaterialBuffer->GetBufferPointer();

for(int a = 0; a < m_NumMats; a++)
{
m_pMaterials[a].m_Material = pMaterials[a].MatD3D;
m_pMaterials[a].m_Material.Ambient = m_pMaterials[a].m_Material.Diffuse;

if(!pMaterials[a].pTextureFilename)
{
m_pTextures[a].Create("default.bmp");

continue;
}

m_pTextures[a].Create(pMaterials[a].pTextureFilename);
}

SafeRelease(pMaterialBuffer);
}

void CMesh::Render()
{
for(int a = 0; a < m_NumMats; a++)
{
m_pDevice->SetMaterial(&m_pMaterials[a].GetMaterial());
m_pDevice->SetTexture(0,m_pTextures[a].GetTexture());
m_pMesh->DrawSubset(a);

}

m_pDevice->SetVertexShader(CVertexFVF);
}

This is my vertex class

#define CVertexFVF (D3DFVF_XYZ | D3DFVF_NORMAL)

class CVertex
{
public:
float x,y,z;
float nx,ny,nz;
};

And finally my VLegends cpp file

#include <windows.h>
#include <mmsystem.h>
#include <fstream.h>
#include <string.h>
#include <time.h>

#include "System.h"
#include "Graphics.h"
#include "TileEngine.h"
#include "Font.h"
///////////////////////////////////////////////////////////////////////////

// Global variables

///////////////////////////////////////////////////////////////////////////

CWindow g_Window;
CDirect3D8 g_Direct3D;
CTexture g_Texture;
CTileEngine g_TileEngine;
CFontEngine g_FontEngine;
IDirect3DVertexBuffer8* g_pVertexBuffer = NULL;

static int iRotationDegrees = 0;
static float iYMovement = 0.0f;
static float iXMovement = 0.0f;

CMesh g_Mesh;

///////////////////////////////////////////////////////////////////////////

// Function prototpyes

///////////////////////////////////////////////////////////////////////////

LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);

void CreateGame(HINSTANCE hInstance);
void Frame();
void DestroyGame(HINSTANCE hInstance);

///////////////////////////////////////////////////////////////////////////

// WinMain()

///////////////////////////////////////////////////////////////////////////

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR,int)
{
CreateGame(hInstance);

MSG msg;
ZeroMemory(&msg,sizeof(MSG));

while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Frame();
}
}

DestroyGame(hInstance);
return msg.wParam;
}

///////////////////////////////////////////////////////////////////////////

// WndProc()

///////////////////////////////////////////////////////////////////////////

LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
ShowCursor(false);
return 0;
}

case WM_DESTROY:
{
ShowCursor(true);
return 0;
}

case WM_KEYDOWN:
{
switch(wParam)
{
case VK_ESCAPE:
{
PostQuitMessage(WM_QUIT);
return 0;
}
}
}
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
///////////////////////////////////////////////////////////////////////////

// CreateGame()

///////////////////////////////////////////////////////////////////////////

void CreateGame(HINSTANCE hInstance)
{
g_Window.Create("FlameBrain Engine",640,480,hInstance,(WNDPROC)WndProc);
ShowWindow(g_Window.GetHWND(),SW_SHOWNORMAL);
UpdateWindow(g_Window.GetHWND());

g_Direct3D.Create(g_Window.GetSize(),g_Window.GetHWND());

CVertex Triangle[] =
{
{0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,},
{-1.0f, -1.0f, -1.0f, -1.0f,-1.0f,-1.0f,},
{1.0f, -1.0f, -1.0f, 1.0f,-1.0f,-1.0f,},

{1.0f, -1.0f, 1.0f, 1.0f,-1.0f,1.0f,},

{-1.0f, -1.0f, 1.0f, -1.0f,-1.0f,1.0f,},

{-1.0f, -1.0f, -1.0f, -1.0f,-1.0f,-1.0f,},
};

g_Direct3D.GetDevice()->CreateVertexBuffer(6 * sizeof(CVertex),D3DUSAGE_WRITEONLY,CVertexFVF,D3DPOOL_MANAGED,&g_pVertexBuffer);

BYTE *pVertices;

g_pVertexBuffer->Lock(0,0,(BYTE**)&pVertices,D3DLOCK_NOSYSLOCK);

memcpy(pVertices,Triangle,sizeof(&Triangle));
g_pVertexBuffer->Unlock();

g_Direct3D.CreateViewport();
g_Direct3D.SetProjectionMatrix();
g_Direct3D.SetViewMatrix();

g_Texture.SetDevice(g_Direct3D.GetDevice());

g_FontEngine.Create(g_Direct3D.GetDevice(),"Lydian");
g_FontEngine.SetColor(D3DCOLOR_XRGB(0,0,0));

g_Mesh.SetDevice(g_Direct3D.GetDevice());
g_Mesh.Create();
g_Mesh.LoadXFile("Ducky.x");
}
///////////////////////////////////////////////////////////////////////////

// Frame()

///////////////////////////////////////////////////////////////////////////

void Frame()
{
if(GetAsyncKeyState(VK_SPACE))
{
iRotationDegrees--;
if (iRotationDegrees<=0)
iRotationDegrees=360;
}

if(GetAsyncKeyState(VK_RETURN))
{
iRotationDegrees++;
if (iRotationDegrees>=360)
iRotationDegrees=0;
}

if(GetAsyncKeyState(VK_UP))
{
iYMovement+= 0.1f;
if(iYMovement >= 23)
iYMovement = -4;
}

if(GetAsyncKeyState(VK_DOWN))
{
iYMovement-= 0.1f;
//if(iYMovement <= -4)

//iYMovement = 23;

}

if(GetAsyncKeyState(VK_RIGHT))
{
iXMovement+= 0.1f;
if(iXMovement >= 15)
iXMovement = -15;
}

if(GetAsyncKeyState(VK_LEFT))
{
iXMovement-= 0.1f;
if(iXMovement <= -15)
iXMovement = 15;
}

g_Direct3D.Clear(D3DCOLOR_XRGB(0,255,255));
g_Direct3D.BeginScene();
g_Direct3D.Begin();

// We have a static integer for how many degrees to generate the rotation matrix for


// Setup a matrix to rotate iRotationDegrees degrees around the Y axis

D3DXMATRIX matRotation;
D3DXMATRIX matRotationX;
D3DXMATRIX WorldMatrix;
D3DXMATRIX transMatrix;

D3DXMatrixRotationY(&matRotation, D3DXToRadian(iRotationDegrees));
D3DXMatrixRotationX(&matRotationX, D3DXToRadian(0));

D3DXMatrixTranslation(&transMatrix,iXMovement,0.0f,iYMovement);

D3DXMatrixMultiply(&WorldMatrix,&matRotationX,&matRotation);

D3DXMatrixMultiply(&WorldMatrix,&WorldMatrix,&transMatrix);

// Set that matrix to the world transformation

g_Direct3D.GetDevice()->SetTransform(D3DTS_WORLD, &WorldMatrix);

g_Direct3D.GetDevice()->SetStreamSource(0,g_pVertexBuffer,sizeof(CVertex));
g_Direct3D.GetDevice()->SetVertexShader(CVertexFVF);

//g_Direct3D.GetDevice()->DrawPrimitive(D3DPT_TRIANGLEFAN,0,4);


g_Mesh.Render();

g_Direct3D.GetDevice()->SetTexture(0,NULL);

g_Direct3D.End();
g_Direct3D.EndScene();
g_Direct3D.Present();
}
///////////////////////////////////////////////////////////////////////////

// DestroyGame()

///////////////////////////////////////////////////////////////////////////

void DestroyGame(HINSTANCE hInstance)
{
///////////////////////////////////////////////////////////////////////

// Release all of the COM objects

///////////////////////////////////////////////////////////////////////

g_Mesh.Destroy();
SafeRelease(g_pVertexBuffer);
g_FontEngine.Destroy();
g_Texture.Destroy();
g_Direct3D.Destroy();
g_Window.Destroy(hInstance);
}

Share this post


Link to post
Share on other sites