在OpenGL中绘制抖动线

时间:2011-08-26 02:43:07

标签: opengl graphics

假设我在OpenGL中绘制了一些简单的行:

glBegin(GL_LINES);
glVertex2f(1, 5);
glVertex2f(0, 1);
glEnd();

如何让线条显得紧张,就像用手勾画或绘制一样?

1 个答案:

答案 0 :(得分:1)

您可以尝试将您的阵容分成几段,然后使用rand()添加一些随机噪音。

这是一些丑陋但希望有些有用的代码。您可以根据需要重构:

const float X1= 1.0f, Y1 = 5.0f, X2 = 0.0f, Y2 = 1.0f;
const int NUM_PTS = 10; //however many points in between

//you will need to call srand() to seed your random numbers

glBegin(GL_LINES);
glVertex2f(START_X, START_Y);
for(unsigned i = 0; i < NUM_PTS; i += 2)
{
  float t = (float)i/NUM_PTS;
  float rx = (rand() % 200 - 100)/100.0f; //random perturbation in x
  float ry = (rand() % 200 - 100)/100.0f; //random perturbation in y
  glVertex2f( t * (END_X - START_X) + r, t * (END_Y - START_Y) + r);
  glVertex2f((t + 1) * (END_X - START_X), (t + 1) * (END_Y - START_Y));
}
glVertex2f(END_X, END_Y);
glEnd();

我将循环递增2并绘制每个其他点而没有随机扰动,以便线段全部连接在一起。

您可能想要了解glBegin / glEnd样式被称为“立即模式”并且效率不高的事实。某些移动平台甚至不支持它。如果你发现你的东西很迟钝,那就看看使用顶点数组。

为了使线看起来像手绘和更好,你可能还想让它变得更胖并使用抗锯齿。