User Tools

Site Tools


tutorial_204_20-_203d_20spaces

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tutorial_204_20-_203d_20spaces [2018/03/31 13:19] – external edit 127.0.0.1tutorial_204_20-_203d_20spaces [2024/01/05 00:21] (current) – external edit 127.0.0.1
Line 27: Line 27:
 \\  In the previous tutorial, we wrote a program that renders a single triangle to screen. When we create the vertex buffer, the vertex positions that we use are directly in projection space so that we don't have to perform any transformation. Now that we have an understanding of 3D space and transformation, we are going to modify the program so that the vertex buffer is defined in object space, as it should be. Then, we will modify our vertex shader to transform the vertices from object space to projection space.\\ \\  \\  In the previous tutorial, we wrote a program that renders a single triangle to screen. When we create the vertex buffer, the vertex positions that we use are directly in projection space so that we don't have to perform any transformation. Now that we have an understanding of 3D space and transformation, we are going to modify the program so that the vertex buffer is defined in object space, as it should be. Then, we will modify our vertex shader to transform the vertices from object space to projection space.\\ \\ 
 ===== Modifying the Vertex Buffer ===== ===== Modifying the Vertex Buffer =====
-\\  Since we started representing things in three dimensions, we have changed the flat triangle from the previous tutorial to a cube. This will allow us to demonstrate these concepts much clearer.\\ \\ +\\  Since we started representing things in three dimensions, we have changed the flat triangle from the previous tutorial to a cube. This will allow us to demonstrate these concepts much clearer. 
 + 
 +<code bb4w> 
         REM Create vertex buffer:         REM Create vertex buffer:
         DIM vertices{(7)} = SimpleVertex{}         DIM vertices{(7)} = SimpleVertex{}
Line 48: Line 50:
         DATA   1.0, -1.0,  1.0,  1.0, 1.0, 1.0, 1.0         DATA   1.0, -1.0,  1.0,  1.0, 1.0, 1.0, 1.0
         DATA  -1.0, -1.0,  1.0,  0.0, 0.0, 0.0, 1.0         DATA  -1.0, -1.0,  1.0,  0.0, 0.0, 0.0, 1.0
-\\  On a cube, many triangles will be sharing the same vertex and it would be a waste of space to redefine the same points over and over again. As such, there is a method to specify just the eight points, and then let Direct3D know which points to pick for a triangle. This is done through an index buffer. An index buffer will contain a list, which will refer to the index of vertices in the buffer, to specify which points to use in each triangle. The code below shows which points make up each of our triangles:\\ \\ +</code> 
 + 
 +On a cube, many triangles will be sharing the same vertex and it would be a waste of space to redefine the same points over and over again. As such, there is a method to specify just the eight points, and then let Direct3D know which points to pick for a triangle. This is done through an index buffer. An index buffer will contain a list, which will refer to the index of vertices in the buffer, to specify which points to use in each triangle. The code below shows which points make up each of our triangles: 
 + 
 +<code bb4w>
         REM Create index buffer:         REM Create index buffer:
         DIM indices{(5,5)} = WORD{}         DIM indices{(5,5)} = WORD{}
Line 75: Line 81:
         SYS ID3D11DeviceContext.IASetIndexBuffer%, pImmediateContext%, pIndexBuffer%, \         SYS ID3D11DeviceContext.IASetIndexBuffer%, pImmediateContext%, pIndexBuffer%, \
         \                                          DXGI_FORMAT_R16_UINT, 0         \                                          DXGI_FORMAT_R16_UINT, 0
-\\ +</code> 
 ===== Modifying the Vertex Shader ===== ===== Modifying the Vertex Shader =====
 \\  In our vertex shader from the previous tutorial, we take the input vertex position and output the same position without any modification. We can do this because the input vertex position is already defined in projection space. Now, because the input vertex position is defined in object space, we must transform it before outputting from the vertex shader. We do this with three steps: transform from object to world space, transform from world to view space, and transform from view to projection space. A vector is transformed by multiplying the vector by a matrix. In HLSL, this is done using the mul() intrinsic function. Our variable declaration and new vertex shader are shown below:\\ \\  \\  In our vertex shader from the previous tutorial, we take the input vertex position and output the same position without any modification. We can do this because the input vertex position is already defined in projection space. Now, because the input vertex position is defined in object space, we must transform it before outputting from the vertex shader. We do this with three steps: transform from object to world space, transform from world to view space, and transform from view to projection space. A vector is transformed by multiplying the vector by a matrix. In HLSL, this is done using the mul() intrinsic function. Our variable declaration and new vertex shader are shown below:\\ \\ 
 +<code glsl>
       //       //
       // Vertex Shader       // Vertex Shader
Line 90: Line 98:
           return output;           return output;
       }       }
 +</code>
 \\  \\ 
 ===== Setting up the Matrices ===== ===== Setting up the Matrices =====
-\\  We have updated our vertex shader to transform using matrices, but we also need to define three matrices in our program:\\ \\ +\\  We have updated our vertex shader to transform using matrices, but we also need to define three matrices in our program: 
 + 
 +<code bb4w>
         REM Define the three transformation matrices:         REM Define the three transformation matrices:
         DIM mWorld(3,3), mView(3,3), mProjection(3,3)         DIM mWorld(3,3), mView(3,3), mProjection(3,3)
