hanna
hanna

Reputation: 59

creating a cube in 3d

I am trying to add walls to my checkboard floor so that they form a cube, I have the bottom already but I am having difficulties to draw the back side or the walls.

Here is my code:

class Checkerboard {
  int displayListId;
  int width;
  int depth;
public:
  Checkerboard(int width, int depth): width(width), depth(depth) {}
  double centerx() {return width / 2;}
  double centerz() {return depth / 2;}
  void create() {
    displayListId = glGenLists(1);
    glNewList(displayListId, GL_COMPILE);
    GLfloat lightPosition[] = {4, 3, 7, 1};
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
    glBegin(GL_QUADS);
    glNormal3d(0, 1, 0);
    for (int x = 0; x < width - 1; x++) {
      for (int z = 0; z < depth - 1; z++) {
        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,
                     (x + z) % 2 == 0 ? RED : WHITE);
        glVertex3d(x, 0, z);
        glVertex3d(x+1, 0, z);
        glVertex3d(x+1, 0, z+1);
        glVertex3d(x, 0, z+1);
      }
    }
    glEnd();
    glEndList();
  }
  void draw() {
    glCallList(displayListId);
  }
}

;

I am trying to add the following code:

glColor3f(1.0f,1.0f,0.0f);    // Color Yellow
    glVertex3d( 1.0f,-1.0f,-1.0f);    // Top Right Of The Quad (Back)
    glVertex3d(-1.0f,-1.0f,-1.0f);    // Top Left Of The Quad (Back)
    glVertex3d(-1.0f, 1.0f,-1.0f);    // Bottom Left Of The Quad (Back)
    glVertex3d( 1.0f, 1.0f,-1.0f);    // Bottom Right Of The Quad (Back)

Can somebody point out what I am doing wrong?

Upvotes: 0

Views: 229

Answers (2)

Stephan van den Heuvel
Stephan van den Heuvel

Reputation: 2138

Change the code to:

glColor3f(1.0f,1.0f,0.0f);    // Color Yellow
glVertex3d( 1.0f, 1.0f,-1.0f);    // Top Right Of The Quad (Back)
glVertex3d( -1.0f, 1.0f,-1.0f);    // Top Left Of The Quad (Back)
glVertex3d( -1.0f, -1.0f,-1.0f);    // Bottom Left Of The Quad (Back)
glVertex3d( 1.0f, -1.0f,-1.0f);    // Bottom Right Of The Quad (Back)

Note your comments were in the right winding order (CCW) but your vertices were not (They were clock wise). OpenGL by default will cull them (not show them) because it thinks they are back faces if culling is enabled.

Upvotes: 0

neodelphi
neodelphi

Reputation: 2786

Maybe you are effectively drawing the face you're trying to add, but you can't see it because of backface culling. Try glDisable(GL_CULL_FACE); before drawing you quads, or try to change the order of the vertices.

Note: if you disable backface culling, consider it as a temporary workaround to debug your program. Backface culling is an important optimisation and it should be enabled in most situations.

Upvotes: 1

Related Questions