Applying an offset during runtime, MAKE THE SQUARES MOVE

Started by
20 comments, last by JWColeman 5 years, 7 months ago

If you're using D3DX consider using XM instead, D3DX is deprecated and doesn't use SIMD AFAIK :)

Edit: Ah you're using DirectX 9, thought you were using DirectX 11 - not sure what's best then :)

 

 

.:vinterberg:.

Advertisement
5 hours ago, vinterberg said:

If you're using D3DX consider using XM instead, D3DX is deprecated and doesn't use SIMD AFAIK :)

Edit: Ah you're using DirectX 9, thought you were using DirectX 11 - not sure what's best then :)

 

 

Can you elaborate on what you mean? I thought I was using direct x 11.

7 hours ago, ChuckNovice said:

If you want to make your life even easier you could deal with a matrix instead of a float3. A matrix allow you to perform any combination of translation, rotation and scaling.

Which involves changing this :



cbuffer PositionConstantBuffer : register (b1)
{
	float3 xyz;
}

to this :



cbuffer PositionConstantBuffer : register (b1)
{
	float4x4 world;
}

 

And adjusting the shader a little bit to something like this :



VOut VShader(float4 position : POSITION, float4 color : COLOR)
{
	VOut output;
	output.position = mul(position, world);
        //output.position = mul(output.position, view); // you'll very soon want a view matrix there
	output.position = mul(output.position, projection); // Apply ortho projection matrix
	output.color = color; // Apply color from vertex input
	return output;
}

 

I'm assuming that you are already using a math library that provides common matrix operations such as Translate/Rotate/Scale.

Thanks for the advice! So, in all the tutorials I've seen, there has been a world matrix. In your example are you suggesting the world matrix is used for translating each object in the world?

Through all the tutorials I've seen, this hasn't been so simply explained.

Not sure how I broke my shader trying to do what you recommended:

 


cbuffer MatrixConstantBuffer : register(b0)
{
	matrix world;
	matrix projection;
}

struct VOut
{
	float4 position : SV_POSITION;
	float4 color : COLOR;
};

VOut VShader(float4 position : POSITION, float4 color : COLOR)
{
	VOut output;
	output.position = mul(position, world); 			// Apply world translation matrix
	output.position = mul(output.position, projection); // Apply ortho projection matrix
	output.color = color; // Apply color from vertex input

	return output;
}


float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
	return color;
}

 

You should read the compile results when you compile your shaders. It would've quickly told you that matrix isn't a recognized type. My example has float4x4, this is how you declare a 4x4 matrix in HLSL.

 

Secondly you grouped the two matrix back in the same constant buffer so unless you changed your code accordingly this may be another problem. You didn't need to group them back together by the way since world will be updated once per object and projection almost never changes. Grouping them back together force you to unnecessarily update projection everytime you want to change the world matrix.

On ‎9‎/‎3‎/‎2018 at 10:30 AM, ChuckNovice said:

You should read the compile results when you compile your shaders. It would've quickly told you that matrix isn't a recognized type. My example has float4x4, this is how you declare a 4x4 matrix in HLSL.

 

Secondly you grouped the two matrix back in the same constant buffer so unless you changed your code accordingly this may be another problem. You didn't need to group them back together by the way since world will be updated once per object and projection almost never changes. Grouping them back together force you to unnecessarily update projection everytime you want to change the world matrix.

Okay, good catch, I was using matrix because it was in an example tutorial I began with. Also, the tutorial I was using shows to create our own file, and I have a procedure that compiles the shaders into a blob object.

I've tried using legit hlsl files, and I get can them to compile, however I'm not sure how to go about bringing them into my code for use, that can be another topic though perhaps? Unless you're wanting to share here of course!

 

 

 

I will make changes again to separate the constant buffers, and then see if I can switch over from MATRIX to float4x4, and then see if I can get this working like I did before I broke it.

Okay, I have things back and rearranged.

I think I'm missing a step because my square is very tiny :D

image.png.e879e63d5658cfc6e95d8ea06f266820.png

 

This isn't quite the desired effect, if I pass an identity matrix instead everything looks hunky dory...

 

image.png.f29d6780520bf4cd7375008bed9bf234.png

 

So, my shader is doing I think exactly as you suggested now:


cbuffer ProjectionConstantBuffer : register(b0)
{
	float4x4 projection;
}

