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

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

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

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


  • Forum Statistics

    • Total Topics
      628651
    • Total Posts
      2984043
  • Similar Content

    • By ADDMX
      Hi
      Just a simple question about compute shaders (CS5, DX11).
      Do the atomic operations (InterlockedAdd in my case) should work without any issues on RWByteAddressBuffer and be globaly coherent ?
      I'v come back from CUDA world and commited fairly simple kernel that does some job, the pseudo-code is as follows:
      (both kernels use that same RWByteAddressBuffer)
      first kernel does some job and sets Result[0] = 0;
      (using Result.Store(0, 0))
      I'v checked with debugger, and indeed the value stored at dword 0 is 0
      now my second kernel
      RWByteAddressBuffer Result;  [numthreads(8, 8, 8)] void main() {     for (int i = 0; i < 5; i++)     {         uint4 v0 = DoSomeCalculations1();         uint4 v1 = DoSomeCalculations2();         uint4 v2 = DoSomeCalculations3();                  if (v0.w == 0 && v1.w == 0 && v2.w)             continue;         //    increment counter by 3, and get it previous value         // this should basically allocate space for 3 uint4 values in buffer         uint prev;         Result.InterlockedAdd(0, 3, prev);                  // this fills the buffer with 3 uint4 values (+1 is here as the first 16 bytes is occupied by DrawInstancedIndirect data)         Result.Store4((prev+0+1)*16, v0);         Result.Store4((prev+1+1)*16, v1);         Result.Store4((prev+2+1)*16, v2);     } } Now I invoke it with Dispatch(4,4,4)
      Now I use DrawInstancedIndirect to draw the buffer, but ocassionaly there is missed triangle here and there for a frame, as if the atomic counter does not work as expected
      do I need any additional synchronization there ?
      I'v tried 'AllMemoryBarrierWithGroupSync' at the end of kernel, but without effect.
      If I do not use atomic counter, and istead just output empty vertices (that will transform into degenerated triangles) the all is OK - as if I'm missing some form of synchronization, but I do not see such a thing in DX11.
      I'v tested on both old and new nvidia hardware (680M and 1080, the behaviour is that same).
       
    • By Josheir
      In the following code:

       
      Point p = a[1]; center of rotation for (int i = 0; I<4; i++) { int x = a[i].x - p.x; int y = a[i].y - p.y; a[i].x = y + p.x; a[i].y = - x + p.y; }  
      I am understanding that a 90 degree shift results in a change like:   
      xNew = -y
      yNew = x
       
      Could someone please explain how the two additions and subtractions of the p.x and p.y works?
       
      Thank you,
      Josheir
    • By ForgedInteractive
      Who We Are
      We are Forged Interactive, a small team of like-minded game developers with the sole purpose of making games we love! Currently, we're progressing very quickly with our first project and there are plenty of opportunities and work for new interested programmers. With this project, our development platform is Unity 5.5.2 and C# as our behavioral language. Since this project is our first release, the game itself is a smaller project though progress is moving quickly. We are looking to finalize the current project and get started on future projects in the near future and are expanding our team to do so.
       
      Who We Are Looking For:
      Programmer Level Designer  
      About the Game
      Ours is the tale of two siblings, thrown into a world of chaos. Living in the shadow of their parents' heroic deeds and their Uncle's colorful military career, Finn and Atia are about to become the next force to shape our world. How will you rise through the ranks of Hereilla and what will be your legacy? Once defeated your enemies turn coat and join you in your adventures. Players can enjoy a range of troops and abilities based on their gameplay style which become more important as maps introduce more challenging terrain, enemies and bosses. Strong orc knights, dangerous shamans, and even a dragon are out on the prowl. Knowing when to fight and when to run, and how to manage your army is essential. Your actions alone decide the fate of this world.
       
      Previous Work by Team
      Although we are working towards our first game as Forged Interactive, our team members themselves have worked on titles including and not limited to:
      Final Fantasy Kingsglaive FIFA 2017 Xcom 2 Civilization  
      What do we expect?
      Reference work or portfolio. Examples what have you already done and what projects you have worked on academic or otherwise. The ability to commit to the project on a regular basis. If you are going on a two-week trip, we don't mind, but it would be good if you could commit 10+ hours to the project each week. Willingness to work with a royalty based compensation model, you will be paid when the game launches. Openness to learning new tools and techniques
       
      What can we offer?
      Continuous support and availability from our side. You have the ability to give design input, and creative say in the development of the game. Shown in credits on websites, in-game and more. Insight and contacts from within the Industry.
       
      Contact
      If you are interested in knowing more or joining, please email or PM us on Skype. A member of our management team will reply to you within 48 hours.
       
      E-mail: Recruitment@ForgedInteractive.com
      Skype: ForgedInteractive
       
      Regards,
      David, Colin and Joseph
       
      Follow us on:
      Facebook: https://www.facebook.com/ForgedInteractive/
      Twitter: @ForgedInteract
      Youtube: https://www.youtube.com/channel/UCpK3zhq5ToOeDpdI0Eik-Ug?view_as=subscriber
      Reddit: www.reddit.com/user/Forged_Interactive

    • By Michael Pearson
      Hello all!   I'm currently in my third year on my 3D Animation & Games Development course, and I am in the process of doing some basic primary research for my dissertation project, which is to create a high quality 3D Environment for use in video games and potentially VR.   I have a questionnaire (targeting other artists in the field), and I would really appreciate it if you took some time to have a look and fill it out:   https://docs.google.com/forms/d/e/1FAIpQLScW12nFI8-fMlAUMNSQvOInxhnIfXpG91iRCm25TVlZufrvbQ/viewform?usp=sf_link   Thank you in advance!   Mike.
  • Popular Now