藍(lán)寶書 第三章
畫線 Line
1)兩點(diǎn)定一條實線
glBegin(GL_LINES);
glVertex3f(……);//點(diǎn)a
glVertex3f(……);//點(diǎn)b
glVertex3f(……);//點(diǎn)c
glVertex3f(……);//點(diǎn)d
glEnd();
繪制線段ab、cd
相關(guān)代碼見例3.4
2)繪制連續(xù)線段,連接密集的點(diǎn)成曲線
glBegin(GL_LINE_STRip);
glVertex3f(……);//點(diǎn)a
glVertex3f(……);//點(diǎn)b
glVertex3f(……);//點(diǎn)c
glVertex3f(……);//點(diǎn)d
glEnd();
繪制線段ab、bc、cd相關(guān)代碼見例3.5
3)設(shè)置線寬度voidglLineWidth(GLfloatwidth);設(shè)置線寬度
GLfloat sizes[2]; // 存放線寬度的范圍
GLfloat step; // 存放每次改變線寬度的最小增量
glGetFloatv(GL_LINE_WIDTH_RANGE,sizes);//獲取線寬范圍
glGetFloatv(GL_LINE_WIDTH_GRANULARITY,&step);//獲取最小增量
相關(guān)代碼見例3.6
4)繪制虛線,自定義虛線模板
glEnable(GL_LINE_STIPPLE);//啟用模板功能
void glLineStipple(GLintfactor, GLushort pattern);//設(shè)置影響因子及模板格式

factor代表每位二進(jìn)制數(shù)在線上用幾個單位顯示
相關(guān)代碼見例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);//顯示回調(diào)函數(shù)  	glutReshapeFunc(ChangeSize);//窗口大小變形回調(diào)函數(shù)  	SetupRC();	glutMainLoop();	return 0;}例3.5 點(diǎn)連曲線#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);//顯示回調(diào)函數(shù)  	glutReshapeFunc(ChangeSize);//窗口大小變形回調(diào)函數(shù)  	SetupRC();	glutMainLoop();	return 0;}例3.6 調(diào)整線寬度#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);//顯示回調(diào)函數(shù)  	glutReshapeFunc(ChangeSize);//窗口大小變形回調(diào)函數(shù)  	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);//顯示回調(diào)函數(shù)  	glutReshapeFunc(ChangeSize);//窗口大小變形回調(diào)函數(shù)  	SetupRC();	glutMainLoop();	return 0;}
新聞熱點(diǎn)
疑難解答