欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OpenGL实现画线-线形-线宽转变

程序员文章站 2022-07-04 15:00:59
...

思路:

在画线算法的基础上,添加像素掩膜,&1为1则画否则不画。

// ====== Computer Graphics Experiment #4 ======
// |      Line style using pixel mask          |
// |         and line width                    |  
// =============================================
//
// Requirement:
// (1) Implement a general Bresenham's Line Drawing Algorithm.
// (2) Implement solid, dashed, dotted and dot-dashed line styles 
//     using pixel mask. 
// (3) Implement keyboard function so that the user can switch 
//     line style by pressing a key.
// Only use GL_POINTS drawing mode. 
// Do not use OpenGL line drawing capability.
// Use bit shift statement in C: "<<" or ">>" 
// to implement pixel mask .

#include <windows.h>
#include <GL/glut.h>
#include <math.h>

#define PI 3.14159265359
int width, height;

int line_style=0;
unsigned int pixel_mask[]={0xffff, 0xf0f00, 0xfff0, 0xf00  };
int line_width=1;


void initcor(int &dx, int &dy, int &x, int& y, int &p, int &xs, int &ys, int& xe, int& ye)
{

	dx = xe - xs;
	dy = ye - ys;
	x = xs, y = ys;
	p = 2 * dy - dx;
}
// Bresenham line algorithm
void MyLine(int xs, int ys, int xe, int ye)
{
	// Write your code here
	int dx, dy, x, y, p;
	initcor(dx, dy, x, y, p, xs, ys, xe, ye);
	glBegin(GL_POINTS);
	unsigned int bit_mask = pixel_mask[line_style];
	for (int i = 0; i < dx; ++i)
	{
		bit_mask >>= 1;
		if (!bit_mask)
			bit_mask = pixel_mask[line_style];
		if (bit_mask&1)
		{
			glVertex2i(x, y);
			glVertex2i(y, x);
		}
		++x;
		if (p < 0)
			p += 2 * dy;
		else
		{
			++y; p += 2 * dy - 2 * dx;
		}
	}
	initcor(dx, dy, x, y, p, xs, ys, xe, ye);
	bit_mask = pixel_mask[line_style];
	for (int i = 0; i < dx; ++i)
	{
		bit_mask >>= 1;
		if (!bit_mask)
			bit_mask = pixel_mask[line_style];
		if (bit_mask & 1)
		{
			glVertex2i(x, y);
			glVertex2i(y, x);
		} 
		--x;
		if (p < 0)
			p += 2 * dy;
		else
		{
			--y; p += 2 * dy - 2 * dx;
		}
	}
	initcor(dx, dy, x, y, p, xs, ys, xe, ye); 
	bit_mask = pixel_mask[line_style];
	for (int i = 0; i < dx; ++i)
	{

		bit_mask >>= 1;
		if (!bit_mask)
			bit_mask = pixel_mask[line_style];
		if (bit_mask & 1)
		{
			glVertex2i(x, y);
			glVertex2i(y, x);
		}
		++x;
		if (p < 0)
			p += 2 * dy;
		else
		{
			--y; p += 2 * dy - 2 * dx;
		}
	}
	initcor(dx, dy, x, y, p, xs, ys, xe, ye);
	bit_mask = pixel_mask[line_style];
	for (int i = 0; i < dx; ++i)
	{
		bit_mask >>= 1;
		if (!bit_mask)
			bit_mask = pixel_mask[line_style];
		if (bit_mask & 1)
		{
			glVertex2i(x, y);
			glVertex2i(y, x);
		}
		--x;
		if (p < 0)
			p += 2 * dy;
		else
		{
			++y; p += 2 * dy - 2 * dx;
		}
	}
	glEnd();

}

// Initialization function
void init(void)
{
	glClearColor (0.0, 0.0, 0.0, 0.0);
}

// Display callback function
void display(void)
{
	int i, x0, y0, x, y;
	double a;

	glClear (GL_COLOR_BUFFER_BIT);

	x0=width/2;
	y0=height/2;

	glColor3f(1.0, 1.0, 1.0);

	// Draw lines
	for (i=0; i<360; i+=15)
	{
		a=(double)i/180.0*PI;
		x=0.45*width*cos(a);
		y=0.45*height*sin(a);
		if (i==0 || i==180) y=0;
		if (i==90 || i==270) x=0;
		MyLine(x0, y0, x0 + x, y0 + y);
		for (int j = 1; j < line_width; j++)
		{

			MyLine(x0, y0, x0 + x+j, y0 + y+j);
		}
	}

	glFlush();
}

// Reshape callback function
void reshape(int w, int h)
{
	// Record width and height of program window
	width=w;
	height=h;

	// Set clipping window and viewport equal to
	// view area of program window
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D (0.0, w, 0.0, h);
}

// Keyboard callback function
void keyboard(unsigned char key, int x, int y)
{
	switch (key) {
		case 27:
			exit(0);
		case 'q':
			++line_style;
			if (line_style > 3)
				line_style = 0;
			glutPostRedisplay();
			break;
		case 'w':
			++line_width; 
			glutPostRedisplay();
			break;
		// Write your code for line style switching and line width switching
	}
}

// Main program entrance
int main(int argc, char* argv[])
{
	// Create window
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_RGB);
	glutInitWindowSize(500, 500);
	glutCreateWindow("Lines");

	// Initialization
	init();

	// Define callback functions
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutDisplayFunc(display);

	// Main program loop. (Event loop)
	glutMainLoop();

	return 0;
}