Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 10

Thread: Newbie questions!

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2000
    Posts
    4

    Newbie questions!

    These are the two questions I have encountered so far:
    - when I wanted to draw a square using:

    glBegin(GL_POLYGON); or glBegin(GL_QUADS);
    glVertex3f(-5.0, -5.0, 0.0);
    glVertex3f(5.0, -5.0, 0.0);
    glVertex3f(-5.0, 5.0, 0.0);
    glVertex3f(5.0, 5.0, 0.0);
    glEnd();

    the output is not a square but a polygon. Why?

    - I got myself the Redbook and most of the examples does not require function protoype. I cut and pasted and compiled it without error but when I tried to write out 1 without function prototype, my compiler give me errors. Why is that so?

    Thanks for the reply!

    [This message has been edited by EeeK (edited 07-12-2000).]
    I'm sending 01010101010...
    <a href="http://eeekmulation.virtualave.net" target="_blank">EeeKmuLatioN
    </a>

  2. #2
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,527

    Re: Newbie questions!

    Watch out for the vertex winding order.
    Your vertices are not building a convex polygon.
    Try swapping the last two vertices to get counterclockwise orientation of the four vertices like this:

    glBegin(GL_POLYGON); or glBegin(GL_QUADS);
    glVertex3f(-5.0, -5.0, 0.0);
    glVertex3f(5.0, -5.0, 0.0);
    glVertex3f(5.0, 5.0, 0.0);
    glVertex3f(-5.0, 5.0, 0.0);
    glEnd();

    Though with glBegin(GL_QUAD_STRIP) your order would have worked. Puzzled?

    If you have
    #include <GL/gl.h>
    #include <GL/glu.h>
    in you source files using OpenGL everything should compile fine.

    "- I got myself the Redbook and most of the examples does not require function protoype. I cut and pasted and compiled it without error but when I tried to write out 1 without function prototype, my compiler give me errors. Why is that so?
    "

    Depending on the function you either have not included the file with the implementation into your project, not included the header and the lib containing the prototype and the implementation.
    But that's a wild guess wiothout knowing what the function and the progrma looks like.

    Hope that helps.



    [This message has been edited by Relic (edited 07-12-2000).]

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2000
    Posts
    4

    Re: Newbie questions!

    So you mean that in building a convex polygon, the vertices must be in the order of counter-clockwise? I tried and it work but how about with the z-coordinates?

    glBegin(GL_POLYGON); or glBegin(GL_QUADS);
    glVertex3f(-5.0, -5.0, 2.0);
    glVertex3f(5.0, -5.0, 2.0);
    glVertex3f(5.0, -5.0, -2.0);
    glVertex3f(-5.0, -5.0, -2.0);
    glEnd();

    The above vertices are in counter-clockwise but the output is not a square. Why?
    I'm sending 01010101010...
    <a href="http://eeekmulation.virtualave.net" target="_blank">EeeKmuLatioN
    </a>

  4. #4
    Guest

    Re: Newbie questions!

    Probably... your verts are getting clipped out because they are to close....

    Question are you using perspective or ortho viewing?

    If you want a square... try this

    void init()
    {
    glClearColor(0.0,0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
    }


    and


    void display()
    {

    glClear (GL_COLOR_BUFFER_BIT); //clear the screen with black

    glColor3f(.5,.75,0.3); //change to a green color
    glBegin(GL_TRIANGLES); //begins the declaration of a triangle
    //vertices
    glVertex3f(.3,.5,0.0);
    glVertex3f(.7, .5, 0.0);
    glVertex3f(.5, .25, 0.0);
    glEnd(); //end declaration

    glColor3f(1.0, 1.0, 0); //change to yellow
    glBegin(GL_POLYGON); //declare a polygon
    //define verts
    glVertex3f(0.15, .4,0.0);
    glVertex3f(0.1, .3,0.0);
    glVertex3f(0.2, .2,0.0);
    glVertex3f(0.25, .2, 0.0);
    glVertex3f(0.3,.1,0.0);

    glEnd(); //end declaration

    glColor3f(1.0, 0, 0); //change to red
    glBegin(GL_POLYGON); //declare new poly
    //define verts
    glVertex3f(0.45, 0.9, 0.0);
    glVertex3f(0.35, 0.8,0.0);
    glVertex3f(0.35, 0.7, 0.0);
    glVertex3f(0.45,0.6,0.0);
    glVertex3f(0.55,0.6,0.0);
    glVertex3f(0.65,0.7,0.0);
    glVertex3f(0.65, 0.8,0.0);
    glVertex3f(0.55,0.9, 0.0);

    glEnd(); //end declaration
    glFlush(); //perform all buffered gl instruction
    //paint all polys

    }

  5. #5
    Guest

    Re: Newbie questions!

    poop im sorry just use this

    void init()
    {
    glClearColor(0.0,0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
    glMatrixMode(GL_MODELVIEW);
    }

    //this function paints the window
    void display()
    {

    glClear (GL_COLOR_BUFFER_BIT); //clear the screen with black

    glColor3f(.5,.75,0.3); //change to a green color
    glBegin(GL_POLYGON);
    glVertex3f (0.25, 0.25, 0.0);
    glVertex3f (0.75, 0.25, 0.0);
    glVertex3f (0.75, 0.75, 0.0);
    glVertex3f (0.25, 0.75, 0.0);
    glEnd();
    glFlush();


    }

  6. #6
    Junior Member Newbie
    Join Date
    Jun 2000
    Posts
    14

    Re: Newbie questions!

    Eeek, your second example is a square but
    the output is what you see in real life.

    its smaller when its far away

  7. #7
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,527

    Re: Newbie questions!

    "So you mean that in building a convex polygon, the vertices must be in the order of counter-clockwise?"

    To be precise, it actually does not matter if the orientatoin is clockwise or counter-clockwise as long as the polygon is convex. You can select the winding order in OpenGL with glFrontFace() and default is GL_CCW (counter-clockwise).
    You'll find OpenGL easier to learn if you keep some defaults.

  8. #8
    Junior Member Newbie
    Join Date
    Jun 2000
    Posts
    4

    Re: Newbie questions!

    Ok, here is part of my source for creating my first 3D application:

    void init(void)
    {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0);
    }
    void display(void)
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    glColor3f(1.0, 0.0, 0.0); //red
    glVertex3f(-6.0, -6.0, 6.0); //left
    glColor3f(0.0, 1.0, 0.0); //green
    glVertex3f(6.0, -6.0, 6.0); //right
    glColor3f(0.0, 0.0, 1.0); //blue
    glVertex3f(0.0, 6.0, 0.0); //top

    glColor3f(0.0, 1.0, 0.0); //green
    glVertex3f(-6.0, -6.0, -6.0);
    glColor3f(1.0, 0.0, 0.0); //red
    glVertex3f(6.0, -6.0, -6.0);
    glColor3f(0.0, 0.0, 1.0); //blue
    glVertex3f(0.0, 6.0, 0.0);

    glColor3f(1.0, 0.0, 0.0); //red
    glVertex3f(-6.0, -6.0, 6.0);
    glColor3f(0.0, 1.0, 0.0); //green
    glVertex3f(-6.0, -6.0, -6.0);
    glColor3f(0.0, 0.0, 1.0); //blue
    glVertex3f(0.0, 6.0, 0.0);

    glColor3f(0.0, 1.0, 0.0); //green
    glVertex3f(6.0, -6.0, 6.0); //left
    glColor3f(1.0, 0.0, 0.0); //red
    glVertex3f(6.0, -6.0, -6.0); //right
    glColor3f(0.0, 0.0, 1.0); //blue
    glVertex3f(0.0, 6.0, 0.0); //top
    glEnd();
    glBegin(GL_QUADS);
    glVertex3f(-6.0, -6.0, -6.0);
    glVertex3f(-6.0, -6.0, 6.0);
    glVertex3f(6.0, -6.0, 6.0);
    glVertex3f(6.0, -6.0, -6.0);
    glEnd();
    glutSwapBuffers();
    }

    Why does color of the last polygon always override the color even it is at the back.
    Please point out what mistakes I have made, thanks!

    [This message has been edited by EeeK (edited 07-19-2000).]
    I'm sending 01010101010...
    <a href="http://eeekmulation.virtualave.net" target="_blank">EeeKmuLatioN
    </a>

  9. #9
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,527

    Re: Newbie questions!

    You mean why is the color of the quad always the same color as the last triangle vertex (here blue)?

    This is by design. OpenGL is a state machine.
    All commands changing one of the current states stay valid until the next change occurs. In your case the last time you set the color to blue, this color will be taken for all vertices thereafter until you issue the next color command.
    Put another glColor call before the glBegin(GL_QUADS) and you'll see.

  10. #10
    Junior Member Newbie
    Join Date
    Jun 2000
    Posts
    4

    Re: Newbie questions!

    I found out what is wrong, I did not put the glEnable(GL_DEPTH_TEST); in the init().

    Thanks for you reply thought.
    I'm sending 01010101010...
    <a href="http://eeekmulation.virtualave.net" target="_blank">EeeKmuLatioN
    </a>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •