Fooling around in directx11 template

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Lorth
Posts: 139
Joined: July 19th, 2012, 4:12 pm

Fooling around in directx11 template

Post by Lorth » March 31st, 2017, 10:06 am

Hey Everyone!

Now it is a while since i showed myself on the forum. I read it everyday and i enjoy all topics that appears.

Now i got an problem as well. I have taken the step to create an new app with the windows DirectX 11 template. It gives you an app with some fps text and an cube rotating on the screen. And as chili says, fool around with the code is a good way to learn s*it. This inspired me to try it out.

With this new app i challanged myself to try to do small things.
-Change rotation to anti clockwise.
-Change so the cube rotate in the y direction instead.
-Change background color.
-Change so the cube uses roll pitch yaw, instead of only rotate.
-Try change text
-Modify XAML, add buttons, slides, textboxes and s*it.

These things i have accompliced with some messy code and many hours reading up on rastertek.com
directxtutorials.com and my favorite site atm msdn. Have also found some samples that helps alot, 3d shooter game, and marble maze. both have alot of documentation on msdn as well.

But there is also things i want to complete but i can't figure out how to, or even where to begin.
-Change color of the cube.
I know where the cube is created and where the in vertices are created. have found the code that:
// Load mesh vertices. Each vertex has a position and a color.
This is pretty straight forward and here i can change the color of the vertices.

But what i wanna do is to actually change the color at runtime. What i have found is the colors set when the cube is created. my idea is to create an function that set all vertices color to something else. but i have no clue how to do this...
-Make the cube change color every rotation
This will be the next goal when i have figured out how to actually change the cube color.
-Change the cube to an pyramid at runtime

-Make the cube an own class and object.
It feels like the cube is not it's own object. Have tried to implement the frustrum calling tutorial from rastertek so i would get 10 objects on the screen. This implementation has been hard to do, but i think i have managed to implement it. But this part of code is quite messy and it have been alot of try and retries.

Things i will try to implement later on:
-A grid and camera movement.
-collision control and interaction with objects.
-Try to make the cubes move around.

A lot of the things i wanna do is covered by the rastertek.com tutorials but i can not figure out how to do it. It feels like just changing color should be super simple but i can't get it to work...

Hope there is someone that could assist

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Fooling around in directx11 template

Post by albinopapa » March 31st, 2017, 5:55 pm

What you want is to deal with constant buffers and the shaders.

You know how each frame you update the constant buffer with the matrix transforms; world, view and projection? The same could be applied with the color.

You could
add a float4 to the constant buffer in the vertex shader
add an XMFLOAT4 or XMVECTOR or your own color struct that stores four floats to the struct you pass to the constant buffer in your application
In the actual vertex shader body, make your output.color equal to the color value from the constant buffer.

This way, you can change the color each full rotation in your application buffer. When you update your gpu constant buffer with the new color, instead of using the vertex color that gets created with the cube, you would pass the cbuffer color to the pixel shader data object ( rastertek calls this struct PixelInputType ).
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Fooling around in directx11 template

Post by albinopapa » March 31st, 2017, 6:05 pm

Anything you want to do to the objects you render, you have to tell the GPU what to do using those steps.

Layout data application-side to be sent to GPU:
This means either creating structs or adding data to existing structs that can be sent to the GPU.
Send to GPU:
You have to give the GPU the data through the ID3D11Buffer which is bound as a constant buffer and updated or swapped out for each change or each object. For instance, the vertex shader's constant buffer is updated each frame with new transforms. Each object might have a different position, orientation or scale so the matrices would be different for each object so the constant buffer needs to be swapped for the next objects transforms.
Add relevant code to the shaders:
Once you have added variables to the app's cbuffer representation, you must do the exact same thing for the shader's cbuffer. Then of course in the shader body, use the new data in calculations as to affect the scene objects.

Notice how in the light tutorials, each time you add new data, you have to change the pixel shader and it's cbuffer. Same would apply to changing the color of your cube. You can do this in the vertex shader though since changing the color of the vertices wouldn't have to be done at the pixel level.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Lorth
Posts: 139
Joined: July 19th, 2012, 4:12 pm

Re: Fooling around in directx11 template

Post by Lorth » April 1st, 2017, 12:29 am

I have no problem understanding your explanation, but i can't figure out how to implement it in the project...
tries to add an float4 in the vertexshaderinput struct, but this gives me errors, i also tries to add it in the struct for pixelshaderinput, but it dosn't help, so i can't change my output.color to my new float...

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Fooling around in directx11 template

Post by albinopapa » April 1st, 2017, 12:37 am

Can you share your project? Either through GitHub or a ZIP file, and I'll take a look.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Fooling around in directx11 template

Post by albinopapa » April 1st, 2017, 12:39 am

Oh, and I've said this before I think on this forum, but the rastertek tuts set the code up (IMO) in a way that really is only meant for the tutorials. I can't imagine having the Graphics class handle so much of the game logic, but for learning the basics on how to setup D3D it's a pretty good resource.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Lorth
Posts: 139
Joined: July 19th, 2012, 4:12 pm

Re: Fooling around in directx11 template

Post by Lorth » April 1st, 2017, 10:26 am

Don't you think i got it to work 15 min after i put up my last post. Feels good and motivation when i got it to work. :)

now i will try to change and add an function that change the color of an specified vertices. Wanna be able to change color of the corners individuell.

thanks for the help albinopapa

Post Reply