OpenGL(四)点、线、多边形

       像素是计算机屏幕上显示最小的单位。点是最简单的图元,我们可以理解为点的大小为一个像素,两个点可以确定一条线段,三条线段或三条以上可以组成为多边形,当然三角形为最简单的多边形。这篇我们一起来绘制点,线,多边形。

一、几何图元

  • GL_POINTS:点
  • GL_LINES:线段,二个点确定线段
  • GL_LINE_STRIP:第一个点依次连接的线段
  • GL_LINE_LOOP:和GL_LINE_STRIP相同,但首尾连接,形成环状
  • GL_POLYGON:多边形
  • GL_QUADS:由四个点组成一个四边形
  • GL_QUADS_STRIP:四边形带
  • GL_TRIANGLES:三角形,三个点确定
  • GL_TRIANGLE_STRIP:共用一个条带上的顶点的一组三角形
  • GL_TRIANGLE_FAN:以一个原点为中心呈扇形排列,公共相邻顶点的一组三角形

OpenGL(四)点、线、多边形_第1张图片


二、点

1.点的大小:glPointSize(GLfloat size);

2.获得点的大小范围

  • 定义点的大小范围数组:GLfloat  sizes[2];  //sizes[0]:最小值;sizes[1]:最大值
  • 获取点的大小范围:glGetFloatv(GL_POINT_SIZE_RANGE, sizes); //参数一:点范围枚举值;参数二:数组

3.获得点的步长(增量大小)

  • 定义点步长:GLfloat step;
  • 获取点的步长:glGetFloatv(GL_POINT_SIZE_GRANULARITY,step); //参数一:点步长枚举值;参数二:步长值


二、线

1.线的宽度:glLineWidth(GLfloat  width);


三、三角形

在默认情况下,逆时针方向环绕的多边形是正面,顺时针方向环绕是背面。

glFrontFace(GL_CW); //GL_CW:设置顺时针环绕为正面;GL_CCW:设置逆时针环绕为正面。


四、例子代码



#include 
#include 

#include 

GLBatch pointsBatch;
GLBatch linesBatch;
GLBatch lineStripBatch;
GLBatch lineLoopBatch;
GLBatch polygonBatch;
GLBatch guadsBatch;
GLBatch trianglesBatch; 
GLBatch trianglesStripBatch;
GLBatch guadStripBatch;
GLBatch triangleFanBatch;
GLShaderManager shaderManager;

int iStep = 0;    
int iPelType = 9; //图元类型数量

void SetupRec(void){
	//背景
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	shaderManager.InitializeStockShaders();
	GLfloat vVertex_1[4][3] = { { -0.5f, -0.5f, 0.0f },
							  { -0.5f, 0.5f, 0.0f },
							  { 0.5f, 0.5f, 0.0f },
							  { 0.5f, -0.5f, 0.0f } };

	//GL_POINTS,点
	pointsBatch.Begin(GL_POINTS, 4);  //参数二:4个顶点
	pointsBatch.CopyVertexData3f(vVertex_1);
	pointsBatch.End();

	//GL_LINES,线
	linesBatch.Begin(GL_LINES, 4);
	linesBatch.CopyVertexData3f(vVertex_1);
	linesBatch.End();
	
	//GL_LINE_STRIP,线带
	lineStripBatch.Begin(GL_LINE_STRIP, 4);
	lineStripBatch.CopyVertexData3f(vVertex_1);
	lineStripBatch.End();
	
	//GL_LINE_LOOP,线环
	lineLoopBatch.Begin(GL_LINE_LOOP, 4);
	lineLoopBatch.CopyVertexData3f(vVertex_1);
	lineLoopBatch.End();

	//GL_POLYGON,多边形
	polygonBatch.Begin(GL_POLYGON, 4);
	polygonBatch.CopyVertexData3f(vVertex_1);
	polygonBatch.End();


	GLfloat vVertex_2[8][3] = { { -0.5f, -0.5f, 0.0f },
								   { -0.5f, 0.5f, 0.0f },
								   { -0.1f, 0.5f, 0.0f },
								   { -0.1f, -0.5f, 0.0f },
								   { 0.1f, -0.5f, 0.0f },
								   { 0.1f, 0.5f, 0.0f },
								   { 0.5f, 0.5f, 0.0f },
								   { 0.5f, -0.5f, 0.0f } };

	//GL_QUADS,四边形
	guadsBatch.Begin(GL_QUADS, 8);
	guadsBatch.CopyVertexData3f(vVertex_2);
	guadsBatch.End();

	//GL_QUADS_STRIP,四边形带
	guadStripBatch.Begin(GL_QUAD_STRIP, 8);
	guadStripBatch.CopyVertexData3f(vVertex_2);
	guadStripBatch.End();

	//GL_TRIANGLES,三角形
	trianglesBatch.Begin(GL_TRIANGLES, 8);
	trianglesBatch.CopyVertexData3f(vVertex_2);
	trianglesBatch.End();

	//GL_TRIANGLE_STRIP,三角形带
	trianglesStripBatch.Begin(GL_TRIANGLE_STRIP, 8);
	trianglesStripBatch.CopyVertexData3f(vVertex_2);
	trianglesStripBatch.End();

	GLfloat vVertex_3[4][3] = { { 0.0f, 0.0f, 0.0f },
								{ 0.1f, 0.45f, 0.0f },
								{ 0.2f, 0.4f, 0.0f },
								{ 0.3f, 0.2f, 0.0f } };

	//GL_TRIANGLE_FAN,扇形
	triangleFanBatch.Begin(GL_TRIANGLE_FAN, 4);
	triangleFanBatch.CopyVertexData3f(vVertex_3);
	triangleFanBatch.End();
}

