gpt4 book ai didi

c++ - 使用 Frank Luna 的技术创建 3d 平面 : what are sinf and cosf?

转载 作者:太空宇宙 更新时间:2023-11-04 15:10:28 25 4
gpt4 key购买 nike

我正在创建一个 3d 平面,它位于 x 轴和 z 轴上,山丘在 y 轴上延伸。大部分代码如下所示:

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));
}

我的问题与 cosf 和 sinf 有关。我熟悉三角函数,我了解 sin、cosine 和 tangent,但我不熟悉 cosf 和 sinf 以及它们的作用。从这个例子来看,他们与找到一个 y 值有很大关系。

最佳答案

cosfsinf 只是 cossinfloat 版本.正常的 cossin 函数返回 double 值而不是 float。请注意,所有这些函数都以弧度而不是度数为单位。

以上述方式组合,它们给出了一个看起来有点像山脉的景观,如 this plot .

关于c++ - 使用 Frank Luna 的技术创建 3d 平面 : what are sinf and cosf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2709092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com