Jump to content
  • Advertisement
Smarkus

3D -DIRECTX 9- Can someone help me out with this vertex buffer error?

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

Can someone help out with this. The code builds but I get "Exception thrown: Read Access Violation" and says my index buffer was a nullptr.

I'm going to attach my code and a screenshot of the error below or above. Any help is greatly appreciated.

//-------------------------------
//ヘッダファイル
//-------------------------------
#include "manager.h"
#include "renderer.h"
#include "dome.h"
#include "camera.h"

//-------------------------------
//コンストラクタ
//-------------------------------
CDome::CDome()
{
	m_pIndxBuff = nullptr;
	m_pVtxBuff = nullptr;
	m_HorizontalGrid = NULL;
	m_VerticalGrid = NULL;

	// ワールドの位置・拡大・回転を設定
	m_Scale = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
	m_Pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	//m_Rotate = 0.0f;
}

CDome::CDome(int HorizontalGrid, int VerticalGrid, float Length)
{
	m_pIndxBuff = nullptr;
	m_pVtxBuff = nullptr;
	m_HorizontalGrid = HorizontalGrid;
	m_VerticalGrid = VerticalGrid;

	// ワールドの位置・拡大・回転を設定
	m_Scale = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
	m_Pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	m_Length = Length;
}

CDome::CDome(int HorizontalGrid, int VerticalGrid, float Length, D3DXVECTOR3 Pos)
{
	m_pIndxBuff = nullptr;
	m_pVtxBuff = nullptr;
	m_HorizontalGrid = HorizontalGrid;
	m_VerticalGrid = VerticalGrid;

	// ワールドの位置・拡大・回転を設定
	m_Scale = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
	m_Pos = Pos;
	m_Length = Length;
}

//-------------------------------
//デストラクタ
//-------------------------------
CDome::~CDome()
{
}

//-------------------------------
//初期化処理
//-------------------------------
void CDome::Init(void)
{
	LPDIRECT3DDEVICE9 pDevice;
	pDevice = CManager::GetRenderer()->GetDevice();

	m_VtxNum = (m_HorizontalGrid + 1) * (m_VerticalGrid + 1);
	m_IndxNum = (m_HorizontalGrid * 2 + 2) * m_VerticalGrid + (m_VerticalGrid - 1) * 2;

	// テクスチャの生成
	if (FAILED(D3DXCreateTextureFromFile(pDevice, "data/TEXTURE/dome.jpg", &m_pTexture)))
	{
		MessageBox(NULL, "Couldn't read Texture file destination", "Error Loading Texture", MB_OK | MB_ICONHAND);
	}

	//頂点バッファの作成
	if (FAILED(pDevice->CreateVertexBuffer(sizeof(VERTEX_3D) * m_VtxNum,
		D3DUSAGE_WRITEONLY,
		FVF_VERTEX_3D,
		D3DPOOL_MANAGED,
		&m_pVtxBuff,
		NULL))) //作成した頂点バッファのサイズ
	{
		MessageBox(NULL, "Error making VertexBuffer", "Error", MB_OK);
	}

	//インデクスバッファの作成
	if (FAILED(pDevice->CreateIndexBuffer(sizeof(VERTEX_3D) * m_IndxNum,
		D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16,
		D3DPOOL_MANAGED,
		&m_pIndxBuff,
		NULL)))
	{
		MessageBox(NULL, "Error making IndexBuffer", "Error", MB_OK);
	}

	VERTEX_3D *pVtx;			//仮想アドレス用ポインターVertex
	WORD *pIndx;				//仮想アドレス用ポインターIndex

								//頂点バッファをロックして仮想アドレスを取得する。
	m_pVtxBuff->Lock(0, 0, (void**)&pVtx, 0);
	//インデクスをロックして仮想アドレスを取得する。
	m_pIndxBuff->Lock(0, 0, (void**)&pIndx, 0);

	for (int i = 0; i < (m_VerticalGrid + 1); i++)
	{
		for (int j = 0; j < (m_HorizontalGrid + 1); j++)
		{
			pVtx[0].pos = D3DXVECTOR3(m_Length * sinf(i * (D3DX_PI * 0.5f / ((int)m_VerticalGrid - 1))) * sinf(j * (D3DX_PI * 2 / ((int)m_HorizontalGrid - 1))),
				m_Length * cosf(i * (D3DX_PI * 0.5f / ((int)m_VerticalGrid - 1))),
				m_Length * sinf(i * (D3DX_PI* 0.5f / ((int)m_VerticalGrid - 1))) * cosf(j * (D3DX_PI * 2 / ((int)m_HorizontalGrid - 1))));
			D3DXVECTOR3 tempNormalize;
			D3DXVec3Normalize(&tempNormalize, &pVtx[0].pos);
			pVtx[0].normal = -tempNormalize;
			pVtx[0].color = D3DXCOLOR(255, 255, 255, 255);
			pVtx[0].tex = D3DXVECTOR2((float)j / (m_HorizontalGrid - 1), (float)i / (m_VerticalGrid - 1));
			pVtx++;
		}
	}

	for (int i = 0; i < m_VerticalGrid; i++)
	{
		if (i != 0)
		{
			pIndx[0] = ((m_HorizontalGrid + 1) * (i + 1));
			pIndx++;
		}
		for (int j = 0; j < (m_HorizontalGrid + 1); j++)
		{
			pIndx[0] = ((m_HorizontalGrid + 1) * (i + 1)) + j;
			pIndx[1] = ((m_HorizontalGrid + 1) * i) + j;
			pIndx += 2;
		}
		if (i + 1 != m_VerticalGrid)
		{
			pIndx[0] = pIndx[-1];
			pIndx++;
		}
	}

	//インデクスをアンロックする
	m_pIndxBuff->Unlock();
	//頂点バッファをアンロックする
	m_pVtxBuff->Unlock();
}

