国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

OpenGL 學習筆記3_3(繪制三角形相關(guān))

2019-11-11 02:52:24
字體:
供稿:網(wǎng)友

藍寶書 第三章

畫三角形 triangle

1)繪制三角形(三個點確定一個三角形)

glBegin(GL_TRIANGLES);glVertex2f(0.0f, 0.0f); // 點aglVertex2f(25.0f, 25.0f); // 點bglVertex2f(50.0f, 0.0f); // 點cglVertex2f(-50.0f, 0.0f); // 點d glVertex2f(-75.0f, 50.0f); // 點eglVertex2f(-25.0f, 0.0f); // 點fglEnd(); 繪制三角形abc和三角形def

2)繪制三角形(新增三角形頂點)

glBegin(GL_TRIANGLE_STRip);glVertex2f(0.0f, 0.0f); // 點aglVertex2f(25.0f, 25.0f); // 點bglVertex2f(50.0f, 0.0f); // 點cglVertex2f(-50.0f, 0.0f); // 點d glVertex2f(-75.0f, 50.0f); // 點eglVertex2f(-25.0f, 0.0f); // 點fglEnd(); 繪制三角形abc、bcd、def(三點連線順序不定,應(yīng)該是與第一個三角形順時針or逆時針連線統(tǒng)一)

3)繪制三角形(同一頂點,最后封閉)

glBegin(GL_TRIANGLE_FAN);glVertex2f(0.0f, 0.0f); // 點aglVertex2f(25.0f, 25.0f); // 點bglVertex2f(50.0f, 0.0f); // 點cglVertex2f(-50.0f, 0.0f); // 點d glVertex2f(-75.0f, 50.0f); // 點eglVertex2f(-25.0f, 0.0f); // 點fglEnd();

繪制三角形abc、acd、ade、aef及afb

相關(guān)代碼見例3.8

例3.8 繪制三角形

#include <windows.h>  #include <math.h>  #include <GL/GL.h>  #include <GL/GLU.h>  #include <GL/glut.h>  GLboolean bCull = true;GLboolean bDepth = true;GLboolean bOutline = true;GLfloat xRot = 30.0f;GLfloat yRot = 30.0f;// Define a constant for the value of PI  #define GL_PI 3.1415f  // This function does any needed initialization on the rendering  void RenderScene(void){	GLfloat x, y, angle; // Storage for coordinates and angles	int iPivot = 1; // Used to flag alternating colors	// Clear the window and the depth buffer	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Turn culling on if flag is set	if (bCull)		glEnable(GL_CULL_FACE);	else		glDisable(GL_CULL_FACE);	// Enable depth testing if flag is set	if (bDepth)		glEnable(GL_DEPTH_TEST);	else		glDisable(GL_DEPTH_TEST);	// Draw the back side as a wireframe only, if flag is set	if (bOutline)		glPolygonMode(GL_BACK, GL_LINE);	else		glPolygonMode(GL_BACK, GL_FILL);	// Save matrix state and do the rotation	glPushMatrix();	glRotatef(xRot, 1.0f, 0.0f, 0.0f);	glRotatef(yRot, 0.0f, 1.0f, 0.0f);	// Begin a triangle fan	glBegin(GL_TRIANGLE_FAN);	// Pinnacle of cone is shared vertex for fan, moved up z-axis	// to PRoduce a cone instead of a circle	glVertex3f(0.0f, 0.0f, 75.0f);	// Loop around in a circle and specify even points along the circle	// as the vertices of the triangle fan	for (angle = 0.0f; angle < (2.0f*GL_PI); angle += (GL_PI / 8.0f))	{		// Calculate x and y position of the next vertex		x = 50.0f*sin(angle);		y = 50.0f*cos(angle);		// Alternate color between red and green		if ((iPivot % 2) == 0)			glColor3f(0.0f, 1.0f, 0.0f);		else			glColor3f(1.0f, 0.0f, 0.0f);		// Increment pivot to change color next time		iPivot++;		// Specify the next vertex for the triangle fan		glVertex2f(x, y);	}	// Done drawing fan for cone	glEnd();	// Restore transformations	glPopMatrix();	glutSwapBuffers();}void SetupRC(){	// Black background	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);	// Set drawing color to green	glColor3f(0.0f, 1.0f, 0.0f);	// Set color shading model to flat	glShadeModel(GL_FLAT);	// Clockwise-wound polygons are front facing; this is reversed	// because we are using triangle fans	glFrontFace(GL_CW);}void ChangeSize(GLsizei w, GLsizei h){	GLfloat nRange = 100.0f;	// Prevent a divide by zero  	if (h == 0)		h = 1;	// Set Viewport to window dimensions  	glViewport(0, 0, w, h);	// Reset projection matrix stack  	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	// Establish clipping volume (left, right, bottom, top, near, far)  	if (w <= h)		glOrtho(-nRange, nRange, -nRange*h / w, nRange*h / w, -nRange, nRange);	else		glOrtho(-nRange*w / h, nRange*w / h, -nRange, nRange, -nRange, nRange);	// Reset Model view matrix stack  	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();}int main(int argc, char* argv[]){	glutInit(&argc, argv);	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);	glutInitWindowSize(800, 600);	glutCreateWindow("Bounce");	glutDisplayFunc(RenderScene);//顯示回調(diào)函數(shù)  	glutReshapeFunc(ChangeSize);//窗口大小變形回調(diào)函數(shù)  	SetupRC();	glutMainLoop();	return 0;}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿合奇县| 鹤庆县| 阿克| 浙江省| 白沙| 峨眉山市| 澎湖县| 衡山县| 湖北省| 玉林市| 双峰县| 许昌县| 平度市| 平陆县| 类乌齐县| 西充县| 沂源县| 青神县| 吉木萨尔县| 饶河县| 富宁县| 霍林郭勒市| 准格尔旗| 五莲县| 汾阳市| 三河市| 弋阳县| 平原县| 南岸区| 扶风县| 尉犁县| 松溪县| 中方县| 余姚市| 灵武市| 海南省| 文水县| 库尔勒市| 嫩江县| 沈阳市| 黄浦区|