Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: OpenGL cubes some faces see-through?  (Read 7405 times)

0 Members and 1 Guest are viewing this topic.

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
OpenGL cubes some faces see-through?
« on: January 29, 2011, 01:54:52 am »
This might not be the right place to ask this, but I am really frustrated at the moment.
I have some OpenGL code that draws a cube. The problem is that some of the faces end up being partially see-through, or not visible at all from the outside. When I move inside the cube, all faces can be seen, but a second cube I placed can be seen through them.
I tried making the faces drawn anticlockwise, clockwise, I tried setting glFrontFace() to GL_CW and GL_CCW, and it changes absolutely nothing.

This exact code is also used in another of my projects and it displays fine!
What's going on?
Here is the code:
Code: [Select]

const float HALFSIZE = 8.f;
glBegin(GL_QUADS);
  glTexCoord2f(0.f, 0.f); glVertex3f(-HALFSIZE, -HALFSIZE, -HALFSIZE);
  glTexCoord2f(0.f, 1.f); glVertex3f(-HALFSIZE,  HALFSIZE, -HALFSIZE);
  glTexCoord2f(1.f, 1.f); glVertex3f( HALFSIZE,  HALFSIZE, -HALFSIZE);
  glTexCoord2f(1.f, 0.f); glVertex3f( HALFSIZE, -HALFSIZE, -HALFSIZE);

  glTexCoord2f(0.f, 0.f); glVertex3f(-HALFSIZE, -HALFSIZE, HALFSIZE);
  glTexCoord2f(0.f, 1.f); glVertex3f(-HALFSIZE,  HALFSIZE, HALFSIZE);
  glTexCoord2f(1.f, 1.f); glVertex3f( HALFSIZE,  HALFSIZE, HALFSIZE);
  glTexCoord2f(1.f, 0.f); glVertex3f( HALFSIZE, -HALFSIZE, HALFSIZE);

  glTexCoord2f(0.f, 0.f); glVertex3f(-HALFSIZE, -HALFSIZE, -HALFSIZE);
  glTexCoord2f(0.f, 1.f); glVertex3f(-HALFSIZE,  HALFSIZE, -HALFSIZE);
  glTexCoord2f(1.f, 1.f); glVertex3f(-HALFSIZE,  HALFSIZE,  HALFSIZE);
  glTexCoord2f(1.f, 0.f); glVertex3f(-HALFSIZE, -HALFSIZE,  HALFSIZE);

  glTexCoord2f(0.f, 0.f); glVertex3f(HALFSIZE, -HALFSIZE, -HALFSIZE);
  glTexCoord2f(0.f, 1.f); glVertex3f(HALFSIZE,  HALFSIZE, -HALFSIZE);
  glTexCoord2f(1.f, 1.f); glVertex3f(HALFSIZE,  HALFSIZE,  HALFSIZE);
  glTexCoord2f(1.f, 0.f); glVertex3f(HALFSIZE, -HALFSIZE,  HALFSIZE);

  //this is the magenta face
  glTexCoord2f(0.f, 0.f); glVertex3f(-HALFSIZE, -HALFSIZE,  HALFSIZE);
  glTexCoord2f(0.f, 1.f); glVertex3f(-HALFSIZE, -HALFSIZE, -HALFSIZE);
  glTexCoord2f(1.f, 1.f); glVertex3f( HALFSIZE, -HALFSIZE, -HALFSIZE);
  glTexCoord2f(1.f, 0.f); glVertex3f( HALFSIZE, -HALFSIZE,  HALFSIZE);

  //This is the cyan face
  glTexCoord2f(0.f, 0.f); glVertex3f(-HALFSIZE, HALFSIZE,  HALFSIZE);
  glTexCoord2f(0.f, 1.f); glVertex3f(-HALFSIZE, HALFSIZE, -HALFSIZE);
  glTexCoord2f(1.f, 1.f); glVertex3f( HALFSIZE, HALFSIZE, -HALFSIZE);
  glTexCoord2f(1.f, 0.f); glVertex3f( HALFSIZE, HALFSIZE,  HALFSIZE);
glEnd();

And some screenshots of the problem (the faces have been coloured for your convenience - magenta is the front of the cube, cyan is the back):
http://oi56.tinypic.com/2sb6102.jpg (magenta should be in front, it's not)
http://oi55.tinypic.com/11tyfb8.jpg
http://oi56.tinypic.com/21jajp4.jpg
http://oi52.tinypic.com/5x4k0i.jpg (cyan is always in front. This is looking at the back - it looks as it should)
Note that the only face displaying correctly is the cyan one. What's different about it?
Thanks for any help, I'm really stumped by this.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
OpenGL cubes some faces see-through?
« Reply #1 on: January 29, 2011, 02:16:39 am »
glDisable(GL_BLEND);