cbuffer PositionConstantBuffer : register(b1)
{
	float4x4 world;
}

struct VOut
{
	float4 position : SV_POSITION;
	float4 color : COLOR;
};

VOut VShader(float4 position : POSITION, float4 color : COLOR)
{
	VOut output;

	output.position = mul(position, world);				// Apply world matrix
	output.position = mul(output.position, projection); // Apply ortho projection matrix
	output.color = color; // Apply color from vertex input

	return output;
}

 

Heyyyyy. I think I figured it out, but I don't really understand whats going on here:

 


	D3DXMatrixTranslation(&cbuffer.world, 100, 100, 0);

	D3DXMatrixTranspose(&cbuffer.world, &cbuffer.world);

 

Anyways, I have it doing the thing now!

 

image.thumb.png.c97dc2a1962ce9a61bf00ed52446c23f.png

On ‎9‎/‎3‎/‎2018 at 12:58 AM, ChuckNovice said:

//output.position = mul(output.position, view); // you'll very soon want a view matrix there

Hey Chuck, you mentioned I'm probably going to want a view matrix, and you're probably right. Currently, I have a very limited understanding of what the view matrix will accomplish for me in an orthographic setting. I just in this thread alone learned what the world matrix is for, maybe you can help me understand how I can make use of a view matrix in an ortho projection?

1 minute ago, JWColeman said:

Hey Chuck, you mentioned I'm probably going to want a view matrix, and you're probably right. Currently, I have a very limited understanding of what the view matrix will accomplish for me in an orthographic setting. I just in this thread alone learned what the world matrix is for, maybe you can help me understand how I can make use of a view matrix in an ortho projection?

To put it very simply a view matrix is basically the camera's position and looking direction. It'll be useful if you also want the camera to move. With world / view / projection you are now able to translate/rotate/scale objects, move/rotate the camera and project it to the screen in ortho/perspective as you wish.

1 minute ago, ChuckNovice said:

To put it very simply a view matrix is basically the camera's position and looking direction. It'll be useful if you also want the camera to move. With world / view / projection you are now able to translate/rotate/scale objects, move/rotate the camera and project it to the screen in ortho/perspective as you wish.

So, as a real example, say I'm playing a 2D side scroller, my camera, or view matrix, would be what determines what comes into view as I move my character?

 

I understand the view matrix is responsible for scaling too, so if I wanted to zoom in on a specific sprite, I could use the camera to do so...

 

So, right now, the scope of my game is like... Pong, for traditional pong I'm not sure if I really need a camera/view yet? 

 

Anyways, I'm digressing, my next step is to update my world matrix based on user input (perhaps w, a, s, and d key input)

The tricky portion here is going to be applying translation to specific sets of vertices, say I have two squares. I'll need to essentially loop my Draw function so that my world matrix is correct for each object I draw.

16 minutes ago, JWColeman said:

So, as a real example, say I'm playing a 2D side scroller, my camera, or view matrix, would be what determines what comes into view as I move my character?

 

I understand the view matrix is responsible for scaling too, so if I wanted to zoom in on a specific sprite, I could use the camera to do so...

 

So, right now, the scope of my game is like... Pong, for traditional pong I'm not sure if I really need a camera/view yet? 

 

Anyways, I'm digressing, my next step is to update my world matrix based on user input (perhaps w, a, s, and d key input)

The tricky portion here is going to be applying translation to specific sets of vertices, say I have two squares. I'll need to essentially loop my Draw function so that my world matrix is correct for each object I draw.

Yes, the view matrix is only another transformation that you add on top of the world transformation. As a 2D side scroller let's say you move the camera to the right / left all the view matrix will essentially contain is a translation and we apply it on top of the world transformation.

Sure you could get away with a translation on every world object to fake the camera movement but that isn't as efficient as updating the view matrix only once at the beginning of the frame. World / View / Projection are the 3 you'll usually see everywhere.

 

For your other question, a draw call will invoke your shader once and no there is no way to change your world matrix during that moment. You'll either have to loop some draw call as you said or learn about instancing as someone suggested in this thread. Instancing allow you to draw the same mesh as many time as you like with one single call and you pass it a buffer that contains all the per instance parameters that you like (a world matrix in your case). With instancing you can draw much more stuff, draw calls are expensive.

This topic is closed to new replies.

Advertisement