简体   繁体   English

现代OpenGL中的分形树(OpenGL 3.3及以上版本)

[英]Fractal tree in modern OpenGL (OpenGL 3.3 and above)

I need an approach to draw a 3D fractal tree with modern OpenGL. 我需要一种方法来绘制具有现代OpenGL的3D分形树。 Any suggestions? 有什么建议么?

I don't necessarily require full source code, just an idea how to do it. 我不一定需要完整的源代码,只是想知道如何做到这一点。

What I'm trying to do is to make a 3D tree. 我要做的是制作3D树。 In fixed-function OpenGL it's not so difficult to draw a good looking realistic tree, but I don't know how to do it in modern Opengl. 在固定功能的OpenGL中,绘制一个好看的真实树并不是那么困难,但我不知道如何在现代的Opengl中做到这一点。

I thought it would be a good idea to use a cylinder model for every branch and use glm to transform it in the right place and size because in this case I could use the texture coordinates and normals of the model, but I got stuck and I don't know how to do it. 我认为为每个分支使用圆柱模型并使用glm在正确的位置和大小进行变换是个好主意,因为在这种情况下我可以使用模型的纹理坐标和法线,但我卡住了,我不知道怎么做。

Here is fractal 2D tree made in fixed function OpenGL with recursion. 这是在具有递归的固定函数OpenGL中制作的分形2D树。 If anyone interested I can send the entire source code. 如果有兴趣我可以发送完整的源代码。

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

How can I make something like this in modern OpenGL with VAOs and VBOs and shaders? 如何在现代OpenGL中使用VAO和VBO以及着色器制作类似的东西?

在此输入图像描述

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM