eg: dotted/dashed Using glLineStiple(GLint factor,GLushort pattern)
// Lstipple.c
// OpenGL SuperBible, Chapter 4
// Demonstrates line stippling
// Program by Richard S. Wright Jr.
#define FREEGLUT_STATIC
#include <GL/glut.h>
// Define a constant for the value of PI
#define GL_PI 3.1415f
// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
// Called to draw scene
void RenderScene(void)
{
GLfloat y; // Storeage for varying Y coordinate
GLint factor = 1; // Stippling factor
GLushort pattern = 0x55ff; // Stipple pattern
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
// Save matrix state and do the rotation
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
// Enable Stippling
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();
}
// This function does any needed initialization on the rendering
// context.
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 SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(xRot > 356.0f)
{
xRot = 0.0f;
printf("here 1\n");
}
if(xRot < -1.0f)
{
xRot = 355.0f;
printf("here 2\n");
}
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void ChangeSize(int w, int 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 coordinate system
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);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Stippled Line Example");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}
glLineStipple(GLint factor,GLushort pattern)中参数的含义:
pattern是由1或0组成的16位序列,“1”代表绘制这个像素,“0”代表不绘制这个像素。结合factor,“1”代表连续绘制factor个像素,“0”代表不绘制连续factor个像素。如此,重复pattern。
举个例子:0xffff:默认值,代表直线;0x5555:dotted;0x00ff:dashed;0x1c47:dash/dot/dash..
注意:LineStipple的绘制pattern与实际绘制的顺序是相反的,比如上面这个例子,pattern=0x55ff,实际绘制是这个顺序:“1111 1111 1010 1010”。原因:在内部,OpenGL通过左移pattern来操作pattern以获取下一个掩码值是更加快速的。OpenGL的设计目标就是实现高性能的图形,因此它常常使用类似的技巧。
有图为证:

浙公网安备 33010602011771号