Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ OpenGL drawing a room

Tags:

c++

opengl

I'm trying to draw a square room in openGL, and I have this:

void drawWalls()
{

glColor3f(1,0,0);
glPushMatrix();
//glRotatef(0,0,0,1);
//glScalef(2,1,2);
glBegin(GL_QUADS);
/* Floor */
glVertex3f(-1,-1,-1);
glVertex3f(1,-1,-1);
glVertex3f(1,-1,1);
glVertex3f(-1,-1,1);
/* Ceiling */
glVertex3f(-1,1,-1);
glVertex3f(1,1,-1);
glVertex3f(1,1,1);
glVertex3f(-1,1,1);
    /* Walls */
glVertex3f(-1,-1,1);
glVertex3f(1,-1,1);
glVertex3f(1,1,1);
glVertex3f(-1,1,1);

glVertex3f(-1,-1,-1);
glVertex3f(1,-1,-1);
glVertex3f(1,1,-1);
glVertex3f(-1,1,-1);

glVertex3f(1,1,1);
glVertex3f(1,-1,1);
glVertex3f(1,-1,-1);
glVertex3f(1,1,-1);

glVertex3f(-1,1,1);
glVertex3f(-1,-1,1);
glVertex3f(-1,-1,-1);
glVertex3f(-1,1,-1);
glEnd();

glPopMatrix();

}

For whatever reasons, it's not drawing all of my sides! I've looked through my vector and it appears to be correct... But when you look inside, you see this:

walls

What am I doing wrong? Or if possible, is there a better way to draw a room rather than this? Thanks

like image 604
Stupid.Fat.Cat Avatar asked Jan 28 '26 03:01

Stupid.Fat.Cat


1 Answers

Your floor is defined clockwise, while your ceiling is defined counter clockwise (concerning the normals I implied from your description). When culling is enabled every quad that faces away from the camera isn't drawn.

In order to fix this, the floor has to be defined like this:

(-1, -1, -1)
(-1, -1, 1)
(1, -1, 1)
(1, -1, -1)

Do the same with your other walls that don't show up. Just define them counter clockwise, too.

like image 102
Uli Holtmann Avatar answered Jan 29 '26 18:01

Uli Holtmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!