- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在创建一个 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 值有很大关系。
最佳答案
cosf
和 sinf
只是 cos
和 sin
的 float
版本.正常的 cos
和 sin
函数返回 double
值而不是 float
。请注意,所有这些函数都以弧度而不是度数为单位。
以上述方式组合,它们给出了一个看起来有点像山脉的景观,如 this plot .
关于c++ - 使用 Frank Luna 的技术创建 3d 平面 : what are sinf and cosf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2709092/
为什么我从 sin(x) 和 sinf(x) 得到的值与下面的 (8) 和 (9) 相同?为什么我从下面的 (2) 和 (3) 两个不同的实现中得到不同的 [ x - sinf(x) ] 值?为什么
我很想知道 sinf 函数是否存在于 C++ 中,通过包含 math.h。 在 Qt Creator 中查看我的自动完成时,它似乎没有弹出。这让我想知道是否出于某种原因,它被取出了。谷歌搜索似乎也没有
这个问题在这里已经有了答案: std::expf and std::logf not recognized by gcc 7.2.0 (3 个回答) 关闭3年前。 根据这里的讨论,我有 reporte
我正在尝试通过使用正弦函数创建示例数据并使用 SDL 播放来生成各种频率的音调。 我在用 buffer[sample] = 32767 * sinf( 2 * PI * sample * sound_
我在尝试编译时遇到此错误: error: undefined reference to `sinf' 我已经包含了 math.h,并验证它在其中定义: #include 但是,我在尝试链接到数学库时
我最近读了很多旧的 C 语言,一个常见的习惯用法是简单地向前声明标准库,而不是包含头文件。我想尝试一下,所以不是 #include 我刚刚宣布 float sinf(float); 在文件的顶部。令我
当我在 ARMv7 上编译我的程序时,与在 Windows 上获得的相比,具有数学 sin() 函数的相同 C 代码运行速度要慢得多。我正在使用 -O2 -Wall -Wextra -mfpu=neo
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why doesn't sin() return the correct value here? 我正在构建
我的代码主要处理单精度 float 。对超越函数的调用相当频繁。现在,我正在使用 sin()、cos()、sqrt() 等——接受并返回 double 的函数。当它仅为 x87 编译时,我知道单精度和
我只是想知道自从我在浮点变量中使用 sin 和 cos 以来我的程序会更快,所以我决定以这种方式进行测试: int c; float a; float b = 0.5; /**************
我试图在我的 C 程序中使用 sinf 函数,但它在 MSVC 6.0 下给我一个 undefined reference 错误,但是 sin 工作正常。 这让我很想知道 sin 和 sinf 之间的
我正在创建一个 3d 平面,它位于 x 轴和 z 轴上,山丘在 y 轴上延伸。大部分代码如下所示: float PeaksAndValleys::getHeight(float x, float z)
我是一名优秀的程序员,十分优秀!