//-------------------------------
//終了処理
//-------------------------------
void CDome::Uninit(void)
{
	// 頂点バッファの破棄
	SAFE_RELEASE(m_pVtxBuff);

	// インデクスの破棄
	SAFE_RELEASE(m_pIndxBuff);

	Release();
}

//-------------------------------
//更新処理
//-------------------------------
void CDome::Update(void)
{
	m_Pos = CManager::GetCamera()->GetCameraPosEye();
}

//-------------------------------
//描画処理
//-------------------------------
void CDome::Draw(void)
{
	LPDIRECT3DDEVICE9 pDevice;
	pDevice = CManager::GetRenderer()->GetDevice();

	D3DXMATRIX mtxWorld;
	D3DXMATRIX mtxPos;
	D3DXMATRIX mtxScale;
	D3DXMATRIX mtxRotation;

	// ワールドID
	D3DXMatrixIdentity(&mtxWorld);

	// 3D拡大行列
	D3DXMatrixScaling(&mtxScale, m_Scale.x, m_Scale.y, m_Scale.z);
	D3DXMatrixMultiply(&mtxWorld, &mtxWorld, &mtxScale);

	// 3D平行移動行列
	D3DXMatrixTranslation(&mtxPos, m_Pos.x, m_Pos.y + 70.0f, m_Pos.z);
	D3DXMatrixMultiply(&mtxWorld, &mtxWorld, &mtxPos);

	// ワールド座標変換
	pDevice->SetTransform(D3DTS_WORLD, &mtxWorld);

	// 頂点バッファをデータストリームに設定
	pDevice->SetStreamSource(0, m_pVtxBuff, 0, sizeof(VERTEX_3D));

	// 頂点フォーマットの設定
	pDevice->SetFVF(FVF_VERTEX_3D);

	// テクスチャの設定
	pDevice->SetTexture(0, m_pTexture);

	// インデクスの設定
	pDevice->SetIndices(m_pIndxBuff);

	// カラーが見えるようにライトを消す
	pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

	// ポリゴンの描画
	pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP,
		0,
		0,
		m_VtxNum,
		0,
		m_IndxNum - 2);

	// ライトを元に戻す
	pDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
}

//-------------------------------
//Create MeshDome
//-------------------------------
CDome *CDome::Create(int HorizontalGrid, int VerticalGrid, float Length)
{
	CDome *pMeshDome;
	pMeshDome = new CDome(HorizontalGrid, VerticalGrid, Length);
	pMeshDome->Init();

	return pMeshDome;
}

CDome *CDome::Create(int HorizontalGrid, int VerticalGrid, float Length, D3DXVECTOR3 Pos)
{
	CDome *pMeshDome;
	pMeshDome = new CDome(HorizontalGrid, VerticalGrid, Length, Pos);
	pMeshDome->Init();

	return pMeshDome;
}

 

Capture1.PNG

Share this post


Link to post
Share on other sites
Advertisement

I never worked with D3D9 much, more of an OpenGL/Vulkan programmer, but why are you using a more or less dead API?  D3D11 was reveled in 2008.

Share this post


Link to post
Share on other sites

Hi!

It looks like you are passing the size of vertex instead of index size.
Maybe better would be: sizeof(WORD)*m_IndxNum

Maybe DirectX would write some informations to console in debug mode.

 

Edited by kubera

Share this post


Link to post
Share on other sites
On 11/14/2017 at 5:31 AM, kubera said:

Hi!

It looks like you are passing the size of vertex instead of index size.
Maybe better would be: sizeof(WORD)*m_IndxNum

Maybe DirectX would write some informations to console in debug mode.

 

Thank you so much! I was able to fix it

Share this post


Link to post
Share on other sites
On 11/14/2017 at 4:13 AM, Mike2343 said:

I never worked with D3D9 much, more of an OpenGL/Vulkan programmer, but why are you using a more or less dead API?  D3D11 was reveled in 2008.

I know it's stupid but it's required by my school for some reason

Share this post


Link to post
Share on other sites
12 hours ago, Smarkus said:

I know it's stupid but it's required by my school for some reason

I'm hoping this is public schooling in the US and not post secondary :)

Share this post


Link to post
Share on other sites
22 hours ago, Mike2343 said:

I'm hoping this is public schooling in the US and not post secondary

Neither. It"s actually in Tokyo lol hence the Japanese comments

Edited by Smarkus

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!