OpenGL基本图元的绘制

目录

      • GL_POINTS
    • 线段
      • GL_LINES
      • GL_LINE_STRIP
      • GL_LINE_LOOP
    • 三角形
      • GL_TRIANGLES
      • GL_TRIANGLE_STRIP
      • GL_TRIANGLE_FAN
    • 四边形
      • GL_QUADS
      • GL_QUAD_STRIP
    • 多边形
      • GL_POLYGON逼近圆


GL_POINTS

直线,顶点两两一对

	glClearColor(0.0, 0.0, 0.0, 0.0);//清空背景颜色为黑色
	glPointSize(4.0f);//设置点的大小
	glBegin(GL_POINTS);		
		glVertex2f(-0.5, -0.5);
		glVertex2f(-0.5, 0.5);
		glVertex2f(0.5, 0.5);
		glVertex2f(0.5, -0.5);
	glEnd();

OpenGL基本图元的绘制_第1张图片

线段

GL_LINES

	glLineWidth(4.0f);//设置线宽
	glEnable(GL_LINE_STIPPLE);//启用点划模式
	glLineStipple(3, 0xff00);//点划模式,可建立虚线
	glColor3f(1.0, 0.0, 0.0);//设置颜色
	glBegin(GL_LINES);
		glVertex2f(-0.5, -0.5);
		glVertex2f(-0.5, 0.5);
		glVertex2f(0.5, 0.5);
		glVertex2f(0.5, -0.5);
	glEnd();

OpenGL基本图元的绘制_第2张图片

GL_LINE_STRIP

连续线段,n个点n-1条线段

	glBegin(GL_LINE_STRIP);
		glVertex2f(-0.5, -0.5);
		glVertex2f(-0.5, 0.5);
		glVertex2f(0.5, 0.5);
		glVertex2f(0.5, -0.5);
	glEnd();

OpenGL基本图元的绘制_第3张图片

GL_LINE_LOOP

连续线段成环,最后一个点和第一个点连接,n个点n条线段。

	glBegin(GL_LINE_LOOP);
		glVertex2f(-0.5, -0.5);
		glVertex2f(-0.5, 0.5);
		glVertex2f(0.5, 0.5);
		glVertex2f(0.5, -0.5);
	glEnd();

OpenGL基本图元的绘制_第4张图片

三角形

GL_TRIANGLES

	glBegin(GL_TRIANGLES);
		glVertex2f(-0.5, -0.5);
		glVertex2f(-0.5, 0.5);
		glVertex2f(0.5, 0.5);
		glVertex2f(0.5, -0.5);
	glEnd();

OpenGL基本图元的绘制_第5张图片

GL_TRIANGLE_STRIP

	glShadeModel(GL_FLAT);//块模式,颜色为三角形最后一个点的颜色
	//glShadeModel(GL_SMOOTH); //为默认,是平滑模式(渐变效果)
	glBegin(GL_TRIANGLE_STRIP);
		glClearColor(0.0, 0.0, 0.0, 0.0);
		glColor3f(1.0, 0.0, 0.0);
		glVertex2f(-0.5, -0.5);
		glColor3f(0.0, 1.0, 0.0);
		glVertex2f(-0.5, 0.5);
		glColor3f(0.0, 0.0, 1.0);
		glVertex2f(0.5, 0.5);
		glColor3f(1.0, 1.0, 0.0);
		glVertex2f(0.5, 1.0);
	glEnd();

OpenGL基本图元的绘制_第6张图片

GL_TRIANGLE_FAN

扇形

	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);//设置线框模式还有GL_FILL填充模式
	glBegin(GL_TRIANGLE_FAN);
		glVertex2f(-0.5, -0.5);
		glVertex2f(-0.5, 0.5);
		glVertex2f(0.5, 0.5);
		glVertex2f(0.5, -0.5);
	glEnd();

OpenGL基本图元的绘制_第7张图片

四边形

GL_QUADS

	glBegin(GL_QUADS);
		glClearColor(0.0, 0.0, 0.0, 0.0);
		glVertex2f(-0.5, -0.5);
		glVertex2f(-0.5, 0.5);
		glVertex2f(0.5, 0.5);
		glVertex2f(0.5, -0.5);
	glEnd();

OpenGL基本图元的绘制_第8张图片

GL_QUAD_STRIP

	glBegin(GL_QUAD_STRIP);
		glVertex2f(-0.5, -0.5);
		glVertex2f(-0.5, 0.5);
		glVertex2f(0.5, 0.5);
		glVertex2f(0.5, -0.5);
	glEnd();

OpenGL基本图元的绘制_第9张图片

多边形

GL_POLYGON逼近圆

	const int max_v = 100;
	float x[max_v];
	float y[max_v];
	float r = 0.5f;
	float alpha = 2 * M_PI / max_v;
	for (int i = 0; i < max_v; i++) {
		y[i] = r * sin(alpha*i);
		x[i] = r * cos(alpha*i);
		//printf("%d(%.2lf,%.2lf)\n", i, x[i], y[i]);
	}
	/* draw unit square polygon */
	//glPolygonStipple();
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	glBegin(GL_POLYGON);
		glClearColor(0.0, 0.0, 0.0, 0.0);
		for (int i = 0; i < max_v; i++) {
			glVertex2f(x[i], y[i]);
		}
	glEnd();

OpenGL基本图元的绘制_第10张图片

你可能感兴趣的:(OpenGL)