void KeyPressFunc(unsigned char key, int x, int y)
{
	
	//空格键
	if (key == 32)
	{
		iStep += 1;
		iStep = iStep > iPelType ? 0 : iStep;
	}

	switch (iStep)
	{
		case 0:
			glutSetWindowTitle("GL_POINTS"); //设置窗口标题
			break;
		case 1:
			glutSetWindowTitle("GL_LINES");
			break;
		case 2:
			glutSetWindowTitle("GL_LINE_STRIP");
			break;
		case 3:
			glutSetWindowTitle("GL_LINE_LOOP");
			break;
		case 4:
			glutSetWindowTitle("GL_POLYGON");
			break;
		case 5:
			glutSetWindowTitle("GL_QUADS");
			break;
		case 6:
			glutSetWindowTitle("GL_QUADS_STRIP");
			break;
		case 7:
			glutSetWindowTitle("GL_TRIANGLES");
			break;
		case 8:
			glutSetWindowTitle("GL_TRIANGLE_STRIP");
			break;
		case 9:
			glutSetWindowTitle("GL_TRIANGLE_FAN");
			break;
	}

	glutPostRedisplay();
}

void MyDisplay(void)
{
	glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

	switch (iStep)
	{
		case 0:
			glPointSize(5.0f); //点的大小
			pointsBatch.Draw();
			glPointSize(1.0f);
			break;
		case 1:
			glLineWidth(2.0f); //线的宽度
			linesBatch.Draw();
			glLineWidth(1.0f);
			break;
		case 2:
			glLineWidth(2.0f);
			lineStripBatch.Draw();
			glLineWidth(1.0f);
			break;
		case 3:
			glLineWidth(2.0f);
			lineLoopBatch.Draw();
			glLineWidth(1.0f);
			break;
		case 4:
			glLineWidth(2.0f);
			polygonBatch.Draw();
			glLineWidth(1.0f);
			break;
		case 5:
			guadStripBatch.Draw();
			break;
		case 6:
			guadsBatch.Draw();
			break;
		case 7:
			trianglesBatch.Draw();
			break;
		case 8:
			trianglesStripBatch.Draw();
			break;
		case 9:
			triangleFanBatch.Draw();
			break;
	}
	glutSwapBuffers();
}

int main(int argc, char *argv[])
{
	gltSetWorkingDirectory(argv[0]);
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_STENCIL);
	glutInitWindowSize(800, 600);
	glutCreateWindow("GL_POINTS");
	glutKeyboardFunc(KeyPressFunc);
	glutDisplayFunc(MyDisplay);

	GLenum err = glewInit();
	if (err != GLEW_OK)
	{
		std::cout << "init glew fail!" << std::endl;
		return 1;
	}
	SetupRec();
	glutMainLoop();

	return 0;
}


五、运行效果

OpenGL(四)点、线、多边形_第2张图片








你可能感兴趣的:(OpenGL)