-\\  In addition to the matrices, we also need an ID3D11Buffer object that represents the constant buffer into which they will be transferred:\\ \\ +</code> 
 + 
 +In addition to the matrices, we also need an ID3D11Buffer object that represents the constant buffer into which they will be transferred: 
 + 
 +<code bb4w>
         REM Create the constant buffer:         REM Create the constant buffer:
         DIM ConstantBuffer{mWorld{}=XMMATRIX{},mView{}=XMMATRIX{},mProjection{}=XMMATRIX{}}         DIM ConstantBuffer{mWorld{}=XMMATRIX{},mView{}=XMMATRIX{},mProjection{}=XMMATRIX{}}
Line 106: Line 121:
         SYS ID3D11Device.CreateBuffer%, pd3dDevice%, bd{}, NULL, ^pConstantBuffer% TO hr%         SYS ID3D11Device.CreateBuffer%, pd3dDevice%, bd{}, NULL, ^pConstantBuffer% TO hr%
         IF hr% <> 0 OR pConstantBuffer% = 0 ERROR 100, "ID3D11Device::CreateBuffer (constant) failed: "+STR$~hr%         IF hr% <> 0 OR pConstantBuffer% = 0 ERROR 100, "ID3D11Device::CreateBuffer (constant) failed: "+STR$~hr%
-\\  The next thing that we need to do is come up with three matrices that we will use to do the transformation. We would like to set up our camera so that it is situated at [0 1 -5], looking at the point [0 1 0]. We can call PROC_MatrixLookAtLH() to conveniently compute a view matrix for us using the up vector [0 1 0] since we would like the +Y direction to always stay at top:\\ \\ +</code> 
 + 
 +The next thing that we need to do is come up with three matrices that we will use to do the transformation. We would like to set up our camera so that it is situated at [0 1 -5], looking at the point [0 1 0]. We can call PROC_MatrixLookAtLH() to conveniently compute a view matrix for us using the up vector [0 1 0] since we would like the +Y direction to always stay at top: 
 + 
 +<code bb4w>
         REM Initialize the view matrix:         REM Initialize the view matrix:
         DIM Eye(2), At(2), Up(2)         DIM Eye(2), At(2), Up(2)
         Eye() = 0.0, 1.0, -5.0 : At() = 0.0, 0.0, 0.0 : Up() = 0.0, 1.0, 0.0         Eye() = 0.0, 1.0, -5.0 : At() = 0.0, 0.0, 0.0 : Up() = 0.0, 1.0, 0.0
         PROC_MatrixLookAtLH(mView(), Eye(), At(), Up())         PROC_MatrixLookAtLH(mView(), Eye(), At(), Up())
-\\  Finally, to come up with a projection matrix, we call PROC_MatrixPerspectiveFovLH(), with a 90 degree vertical field of view (pi/2), an aspect ratio of 640/512 which is from our back buffer size, and near and far Z at 0.1 and 110, respectively. This means that anything closer than 0.1 or further than 110 will not be visible on the screen:\\ \\ +</code> 
 + 
 +Finally, to come up with a projection matrix, we call PROC_MatrixPerspectiveFovLH(), with a 90 degree vertical field of view (pi/2), an aspect ratio of 640/512 which is from our back buffer size, and near and far Z at 0.1 and 110, respectively. This means that anything closer than 0.1 or further than 110 will not be visible on the screen: 
 + 
 +<code bb4w>
         REM Initialize the projection matrix:         REM Initialize the projection matrix:
         PROC_MatrixPerspectiveFovLH(mProjection(), PI/2, Width%/Height%, 0.01, 100)         PROC_MatrixPerspectiveFovLH(mProjection(), PI/2, Width%/Height%, 0.01, 100)
-\\ +</code> 
 ===== Updating Constant Buffers ===== ===== Updating Constant Buffers =====
-\\  We have the matrices, and now we must write them to the constant buffer when rendering so that the GPU can read them. Also, because matrices are arranged differently in memory in C++ and HLSL, we must transpose the matrices before updating them:\\ \\ +\\  We have the matrices, and now we must write them to the constant buffer when rendering so that the GPU can read them. Also, because matrices are arranged differently in memory in C++ and HLSL, we must transpose the matrices before updating them: 
 + 
 +<code bb4w>
         PROC_MatrixTranspose(ConstantBuffer{}, ConstantBuffer.mWorld{}, mWorld())         PROC_MatrixTranspose(ConstantBuffer{}, ConstantBuffer.mWorld{}, mWorld())
         PROC_MatrixTranspose(ConstantBuffer{}, ConstantBuffer.mView{}, mView())         PROC_MatrixTranspose(ConstantBuffer{}, ConstantBuffer.mView{}, mView())
Line 122: Line 148:
         SYS ID3D11DeviceContext.UpdateSubresource%, pImmediateContext%, pConstantBuffer%, \         SYS ID3D11DeviceContext.UpdateSubresource%, pImmediateContext%, pConstantBuffer%, \
         \                                           0, NULL, ConstantBuffer{}, 0, 0         \                                           0, NULL, ConstantBuffer{}, 0, 0
 +</code>
tutorial_204_20-_203d_20spaces.1522502387.txt.gz · Last modified: 2024/01/05 00:16 (external edit)