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

首頁 > 學院 > 開發設計 > 正文

OpenGL 學習筆記3_2(繪制線相關)

2019-11-11 04:38:10
字體:
來源:轉載
供稿:網友

藍寶書 第三章

畫線 Line

1)兩點定一條實線

glBegin(GL_LINES);

glVertex3f(……);//點a

glVertex3f(……);//點b

glVertex3f(……);//點c

glVertex3f(……);//點d

glEnd();

繪制線段ab、cd

相關代碼見例3.4

2)繪制連續線段,連接密集的點成曲線

glBegin(GL_LINE_STRip);

glVertex3f(……);//點a

glVertex3f(……);//點b

glVertex3f(……);//點c

glVertex3f(……);//點d

glEnd();

繪制線段ab、bc、cd

相關代碼見例3.5

3)設置線寬度

voidglLineWidth(GLfloatwidth);設置線寬度

GLfloat sizes[2]; // 存放線寬度的范圍

GLfloat step; // 存放每次改變線寬度的最小增量

glGetFloatv(GL_LINE_WIDTH_RANGE,sizes);//獲取線寬范圍

glGetFloatv(GL_LINE_WIDTH_GRANULARITY,&step);//獲取最小增量

相關代碼見例3.6

4)繪制虛線,自定義虛線模板

glEnable(GL_LINE_STIPPLE);//啟用模板功能

void glLineStipple(GLintfactor, GLushort pattern);//設置影響因子及模板格式

factor代表每位二進制數在線上用幾個單位顯示

相關代碼見例3.7

例3.4 繪制多條線段

#include <windows.h>  #include <math.h>  #include <GL/GL.h>  #include <GL/GLU.h>  #include <GL/glut.h>  // 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, z, angle; // Storage for coordinates and angles  	GLfloat sizes[2]; // Store supported point size range  	GLfloat step; // Store supported point size increments  	GLfloat curSize; // Store current point size  	// Clear the window with current clearing color  	glClear(GL_COLOR_BUFFER_BIT);	// Save matrix state and do the rotation  	glPushMatrix();	//glRotatef(30.0f, 1.0f, 0.0f, 0.0f);//書中的xRot代表x軸偏移的角度  	//glRotatef(30.0f, 0.0f, 1.0f, 0.0f);//書中的yRot代表y軸偏移的角度  	// Get supported point size range and step size  	glGetFloatv(GL_POINT_SIZE_RANGE, sizes);	glGetFloatv(GL_POINT_SIZE_GRANULARITY, &step);	// Set the initial point size  	curSize = sizes[0];	// Set beginning z coordinate  	z = -50.0f;	// Call only once for all remaining points  	// Call only once for all remaining points	glBegin(GL_LINES);	// All lines lie in the xy plane.	z = 0.0f;	for (angle = 0.0f; angle <= GL_PI; angle += (GL_PI / 20.0f))	{		// Top half of the circle		x = 50.0f*sin(angle);		y = 50.0f*cos(angle);		glVertex3f(x, y, z); // First endpoint of line		// Bottom half of the circle		x = 50.0f*sin(angle + GL_PI);		y = 50.0f*cos(angle + GL_PI);		glVertex3f(x, y, z); // Second endpoint of line	}	// Done drawing points	glEnd();	// Restore transformations  	glPopMatrix();	// Flush drawing commands  	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);}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);//顯示回調函數  	glutReshapeFunc(ChangeSize);//窗口大小變形回調函數  	SetupRC();	glutMainLoop();	return 0;}例3.5 點連曲線

