Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw multiple cubes in OpenGL

Tags:

opengl

I want to draw several cubes using glutSolidCube in some points in space. The examples I have found just call glutSolidCube and it works, but the only way a cube gets drawn for me is if the line is enclosed in glBegin(GL_POLYGON), which isn't required in the examples I've seen, and I only get one cube instead of several. What I have is:

glColor3f(1, 0, 0);
glLoadIdentity();
glTranslatef(5,2,1);
glutSolidCube(1);


glLoadIdentity();
glTranslatef(10,8,0);
glutSolidCube(1);

glLoadIdentity();
glTranslatef(3,7,9);
glutSolidCube(1);

glLoadIdentity();
glTranslatef(1,4,6);
glutSolidCube(1);

When I run this nothing happens. I know there's not a problem with the points being outside my view because if I draw vertices at the same points, I can see them. As far as I can tell from the examples and documentation I've read, I'm not doing anything incorrect. Can someone tell me what I'm doing wrong or give me a snippet of code that draws multiple cubes?

like image 305
Bob Avatar asked Apr 30 '12 06:04

Bob


2 Answers

Try this:

glColor3f(1, 0, 0);
glPushMatrix();
glTranslatef(5,2,1);
glutSolidCube(1);
glPopMatrix();

glPushMatrix();
glTranslatef(10,8,0);
glutSolidCube(1);
glPopMatrix();

glPushMatrix();
glTranslatef(3,7,9);
glutSolidCube(1);
glPopMatrix();

glPushMatrix();
glTranslatef(1,4,6);
glutSolidCube(1);
glPopMatrix();

Without re-setting the model view matrix with glLoadIdentity(). Note that to start with you need to call glOrtho() or glPerspective() to set the camera once.

like image 153
John Alexiou Avatar answered Sep 19 '22 14:09

John Alexiou


#include <GL/glut.h>

void init()
{
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_COLOR_MATERIAL);
}

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

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLint viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);
    double aspect = (double)viewport[2] / (double)viewport[3];
    gluPerspective(60, aspect, 1, 100);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // move back a bit
    glTranslatef( 0, 0, -35 );

    static float angle = 0;
    angle += 1.0f;

    glPushMatrix();
        glTranslatef(0,0,0);
        glRotatef(angle, 0.1, 0.2, 0.5);
        glColor3ub(255,0,255);
        glutSolidCube(5);
    glPopMatrix();

    glPushMatrix();
        glTranslatef(10,-10,0);
        glRotatef(angle, 0.1, 0.2, 0.5);
        glColor3ub(255,0,0);
        glutSolidCube(5);
    glPopMatrix();

    glPushMatrix();
        glTranslatef(10,10,0);
        glRotatef(angle, 0.1, 0.2, 0.5);
        glColor3ub(0,255,0);
        glutSolidCube(5);
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-10,10,0);
        glRotatef(angle, 0.1, 0.2, 0.5);
        glColor3ub(0,0,255);
        glutSolidCube(5);
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-10,-10,0);
        glRotatef(angle, 0.1, 0.2, 0.5);
        glColor3ub(255,255,0);
        glutSolidCube(5);
    glPopMatrix();

    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
}

void timer(int extra)
{
    glutPostRedisplay();
    glutTimerFunc(16, timer, 0);
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    glutCreateWindow("CUBES");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutTimerFunc(0, timer, 0);

    init();

    glutMainLoop();
    return 0;
}
like image 34
genpfault Avatar answered Sep 18 '22 14:09

genpfault