蓝宝书 第三章
画线 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(GLfloat width);设置线宽度
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(GLint factor, GLushort pattern);//设置影响因子及模板格式
factor代表每位二进制数在线上用几个单位显示
例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;
}