Opengl 和 C++ 绘制多个立方体

Opengl and c++ drawing multiple cubes

所以我可以画一个立方体,但是我想画多个立方体,我不知道怎么画。你们能帮帮我吗? 这是我如何绘制立方体的代码:

#include <GL/freeglut.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);

    const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
    const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
    const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
    const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
    const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
    const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
    const GLfloat high_shininess[] = { 100.0f };
    glEnable(GL_LIGHT0);glEnable(GL_LIGHTING);
    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,2.0f,100.0f);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_CULL_FACE);glCullFace(GL_BACK);
}

static void display(void)
{
    glLoadIdentity(); 
    glPushMatrix();
        glTranslatef(0.0,0.0,-10); 
        glRotatef(60,1,0,0);
        glRotatef(60,0,1,0); 
        glutSolidCube(2); 

        glLoadIdentity();
        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("Lab 1: Hello OpenGL World !");
    glutDisplayFunc(display);
    glutIdleFunc(idle);
    initGL(width, height);
    glutMainLoop();
    return EXIT_SUCCESS;
}

我需要像我正在绘制的那样绘制多个立方体。我尝试使用 for 但我今天才开始使用 opengl,不知道自己在做什么。我尝试了很多事情并且非常恼火。你也推荐什么书吗?

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

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

你好像在同一个位置画了两个立方体,所以第二个立方体画在第一个上面,试试把代码改成

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

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

"I just started opengl today"

完全理解 OpenGL 需要一段时间,但如果您查阅了您的研究 material,您应该会发现代码中的一些错误。

您需要定义一个 glutReshapeFunc 函数:没有这个函数,您的 视口是未定义的 @KevinSpaghetti 也对你的 glutDisplayFunc 有一个错误:所有立方体都绘制在同一位置 并且未指定 模型视图矩阵

这是实际有效的代码。

#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>

/*******************************************************************************
********************************************************************************
********************************************************************************/

struct { int width = 640; int height = 480; } window;

/*******************************************************************************
********************************************************************************
********************************************************************************/

void resize(int width, int height)
{
  glViewport(0, 0, (window.width = width), (window.height = height));
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45, ((GLdouble)window.width / window.height), 2, 100);
  glMatrixMode(GL_MODELVIEW);
}

/*******************************************************************************
********************************************************************************
********************************************************************************/

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(0, 0, 30, 0, 0, 0, 0, 1, 0);
  glRotatef(-30, 0, 1, 0); // <----- rotate entire scene by 30 degrees
  glColor3f(0.5f, 0.0f, 0.0f);

  for (int i = 0; i < 3; ++i) // <----- draw three cubes
  {
    glPushMatrix();
      glRotatef(60, 1.0, 0.0, 0.0);
      glRotatef(60, 0.0, 0.1, 0.0);
      glutSolidCube(5);
    glPopMatrix();
    glTranslatef(0.0, 0.0, -10.0f);
  }

  glFlush();
}

/*******************************************************************************
********************************************************************************
********************************************************************************/

void initGL(void)
{
  glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LESS);
  glEnable(GL_CULL_FACE);
  glCullFace(GL_BACK);
  glFrontFace(GL_CCW);

  const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
  const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
  const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
  const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
  const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
  const GLfloat high_shininess[] = { 100.0f };

  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  glLightfv(GL_LIGHT0, GL_POSITION, light_position);

  glEnable(GL_COLOR_MATERIAL);
  glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
}

/*******************************************************************************
********************************************************************************
********************************************************************************/

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

  glutInit(&argc, argv);
  glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
  glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);

  glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
  glutInitWindowSize(window.width, window.height);
  glutInitWindowPosition(10, 10);
  glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE);
  glutCreateWindow("Lab 1: Hello OpenGL World !");

  if (glewInit() != GLEW_OK) exit(EXIT_FAILURE);
  glutReshapeFunc(resize);
  glutDisplayFunc(display);

  initGL();
  glutMainLoop();

  std::cout << "...it worked!";

  //.....................

  return 0;
}

我会推荐:OpenGL Programming Guide。它是旧的 OpenGl 但仍然相关。