在不同位置绘制多次glbatch

Drawing GLBatch multiple times at different locations

本文关键字:glbatch 绘制 位置      更新时间:2023-10-16

我正在使用Gltools库中的GlBatch绘制四面体。我想在不同位置多次绘制四面体,以绘制sierpinski四面体。创建几何形状并绘制锥体在四面体的一个实例中正常工作,但是我想在不同位置多次绘制四面体。多次调用geometry.Draw()(具有不同的模型视图矩阵(不起作用。锥仍只绘制一次。

如何多次绘制glbatch?

这是我的代码:

GLBatch geometry;
// This is called once on initialization.    
void CreateTetrahedron()
{
    geometry.Begin(GL_TRIANGLE_STRIP, 6);
    geometry.Color4f(1, 0, 0, 1);
    geometry.Vertex3f(0, 1, 0);
    geometry.Color4f(1, 0, 0, 1);
    geometry.Vertex3f(-0.5f, 0, 0.5f);
    geometry.Color4f(1, 0, 0, 1);
    geometry.Vertex3f(0.5f, 0, 0.5f);
    if (recursionLevel < 3)
    {
        geometry.Color4f(0, 1, 0, 1);
        geometry.Vertex3f(0, 0, -0.7f);
        geometry.Color4f(0, 0, 1, 1);
        geometry.Vertex3f(0, 1, 0);
        geometry.Color4f(1, 1, 1, 1);
        geometry.Vertex3f(-0.5f, 0, 0.5f);
    }
    geometry.End();
}
// This is where I want to draw it. The function is recursive, and thus the geometry should be drawn multiple times with different matrices.
void DrawSierpinskiTetrahedron(UINT32 level)
{
    if (level == 0)
    {
        geometry.Draw();
    }
    else
    {
        modelViewMatrix.PushMatrix();
        modelViewMatrix.Scale(0.5f, 0.5f, 0.5f);
        modelViewMatrix.PushMatrix();
        modelViewMatrix.Translate(0, 1, 0);
        DrawSierpinskiTetrahedron(level - 1);
        modelViewMatrix.PopMatrix();
        modelViewMatrix.PushMatrix();
        modelViewMatrix.Translate(-0.5f, 0, 0.5f);
        DrawSierpinskiTetrahedron(level - 1);
        modelViewMatrix.PopMatrix();
        modelViewMatrix.PushMatrix();
        modelViewMatrix.Translate(0.5f, 0, 0.5f);
        DrawSierpinskiTetrahedron(level - 1);
        modelViewMatrix.PopMatrix();
        modelViewMatrix.PushMatrix();
        modelViewMatrix.Translate(0, 0, -0.7f);
        DrawSierpinskiTetrahedron(level - 1);
        modelViewMatrix.PopMatrix();
        modelViewMatrix.PopMatrix();
    }
}
void RenderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    modelViewMatrix.PushMatrix();
    // Camera Translation
    modelViewMatrix.Translate(xTrans, yTrans, zTrans);
    // Model Translation, Rotatation und Scale
    glm::mat4 trans = glm::translate(glm::mat4(), translation);
    modelViewMatrix.MultMatrix(glm::value_ptr(trans));
    glm::mat4 rot = glm::mat4_cast(rotation);
    modelViewMatrix.MultMatrix(glm::value_ptr(rot));
    glm::mat4 sc = glm::scale(glm::mat4(), scale);
    modelViewMatrix.MultMatrix(glm::value_ptr(sc));
    // Set the shader for rendering
    shaderManager.UseStockShader(GLT_SHADER_FLAT_ATTRIBUTES, transformPipeline.GetModelViewProjectionMatrix());
    if (recursionLevel < 0)
    {
        recursionLevel = 0;
    }
    if (recursionLevel > 5)
    {
        recursionLevel = 5;
    }
    DrawSierpinskiTetrahedron(recursionLevel);
    gltCheckErrors(0);
    modelViewMatrix.PopMatrix();
    TwDraw();
    glutSwapBuffers();
    glutPostRedisplay();
}
// Initialize rendering context
void SetupRC()
{
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glFrontFace(GL_CW);
    glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
    shaderManager.InitializeStockShaders();
    transformPipeline.SetMatrixStacks(modelViewMatrix,projectionMatrix);
    // Create the geometry
    CreateTetrahedron();
    InitGUI();
}

更新:

进行以下操作不会缩放四面体。

modelViewMatrix.PushMatrix();
modelViewMatrix.Scale(1000, 1000, 1000);
geometry.Draw();
modelViewMatrix.PopMatrix();

glmatrixstack是一个类,可为堆栈的顶部矩阵提供矩阵堆栈和操作。

类GlshaderManager可以为着色器的均匀变量加载着着色器和手柄设置。

着色器程序通过模型视图矩阵转换网格的顶点坐标。这导致可以将MSEH渲染到不同位置的不同位置。如果更改了模型视图矩阵,则必须将其设置为阴暗器程序中的相应统一变量,以使更改起作用。这可以通过在绘制网格之前调用GLShaderManager::UseStockShader来完成。

这样调整您的代码:

void DrawSierpinskiTetrahedron(UINT32 level)
{
    if (level == 0)
    {
        shaderManager.UseStockShader(GLT_SHADER_FLAT_ATTRIBUTES, transformPipeline.GetModelViewProjectionMatrix());
        geometry.Draw();
    }
    else
    {
        modelViewMatrix.PushMatrix();
        modelViewMatrix.Scale(0.5f, 0.5f, 0.5f);
        modelViewMatrix.PushMatrix();
        modelViewMatrix.Translate(0, 1, 0);
        DrawSierpinskiTetrahedron(level - 1);
        modelViewMatrix.PopMatrix();
        modelViewMatrix.PushMatrix();
        modelViewMatrix.Translate(-0.5f, 0, 0.5f);
        DrawSierpinskiTetrahedron(level - 1);
        modelViewMatrix.PopMatrix();
        modelViewMatrix.PushMatrix();
        modelViewMatrix.Translate(0.5f, 0, 0.5f);
        DrawSierpinskiTetrahedron(level - 1);
        modelViewMatrix.PopMatrix();
        modelViewMatrix.PushMatrix();
        modelViewMatrix.Translate(0, 0, -0.7f);
        DrawSierpinskiTetrahedron(level - 1);
        modelViewMatrix.PopMatrix();
        modelViewMatrix.PopMatrix();
    }
}