How can I extend this graphics code to draw more vectors of vertices?

SirYakalot
  • How can I extend this graphics code to draw more vectors of vertices? SirYakalot

    I want to include the drawing of other vectors of vertices in the following code, in a number of places it seems easy enough to simply add or multiply size()s of the two vectors, for instance in the first check. But then it gets a bit fuzzy simply because of a lack of understanding.

    Should I add them together to create a bigger buffer description and copy everything together? Or should I do it all separately?

    Basically how would I extend this code to include more vectors of vertices?

    if (temporary2DVerts.size() > 0)
    {
        // create the vertex buffer and store the pointer into pBuffer, which is created globally
        D3D10_BUFFER_DESC bd;
        bd.Usage = D3D10_USAGE_DYNAMIC;
        bd.ByteWidth = sizeof(VERTEX) * temporary2DVerts.size();// this breaks the create buffer line : temp2DVerts.size();
        bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
        bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
        bd.MiscFlags = 0;
    
        device->CreateBuffer(&bd, NULL, &pBuffer);
    
        void* pVoid;    // the void pointer
    
        pBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pVoid);    // map the vertex buffer
        memcpy(pVoid, &temporary2DVerts[0], sizeof(temporary2DVerts[0]) * temporary2DVerts.size());
        pBuffer->Unmap();
    
        // select which input layout we are using
        device->IASetInputLayout(screenVertexLayout);
    
        // select which primtive type we are using
        device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_LINELIST);
    
        // select which vertex buffer to display
        UINT stride = sizeof(VERTEX);
        UINT offset = 0;
        device->IASetVertexBuffers(0, 1, &pBuffer, &stride, &offset);
    
        // apply the appropriate pass
        screenPass->Apply(0);
    
        // draw the vertex buffer to the back buffer
        device->Draw(temporary2DVerts.size(), 0);
    
        temporary2DVerts.clear();
    }
    

    EDIT - just to be clearer - there are 4 vectors of vertices. two that don't change and two that constantly change. permanent 2D and permanent 3D and a temporary version of both. This is so the permanent ones can just be loaded in and are always drawn, and the temporary ones only get drawn for a frame. This way I can create moving lines for debugging, like line of sight cones etc.

    EDIT - the naive approach would be to duplicate this code 4 times for each vector, but surely there is a better way? I don't think this is a hard thing to do, but trying to do it has exposed a basic gap in my knowledge about what some of these lines of code do. should I just populate one big list from all the vectors and push that through?

    some need to be drawn with different passes, and some don't need to be changed each frame. for example, the permanent ones can just stay on the gpu. right?

  • First of all, you shouldn't create buffers at rendering time as it is a costly operation, putting pressure on the GPU memory (it is also not clear that you are actually releasing the vertex buffer). It is better to allocate buffers at the beginning of your application.

    If I understand your use case, you have a set of non-moving vertices-lines and a set of moving vertices-lines: It is better to allocate two buffers for each usage, one that is not declared as dynamic and the other dynamic.

    You should first draw your buffer of constant vertices. Then for the moving lines, you just have to update the other specific dynamic buffer on each frame. This buffer should be allocated with enough space for the maximum number of expected vertices to move. If number of vertices could change heavily between the calls (ranging from 10 to 10000), you could create 2-3 dynamic buffers for the right maximum size (for example 10, 1000, 10000) and select the right dynamic buffer according to your temporary2DVerts.size();.

