Dar Propiedades a Los Primitivos de OpenGL

download Dar Propiedades a Los Primitivos de OpenGL

of 28

Transcript of Dar Propiedades a Los Primitivos de OpenGL

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    1/28

    Dar varias propiedades a

    los Primitivos de OpenGL

    Curso: Graficacin

    Prof. Gueorgi Khatchatourov

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    2/28

    Contenido

    Cmover un polgono?

    Con qu colorver un objeto?

    Como llenar Polgonos, o re-visitandoStippling (ver Other Primitives de Chapter IIIDrawing in Space de OpenGL SuperBible)

    Cmo tratar los polgonos no convexos

    Cmo tratar los polgonos no planos Usar Stencil Buffer (plantilla para pintar)

    Usar Scissors (tijeras)

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    3/28

    Regresar al indiceCmover un polgono?(I de II)

    Ver lo interior de un polgono? Ver ambos lados de un polgono? Ver unpolgono desde ambos lados? Ver los bordes de un polgono ?

    -----------------------------------------------------------------------------------------------------------------------

    Polygons as Points, Outlines, or Solids

    A polygon has two sides- front and back - and might be rendered differently depending on whichside is facing the viewer. By default, both front and back faces are drawn in the same way. Tochange this, or to draw only outlines or vertices, use glPolygonMode().

    void g lPo lygonMode(GLenum face, GLenum mode);

    _ Controls the drawing mode for a polygon's front and back faces. The parameter

    facecan be GL_FRONT_AND_BACK, GL_FRONT, or GL_BACK;

    modecan be GL_POINT, GL_LINE, or GL_FILL to indicate whether the polygon should bedrawn as points, outlined, or filled.

    By default , bo th the fron t and back faces are drawn f i l led.

    For example, you can have the front faces filled and the back faces outlined with two calls to thisroutine:

    glPolygonMode(GL_FRONT, GL_FILL);

    glPolygonMode(GL_BACK, GL_LINE);

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    4/28

    Regresar al indiceCmover un polgono?(II de II):

    Ver ambos lados de un polgono?Los controles para cambiar manera de ver diferentes lados de un

    polgono son:

    Prender/apagar eliminacin de las superficies ocultas ()

    glCullFace

    void glCullFace(GLenum mode);

    Indica cuales polgonos deben ser eliminados (culled) antes que se convierten enlas coordenadas de pantalla. El mode es uno de GL_FRONT, GL_BACK, oGL_FRONT_AND_BACK para indicar el lado anverso, reverso o todos lospolgonos.

    To take effect, culling must be enabled usingglEnable() with GL_CULL_FACE; itcan be disabled withglDisable() and the same argument.

    Cambia el sentido de los lados frontales y traserasglFrontFace(GL_CW);

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    5/28

    Regresar al ndiceCon qu color ver un objeto?

    Con que color ver lo interior de un objeto? Cmo el color

    interior de un polgono depende de los vrtices? Cmo

    lo depende de propiedades de luz, de material?

    -----------------------------------------------------------------------------

    Mecansmos de formacin de color de un

    pixel

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    6/28

    ir al ndiceCmo tratar los polgonos no convexos (Ide II)

    Figure 2-12 :Subdividing a

    Nonconvex Polygon

    you can manually control the setting of the edge flag with the command

    glEdgeFlag*(). It is used between glBegin() and glEnd() pairs, and it affects all

    the vertices specified after it until the next glEdgeFlag() call is made. It applies

    only to vertices specified for polygons, triangles, and quads, not to those

    specified for strips of triangles or quads.

    void glEdgeFlag(GLboolean flag);

    void glEdgeFlagv(const GLboolean *flag);

    Indicates whether a vertex should be considered as initializing a boundary edge of

    a polygon. If flag is GL_TRUE, the edge flag is set to TRUE (the default), and any

    vertices created are considered to precede boundary edges until this function is

    called again with flag being GL_FALSE.

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    7/28

    ir al ndiceCmo tratar los polgonos no convexos (IIde II)

    Example 2-7 draws the outline

    shown in Figure 2-13.

    Figure 2-13 : Outlined

    Polygon Drawn

    Using Edge Flags

    Example 2-7 : Marking Polygon Boundary Edges

    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    glBegin(GL_POLYGON);glEdgeFlag(GL_TRUE);

    glVertex3fv(V0);glEdgeFlag(GL_FALSE);

    glVertex3fv(V1);

    glEdgeFlag(GL_TRUE);

    glVertex3fv(V2);

    glEnd();

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    8/28

    Cmo tratar los polgonos no

    planos

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    9/28

    Regresar al ndiceUsar Stencil Buffer

    (plantilla para pintar)Frequently, we want to mask out an irregularly shaped area using a

    stencil pattern. In the real world, a stencil is a flat piece of cardboard or

    other material that has a pattern cut out of it. Painters use the stencil to

    apply paint to a surface using the pattern in the stencil. Figure shows

    how this process works.

    The stencil test takes place only if there is a stencil buffer. (If there is no

    stencil buffer, the stencil test always passes.) Stenciling applies a test

    that compares a reference value with the value stored at a pixel in the

    stencil buffer. Depending on

    the result of the test, the value

    in the stencil buffer is modified.

    El stencil test modifica contenido

    del pixel en stencil buffer basandose

    en el resultado de una comparicin del

    valor de referencia con el valor en el stencil buffer

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    10/28

    Regresar al ndiceUsar Stencil Buffer (II)ir al inicio del stencil buffer

    void init (void)

    { .glClearStencil(0x0);/*specifica el valor del

    vacio para el

    "stencil buffer"*/

    glEnable(GL_STENCIL_TEST);

    }

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    11/28

    Ir a lamina anteriorUsar Stencil Buffer (III):

    lgica del uso ir al inicio del stencil buffer glStencilFunc ( // establece la funcin y el valor de referencia para stencil test

    GLenum func, // especifica tipo de comparicin para stencil test

    GLint ref, // valor de referencia;

    GLuint mask // mscara.);

    Por ejemplo, si

    func=GL_EQUAL

    Entonces, el pixel pasa satisfactoriamente la prueba (stencil test)

    siempre y cuando se cumple(ref&mask)=(stencil&mask)

    }

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    12/28

    Regresar al ndiceUsar Stencil Buffer (IV)

    ir al inicio del stencil buffer

    void display(void)

    {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /* draw blue sphere where the stencil is 1 */

    glStencilFunc (GL_EQUAL, 0x1, 0x1 ); //Ver un ejemplo de lgica de uso de los parametros

    glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP); //define la accin del stencil test

    glCallList (BLUEMAT);glutSolidSphere (0.5, 15, 15);

    /* draw the tori where the stencil is not 1 */

    glStencilFunc (GL_NOTEQUAL, 0x1, 0x1);

    glPushMatrix();

    glRotatef (45.0, 0.0, 0.0, 1.0);

    glRotatef (45.0, 0.0, 1.0, 0.0);

    glCallList (YELLOWMAT);

    glutSolidTorus (0.275, 0.85, 15, 15);glPushMatrix();

    glRotatef (90.0, 1.0, 0.0, 0.0);

    glutSolidTorus (0.275, 0.85, 15, 15);

    glPopMatrix();

    glPopMatrix();

    }

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    13/28

    Regresar al ndiceUsar Stencil Buffer (V)

    ir al inicio del stencil buffer

    void reshape(int w, int h)

    {.

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    glClear(GL_STENCIL_BUFFER_BIT);

    glStencilFunc (GL_ALWAYS, 0x1, 0x1);

    glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE);

    glBegin(GL_QUADS);glVertex2f (-1.0, 0.0);

    glVertex2f (0.0, 1.0);

    glVertex2f (1.0, 0.0);

    glVertex2f (0.0, -1.0);

    glEnd();

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluPerspective(45.0, (GLfloat) w/(GLfloat) h, 3.0, 7.0);glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    glTranslatef(0.0, 0.0, -5.0);

    }

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    14/28

    ir al indice de la presentacionUsar Scissors

    (tijeras) (I de III) One way to improve rendering performance is to update

    only the portion of the screen that has changed. Youmay also need to restrict OpenGL rendering to a smallerrectangular region inside the window. OpenGL allowsyou to specify a scissor rectangle within your window

    where rendering can take place. By default, the scissorrectangle is the size of the window, and no scissor testtakes place. You turn on the scissor test with:

    glEnable(GL_SCISSOR_TEST);

    You can turn off the scissor test again with thecorresponding glDisable function call.

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    15/28

    ir al indice de la presentacionUsar Scissors

    (tijeras) (II de III) Listing 3.13. Using the Scissor Box to Render a Series of Rectangles

    void RenderScene(void) {

    // Clear blue window

    glClearColor(0.0f, 0.0f, 1.0f, 0.0f);

    glClear(GL_COLOR_BUFFER_BIT);

    // Now set scissor to smaller red sub regionglClearColor(1.0f, 0.0f, 0.0f, 0.0f);

    glScissor(100, 100, 600, 400);

    glEnable(GL_SCISSOR_TEST);

    glClear(GL_COLOR_BUFFER_BIT);

    // Finally, an even smaller green rectangle

    glClearColor(0.0f, 1.0f, 0.0f, 0.0f);

    glScissor(200, 200, 400, 200);glClear(GL_COLOR_BUFFER_BIT);

    // Turn scissor back off for next render

    glDisable(GL_SCISSOR_TEST);

    glutSwapBuffers();

    }

    U S i

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    16/28

    ir al indice de la presentacionUsar Scissors

    (tijeras): salida del programa (III de

    III)

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    17/28

    Regresar a Cmo ver un polgonoOrientacin de polgonos (I de

    II)

    OpenGL, by default, considers polygons that havecounterclockwise winding to be front facing. This means thatthe triangle on the left in Figure 3.15 shows the front ofthe triangle, and the one on the right shows the back sideof the triangle.

    (II

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    18/28

    Regresar a Cmo ver un polgonoOrientacin de polgonos (II

    de II) regresar a I de II

    you will often want to give the front and back of apolygon different physical characteristics. You can hidethe back of a polygon altogether or give it a differentcolor and reflective property (see SuperBibliaOPenGLChapter 5, "Color, Materials, and Lighting: The Basics").It's important to keep the winding of all polygons in a

    scene consistent, using front-facing polygons todraw the outside surface of any solid objects.

    If you need to reverse the default behavior of OpenGL,you can do so by calling the following function:

    glFrontFace(GL_CW);

    The GL_CW parameter tells OpenGL that clockwise-wound polygons are to be considered front facing. Tochange back to counterclockwise winding for the front

    face, use GL_CCW.

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    19/28

    ir al indice de la presentacionMecansmos de

    formacin de color de un pixel ir al Conque color

    Limpiar el buffer de color y dar un color

    para todos los pxeles desocupados

    Lgica de coloracin de OpenGLColor solido

    (GL_FLAT) o interpolado (GL_SMOOTH)

    Los factores geomtricos y fsicos que

    influyen a la percepcin de luz y

    generacin del color final de un pxel

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    20/28

    ir al indice de la presentacionMecansmos de formacin de color

    de un pixel:Limpiar el buffer de color y dar un

    color para los pixeles desocupadosir al indice demecansmos de coloracion

    // limpia el buffer de color

    glClear(GL_COLOR_BUFFER_BIT);

    // dar color a los pixeles desocupados

    glClearColor ();

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    21/28

    ir al indice de la presentacionMecansmos de formacin de color

    de un pixel:Lgica de coloracin de OpenGLir al indice de mecansmos de coloracion

    Podemos considerar la coloracin de un objeto como dar una pintura al objeto sin tomar en cuenta cmo lo se ver enuna posicin especifica respecto a una camara, o de que material se ha hecho o cuanto y cuales luces se tienenen el entorno.

    En este sentido hay dos opciones (dos modelos de SHADING) de pintura:

    con un color slido (GL_FLAT)

    con lo interpolado (GL_SMOOTH)

    -------------------------------------------------------------------------------------------------------

    In general, an OpenGL programmer first sets the color or coloring scheme and then draws the objects. Until the coloror coloring scheme is changed, all objects are drawn in that color or using that coloring scheme.Thismethod helps OpenGL achieve higher drawing performance than would result if it didn't keep track of the currentcolor.

    For example, the pseudocode

    set_current_color(red);

    draw_object(A);

    draw_object(B);set_current_color(green);

    set_current_color(blue);

    draw_object(C);

    draws objects A and B in red, and object C in blue. The command on the fourth line that sets the current color to greenis wasted.

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    22/28

    ir al indiceLgica de coloracin de OpenGL (IIde ) : Color solido o interpolado:

    lamina II.1: Color solido (GL_FLAT) ir alindice de mecansmos de coloracion

    Por omisin se supone que el modelo de coloracin es comodespus del comando

    glShadeModel (GL_FLAT);

    glColor3f (1.0, 0.0, 0.0); /* the current RGB color is red: */

    /* full red, no green, no blue. */

    glBegin (GL_POINTS);glVertex3fv (point_array);

    glEnd ();

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    23/28

    ir al indiceLgica de coloracin de OpenGL (II de ) : Colorsolido o interpolado:

    lamina II.2: Color interpolado (GL_SMOOTH);

    ir al indice de mecansmos de coloracion

    #include

    void init(void)

    { glClearColor (0.0, 0.0, 0.0, 0.0);

    glShadeModel (GL_SMOOTH);}

    void display(void){ glClear (GL_COLOR_BUFFER_BIT);

    glBegin (GL_TRIANGLES);

    glColor3f (1.0, 0.0, 0.0);

    glVertex2f (5.0, 5.0);

    glColor3f (0.0, 1.0, 0.0);

    glVertex2f (25.0, 5.0);

    glColor3f (0.0, 0.0, 1.0);

    glVertex2f (5.0, 25.0);glEnd();

    glFlush ();

    }

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    24/28

    ir al indiceLgica de coloracin de OpenGL (II de ) : Colorsolido o interpolado:

    lamina II.2': Color interpolado (GL_SMOOTH);

    ir al indice de mecansmos de coloracion

    void reshape (int w, int h)

    { glViewport (0, 0, (GLsizei) w, (GLsizei) h);glMatrixMode (GL_PROJECTION);

    glLoadIdentity ();

    if (w

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    25/28

    ir al ndiceLos factores geomtricos y fsicos queinfluyen a la percepcin de luz y la generacin del

    color final de un pxel ir al ndice de mecansmos de coloracion

    Los factores geometricos incluyen posicion y orientacionespacial del objeto y de la cmara, posicin(es) del(as)fuentes de luz.

    Para mejorar rendiminto, tomando en cuenta esos factores, elparadigma de OpenGL supone procesamiento de color finalno para los puntos individuales, sino para polgonosenteros.En su vez, para evitar varios efectos de malinterpretacin, cmo los polgonos se usan los tringulos a loscuales previamente se calculan y se adjuntan vectores normales

    Los factores fsicos incluyen color del objeto,propiedades de reflexin del material, propiedades (detransparencia y de iluminacin) del medio ambiente

    R l I d C t i d

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    26/28

    Regla I de Construccin de

    Polgonos regresar a factores

    geomtricos y fsicos que influyenWhen you are using many polygons to construct a complex surface, you needto remember two important rules.

    The first rule is that all polygons must be planar. That is, all the vertices ofthe polygon must lie in a single plane, as illustrated in Figure 3.31. Thepolygon cannot twist or bend in space.

    Figure 3.31. Planar versus nonplanar polygons.

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    27/28

    Regla II de Construccin de Polgonosregresar a factores geomtricos y fsicos que influyen

    The second rule of polygon construction is that the polygon's edgesmust not intersect, and the polygon must be convex. If any given

    line enters and leaves the polygon more than once, the polygon is not

    convex. Figure 3.32gives examples of good and bad polygons.

    Figure 3.32. Some valid and invalid primitive polygons.

    U St il B ff (IV)

  • 8/12/2019 Dar Propiedades a Los Primitivos de OpenGL

    28/28

    Regresar al ndiceUsar Stencil Buffer (IV):

    la accin del stencil testir atras

    void glStenci lOp(

    GLenum fai l ,

    GLenum zfail,

    GLenum zpass

    ); The value of any of fail, zfail,and zpasscan be GL_KEEP,

    GL_ZERO, GL_REPLACE, GL_INCR, GL_DECR, or GL_INVERT.

    The fai lfunction is applied if the fragment fails the stencil

    test; if it passes, then zfailis applied if the depth testfails and zpassif the depth test passes, or if no depthtest is performed.

    By default, all three stencil operations are GL_KEEP.