假設我畫在OpenGL一些簡單的線條像這樣:繪製抖動的線條在OpenGL
glBegin(GL_LINES);
glVertex2f(1, 5);
glVertex2f(0, 1);
glEnd();
如何使線看起來戰戰兢兢,就像是被繪製或手工繪製的?
假設我畫在OpenGL一些簡單的線條像這樣:繪製抖動的線條在OpenGL
glBegin(GL_LINES);
glVertex2f(1, 5);
glVertex2f(0, 1);
glEnd();
如何使線看起來戰戰兢兢,就像是被繪製或手工繪製的?
你可以嘗試把你的排隊分成幾部分,然後用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樣式被稱爲「即時模式」並且效率不高。它甚至在一些移動平臺上不被支持。如果你發現你的東西很呆板,看看使用頂點數組。
爲了使線看起來手繪和更好,你可能也想使它更胖,並使用抗鋸齒。
喜歡它一次畫一點,或者像線本身看起來破爛(不像素)? –
基本上它意味着你需要更新每個幀的頂點位置,從初始位置有一點抖動。我有一個示例代碼完成這個在Delphi中:http://doodles.googlecode.com/svn/trunk/Sketching/ – Kromster