[英]Creating a 3d plane using Frank Luna's technique: what are sinf and cosf?
I am creating a 3d plane that lies on the x and z axis, and has hills that extend on the y axis. 我正在创建一个3d平面,该平面位于x和z轴上,并且具有在y轴上延伸的山丘。 The bulk of the code looks like this:
大部分代码如下所示:
float PeaksAndValleys::getHeight(float x, float z)const
{
return 0.3f*( z*sinf(0.1f*x) + x*cosf(0.1f*z) );
}
void PeaksAndValleys::init(ID3D10Device* device, DWORD m, DWORD n, float dx)
{
md3dDevice = device;
mNumRows = m;
mNumCols = n;
mNumVertices = m*n;
mNumFaces = (m-1)*(n-1)*2;
// Create the geometry and fill the vertex buffer.
std::vector<Vertex> vertices(mNumVertices);
float halfWidth = (n-1)*dx*0.5f;
float halfDepth = (m-1)*dx*0.5f;
for(DWORD i = 0; i < m; ++i)
{
float z = halfDepth - i*dx;
for(DWORD j = 0; j < n; ++j)
{
float x = -halfWidth + j*dx;
// Graph of this function looks like a mountain range.
float y = getHeight(x,z);
vertices[i*n+j].pos = D3DXVECTOR3(x, y, z);
// Color the vertex based on its height.
if( y < -10.0f )
vertices[i*n+j].color = BEACH_SAND;
else if( y < 5.0f )
vertices[i*n+j].color = LIGHT_YELLOW_GREEN;
else if( y < 12.0f )
vertices[i*n+j].color = DARK_YELLOW_GREEN;
else if( y < 20.0f )
vertices[i*n+j].color = DARKBROWN;
else
vertices[i*n+j].color = WHITE;
}
}
D3D10_BUFFER_DESC vbd;
vbd.Usage = D3D10_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(Vertex) * mNumVertices;
vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = &vertices[0];
HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB));
// Create the index buffer. The index buffer is fixed, so we only
// need to create and set once.
std::vector<DWORD> indices(mNumFaces*3); // 3 indices per face
// Iterate over each quad and compute indices.
int k = 0;
for(DWORD i = 0; i < m-1; ++i)
{
for(DWORD j = 0; j < n-1; ++j)
{
indices[k] = i*n+j;
indices[k+1] = i*n+j+1;
indices[k+2] = (i+1)*n+j;
indices[k+3] = (i+1)*n+j;
indices[k+4] = i*n+j+1;
indices[k+5] = (i+1)*n+j+1;
k += 6; // next quad
}
}
D3D10_BUFFER_DESC ibd;
ibd.Usage = D3D10_USAGE_IMMUTABLE;
ibd.ByteWidth = sizeof(DWORD) * mNumFaces*3;
ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0;
ibd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA iinitData;
iinitData.pSysMem = &indices[0];
HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mIB));
}
My question pertains to the cosf and sinf. 我的问题与cosf和sinf有关。 I am familiar with trigonometry and I understand sin, cosine, and tangent, but I am not familiar with cosf and sinf and what they do.
我熟悉三角函数,也了解罪,余弦和正切,但我不熟悉cosf和sinf及其作用。 From looking at this example, they have a lot to do with finding ay value.
通过查看此示例,他们与寻找ay值有很大关系。
cosf
and sinf
are simply the float
versions of cos
and sin
. cosf
和sinf
只是cos
和sin
的float
形式。 The normal cos
and sin
functions return double
values instead of float
s. 正常的
cos
和sin
函数返回double
float
值而不是float
。 Note that all these functions work in radians, not degrees. 请注意,所有这些功能都以弧度而不是度为单位。
Combined in the way above, they give a landscape that looks somewhat like a mountain range, as in this plot . 在上面的方式相结合,他们给一个景观,看起来有点像一个山脉,在这个情节 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.