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

Author Topic: [SOLVED]I've screwed up my depth buffer :S  (Read 5753 times)

0 Members and 1 Guest are viewing this topic.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
[SOLVED]I've screwed up my depth buffer :S
« on: May 04, 2011, 10:51:31 pm »
Ah well I somehow screwed up my depth buffer somehow and I don't know how I did it and even worse, how to reverse it. What the problem is that I can't have any value outside the range 1 to -1 if I want it to show. If z goes outside that range the vertexes won't be rendered.

Here's my test code:
Code: [Select]

// aRenderer just wraps gl* commands.
aRenderer.PushModelView();
aRenderer.LoadModelView();
aRenderer.Translate( 0, 0, -2 ); // Won't show anything!
aRenderer.StartPrimitive( Graphics::Renderer::QuadPrimitive );
aRenderer.AddVertex( 0, 0, 0 );
aRenderer.AddVertex( 1, 0, 0 );
aRenderer.AddVertex( 1, 1, 0 );
aRenderer.AddVertex( 0, 1, 0 );
aRenderer.EndPrimitive();
aRenderer.PopModelView();

Also tried without pushing the matrix, still same effect.

My Init function:
Code: [Select]
// Setup some basic opengl.
glClearColor( 0.f, 0.f, 0.f, 0.f );

glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
glDepthMask( GL_TRUE );
glClearDepth( 1.f );

//glCullFace( GL_BACK );

float width = static_cast< float >( myContext->GetWidth() );
float height = static_cast< float >( myContext->GetHeight() );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 90, width/height , 1, 500 );
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
[SOLVED]I've screwed up my depth buffer :S
« Reply #1 on: May 06, 2011, 03:22:19 pm »
just my 2 cents guess, being within the range of 1 to -1 sounds like you are in the NDC coordinate space. when drawing the vertices (0,0), (1,0), (0,1), (1,1) does it cover a quarter of the screen?

is the projection matrix being reset somewhere accidentally?

regards

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
[SOLVED]I've screwed up my depth buffer :S
« Reply #2 on: May 06, 2011, 06:03:06 pm »
Quote from: "mercurio7891"
just my 2 cents guess, being within the range of 1 to -1 sounds like you are in the NDC coordinate space. when drawing the vertices (0,0), (1,0), (0,1), (1,1) does it cover a quarter of the screen?

Yeh it does

Quote from: "mercurio7891"
is the projection matrix being reset somewhere accidentally?

Hmm must be, but where could that be. Though I don't do it specifically what I know in my code, could it be that sf::RenderWindow does it somewhere?

Think I found it, I had forgotten to switch to model view when I cleared for the first frame( and loaded the identity matrix afterwards )

Though I'm probably doing something else stupid, The back face of my cube is rendered in-front of my front face of the cube.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
[SOLVED]I've screwed up my depth buffer :S
« Reply #3 on: May 06, 2011, 06:54:57 pm »


To show what it looks like

Might also help if I show how I create my polygon mesh:

Code: [Select]
Graphics::PolygonMesh mesh;
Graphics::Face face1;
face1.vertex1.position = Vector3f( -1, 1, 0 );
face1.vertex1.color    = Vector4f( 1, 0, 0, 1 );
face1.vertex2.position = Vector3f( -1, -1, 0 );
face1.vertex2.color    = Vector4f( 0, 1, 0, 1 );
face1.vertex3.position = Vector3f( 1, -1, 0 );
face1.vertex3.color    = Vector4f( 0, 0, 1, 1 );
mesh.AddFace( face1 );

Graphics::Face face2;
face2.vertex1.position = Vector3f( 1, 1, 0 );
face2.vertex1.color    = Vector4f( 1, 0, 0, 1 );
face2.vertex2.position = Vector3f( 1, -1, 0 );
face2.vertex2.color    = Vector4f( 0, 1, 0, 1 );
face2.vertex3.position = Vector3f( -1, 1, 0 );
face2.vertex3.color    = Vector4f( 0, 0, 1, 1 );
mesh.AddFace( face2 );

