我遇到了一个奇怪的OpenGL Bug.OpenGL对我来说很新,但我们需要在我的AI课程中使用它(因为老师真的是图形教授).
无论哪种方式,都会发生这种情况:http://img818.imageshack.us/img818/422/reversiothello.png
它恰好发生在最顶部,最左边的多边形上.换句话说,它会找到最远的多边形,然后是最远的多边形,它就是这样做的.(目前没有从董事会中删除多边形的任何内容).
我的显示功能是这样的:
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();
};
Run Code Online (Sandbox Code Playgroud)
我的draw_board函数是这样的:
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();
};
Run Code Online (Sandbox Code Playgroud)
我的鼠标功能如下:
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();
}
}
};
Run Code Online (Sandbox Code Playgroud)
我的gl_drawCircle函数是这样的:
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();
}
Run Code Online (Sandbox Code Playgroud)
谁能帮帮我吗?
唯一值得给出答案的错误是我的draw_board
函数没有正确使用glBegin
和glEnd
语句.你必须glEnd
在打电话前使用一个声明gl_drawCircle
,否则你会得到一个讨厌的行为.
编辑:您首先使用直线绘制圆,因为它glBegin
被忽略(因为您在glBegin
上下文中).所有其他圈子都已完成,因为你glEnd
在glBegin
再次打电话之前做了一次.第一个绘制的圆圈是最左边的最顶部圆圈.
归档时间: |
|
查看次数: |
684 次 |
最近记录: |