   Problem with vertex buffer in D3D
  Hi! I'm currently trying to build a little 2D engine with direct3D. The problem is that i'm not getting anything on the screen. Now, I'm not going to post my whole code. But maby you guys can check out if the vertex bruffer creating code is valid.I'm trying to create a chain of quads that can handle diffrent sizes of textures, so i have written a algorithm that generates quads with the size 2*2, 4*4, 8*8, 16*16, you get the point. nMaxExponent is the maximun exponent i'm going to use to create the biggest quats... I thing you get the rest.inside the .h file...#define VertexFormatTex (D3DFVF_XYZ | D3DFVF_TEX1)struct Vertex{Vertex(float _x, float _y, float _z,    float _v, float _u){x = _x;y = _y;z = _z;v = _v;u = _u;}float x, y, z;float u, v;};IDirect3DVertexBuffer9*m_ppVB;inside the .cpp file...m_Device->CreateVertexBuffer((120 * nMaxExponent), D3DUSAGE_WRITEONLY, VertexFormatTex, D3DPOOL_DEFAULT, &m_ppVB, 0);Vertex* pVertices = 0;m_ppVB->Lock(0, 0, (void**)&pVertices, 0);float nExponent = 1;for(unsigned int i = 0; nExponent < nMaxExponent; nExponent++, i+= 6){//left-downpVertices.x = 0;pVertices.y = 0;pVertices.z = 0;pVertices.u = 0;pVertices.v = 1;//left-toppVertices[i+1].x = 0;pVertices[i+1].y = pow(2, nExponent);pVertices[i+1].z = 0;pVertices[i+1].u = 0;pVertices[i+1].v = 0;//riht-toppVertices[i+2].x = pow(2, nExponent);pVertices[i+2].y = pow(2, nExponent);pVertices[i+2].z = 0;pVertices[i+2].u = 1;pVertices[i+2].v = 0;//riht-toppVertices[i+3].x = pow(2, nExponent);pVertices[i+3].y = pow(2, nExponent);pVertices[i+3].z = 0;pVertices[i+3].u = 1;pVertices[i+3].v = 0;//right-downpVertices[i+4].x = pow(2, nExponent);pVertices[i+4].y = 0;pVertices[i+4].z = 0;pVertices[i+4].u = 1;pVertices[i+4].v = 1;//left-downpVertices[i+5].x = 0;pVertices[i+5].y = 0;pVertices[i+5].z = 0;pVertices[i+5].u = 0;pVertices[i+5].v = 1;}[Edited by - Mockarutan on October 7, 2009 8:19:29 AM]
  Seems like you are only writing to 4 out of 6 vertices, you need to index the last 3 vertices with [i+3] [i+4] [i+5]Hope that helps.   ;Quote:Original post by Marco81Seems like you are only writing to 4 out of 6 vertices, you need to index the last 3 vertices with [i+3] [i+4] [i+5]Hope that helps.I saw that to just befor you said it... just tried it the correct way without any diffrence...   ;  1) Mandatory link.2) You don't unlock the vertex buffer.3) How do you draw the quads?   ;Quote:I'm trying to create a chain of quads that can handle diffrent sizes of textures, so i have written a algorithm that generates quads with the size 2*2, 4*4, 8*8, 16*16, you get the point.Not to point out the obvious, but you can just create a single 1x1 quad and then scale it in the vertex shader to whatever size you want. You could even do it using the fixed function pipeline if that floats your boat.It doesn't matter what size the texture you are applying to the quad is. The quad doesn't care.   ;  Here is the render code:void GraphicsEngine::renderObjects(void){PresValues *LastObj, *CurrentObj;LastObj = new PresValues(0, 0, 0, 0, 0, 0, 0, NULL);D3DXMATRIX mxRotZ;D3DXMATRIX mxTranslationXYZ;m_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);m_Device->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);m_Device->BeginScene();m_Device->SetFVF(VertexFormatTex);m_Device->SetStreamSource(0, m_ppVB, 0, sizeof(Vertex));for(std::list<BaseObject*>::iterator it = m_ObjToDraw.begin(); it != m_ObjToDraw.end(); it++){CurrentObj = (*it)->getPresV();if(CurrentObj->Texture != LastObj->Texture)m_Device->SetTexture(0, CurrentObj->Texture);if(CurrentObj->StdPres){D3DXMatrixRotationZ(&mxRotZ, CurrentObj->RotZ);D3DXMatrixTranslation(&mxTranslationXYZ, CurrentObj->Pos.x, CurrentObj->Pos.y, (float(CurrentObj->Zlvl) * 0.0001f));m_Device->SetTransform(D3DTS_WORLD, &(m_mxStdPres * mxRotZ * mxTranslationXYZ));m_Device->DrawPrimitive(D3DPT_TRIANGLESTRIP, (6 * (CurrentObj->Size - 1)), 2);}delete LastObj;LastObj = CurrentObj;}if(LastObj == NULL)delete LastObj;m_Device->EndScene();m_Device->Present(NULL, NULL, NULL, NULL);}bool GraphicsEngine::initGraphics(int _MaxWidth){int nMax = int(sqrt(float(_MaxWidth)));int nMaxExponent = 0;for(int i = 0; i < nMax; i++){if(pow(2, float(i)) >= _MaxWidth){nMaxExponent = i;break;}}if(nMaxExponent == 0)return 0;//ZeroMemory(&m_ppVB, (120 * nMaxExponent));m_Device->CreateVertexBuffer((6 * sizeof(Vertex) * nMaxExponent), D3DUSAGE_WRITEONLY, VertexFormatTex, D3DPOOL_DEFAULT, &m_ppVB, 0);Vertex* pVertices = 0;m_ppVB->Lock(0, 0, (void**)&pVertices, 0);float nExponent = 1;for(unsigned int i = 0; nExponent < nMaxExponent; nExponent++, i+= 6){//left-downpVertices.x = 0;pVertices.y = 0;pVertices.z = 0;pVertices.u = 0;pVertices.v = 1;pVertices.Color = 0xff00ffff;//left-toppVertices[i+1].x = 0;pVertices[i+1].y = pow(2, nExponent);pVertices[i+1].z = 0;pVertices[i+1].u = 0;pVertices[i+1].v = 0;pVertices[i+1].Color = 0xff00ffff;//riht-toppVertices[i+2].x = pow(2, nExponent);pVertices[i+2].y = pow(2, nExponent);pVertices[i+2].z = 0;pVertices[i+2].u = 1;pVertices[i+2].v = 0;pVertices[i+2].Color = 0xff00ffff;//riht-toppVertices[i+3].x = pow(2, nExponent);pVertices[i+3].y = pow(2, nExponent);pVertices[i+3].z = 0;pVertices[i+3].u = 1;pVertices[i+3].v = 0;pVertices[i+3].Color = 0xff00ffff;//right-downpVertices[i+4].x = pow(2, nExponent);pVertices[i+4].y = 0;pVertices[i+4].z = 0;pVertices[i+4].u = 1;pVertices[i+4].v = 1;pVertices[i+4].Color = 0xff00ffff;//left-downpVertices[i+5].x = 0;pVertices[i+5].y = 0;pVertices[i+5].z = 0;pVertices[i+5].u = 0;pVertices[i+5].v = 1;pVertices[i+5].Color = 0xff00ffff;}m_ppVB->Unlock();return 1;}The DirectX debug thing that someone linked to, don't complain about anything...[Edited by - Mockarutan on October 7, 2009 9:19:47 AM];Quote:Original post by adt7Quote:I'm trying to create a chain of quads that can handle diffrent sizes of textures, so i have written a algorithm that generates quads with the size 2*2, 4*4, 8*8, 16*16, you get the point.Not to point out the obvious, but you can just create a single 1x1 quad and then scale it in the vertex shader to whatever size you want. You could even do it using the fixed function pipeline if that floats your boat.It doesn't matter what size the texture you are applying to the quad is. The quad doesn't care.I know that but i whant to optimize it. I think it takes longer to calc a new scale matrix then specify another index ofset in the DrawPrimitive func.