我正在使用GLTools库中的GLBatch来绘制四面体。我想在不同位置多次绘制四面体以绘制Sierpinski-Tetrahedron。创建几何体并绘制锥体对四面体的一个实例工作正常,但我想在不同位置多次绘制四面体。多次调用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();
答案 0 :(得分:1)
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();
}
}