C++ 如何在OpenGL中用柏林噪声生成程序地形?
我需要在OpenGL中使用噪波(使用柏林噪波)生成程序地形。每次应用程序运行新地形时,都需要使用新种子生成。(不要使用外部库。)为噪声地形创建类时是否需要方法/要求。我需要调用哪些函数/计算以及调用顺序 PS:我使用Visual Studio 2019C++ 如何在OpenGL中用柏林噪声生成程序地形?,c++,opengl,visual-studio-2019,terrain,C++,Opengl,Visual Studio 2019,Terrain,我需要在OpenGL中使用噪波(使用柏林噪波)生成程序地形。每次应用程序运行新地形时,都需要使用新种子生成。(不要使用外部库。)为噪声地形创建类时是否需要方法/要求。我需要调用哪些函数/计算以及调用顺序 PS:我使用Visual Studio 2019 // Copy the array data into a float array, and scale and offset the heights. mHeightmap.resize(NumRows * NumCols, 0);
// Copy the array data into a float array, and scale and offset the heights.
mHeightmap.resize(NumRows * NumCols, 0);
for( int i = 0; i < NumRows * NumCols; ++i)
{
mHeightmap[i] = (float)in[i] * HeightScale;
}
// A height for each vertex
{
std::vector<unsigned char> in(NumRows * NumCols);
// Open the file.
std::ifstream inFile;
inFile.open(heightmapName.c_str(), std::ios_base::binary);
if (inFile)
{
// Read the RAW bytes.
inFile.read((char*)&in[0], (std::streamsize)in.size());
// Done with file.
inFile.close();
}
// Copy the array data into a float array, and scale and offset the heights.
mHeightmap.resize(NumRows * NumCols, 0);
for( int i = 0; i < NumRows * NumCols; ++i)
{
mHeightmap[i] = (float)in[i] * HeightScale;
}
void Terrain::CreateVAO()
{
std::vector<GLfloat> vertices;
vertices.reserve(NumCols * NumRows * 8);
float invTwoDX = 1.0f / (2.0f * CellSpacing);
float invTwoDZ = 1.0f / (2.0f * CellSpacing);
//vertices
for ( int z = 0; z < NumRows; z++)
{
for ( int x = 0; x < NumCols; x++)
{
//vertex data
int i = z * NumCols + x;
vertices.push_back((float)x*CellSpacing);
vertices.push_back(mHeightmap[i]);
vertices.push_back((float)z * CellSpacing);
//normal data
glm::vec3 _N = { 0.0f,1.0f, 0.0f };
if(z >= 1 && z < NumRows -1 && x >= 1 && z < NumCols - 1)
{
float t = mHeightmap[(z - 1) * NumCols + x];
float b = mHeightmap[(z + 1) * NumCols + x];
float l = mHeightmap[z * NumCols + x - 1];
float r = mHeightmap[z * NumCols + x + 1];
glm::vec3 tanZ(0.0f, (b - t) * invTwoDZ, 1.0f);
glm::vec3 tanX(1.0f, (r - l) * invTwoDX, 0.0f);
glm::vec3 _C, _N;
_C = glm::cross(tanZ, tanX);
_N = glm::normalize(_C);
}
vertices.push_back(_N.x);
vertices.push_back(_N.y);
vertices.push_back(_N.z);
vertices.push_back((float)x);
vertices.push_back((float)z);
}
}
std::vector<GLuint> indices;
vertices.reserve((NumCols-1)*(NumRows -1)*6);
//indices
for ( int z = 0; z < NumRows-1; z++)
{
for ( int x = 0; x < NumCols-1; x++)
{
GLint a = z * NumCols + x;
GLint b = (z +1) * NumCols + x;
GLint c = z * NumCols + (x+1);
GLint d = (z+1) * NumCols + (x+1);
indices.push_back(c);
indices.push_back(a);
indices.push_back(b);
indices.push_back(c);
indices.push_back(b);
indices.push_back(d);
}
}
indexcount = indices.size();
GLuint VBO, EBO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), GL_STATIC_DRAW);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(GLfloat), vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(
0,
3,
GL_FLOAT,
GL_FALSE,
8 * sizeof(GLfloat), //Strude of the single vertex(pos)
(GLvoid*)0); //Offset from beginning of Vertex
glEnableVertexAttribArray(0);
glVertexAttribPointer(
1,
3,
GL_FLOAT,
GL_FALSE,
8 * sizeof(GLfloat), //Strude of the single vertex(pos+color)
(GLvoid*)(3 * sizeof(GLfloat))); //Offset from beginning of Vertex
glEnableVertexAttribArray(1);
glVertexAttribPointer(
2,
2, //2 float component for coordinates
GL_FLOAT,
GL_FALSE,
8 * sizeof(GLfloat), //Strude of the single vertex(pos+color+texture)
(GLvoid*)(6 * sizeof(GLfloat)));//Offset from beginning of Vertex
glEnableVertexAttribArray(2);
//将数组数据复制到浮点数组中,并缩放和偏移高度。
mHeightmap.resize(NumRows*NumCols,0);
对于(int i=0;i=1&&z=1&&z
我不确定是否在您的代码中看到柏林噪波的用法。请尝试此轻量级、易于集成的库:
里面有柏林木和很多其他有用的东西,比如可视化工具。
用法非常简单
noise.GetNoise((float)x,(float)y);
你可以把它插入你的高度函数中你的作品在哪里?我刚把它放上去,除非你想让我用拉链把它寄出去file@Loki佐藤,你的问题太宽泛了。就好像你在问“我该怎么做才能建立自己的网站?这是我的html代码…”您应该将问题缩小到更简单的范围,并提出更具体的问题。要做到这一点,您可以编辑自己的问题。您的问题似乎更多地是关于如何使用柏林噪声函数。如果是这样,您可能希望澄清这些函数是从哪个库发布的,以及他们的手册中没有说明哪些内容导致您提出问题n关于堆栈溢出(S.O.)您的研究工作在哪里?