Matrix44f matrix = Matrix44f::CreateRotateAroundY( 0 );
matrix.SetPosition( Vector3f( 0, 0, 1.f ) );
Graphics::Model::Node *frontNode = myModel.GetGraph().AddNode( matrix );

matrix = Matrix44f::CreateRotateAroundY( 0 );
matrix.SetPosition( Vector3f( 0, 0, -1.f ) );
Graphics::Model::Node *backNode = myModel.GetGraph().AddNode( matrix );

Graphics::Model::Node *polygonNode = myModel.GetGraph().AddNode( mesh );
frontNode->LinkTo( polygonNode );
backNode->LinkTo( polygonNode );


Note: I  tried without my Polygon mesh and without my renderer wrapper for clearing and displaying, still same effect so must be some setting for OpenGL I'm doing wrong?
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
[SOLVED]I've screwed up my depth buffer :S
« Reply #4 on: May 06, 2011, 09:25:27 pm »
Nevermind I fixed it, not sure how though. Seems like I have to do certain things in a perfect order at initiation. :?

I definitely need more experience in this.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
[SOLVED]I've screwed up my depth buffer :S
« Reply #5 on: May 06, 2011, 10:07:38 pm »
I can't really see the image you posted, all I see is the ImageShack Frog in an ice cube saying domain unregistered :D

Glad you fixed it, as to the back face being infront, it might have been due to the depth buffer.

Just a side note, not sure if it is intended, but in the last code snippet, I think the winding order for your face1 and face2 is different. Face1 has a CCW order while Face2 has a CW ordering. By default opengl treats front facing as CCW and back facing as CW, so opengl will cull the backfacing ones (Just a small note, you can ignore this if you already know it) :D

regards

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
[SOLVED]I've screwed up my depth buffer :S
« Reply #6 on: May 06, 2011, 10:13:50 pm »
Quote from: "mercurio7891"
Just a side note, not sure if it is intended, but in the last code snippet, I think the winding order for your face1 and face2 is different. Face1 has a CCW order while Face2 has a CW ordering. By default opengl treats front facing as CCW and back facing as CW, so opengl will cull the backfacing ones (Just a small note, you can ignore this if you already know it) :D


I am aware of how that works though I didn't care when putting in the vertexes. Though now that I think about it, I just added to the init that the back should be culled but they still shows clear as day. Maybe I did something wrong in init again so it doesn't do the culling at all?

My new init:
Code: [Select]
Graphics::Renderer::Renderer( Graphics::Context *aContext )
{
assert( aContext != NULL );
myContext = aContext;

float width = static_cast< float >( myContext->GetWidth() );
float height = static_cast< float >( myContext->GetHeight() );

glEnable( GL_DEPTH_TEST );
glDepthMask( GL_TRUE );
glClearDepth( 1.f );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 90, width/height, 1, 500 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glClearColor( 0.f, 0.f, 0.f, 0.f );
glCullFace( GL_BACK );                // <---- CULLING ;D
}
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
[SOLVED]I've screwed up my depth buffer :S
« Reply #7 on: May 06, 2011, 10:26:44 pm »
From the code
Code: [Select]

Matrix44f matrix = Matrix44f::CreateRotateAroundY( 0 );
matrix.SetPosition( Vector3f( 0, 0, 1.f ) );
Graphics::Model::Node *frontNode = myModel.GetGraph().AddNode( matrix );

matrix = Matrix44f::CreateRotateAroundY( 0 );
matrix.SetPosition( Vector3f( 0, 0, -1.f ) );
Graphics::Model::Node *backNode = myModel.GetGraph().AddNode( matrix );


I guess it means you apply the translation (0, 0, 1) to the vertices to get the front and (0, 0, -1) to the back. However I think this might not work, as you end up getting the same winding order for your front and back faces.

Code: [Select]

------ cube face2
|     |
------ cube face 1

   x  <-- eye


Imagine a cube, if you are looking straight at it, as in the diagram face1 need to have a CCW ordering, while face 2 need to have a CW ordering. So I don't think you can just apply a translation matrix to get the front and back faces. you would probably have to respecify the ordering. Probably either by respecifiying the vertices or just using a index buffer

