效果圖
主體代碼
void DrawPoints(void)
{
int i;
GLfloat x, y;
glPushMatrix();
//另一個相對的Z平移可以分離對象
glLoadIdentity();
glTranslatef(0.0, 0.0, 0.f);
//設定點大小
glPointSize(5.0f);
glBegin(GL_POINTS);
// 固定點
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
// 随機點
for ( i = 0; i <100 ; i++ )
{
// 設定随機數點顔色
float redValue = (float)(rand() % 255) / 255;
float greenValue = (float)(rand() % 255) / 255;
float blueValue = (float)(rand() % 255) / 255;
glColor3f(redValue, greenValue, blueValue);
x = (GLfloat)(width / 2 - rand() % width) / width * 2;
y = (GLfloat)(height / 2 - rand() % height) / height * 2;
// 設定點坐标
glVertex3f(x, y, 0.0f);
}
glEnd();
glPopMatrix();
return;
}