opengl 正多边形
我有以下代码,它什么也没绘制。 如果我使用 glBegin(GL_POINTS) 它会绘制一个圆,但在多边形模式下它不会。
int WXSIZE=500,WYSIZE=500;
//Coordinate system
float Xmin=-8, Xmax=8, Ymin=-8, Ymax=8;
void setupmywindow()
{
glClearColor(0,0,0,0);
gluOrtho2D(Xmin, Xmax, Ymin, Ymax);
}
void mypolygon(float radius) //test object
{
glColor3f(1,0,0);
int numPoints=20;
float x,y;
float centerx,centery=0;
for (int i = 0; i < numPoints; i++)
{
x = centerx + radius * sin(2.0*PI*i/numPoints);
y = centery + radius * cos(2.0*PI*i/numPoints);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_POLYGON);
glVertex2f(x, y);
glEnd();
}
}
void myDisplay()
//single object
{
glClear(GL_COLOR_BUFFER_BIT);
mypolygon(2.0);
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(WXSIZE,WYSIZE);
glutCreateWindow("My graphic window");
setupmywindow();
glutDisplayFunc(myDisplay);
glutMainLoop();
}
有什么建议吗?
编辑----------------------
glBegin(GL_POLYGON);
for (int i = 0; i < numPoints; i++)
{
x = centerx + radius * sin(2.0*PI*i/numPoints);
y = centery + radius * cos(2.0*PI*i/numPoints);
glVertex2f(x, y);
}
glEnd();
我把它弄乱了循环。
i have the following code which draws nothing.
If i use glBegin(GL_POINTS) it draws a circle but with polygon mode it doesn't.
int WXSIZE=500,WYSIZE=500;
//Coordinate system
float Xmin=-8, Xmax=8, Ymin=-8, Ymax=8;
void setupmywindow()
{
glClearColor(0,0,0,0);
gluOrtho2D(Xmin, Xmax, Ymin, Ymax);
}
void mypolygon(float radius) //test object
{
glColor3f(1,0,0);
int numPoints=20;
float x,y;
float centerx,centery=0;
for (int i = 0; i < numPoints; i++)
{
x = centerx + radius * sin(2.0*PI*i/numPoints);
y = centery + radius * cos(2.0*PI*i/numPoints);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_POLYGON);
glVertex2f(x, y);
glEnd();
}
}
void myDisplay()
//single object
{
glClear(GL_COLOR_BUFFER_BIT);
mypolygon(2.0);
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(WXSIZE,WYSIZE);
glutCreateWindow("My graphic window");
setupmywindow();
glutDisplayFunc(myDisplay);
glutMainLoop();
}
Any suggestions?
EDIT----------------------
glBegin(GL_POLYGON);
for (int i = 0; i < numPoints; i++)
{
x = centerx + radius * sin(2.0*PI*i/numPoints);
y = centery + radius * cos(2.0*PI*i/numPoints);
glVertex2f(x, y);
}
glEnd();
I messed it with the loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在每个循环中,您都绘制一个由单个顶点组成的多边形,因此什么也没有。只需将
glBegin/glEnd
(和glPolygonMode
)放在 for 循环之外,并且只在循环中绘制glVertex
即可。当然,它适用于点,因为单个点乘以 n 点是相同的。但是n个各由一个点组成的多边形与一个由n个点组成的多边形不同。In every loop you are drawing a polygon, that consists of a single vertex, so nothing. Just put the
glBegin/glEnd
(and theglPolygonMode
) outside of the for loop and only drawglVertex
in the loop. Of course it works with points, as a n times a single point is the same as n points. But n polygon consisting of one point each is not the same as one polygon consisting of n points.你的多边形似乎在错误的一边。默认情况下,OpenGL只显示正面,需要逆时针指定。您可以:
for (int i = numPoints-1; i >= 0 ; i--)
)glFrontFace(GL_CW)< /code>)
glDisable(GL_CULL_FACE)
)。Your polygon seems to be on the wrong side. By default, OpenGL only shows front faces, which need to be specified counterclockwise. You can:
for (int i = numPoints-1; i >= 0 ; i--)
)glFrontFace(GL_CW)
)glDisable(GL_CULL_FACE)
).当您使用 GL_POLYGON 调用 glBegin 时,我认为至少需要三个顶点。标准绘图协议是使用三角形进行绘制,将三个顶点作为一组,因为每个三角形面需要三个顶点。你只给它提供一个顶点,所以你不会看到任何东西。尝试将其更改为这样:
When you call glBegin with GL_POLYGON, it is expecting I believe a minimum of three vertices. Standard drawing protocol is to draw using triangles, witch vertices in sets of three, since you need three vertices for each triangle face. You are only feeding it a single vertex, so you're not going to see anything. Try changing it to this: