• Advertisement
Sign in to follow this  

DX11 Issue with same texture sprites not appearing when added to the vertex buffer

Recommended Posts

I am currently working on my first iteration of my sprite renderer and I'm trying to draw 2 sprites. They both use the same texture and are placed into the same buffer, but unfortunately only the second sprite is shown on the the screen. I assume I messed something up when I place them into the buffer and that I am overwriting the data of the first sprite.

So how should I be mapping my buffer with an offset?

/*
Code that sets up the sprite vertices and etc
*/

D3D11_MAPPED_SUBRESOURCE resource = vertexBuffer->map(vertexBufferMapType);
memcpy(resource.pData, verts, sizeof(SpriteVertex) * VERTEX_PER_QUAD);
vertexBuffer->unmap();
vertexCount += VERTEX_PER_QUAD;

I feel like I should be doing something like:

/*
Code that sets up the sprite vertices and etc
*/

D3D11_MAPPED_SUBRESOURCE resource = vertexBuffer->map(vertexBufferMapType);
//Place the sprite vertex data into the pData using the current vertex count as offset
//The code resource.pData[vertexCount] is syntatically wrong though :( Not sure how it should look since pData is void pointer
memcpy(resource.pData[vertexCount], verts, sizeof(SpriteVertex) * VERTEX_PER_QUAD);
vertexBuffer->unmap();
vertexCount += VERTEX_PER_QUAD;

 

Also speaking of offsets can someone give an example of when the pOffsets param for the IASetVertexBuffers call would not be 0
 

Edited by noodleBowl

Share this post


Link to post
Share on other sites
Advertisement

Your right in that the first code chunk just overwrites what you already have in there if you call it a second time.

resource.pData is a void pointer as you know. it's just an address. resource.pData[vertexCount] is the right idea though, but since it's a void pointer, you can't index it that way. two things you can do. one is to cast resource.pData to a SpriteVertex* (or a char* then multiply vertexCount by the size of your vertex structure), then index it:
(static_cast<SpriteVertex*>resource.pData)[vertexCount]

or:
(static_cast<char*>resource.pData)[vertexCount * sizeof(SpriteVertex)]

The other thing you can do is just add the number of bytes to resource.pData to get to your second chunk of vertices address:

resource.pData + (vertexCount * sizeof(SpriteVertex))

As for the pOffsets param, an example might be that you have a 2d animated sprite. all your animations of the sprite are in a single texture at different texture coordinates. You might have two vertex buffers, one for positions, and one for texture coordinates. the positions vertex buffer might simply contain 6 vertices for a quad (two triangles). the texture coordinates vertex buffer might contain all the texture coordinates for each frame of animation for that sprite. When you call IASetVertexBuffers, you will provide both of those vertex buffers, and two pOffsets. The first time you render your sprite, maybe you want the first animation frame, which is the texture coordinates located at the beginning of your texture coordinates buffer. in this case, the array of pOffsets you pass in would look like [0, 0], meaning start at the beginning of both buffers. Next you want to draw the second frame of animation, so you would pass in an array that might look like this [0, 6] (each quad has 2 triangles, each triangle has 3 texture coordinates, so the quad you are drawing might have 6 texture coordinates), the first one is your positions buffer, you will use the same positions for the quad. the second is your texture coordinates you want to use, offset 6 to get to the second animation frame. this is in bytes though so would actually be 6 * sizeof(texturecoordinate)

So looking at the data (psuedocode), your buffers all together might look like this:

vertex positions:

[0, 1, 2, 3, 4, 5] - just pretend each number is actually a vertex position

 

texture coordinates:

[0, 1, 2, 3, 4, 5, - first animation frame

6, 7, 8, 9, 10, 11, - second animation frame

12, 13, 14, 15, 16, 17] - third animation frame

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

Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Fleshbits
      Back around 2006 I spent a good year or two reading books, articles on this site, and gobbling up everything game dev related I could. I started an engine in DX10 and got through basics. I eventually gave up, because I couldn't do the harder things.
      Now, my C++ is 12 years stronger, my mind is trained better, and I am thinking of giving it another go.
      Alot has changed. There is no more SDK, there is evidently a DX Toolkit, XNA died, all the sweet sites I used to go to are 404, and google searches all point to Unity and Unreal.
      I plainly don't like Unity or Unreal, but might learn them for reference.
      So, what is the current path? Does everyone pretty much use the DX Toolkit? Should I start there? I also read that DX12 is just expert level DX11, so I guess I am going DX 11.
      Is there a current and up to date list of learning resources anywhere?  I am about tired of 404s..
       
       
    • By LifeArtist
      Good Evening,
      I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
      First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
      I am really stucked right now because of the fundamental question:
      Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
      If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on. 
      In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
      Should I treat those debug objects as entities/components?
      For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
      Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
      Regards,
      LifeArtist
    • By nickyc95
      Hi.
      I'm kind of late to this party but I thought I would ask anyway as I haven't found a concrete answer.
       
      When creating a game engine, when should you choose one methodology over another (more specifically OOP and DOD)? Which areas benefit from DOD? Which areas benefit from OOP? Do people typically mix multiple methodologies throughout a project? I.e. certain sub-systems created in one, and others in the another?  
      DOD - Data Oriented Design
      OOP - Object Oriented Design
       
      Pretty simple
      Thanks
    • By Estra
      Memory Trees is a PC game and Life+Farming simulation game. Harvest Moon and Rune Factory , the game will be quite big. I believe that this will take a long time to finish
      Looking for
      Programmer
      1 experience using Unity/C++
      2 have a portfolio of Programmer
      3 like RPG game ( Rune rune factory / zelda series / FF series )
      4 Have responsibility + Time Management
      and friendly easy working with others Programmer willing to use Skype for communication with team please E-mail me if you're interested
      Split %: Revenue share. We can discuss. Fully Funded servers and contents
      and friendly easy working with others willing to use Skype for communication with team please E-mail me if you're interested
      we can talk more detail in Estherfanworld@gmail.com Don't comment here
      Thank you so much for reading
      More about our game
      Memory Trees : forget me not

      Thank you so much for reading
      Ps.Please make sure that you have unity skill and Have responsibility + Time Management,
      because If not it will waste time not one but both of us
       

    • By mike44
      typedef struct FontInfo_t* FontHandle;
      MichaelNS::FontMgr m;
      MichaelNS::FontHandle FontGeladen;
      const char *FontPtr;
      start:
      InFileFont.append("Ubuntu-B.ttf");
      FontPtr = InFileFont.c_str();
      FontGeladen = m.loadFont(FontPtr, NULL, NULL, 14, 0);
      stop:
          m.unloadFont(FontGeladen);
          delete FontPtr;
      delete causes a crash. How to unload the class correctly?
      Thanks
  • Advertisement