GLUT 中的多个对象

Multiple objects in GLUT

我想将一个对象(立方体)乘以某个数,在本例中假设为 25,我有一个立方体的代码,它可以工作,但我不知道如何制作更多的。我是GLUT的新手。

#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>

void initGL(int width, int height)
{

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,2.0f, 100.0f);
 glMatrixMode(GL_MODELVIEW);
}
static void display(void)
{

glLoadIdentity();
glPushMatrix();
    glTranslatef(0.0,0.0,-10);
    glRotatef(60,1,0,0);
    glRotatef(60,0,1,0);
    glutSolidCube(2);
glPopMatrix();

glFlush();
}

static void idle(void)
{
glutPostRedisplay();
}

int main(int argc, char *argv[])
{

int width = 640;
int height = 480;

glutInit(&argc, argv);
glutInitWindowSize(width, height);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

glutCreateWindow("GLUT cube");

glutDisplayFunc(display);
glutIdleFunc(idle);

initGL(width, height);

glutMainLoop();

return EXIT_SUCCESS;

}

如果有更详细的操作指南,欢迎分享。

每次调用glutSolidCube(),它都会绘制一个立方体。如果您希望出现多个立方体,则需要在 display().

的实现中多次调用该函数

(确保在每次调用之间更改模型矩阵!否则,立方体最终会在同一个地方。)