C++ 现代OpenGL中的分形树(OpenGL 3.3及以上版本)
我需要一种使用现代OpenGL绘制3D分形树的方法。有什么建议吗? 我不一定需要完整的源代码,只是一个如何做的想法 我想做的是做一棵3D树。在固定函数OpenGL中,绘制一棵好看的真实树并不困难,但我不知道如何在现代OpenGL中做到这一点 我认为对每个分支使用圆柱体模型并使用C++ 现代OpenGL中的分形树(OpenGL 3.3及以上版本),c++,opengl,fractals,glm-math,l-systems,C++,Opengl,Fractals,Glm Math,L Systems,我需要一种使用现代OpenGL绘制3D分形树的方法。有什么建议吗? 我不一定需要完整的源代码,只是一个如何做的想法 我想做的是做一棵3D树。在固定函数OpenGL中,绘制一棵好看的真实树并不困难,但我不知道如何在现代OpenGL中做到这一点 我认为对每个分支使用圆柱体模型并使用glm将其变换到正确的位置和大小是一个好主意,因为在这种情况下,我可以使用模型的纹理坐标和法线,但我被卡住了,我不知道如何做 这是在固定函数OpenGL中通过递归生成的分形2D树。如果有人感兴趣,我可以发送整个源代码 vo
glm
将其变换到正确的位置和大小是一个好主意,因为在这种情况下,我可以使用模型的纹理坐标和法线,但我被卡住了,我不知道如何做
这是在固定函数OpenGL中通过递归生成的分形2D树。如果有人感兴趣,我可以发送整个源代码
void drawTree(int currentDepth, int maxDepth)
{
if (currentDepth > maxDepth)
return;
if (currentDepth <= maxDepth - 2)
{
glColor3d(0.45, 0.2, 0.05);
glLineWidth(10 * static_cast<GLfloat>(pow(TREE_FACTOR, currentDepth)));
}
else
{
glColor3d(0, 0.5, 0);
glLineWidth(30 * static_cast<GLfloat>(pow(TREE_FACTOR, currentDepth)));
}
double lineLen = TREE_LINE_BASE_LEN * pow(TREE_FACTOR, currentDepth);
glBegin(GL_LINES);
glVertex2d(0, 0);
glVertex2d(0, lineLen);
glEnd();
int angle1 = 10 + rand() % 40;
int angle2 = 10 + rand() % 40;
glTranslated(0, lineLen, 0);
glRotated(-angle1, 0, 0, 1);
drawTree(currentDepth + 1, maxDepth);
glRotated(angle1 + angle2, 0, 0, 1);
drawTree(currentDepth + 1, maxDepth);
glRotated(-angle2, 0, 0, 1);
glTranslated(0, -lineLen, 0);
}
void绘图树(int currentDepth,int maxDepth)
{
如果(currentDepth>maxDepth)
返回;
如果(currentDepthQuick idea)
void SudoCreateChild(const mat4x4& m2w_noscale, const int depth)
{
if(depth>=MAX_DEPTH){
return;
}
/// Creates the M2W matrix
mat4x4 m2w=m2w_noscale * mat4x4::identity*(1/depth);
/// Draw the branch
DrawCylinder(m2w);
/// Create the children branches
for(int i=0; i<CHILDREN_NUMBER; ++i){
float angle=(2.0f*PI/CHILDREN_NUMBER)*i;
/// Initial rotation of PI/4 plus equal rotation between siblins
mat4x4 rotation=mat4x4::rotate(angle, vector3(0,1,0))*mat4x4::rotate(PI*0.25, vector3(1,0,0))
/// Size of the branch is 1/depth
mat4x4 translation=mat4x4::translate(vector3(vector3(0,1,0)*(1/(depth+1))));
/// Recursively create the branches
SudoCreateChild(m2w_noscale*translation*rotation, depth+1);
}
}
int main(void)
{
SudoCreateChild(mat4x4::identity, 1);
return 0;
}
void SudoCreateChild(常量mat4x4和m2w_noscale,常量int depth)
{
如果(深度>=最大深度){
返回;
}
///创建M2W矩阵
mat4x4 m2w=m2w_noscale*mat4x4::identity*(1/深度);
///拔树枝
牵引缸(m2w);
///创建子分支
对于(int i=0;i)我被卡住了-我们不知道在哪里和什么上。使用圆柱体模型是一个好主意-但是你当前的方法使用简单的线条。你想将其重写为现代OpenGL,还是想要一种全新的方法?用现代OpenGL绘制简单的线条是完全可能的,也不是那么困难。坦白地说,第一种方法(从概念上讲,最重要的)更改是将代码转换为使用glVertex2dv
——更改后,只需设置和着色器:)缓冲在递归drawTree
中生成的参数,您将获得顶点缓冲区方法但是glVertex2dv
在glBegin/glEnd
对中使用,这些函数已被弃用。我尝试存储生成的参数,并成功绘制了一个简单的带线分形树,但如何使bole thicker和分支变薄了。现在看起来是这样的:你需要将你的参数缓冲到glLineWidth
,并在你的着色器程序中使用它。我已经按照你在代码示例中显示的那样做了,但它给了我这样的信息:对于CHILDREN\u NUMBER=3,你知道为什么会发生这种情况吗?我的错,我忘了连接父矩阵,我已经编辑代码