Search code examples
c++openglfreeglut

Opengl and c++ drawing multiple cubes


So I am able to draw a cube, but I want to draw multiple cubes, and I don't know how. Can you guys help me? This is the code of how I draw the cube:

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

I need to draw multiple cubes like the one that I am drawing. I tried with a for but I just started opengl today and don't know what I'm doing. I have tried multiple things and got very annoyed. Do you also recommend any book?


Solution

  • "I just started opengl today"

    It will take a while to fully understand OpenGL but if you had consulted your study material, you should have spoted a few errors in you code.

    You need to define a glutReshapeFunc function: without this your viewport is undefined. @KevinSpaghetti is also right your glutDisplayFunc has a bug: all cubes are drawn at the same position and a model-view matrix was not specified.

    Here is a code that actually works.

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

    I would recommend: OpenGL Programming Guide. It is old OpenGl but still relevant.