Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to create new cube on click of a mouse?

Started by ritzmax72 Mar 20, 2018 at 2:35 PM 1 replies 1.6k views
Original Post
ritzmax72
ritzmax72

Hi All, I am struggling with directx 12. I am following Frank D Luna's D3D12 book. I figured out there is some concept like "Staging" buffer,
which is used only during initialization. This staging buffer is vertex and index buffers.

So chapter 6 has box demo which renders a box on the viewport. But I am confused on how someone who wants to generate new boxes during runtime can possibly achieve such thing.
Do I have to presume how many boxes my rendering loop would need? This doesn't sound practical to me.

Let's say I want to generate new boxes on press of a key (Say 'B'), how should I achieve this? Do I put the "Staging buffer" in rendering loop or what?
I tried doing many things but it's either new boxes are not generated or nothing is displayed.

Below is Chapter 6 source code.
https://github.com/discosultan/dx12-game-programming/tree/master/Samples/06-Box

Can anyone modify the code and achieve generating of new boxes on click or explain on how to go about that?Box.thumb.PNG.522221ec369b6adafb669f94b3ca458b.PNG

Burnt_Fyr
Burnt_Fyr

I've been away from gd for awhile so i'm a bit rusty on giving help, but generally the process works better when you have a specific problem that can be shown with code. I'm seeing that you want to create a cube on key press. what does your WM_KEYDOWN handler look like in the windows message procedure? Do you understand the process of creating buffers? if you want a single model(cube) drawn multiple times you will need some method of storing the transforms for each object. then in the render function you setup the transform and render the geometry for each object in a loop.



// c++ psuedo code

// ...in WM_KEYDOWN HANDLER
if(key == b)
{
  sceneobject o;
  // we create the buffers somewhere else maybe on level load, or game start, but they exist already
  o.vertbuffer = &cubebuffer
  o.indexbuffer = &cubeindices
  // world matrix that will be a randon position between 0..5
  o.transform = Matrix4Position(rand % 10 - 5, rand % 10 - 5, rand % 10 - 5) 
  
  sceneobjects.push_back(o)
}
// END WM_KEYDOWN HANDLER
    
// ...somewhere in render function

//begin scene

for(int modelindex =0; modelindex < sceneobjects.size(); modelindex++)
{
  // setup transforms
  
  // render buffers
}

//end scene

I've never really dug into c# but looking at your source code, it looks like the transforms are packed into a constant buffer in your update function.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.