one quick dirty way to test if culling is the culprit is to disable opengl culling, if the model looks right with culling disable, then it is most likely the culling that is the culprit :D

a side note, you need to also put glEnable(GL_CULL_FACE); to enable it :D

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
[SOLVED]I've screwed up my depth buffer :S
« Reply #8 on: May 06, 2011, 10:32:01 pm »
Well the thing is, the two faces are two triangles. Together they become one quad. Anyway the thing is it looks rights that is what's bothering me. Why is it looking right? It shouldn't since I enabled culling. :?

I do not agree with OpenGL's way to initiate things -.-'

I forgot to enable culling:
Code: [Select]
glEnable( GL_CULL_FACE );
glCullFace( GL_BACK );
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
[SOLVED]I've screwed up my depth buffer :S
« Reply #9 on: May 06, 2011, 10:36:25 pm »
you might not have seen the edit that i did to the previous post as i edited while you replied at the same time :D, anyway you also need to put

Code: [Select]

glEnable(GL_CULL_FACE);


to enable enable culling  :D

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
[SOLVED]I've screwed up my depth buffer :S
« Reply #10 on: May 06, 2011, 10:38:25 pm »
Lolz we both realized it ^^

Works perfectly now and I fixed the second face vertex now. YAY!
And this is as far as I've come in school with 3D(Though we implemented our own renderer for it, decided to import it all from HGE to SFML and use OpenGL instead of making my own)

Instance-Model, polygon mesh, Oct-Tree... Hmm What else is there left for me to explore? Maybe missed something?
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
[SOLVED]I've screwed up my depth buffer :S
« Reply #11 on: May 06, 2011, 10:44:14 pm »
Quote

Lolz we both realized it ^^


haha.

p.s btw for your back cube face, I think you would need to respecify another set of vertices instead of just translating the existing vertices, if not you might not see anything when you rotate your viewpoint to the back of the cube. :p

regards

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
[SOLVED]I've screwed up my depth buffer :S
« Reply #12 on: May 06, 2011, 10:46:17 pm »
Quote

Instance-Model, polygon mesh, Oct-Tree... Hmm What else is there left for me to explore? Maybe missed something?


scene graph?? maybe sorting of the state within the scene graph to improve efficiency?? or maybe a generic postprocessing framework for effects?? :D

regards

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
[SOLVED]I've screwed up my depth buffer :S
« Reply #13 on: May 06, 2011, 10:50:58 pm »
Quote from: "mercurio7891"
scene graph?? maybe sorting of the state within the scene graph to improve efficiency??
Already got scene graphs, graphs within the models and an oct-tree to do those things.

Quote from: "mercurio7891"
or maybe a generic postprocessing framework for effects?? :D

Hmm more interesting, You mean shaders? Well I'm very interested in lighting effects(only done 2D so far successfully, failed in 3D) and would love to try that. Though before I do that I might just polish what I got so far and try to make it publicly usable :)

Thing is we won't start with real 3D until the second year. Can't wait ^^
So is experimenting some on my own. Also they use DirectX on school so I'd like to first learn it in OpenGL trough SFML so I can compare the two after my time at the university.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
[SOLVED]I've screwed up my depth buffer :S
« Reply #14 on: May 06, 2011, 11:03:17 pm »
for lighting the most commonly use one is the phong lighting model. It can be done without shader or any postprocessing.

Opengl can enable lighting on the fixed function pipeline via glEnable(GL_LIGHTING);

then you enable and disable specific lights via glEnable(GL_LIGHTn) where n can go from 0 to about 8 normally :D

the you have to specify the light params via glLightfv or their family of functions

Code: [Select]

GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);


lastly what determines the color and lighting for each set of faces is the material. The material governs how the light interact with the surfaces

Code: [Select]

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
//draw the vertices here


basically thats how light works for opengl in the fixed function pipeline.

Personally to me, it is much easier to just use shaders :D

regards