Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL+GLUT Not filling Topmost-Leftmost Polygon?

Tags:

c++

opengl

glut

I'm encountering a strange OpenGL Bug. OpenGL is pretty new to me, but we're required to use it in my AI class (because the teacher is really a Graphics professor).

Either way, this is happening: http://img818.imageshack.us/img818/422/reversiothello.png

It happens to only the topmost, leftmost polygon. In other words, it finds the furthest polygon left, and then the furthest up and it does that to it. (There is not currently anything erasing polygons from the board).

My display function is this:

void display_func(void)
{
    glClearColor(0.0, 0.45, 0.0, 1.0); // Background Color (Forest Green :3)
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 0.0);

    draw_board();

    glFlush();

    glutSwapBuffers();
};

My draw_board function is this:

void draw_board()
{
    int size = 8;
    int stepX = WINDOW_XS / size;
    int stepY = WINDOW_YS / size;

    glColor3f(0.0,0.0,0.0); // line color black

    glBegin(GL_LINES);

    // Draw Columns
    for(int i = 0;i <= WINDOW_XS;i += stepX)
    {
        glVertex2i(i,0);
        glVertex2i(i, WINDOW_YS);
    }

    // Draw Rows
    for(int j = 0;j <= WINDOW_YS;j += stepY)
    {
        glVertex2i(0, j);
        glVertex2i(WINDOW_XS, j);
    }

    // Draw Circles
    for(int i = 0;i < 8;++i)
    {
        for(int j = 0;j < 8;++j)
        {
            if(engine->getOnBoard(i,j) == Reversi::PIECE_NONE) continue;
            if(engine->getOnBoard(i,j) == Reversi::PIECE_WHITE)
                glColor3f(1.0,1.0,1.0);
            if(engine->getOnBoard(i,j) == Reversi::PIECE_BLACK)
                glColor3f(0.0,0.0,0.0);

            int drawX = ((i+1)*64)-32;
            int drawY = 512-((j+1)*64)+32;
            gl_drawCircle(drawX,drawY,30);
        }
    }

    glEnd();
};

My mouse function is this:

void mouse_func(int button, int state, int x, int y)
{
    if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && x < WINDOW_XS)
    {    
        // row and column index
        x = (int)( x / (WINDOW_XS/8) );
        y = (int)( y / (WINDOW_YS/8) );

        std::cout << "Attempting to make a move at " << x << "," << y << std::endl;

        if(engine->makeMove(x,y))
        {
            glutPostRedisplay();
        }
    }
};

and my gl_drawCircle function is this:

void gl_drawCircle(float x, float y, float r)
{
    // http://stackoverflow.com/questions/5094992/c-drawing-a-2d-circle-in-opengl/5095188#5095188
    glBegin( GL_POLYGON );
    float t;
    int n;
    for(t = 0,n = 0; n <= 90; ++n, t = float(n)/90.f ) // increment by a fraction of the maximum 
    {
        glVertex2f( x + sin( t * 2 * PI ) * r, y + cos( t * 2 * PI ) * r );
    }
    glEnd();
}

Can anyone please help me?

like image 574
Navarr Avatar asked Jul 29 '11 06:07

Navarr


1 Answers

The only bug worth giving an answer that I can find is that your draw_board function doesn't use properly the glBegin and glEnd statements. You have to use a glEnd statement before calling gl_drawCircle, otherwise you'll get a nasty behavior.

Edit: you first circle is drawn using lines because the glBegin is ignored (since you are in a glBegin context). All other circles are done ok because you do a glEnd before calling glBegin again. The first drawn circle is the leftmost, topmost circle.

like image 195
Mihai Maruseac Avatar answered Oct 18 '22 13:10

Mihai Maruseac