Tags
c++ directx directx10
Related questions and answers
  • _WRITE; bd.MiscFlags = 0; device->CreateBuffer(&bd, NULL, &pBuffer); void* pVoid; // the void pointer pBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pVoid); // map the vertex buffer...) * 8; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.MiscFlags = 0; device->CreateBuffer(&bd, NULL, &pBuffer); void* pVoid; // the void pointer... type we are using device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // select which vertex buffer and index buffer to display UINT stride = sizeof(VERTEX

  • ; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.MiscFlags = 0; device->CreateBuffer(&bd, NULL, &pBuffer); void* pVoid; pBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pVoid); //HERE memcpy(pVoid, &temporary2DVerts[0], sizeof(temporary2DVerts[0]) * temporary2DVerts.size()); pBuffer->Unmap(); device->IASetInputLayout(screenVertexLayout); device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_LINELIST); UINT stride = sizeof(VERTEX); UINT offset = 0; device->IASetVertexBuffers(0, 1, &pBuffer, &stride, &offset); screenPass->Apply(0

  • ; bd.MiscFlags = 0; device->CreateBuffer(&bd, NULL, &pBuffer); void* pVoid; // the void pointer pBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pVoid); // map the vertex buffer memcpy(pVoid, OurVertices, sizeof(OurVertices)); // copy the vertices to the buffer pBuffer->Unmap(); // create the index buffer out of DWORDs DWORD OurIndices[] = { 0, 1, 2, // side 1...; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.MiscFlags = 0; device->CreateBuffer(&bd, NULL, &iBuffer); iBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pVoid); // map the index buffer memcpy

  • UINT stride = sizeof(LINE); UINT offset = 0; device->IASetVertexBuffers(0, 1, &pBuffer, &stride, &offset); device->IASetIndexBuffer(iBuffer, DXGI_FORMAT_R32_UINT, 0); allLines... matView, D3DMATRIX matProjection) { device->IASetInputLayout(pVertexLayout); mesh.SetTopology();//TODO should not be done multiple times // select which vertex buffer and index buffer to display UINT stride = sizeof(VERTEX); UINT offset = 0; device->IASetVertexBuffers(0, 1, mesh.PBuffer(), &stride, &offset); device->IASetIndexBuffer(mesh.IBuffer(), DXGI_FORMAT_R32_UINT, 0

  • ; bd.ByteWidth = sizeof(VERTEX) * 8; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.MiscFlags = 0; device->CreateBuffer(&bd, NULL, &pBuffer); void* pVoid; // the void pointer pBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pVoid); // map the vertex buffer memcpy(pVoid, OurVertices, sizeof(OurVertices)); // copy the vertices to the buffer... = D3D10_USAGE_DYNAMIC; bd.ByteWidth = sizeof(DWORD) * 36; bd.BindFlags = D3D10_BIND_INDEX_BUFFER; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.MiscFlags = 0; device->CreateBuffer(&bd, NULL

  • shader in every node. Other option I was thinking was making some helper functions to deal with the simpler cases, which would set some default parameters and would ask only the most basic ones... animation I would end up storing a lot of data that I don't really need, even if I'm indexing node and frame data when saving and then store the hierarchy with the indices to the actual data. I don't... the Diffuse color variation or making it store any value that can change but making it so random that I woulnd't know how to relate the data to the other frames, or need a complex system for it. That's why

  • matView, D3DXMATRIX matProjection) { device->IASetInputLayout(pVertexLayout); device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_LINESTRIP); // select which vertex buffer and index buffer to display UINT stride = sizeof(LINE); UINT offset = 0; device->IASetVertexBuffers(0, 1, &pBuffer, &stride, &offset); device->IASetIndexBuffer(iBuffer, DXGI...I have a class which "collects" line mesh transformations throughout the update and then draws them in one place by drawing one line over and over again. I'll post the code below. I'm just wondering

  • /////////////////////////// void Refresh(int command= ALL, int transition= NONE) { //old_map = new_map; // update the old map for(int r= 0; r< nrows; r++){ move(r,0); addstr... and traveling throug it. This is due to some odd reason for the "numb_collisions", which is used to count how many collisions have occured since initial collision (if it's 0 it means it IS the initial... all the code: Thanks again! #include <iostream> #include <time.h&gt; // or "ctime" #include <stdio.h&gt; // for #include <cstdlib> // Windows stuff #include <Windows.h&gt

  • ); // Set The Color Of The Model ( NEW ) // ORIGINAL DRAWING CODE //Draw the model as an interpolation between the two frames glBegin(GL_TRIANGLES); for(int i = 0; i < numTriangles; i++) { MD2Triangle* triangle = triangles + i; for(int j = 0; j < 3; j++) { MD2Vertex* v1 = frame1-&gt;vertices + triangle-&gt;vertices[j... }; // Color Of The Lines ( NEW ) // ORIGINAL PART OF THE FUNCTION //Figure out the two frames between which we are interpolating int frameIndex1 = (int)(time * (endFrame - startFrame + 1

Data information