Dibujar rayas 2D

This simple program was designed to draw 13 even, red and white stripes placed alternately, as in the US flag.

// A Simple OpenGL Project
// Author: Michael Hall
//
// This C++ code and project are provided "as is" without warranty of any kind.
//
// Copyright 2010 XoaX - For personal use only, not for distribution
// 
// Elaborated by me :)

#include <glut.h>

void DrawStripes(const int quantity)
{
    glBegin(GL_QUADS);

    for(int i=1; i <= quantity ; i++)
    {
        if(i%2)
            glColor3f(1.0,0.0,0.0);
        else 
            glColor3f(1.0,1.0,1.0);

            glVertex2f(0,static_cast<float>(i-1)/static_cast<float>(quantity));
            glVertex2f(1.0,static_cast<float>(i-1)/static_cast<float>(quantity));

            glVertex2f(0,static_cast<float>(i)/static_cast<float>(quantity));
            glVertex2f(1.0,2*static_cast<float>(i)/static_cast<float>(quantity));
    }
}



void Draw() 
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0, 0.0, 1.0);
    DrawStripes(13);


    glEnd();
    glFlush();
}

void Initialize() 
{
    glClearColor(0.0, 0.0, 102.0/255.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int iArgc, char** cppArgv) 
{
    glutInit(&iArgc, cppArgv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(950,500);
    glutInitWindowPosition(200, 200);
    glutCreateWindow("Rough draft");
    Initialize();


    glutDisplayFunc(Draw);
    glutMainLoop();

    return 0;
}

This is how it looks in practice: final window Why there are blue triangles on the foreground? I've specified only "QUADS" inside Draw/DrawStripes functions.

preguntado el 04 de julio de 12 a las 01:07

2 Respuestas

The blue comes from your clear color:

 glClearColor(0.0, 0.0, 102.0/255.0, 0.0);

That third parameter is the Blue channel of the clear color. When you call glClear(GL_COLOR_BUFFER_BIT); you initialize the render target with the clear color.

Thus the blue you are seeing is from your vertices not covering up the entire render target, and letting the blue clear color through. To see it, enable wireframe (glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);).

Also you really ought to draw in a counter-clock wise fashion: top left, bottom left, bottom right, top right. That will result in your polygons being drawn front-facing (which en general is what you want to avoid back-face culling).

At the moment your are "folding" the polygon, by doing top left, top right, abajo a la izquierda, then bottom right you essentially twist the quad. Try in clockwise order (invert bottom right and bottom left) and see if you can still see the polygons (you shouldn't if back face culling is on).

Finally you also multiply , solamente the bottom right Y coordinate by 2, why ? That is what is causing your deformation (on top of the twisting). You should have both bottom vertices with the same Y coordinate to make a rectangle (so bottom right and bottom left Y coordinate must match).

Respondido 04 Jul 12, 01:07

It seems illogical for me, but 'swapping 3rd and 4th vertex' turned out to be sufficient to obtain the correct output. I mean it is no difference with multiplying by 2 or not. - 0x6B6F77616C74

Just because you don't see the difference does not mean there is no difference; the skewed quad will hide nicely behind the quad you will draw next, thus you don't see it but if you draw only one quad you should see it. You could say I'm just being pedant :p - julien lebot

I think you're drawing the quad vertices in the wrong order. They should be counterclockwise, but you're drawing a 'twisted' quad.

Try swapping the 3rd and 4th vertex in your for loop.

Respondido 04 Jul 12, 01:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.