Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

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

$
0
0
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; }

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>