openGL实现二维图形和三维图形 – 紫色的小黄人

  OpenGL是一任一某一特别的的粗涂图形库,它的命令基本的是用C空话了解的。。OpenGL界说了一任一某一图形化的顺序界间的,常用于表现处置三维图像,功能特别的,附近的呼叫,图像处置特别的盛行。

  图形的主要用途是OpenGL的成套用品。:GLUT。

  GLUT (判读员) like the glut in gluttony) is the OpenGL Utility Toolkit, a window system independent toolkit for writing OpenGL 顺序。 It implements a simple windowing application programming interface (API) for OpenGL。

  GLUT is designed for constructing small to medium sized OpenGL 顺序。 While GLUT is well-suited to learning OpenGL and developing simple OpenGL applications, GLUT is not a full-featured toolkit so large applications requiring sophisticated user interfaces are better off using native window system toolkits like 动机。 GLUT is simple, easy, and 小的。

  GLUT向巨型以协议约束可能性找错误未受损伤的无效的,它是为大约中小型OpenGL以协议约束而设计的。。

  1。二维图像的了解关系上地复杂,直接下密码:

#include 

void 矩形(净空) {
GL开始的重大聚会是OpenGL的基准重大聚会 运用提出缓冲剂的值重放之物指派的缓冲剂。 glClear(GL_COLOR_BUFFER_BIT);
//画矩形 //glRectf(-0.5f, -0.5f, 0.5f, 0.5f); //画垂线 glBegin(GL_LINES); glVertex2f(0.5f, 0.5f); glVertex2f(-0.5f, -0.5f); glEnd(); //破缓冲,确保可以管理基址图命令 glFlush(); } int 主(int) argc, char *argv[]) { GLUT的设定初值 library glutInit(&argc, argv); 设定初值窗口的上涂料 glutInitWindowSize(300, 300); // 设置窗口的臀部 //glutInitWindowPosition(int) x, int y); 设定初值顺序显示制作样品 glutInitDisplayMode(GLUT_RGBA); glutCreateWindow("project of OpenGL //win: 指派子窗口的父窗口 //glutCreateSubWindow(int) win, int x, int y, int width, int 地平纬度) 设置提出窗口的重大聚会回调,用于基址图 glutDisplayFunc(&Rectangle); GLUT事变运转,别的方式,你看不到图形音响效果。 glutMainLoop(); return 0; }

  了解音响效果:

         

  你也可以在窗口画一任一某一圆。:

#include 
GLfloat r = 0.5f; GLfloat PI = 3.141592653f; int pre = 30; glBegin(GL_POLYGON);//画多角形 for (int) i = 0; i < pre; i++) { glVertex2f(r * cos(2 * PI*i / pre), r * sin(2 * PI*i / pre)); } glEnd();

  确实,这样圆找错误圆的,但多角形的迫近。运用多角形多角形绘制多角形,边数实现一定程度,审视的样子与圆证实。。

  

  2.接下来来了解三维图像Cube。

  三维间隔:  

  样品看待替换(GLYMyVIEW):从对立有关运动的的立场看,变更察看点的臀部和环境判定变更TH。

  视力阴沉间隔(GLP-阴沉):界说视觉未填写的,视觉未填写的要不是的目标将不熟练的被绘制到庇护上。。

  视口间隔(GLVIEWPT):通常形势下,顺序默许像素装填物总计窗口,经过视口间隔在Windows上设置像素装填物。。

  每回变更男朋友,将提出运算的矩阵设为TR矩阵是理由的。,在间隔垄断,有理由将矩阵间隔成单位矩阵。。

glMatrixMode(GL_PROJECTION);
格子存在;

  未受损伤的密码:

#include 
#include 

void setCube(void) {
	glClearColor(1.0, 1.0, 1.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0, 0, 0);// /设置布莱克
	格子存在;//重读单位矩阵
	gluLookAt(6, 0, 2.5, 0, 0, 0, 1, 1, 0);
前三个参项察看器的察看臀部,位于正中的三个参项察看点的臀部。,以下三个参项察看器的测量环境判定
GlLineWidth();/ / /设置修整像素宽度,默许值为1.0F。
	glutWireCube();
	glFlush();
}

void drawCube(void) {
	glClearColor(1.0, 1.0, 1.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	格子存在;
	gluLookAt(4, 0, 1.5, 0, 0, 0, 1, 1, 0);

三次幂的外部
	glColor3f(0, 1, 0);
	glBegin(GL_QUADS);
		//---1---
		glNormal3f(-1, 0, 0);/ / /设定点正规的航向
		glVertex3f(0.5, 0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, -0.5);
		glVertex3f(0.5, 0.5, -0.5);
		//---2---
		glNormal3f(-1, 0, 0);
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(-0.5, -0.5, -0.5);
		glVertex3f(-0.5, 0.5, -0.5);
		//---3---
		glNormal3f(0, 1, 0);
		glVertex3f(0.5, 0.5, 0.5);
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, 0.5, -0.5);
		glVertex3f(0.5, 0.5, -0.5);
		//---4---
		glNormal3f(0, -1, 0);
		glVertex3f(0.5, -0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(-0.5, -0.5, -0.5);
		glVertex3f(0.5, -0.5, -0.5);
		//---5---
		glNormal3f(0, 0, 1);
		glVertex3f(0.5, 0.5, 0.5);
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
		//---6---
		glNormal3f(0, 0, -1);
		glVertex3f(0.5, 0.5, 0.5);
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
	glEnd();

	//draw
	glColor3f(0, 0, 0);
	glLineWidth();
绘制三次幂的正面
	glBegin(GL_LINES);
		//---1---
		glVertex3f(0.5, 0.5, 0.5);
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
		glVertex3f(0.5, 0.5, 0.5);
		//---2---
		glVertex3f(0.5, 0.5, -0.5);
		glVertex3f(-0.5, 0.5, -0.5);
		glVertex3f(-0.5, 0.5, -0.5);
		glVertex3f(-0.5, -0.5, -0.5);
		glVertex3f(-0.5, -0.5, -0.5);
		glVertex3f(0.5, -0.5, -0.5);
		glVertex3f(0.5, -0.5, -0.5);
		glVertex3f(0.5, 0.5, -0.5);
		//---3---
		glVertex3f(0.5, 0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, 0.5);
		glVertex3f(0.5, -0.5, -0.5);
		glVertex3f(0.5, -0.5, -0.5);
		glVertex3f(0.5, 0.5, -0.5);
		glVertex3f(0.5, 0.5, -0.5);
		glVertex3f(0.5, 0.5, 0.5);
		//---4---
		glVertex3f(-0.5, 0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(-0.5, -0.5, 0.5);
		glVertex3f(-0.5, -0.5, -0.5);
		glVertex3f(-0.5, -0.5, -0.5);
		glVertex3f(-0.5, 0.5, -0.5);
		glVertex3f(-0.5, 0.5, -0.5);
		glVertex3f(-0.5, 0.5, 0.5);
	glEnd();
	glFlush();
}

void threeD(int) w, int h) {
	glViewport(0, 0, (GLsizei)w, (格列西)h);/ /整洁的看待窗口上涂料
	glMatrixMode(GL_PROJECTION);
	格子存在;
	glFrustum(-1.0, 1.0, -1.0, 1.0, , 10.0);
	glMatrixMode(GL_MODELVIEW);

}

int 主(int) argc, char *argv[]) {

GLUT的设定初值 library
	glutInit(&argc, argv);
设定初值窗口的上涂料
	glutInitWindowSize(500, 500); 
	// 设置窗口的臀部
	//glutInitWindowPosition(int) x, int y);
设定初值顺序显示制作样品
	glutInitDisplayMode(GLUT_RGBA);
	glutCreateWindow("project of OpenGL
设置提出窗口的重大聚会回调,用于基址图 glutDisplayFunc(drawCube); 窗口杂耍重大聚会 glutReshapeFunc(threeD); glutMainLoop(); return 0; }

  你可以转会OpenGL的GrutWiRebe 上涂料)绘制平面正方形,如SETBUCK重大聚会。

  我以为了解一任一某一绿色的三次幂与黑色的修整。,得绘制三次幂。,如DuffCube重大聚会。

  了解音响效果:

  

  

发表评论

电子邮件地址不会被公开。 必填项已用*标注