用OpenGL绘制国旗出现了一点问题
发布网友
发布时间:2022-08-16 03:32
我来回答
共1个回答
热心网友
时间:2023-09-28 08:08
gl_polygon 只能画 凸多边形,
凹多边形 请 分解后再画
下面是 我改的程序
PS: 我这是偷懒的做法, 不要用来画凹多边形, opengl 不保证渲染正确, 虽然 在下面的程序 画的是对的
#include "stdafx.h"
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glut32.lib")
#include "GL/glut.h"
#include "math.h"
#define PI 3.141592653589793238462643383279
void Rectangle (void)
{
glPolygonMode(GL_FRONT, GL_FILL);
glBegin(GL_POLYGON);
glColor3f(1.0,0,0);
glVertex2f(0.8,0.5);
glVertex2f(-0.8,0.5);
glVertex2f(-0.8,-0.5);
glVertex2f(0.8,-0.5);
glEnd();
}
void star(float x0,float y0,float d)
{
float d0=d*sin(PI/10)/cos(PI/5);
glPolygonMode(GL_FRONT, GL_FILL);
glColor3f(1.0,1.0,0);
glEnable(GL_POLYGON_SMOOTH);
glBegin(GL_POLYGON);
//就是这行出问题了,都画不出这个凹角,但是我怎么看都看不出有什么问题。
glVertex2f(x0+d0*cos(3*PI/10),y0+d0*sin(3*PI/10));
glVertex2f(x0-d*cos(PI/10),y0+d*sin(PI/10));
glVertex2f(x0-d0*cos(PI/10),y0-d0*sin(PI/10));
glVertex2f(x0-d*sin(PI/5),y0-d*cos(PI/5));
glVertex2f(x0,y0-d0);
glVertex2f(x0+d*sin(PI/5),y0-d*cos(PI/5));
glVertex2f(x0+d0*cos(PI/10),y0-d0*sin(PI/10));
glVertex2f(x0+d*cos(PI/10),y0+d*sin(PI/10));
//下面这行也是,怎么看都看不出有什么错误。
glVertex2f(x0+d0*cos(3*PI/10),y0+d0*sin(3*PI/10));
glEnd();
glBegin(GL_POLYGON);
glVertex2f(x0,y0+d);
glVertex2f(x0+d0*cos(3*PI/10),y0+d0*sin(3*PI/10));
glVertex2f(x0-d0*cos(3*PI/10),y0+d0*sin(3*PI/10));
glEnd();
}
void display()
{
glClearColor (1.0, 1.0, 1.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
Rectangle();
star(-0.58,0.25,0.1);
float x=-0.58;
float y=0.25;
float d=0.035;
float l=0.22;
float angle=PI/5;
for (int i=1; i<5; i++)
{
float x0,y0;
x0=x+l*sin(angle*i);
y0=y+l*cos(angle*i);
star(x0,y0,d);
}
glFlush();
}
void init()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(1.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0,1.0,-1.0,1.0);
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(200,150);
glutCreateWindow("Flag");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}