OpenGL experiment draws three kinds of five-pointed stars

OpenGL experiment draws three kinds of five-pointed stars

Since I couldn't find a similar one on the Internet when I was doing it, I wanted to post it, hoping to provide you with an idea.

First on the result map

insert image description here
insert image description here
insert image description here
The three figures belong to (1) wireframe display (2) uniform coloring (3) two colors,

  1. Wireframe display :
    Use glBegin (GL_LINE_LOOP); to draw end-to-end graph lines, so we only need to give the coordinates of ten points to draw a five-pointed star.
    When the coordinates of a point are given, the five-pointed star can be regarded as a concentric circle .
    So only the radius R of the big circle and the radius r of the small circle affect the shape of the five-pointed star.
    insert image description here
    Assignment code : By the way, let me tell you about my code vertex order:
    insert image description here
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);

	glBegin(GL_LINE_LOOP);//图形类型,GL_LINES为线段GL_TRIANGLES,GL_LINE_STRIP,GL_LINE_LOOP
	float R=100,r=40;
	float a,b,c,d;
	a=200.0f;
	b=300.0f;
	c=a;
	d=b-100;
		glColor3f(1.0f,0.0f,0.0f);//指定顶点颜色

		glVertex2f(a,b);//指定顶点坐标 【1】
		  glVertex2f(c-0.59*r,d+0.81*r);//sin36=0.59,cos36=0.81   【2】
		glVertex2f(a-0.95*R,b-R+0.31*R);//cos18=0.95,sin18=0.31【3】
		  glVertex2f(c-0.95*r,d-0.31*r);//【4】
		glVertex2f(a-0.59*R,b-R-0.81*R);//cos54=0.59,sin54=0.81【5】
		   glVertex2f(c,d-r);//【6】
		glVertex2f(a+0.59*R,b-R-0.81*R);//【7】
		 glVertex2f(c+0.95*r,d-0.31*r);//【8】
		glVertex2f(a+0.95*R,b-R+0.31*R);//【9】
		  glVertex2f(c+0.59*r,d+0.81*r);//【10】
	glEnd();
	//glFlush();//单缓冲时必须要,说明绘图命令(函数)结束
	glutSwapBuffers();//交换缓冲(双缓冲时使用)
}
  1. Two colors : Let’s talk about two colors first, because the five-pointed star is not a convex polygon, and OpenGL draws a polygon by default as a filled polygon, and the requirement is a convex polygon, so the result of drawing a concave polygon in fill mode is wrong.
    insert image description here
    So we should adopt another way of thinking: the five-pointed star (concave 10-gon) is decomposed into 10 triangles to draw.
    For simplicity, I just pasted the code for drawing the triangle 10 times o(╥﹏╥)o, I hope everyone Can be faster and more concise. Let’s take a look at the picture, it’s simple and rough, this is my drawing sequence, since all 10 points are known, it’s very simple, and the filling and drawing of the triangle is also very simple, so I’ll just write the code. (Supplementary sentence: the extra 0 point is the center point)
    Double color is to change the fill color every other triangle, which is easy to understand~~insert image description here
void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);

	glBegin(GL_LINE_LOOP);//图形类型,GL_LINES为线段GL_TRIANGLES,GL_LINE_STRIP,GL_LINE_LOOP
	float R=100,r=40;
	float a,b,c,d;
	a=200.0f;
	b=300.0f;
	c=a;
	d=b-100;
	glColor3f(1.0f,0.0f,0.0f);//《1》
	glBegin(GL_TRIANGLES);
	glVertex2f(200,200);//【0】
	glVertex2f(200,300);//指定顶点坐标 【1】
	glVertex2f(c-0.59*r,d+0.81*r);//sin36=0.59,cos36=0.81   【2】
	glEnd();

		glColor3f(0.0f,1.0f,1.0f);//《2》
	glBegin(GL_TRIANGLES);
	glVertex2f(200,200);//【0】
	glVertex2f(c-0.59*r,d+0.81*r);//sin36=0.59,cos36=0.81   【2】
		glVertex2f(a-0.95*R,b-R+0.31*R);//cos18=0.95,sin18=0.31【3】
	glEnd();

		glColor3f(1.0f,0.0f,0.0f);//《3》
	glBegin(GL_TRIANGLES);
	glVertex2f(200,200);//【0】
	glVertex2f(a-0.95*R,b-R+0.31*R);//cos18=0.95,sin18=0.31【3】
		  glVertex2f(c-0.95*r,d-0.31*r);//【4】
	glEnd();

		glColor3f(0.0f,1.0f,1.0f);//《4》
	glBegin(GL_TRIANGLES);
	glVertex2f(200,200);//【0】
	  glVertex2f(c-0.95*r,d-0.31*r);//【4】
		glVertex2f(a-0.59*R,b-R-0.81*R);//cos54=0.59,sin54=0.81【5】
	glEnd();

		glColor3f(1.0f,0.0f,0.0f);//《5》
	glBegin(GL_TRIANGLES);
	glVertex2f(200,200);//【0】
		glVertex2f(a-0.59*R,b-R-0.81*R);//cos54=0.59,sin54=0.81【5】
		   glVertex2f(c,d-r);//【6】
	glEnd();

		glColor3f(0.0f,1.0f,1.0f);//《6》
	glBegin(GL_TRIANGLES);
	glVertex2f(200,200);//【0】
		   glVertex2f(c,d-r);//【6】
		glVertex2f(a+0.59*R,b-R-0.81*R);//【7】
	glEnd();

		glColor3f(1.0f,0.0f,0.0f);//《7》
	glBegin(GL_TRIANGLES);
	glVertex2f(200,200);//【0】
glVertex2f(a+0.59*R,b-R-0.81*R);//【7】
		 glVertex2f(c+0.95*r,d-0.31*r);//【8】
	glEnd();

		glColor3f(0.0f,1.0f,1.0f);//《8》
	glBegin(GL_TRIANGLES);
	glVertex2f(200,200);//【0】
	 glVertex2f(c+0.95*r,d-0.31*r);//【8】
		glVertex2f(a+0.95*R,b-R+0.31*R);//【9】
	glEnd();

		glColor3f(1.0f,0.0f,0.0f);//《9》
	glBegin(GL_TRIANGLES);
	glVertex2f(200,200);//【0】
glVertex2f(a+0.95*R,b-R+0.31*R);//【9】
		  glVertex2f(c+0.59*r,d+0.81*r);//【10】
	glEnd();

		glColor3f(0.0f,1.0f,1.0f);//《10》
	glBegin(GL_TRIANGLES);
	glVertex2f(200,200);//【0】
	 glVertex2f(c+0.59*r,d+0.81*r);//【10】
	glVertex2f(200,300);//指定顶点坐标 【1】
	glEnd();
	//glFlush();//单缓冲时必须要,说明绘图命令(函数)结束
	glutSwapBuffers();//交换缓冲(双缓冲时使用)
}
  1. Uniform coloring
    is very simple, just put 10 triangles in one color * *.

The idea is relatively simple, everyone can make do with it, if you have any ideas, you can leave a message, Nice!

Guess you like

Origin blog.csdn.net/weixin_42650480/article/details/104972454
Recommended