#include <windows.h>  #include <math.h>  #include <GL/GL.h>  #include <GL/GLU.h>  #include <GL/glut.h>  // 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, z, angle; // Storage for coordinates and angles  	GLfloat sizes[2]; // Store supported point size range  	GLfloat step; // Store supported point size increments  	GLfloat curSize; // Store current point size  	// Clear the window with current clearing color  	glClear(GL_COLOR_BUFFER_BIT);	// Save matrix state and do the rotation  	glPushMatrix();	glRotatef(30.0f, 1.0f, 0.0f, 0.0f);//書中的xRot代表x軸偏移的角度  	glRotatef(30.0f, 0.0f, 1.0f, 0.0f);//書中的yRot代表y軸偏移的角度  	// Get supported point size range and step size  	glGetFloatv(GL_POINT_SIZE_RANGE, sizes);	glGetFloatv(GL_POINT_SIZE_GRANULARITY, &step);	// Set the initial point size  	curSize = sizes[0];	// Set beginning z coordinate  	z = -50.0f;	// Call only once for all remaining points  	// Call only once for all remaining points	glBegin(GL_LINE_STRIP);	z = -50.0f;	for (angle = 0.0f; angle <= (2.0f*GL_PI)*3.0f; angle += 0.1f)	{		x = 50.0f*sin(angle);		y = 50.0f*cos(angle);		// Specify the point and move the z value up a little		glVertex3f(x, y, z);		z += 0.5f;	}	// Done drawing points	glEnd();	// Restore transformations  	glPopMatrix();	// Flush drawing commands  	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);}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);//顯示回調函數  	glutReshapeFunc(ChangeSize);//窗口大小變形回調函數  	SetupRC();	glutMainLoop();	return 0;}例3.6 調整線寬度

#include <windows.h>  #include <math.h>  #include <GL/GL.h>  #include <GL/GLU.h>  #include <GL/glut.h>  // 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 y; // Storage for varying Y coordinate	GLfloat fSizes[2]; // Line width range metrics	GLfloat fCurrSize; // Save current size	// Clear the window with current clearing color  	glClear(GL_COLOR_BUFFER_BIT);	// Save matrix state and do the rotation  	glPushMatrix();	//glRotatef(30.0f, 1.0f, 0.0f, 0.0f);//書中的xRot代表x軸偏移的角度  	//glRotatef(30.0f, 0.0f, 1.0f, 0.0f);//書中的yRot代表y軸偏移的角度  	// Get supported point size range and step size  	glGetFloatv(GL_LINE_WIDTH_RANGE, fSizes);	fCurrSize = fSizes[0];	// Set the initial point size  	// Step up y axis 20 units at a time	for (y = -90.0f; y < 90.0f; y += 20.0f)	{		// Set the line width		glLineWidth(fCurrSize);		// Draw the line		glBegin(GL_LINES);		glVertex2f(-80.0f, y);		glVertex2f(80.0f, y);		glEnd();		// Increase the line width		fCurrSize += 1.0f;	}	// Restore transformations  	glPopMatrix();	// Flush drawing commands  	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);}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);//顯示回調函數  	glutReshapeFunc(ChangeSize);//窗口大小變形回調函數  	SetupRC();	glutMainLoop();	return 0;}

例3.7 自定義模板繪制虛線

#include <windows.h>  #include <math.h>  #include <GL/GL.h>  #include <GL/GLU.h>  #include <GL/glut.h>  // 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 y; // Storage for varying y coordinate	GLint factor = 1; // Stippling factor	GLushort pattern = 0x5555; // Stipple pattern	// Clear the window with current clearing color  	glClear(GL_COLOR_BUFFER_BIT);	// Save matrix state and do the rotation  	glPushMatrix();	//glRotatef(30.0f, 1.0f, 0.0f, 0.0f);//書中的xRot代表x軸偏移的角度  	//glRotatef(30.0f, 0.0f, 1.0f, 0.0f);//書中的yRot代表y軸偏移的角度  	// Get supported point size range and step size  	glEnable(GL_LINE_STIPPLE);	// Step up Y axis 20 units at a time	for (y = -90.0f; y < 90.0f; y += 20.0f)	{		// Reset the repeat factor and pattern		glLineStipple(factor, pattern);		// Draw the line		glBegin(GL_LINES);		glVertex2f(-80.0f, y);		glVertex2f(80.0f, y);		glEnd();		factor++;	}	// Restore transformations  	glPopMatrix();	// Flush drawing commands  	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);}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);//顯示回調函數  	glutReshapeFunc(ChangeSize);//窗口大小變形回調函數  	SetupRC();	glutMainLoop();	return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 卫辉市| 霍山县| 金沙县| 新巴尔虎右旗| 邵阳市| 峨山| 格尔木市| 西林县| 青川县| 大关县| 仪征市| 错那县| 苏尼特左旗| 房产| 乌兰察布市| 徐汇区| 崇仁县| 衡阳县| 镇沅| 平乡县| 洛川县| 京山县| 七台河市| 乌审旗| 石门县| 神木县| 洮南市| 汽车| 新蔡县| 长子县| 桑植县| 福海县| 两当县| 柞水县| 酒泉市| 锦屏县| 海安县| 佛学| 怀宁县| 金昌市| 汕头市|