could be a blending issue, not sure tho!

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
OpenGL cubes some faces see-through?
« Reply #2 on: January 29, 2011, 02:25:28 am »
No, that didn't help :(

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
OpenGL cubes some faces see-through?
« Reply #3 on: January 29, 2011, 03:27:01 am »
I managed to half-fix it. I have a class for rendering, and the OpenGL initialisation stuff was in the constructor, andf I have a seperate draw method called by the game loop. For some reason anything called in the constructor is forgotten about when it comes to the draw method. I have to put the OpenGL initialisation in the draw method, which is called each frame. Why is this? Is there any way to only call the initialisation once?

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
OpenGL cubes some faces see-through?
« Reply #4 on: January 29, 2011, 03:37:34 am »
Quote from: "tntexplosivesltd"
I managed to half-fix it. I have a class for rendering, and the OpenGL initialisation stuff was in the constructor, andf I have a seperate draw method called by the game loop. For some reason anything called in the constructor is forgotten about when it comes to the draw method. I have to put the OpenGL initialisation in the draw method, which is called each frame. Why is this? Is there any way to only call the initialisation once?

Try setting your Window's PreserveOpenGLStates to true.

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
OpenGL cubes some faces see-through?
« Reply #5 on: January 29, 2011, 07:16:25 am »
Is there any way to do it without the increased CPU load?

l0calh05t

  • Full Member
  • ***
  • Posts: 200
    • View Profile
OpenGL cubes some faces see-through?
« Reply #6 on: January 29, 2011, 11:06:09 am »
Yes: When preserveopenglstates is enabled, every draw that SFML2 does is wrapped with popping/pushing all opengl state. You can instead wrap your own opengl code (which is probably all in one place instead of many draw calls) with pushing/popping all opengl state. Much more efficient. see: http://www.sfml-dev.org/forum/viewtopic.php?t=1480&highlight=state

Lynix

  • Sr. Member
  • ****
  • Posts: 403
    • View Profile
OpenGL cubes some faces see-through?
« Reply #7 on: January 29, 2011, 11:52:58 am »
What about glEnable(GL_DEPTH_TEST); ?

l0calh05t

  • Full Member
  • ***
  • Posts: 200
    • View Profile
OpenGL cubes some faces see-through?
« Reply #8 on: January 29, 2011, 12:08:57 pm »
Quote from: "Lynix"
What about glEnable(GL_DEPTH_TEST); ?


Is that directed at my post? If yes, this is covered by

glPushAttrib(/* ... */  | GL_ENABLE_BIT  | /* ... */ );

respectively

glPopAttrib();

Lynix

  • Sr. Member
  • ****
  • Posts: 403
    • View Profile
OpenGL cubes some faces see-through?
« Reply #9 on: January 29, 2011, 02:47:54 pm »
Nope, i think he doesn't have a depth buffer, or the depth test is not enabled

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
OpenGL cubes some faces see-through?
« Reply #10 on: January 30, 2011, 07:40:41 am »
I did have it enabled, OpenGL just wasn't "remembering" it.

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
OpenGL cubes some faces see-through?
« Reply #11 on: February 03, 2011, 10:33:12 am »
Hmm... Even calling PreserveOpenGLStates didn't work. Can anyone help?

l0calh05t

  • Full Member
  • ***
  • Posts: 200
    • View Profile
OpenGL cubes some faces see-through?
« Reply #12 on: February 03, 2011, 10:54:40 am »
Quote from: "tntexplosivesltd"
Hmm... Even calling PreserveOpenGLStates didn't work. Can anyone help?


You are either using some state, which SFML uses but does not preserve (unlikely), or you are not setting your states correctly in the first place / doing something wrong.

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
OpenGL cubes some faces see-through?
« Reply #13 on: February 03, 2011, 11:10:19 am »
My problem is that I have the lighting stuff  in the constructor of my rendering class. The polygon drawing stuff is in another method, which is called once each frame by the main loop. The initialisation stuff is forgotten about after the constructor has finished. Where do the push and pop attrib calls go in my class? My class has no SFML stuff in it, but some SFML stuff takes place in the main loop, much after the constructor is called.

By the way, I am using SFML 1.6 :wink:

 

anything