Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Simple example, don't get it ...

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    4

    Simple example, don't get it ...


    I read several OpenGL tutorials, and now I'm trying to experiment with simple examples. But, I have results which I can't understand. Let's look at the following example. I have a rectangle in Z=0 plane. When my camera is looking from (0.0, 0.0, 1.0) to (0.0, 0.0, 0.0) rectangle is visible. But, when looking from (0.0, 0.0, 1.1) rectangle is not visible. Why camera can't see rectangle in front of it, when viewing from Z>1.0?

    Code :
     
    void Redraw() {
        glClearColor( 0.0, 0.0, 0.0, 0.0 );
        glClear(GL_COLOR_BUFFER_BIT);
     
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
        gluLookAt( 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); // rectangle is visible
        // gluLookAt( 0.0, 0.0, 1.1, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); rectangle is not visible
     
        glColor3f( 1.0, 0.0, 0.0 );
        glBegin(GL_LINE_LOOP);
           glVertex3f( 0.5, 0.5, 0.0 );
           glVertex3f( 0.5, -0.5, 0.0 );
           glVertex3f( -0.5, -0.5, 0.0 );
           glVertex3f( -0.5, 0.5, 0.0 );
        glEnd();
     
        glFlush();
    }
     
    int main(int argc, char **argv) {
     
        glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
        glutInit( &argc, argv );
     
        glutInitWindowPosition( 200, 200 );
        glutInitWindowSize( 300, 300 );
        glutCreateWindow( "Test" );
     
        glutDisplayFunc( Redraw );
     
        glutMainLoop();
     
        return 0;
    }

  2. #2
    Senior Member Regular Contributor
    Join Date
    Aug 2007
    Posts
    107

    Re: Simple example, don't get it ...

    Because polygons are thought of as having front and back faces, and because the OpenGL default state is to cull (not rasterize) back face polygons. A front facing is one in which the winding is usually counter-clockwise (this is the default). If you want to see both sides (i.e. faces), you have to instruct OpenGL NOT to cull back faces with the function call, glDisable(GL_CULL_FACE) when initializing. If you have some polygon with clockwise winding (the opposite of the default), you can temporarily change the way GL determines polygon winding with glFrontFace(GL_CW). I recommend you revert to the default (GL_CCW) after specifying your clockwise-winding polygon, to avoid potential problems with mixed winding if you later enable face culling.

    Most of the tutorials on the net are not very thorough. I think you will benefit from picking up a good book if you want to learn the details and avoid these pitfalls

  3. #3
    Senior Member Regular Contributor
    Join Date
    Aug 2007
    Posts
    107

    Re: Simple example, don't get it ...

    Ouch, I'm sorry. I completely misread your post and the problem

    What you need to do is to set up a perspective projection with a far and near plane, that contains what you want to render. You can do this, for instance, with the utility function gluPerspective (see http://www.opengl.org/sdk/docs/man/x...spective.xml).

  4. #4
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    4

    Re: Simple example, don't get it ...

    You're saying that this is because I didn't defined projection transformation? This leads me to the following questions:

    - Is there a default transformation which defines clipping space which makes my rectangle unvisible when it is too far from camera (outside of clipping space)?

    - Can gluLookAt function be used without defining projection transformation?

    - Can it be used with orthographic projection also?

    I already read some interesting chapters of popular OpenGL books, but they doesn't answer most of the questions I have. These questions are for start, a have a bunch of them ...

  5. #5
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Simple example, don't get it ...

    - Is there a default transformation which defines clipping space which makes my rectangle unvisible when it is too far from camera (outside of clipping space)?
    The default projection matrix should be the identity matrix, but it is not said in the spec, so this may change depending on the hardware. I advise you to always set explicitly the projection matrix, even if it is identity.

    Can gluLookAt function be used without defining projection transformation?
    It does not have any sense, since gluLookAt only affect the modelview matrix. This function is only used to change the view not the projection.

  6. #6
    Senior Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: Simple example, don't get it ...

    to put it the simple way, modelview matrix describes your object orientation in the view space. imagine you have a camera always at point (0,0,0) looking at down some axes and seeing some objects. the projection matrix on the other hand defines how all those visible objects in your view get "projected" on your screen. f.e. parallel projection or perspective projection or oblique projection and so on. because of that any application needs to define how it wants the vertices to be projected. without a proper projection matrix you wont be able to go far. references gluPerspective/gluOrtho.

  7. #7
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    4

    Re: Simple example, don't get it ...

    - So, the problem with my example is projection matrix, which I didn't set, which default value I don't know (because it isn't specified)?

    - If I don't know default projection matrix, projection transformation should always be defined?

    - I read in few tutorials on the net that the default OpenGL projection transformation is orthogonal, gluOrtho2D( -1.0, 1.0, -1.0, 1.0); is that true?

    It would be much easier for me if those opengl tutorials had more always and never words ...

  8. #8
    Senior Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: Simple example, don't get it ...

    "- So, the problem with my example is projection matrix, which I didn't set, which default value I don't know (because it isn't specified)?"
    actually i never saw an OpenGL sample which hasn't defined the projection matrix before rendering.

    "- If I don't know default projection matrix, projection transformation should always be defined?"
    you can always retrieve a matrix with glGet (glGetFloat(GL_PROJECTION_MATRIX,array)) but at startup its either identity or data trash (INFs or NANs or something).

    "- I read in few tutorials on the net that the default OpenGL projection transformation is orthogonal, gluOrtho2D( -1.0, 1.0, -1.0, 1.0); is that true?"
    see above and check yourself.

  9. #9
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: Simple example, don't get it ...

    Quote Originally Posted by medi84
    Is there a default transformation which defines clipping space which makes my rectangle unvisible when it is too far from camera (outside of clipping space)?
    I read through my OpenGL texts and could not find the answer to your question. No where did I find it stated where the default clipping planes are. So I don't blame you for being a little upset by that. I took your sample program and played around with it to discover where the default clipping planes are. The results seem natural. The near clipping plane is where the eyepoint is, and the far clipping plane is one unit away. In your example, this would put the near clipping plane at z = 1.0, and the far clipping plane at z = 0.0. Your line of sight is in the -Z direction. My version of your code is here. Instead of changing the gluLookAt parameters, I added a variable 'z' which can be used to move the rectangle towards or away from the eyepoint (0,0,1). In your version of the code, the red square doesn't show up when the eyepoint is at z = 1.1, simply because it is MORE than one unit away. I agree with the other posters who said that it's a good idea to explicitly state how far the clipping planes are from the camera via glOrtho or gluPerspective commands.
    Am I doing your homework for you?

  10. #10
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    4

    Re: Simple example, don't get it ...


    Thanks Max.

    Let's stop on orthographic projection. I moved camera back a little bit, from (0.0, 0.0, 1.0) to position (0.0, 0.0, 1.5), and tried to experiment with orthographic projection and it's clipping planes. But, whatever value I use for near and far clipping planes I can't get that rectangle (Z=0) displayed.
    I read OpenGL reference pages, and a bunch of tutorials and neither of them explains clearly what is the reference point from which near and far are measured, and in which direction. Is that a camera (eye) point, in this case (0.0, 0.0, 1.5), with it's direction to (0.0, 0.0, 0.0)?

    This is the example, variable parts are commented. None of them makes my rectagle displayed.

    Code :
    void Redraw() {
        glClearColor( 0.0, 0.0, 0.0, 0.0 );
        glClear(GL_COLOR_BUFFER_BIT);
     
        // VIEW TRANSFORMATION
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
        gluLookAt( 0.0, 0.0, 1.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); // camera point moved to z=1.5 position
     
        // MODEL TRANSFORMATION
        glColor3f( 1.0, 0.0, 0.0 );
        glBegin(GL_LINE_LOOP);
           glVertex3f( 0.5, 0.5, 0.0 );
           glVertex3f( 0.5, -0.5, 0.0 );
           glVertex3f( -0.5, -0.5, 0.0 );
           glVertex3f( -0.5, 0.5, 0.0 );
        glEnd();
     
        // PROJECTION TRANSFORMATION - none of these below work
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        glOrtho( -1.0, 1.0, -1.0, 1.0, -5.0, 5.0); 
        // glOrtho( -1.0, 1.0, -1.0, 1.0, 0.0, 5.0);
        // glOrtho( -1.0, 1.0, -1.0, 1.0, -5.0, 0.0);
        // glOrtho( -1.0, 1.0, -1.0, 1.0, 5.0, -5.0);
        // etc.
     
        glFlush();
    }
     
    int main(int argc, char **argv) {
     
        glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
        glutInit( &argc, argv );
     
        glutInitWindowPosition( 200, 200 );
        glutInitWindowSize( 300, 300 );
        glutCreateWindow( "Test" );
     
        glutDisplayFunc( Redraw );
     
        glutMainLoop();
     
        return 0;
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Simple Cube not Showing up with simple Example ...
    By tmason in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-26-2014, 11:42 PM
  2. simple bug that i can't fix..
    By tanaka in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 05-21-2012, 08:49 PM
  3. shaders, simple to well a bit more than simple :)
    By Andy Gilbert in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-22-2010, 08:05 AM
  4. Need a simple example
    By Glutenblungen in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 10-22-2005, 02:50 PM
  5. simple 2d
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 05-27-2002, 06:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean