GLfloat initX = 0, initY = 0; GLfloat oldx = 0, oldy = 0; int times = 0; bool gDrawline = false; void drawkoch(GLfloat dir, GLfloat len, GLint iter) { GLdouble dirRad = dir * 3.1415926 / 180.f; GLfloat newX = oldx + len * cos(dirRad); GLfloat newY = oldy + len * sin(dirRad); if (iter == 0) { glVertex2f(oldx, oldy); glVertex2f(newX, newY); oldx = newX; oldy = newY; } else { iter--; len = len / 3; drawkoch(dir, len, iter); dir += 60; drawkoch(dir, len, iter); dir -= 120; drawkoch(dir, len, iter); dir += 60; drawkoch(dir, len, iter); } } typedef GLfloat point2d[2]; int iter = 0; float snowAngle = 0; point2d p = { 960, 20 }; point2d anyline[2]; point2d pp; void CMFCGLSetupView::OnDraw(CDC* pDC) { CMFCGLSetupDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: add draw code for native data here wglMakeCurrent(pDC->m_hDC, m_hRC); oldx = initX; oldy = initY; glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, Cx, 0, Cy, -1000, 1000); switch (drawMode) { case 0: glTranslatef(400, 450, 0); glBegin(GL_LINES); glColor3f(0.0, 1.0, 0.0); drawkoch(0, 500, iter); glEnd(); break; case 1: glTranslatef(400, 450, 0); glPushMatrix(); glTranslated(250, -125 * sqrt(3), 0); glRotated(snowAngle, 0, 1, 0); glTranslated(-250, 125 * sqrt(3), 0); glBegin(GL_LINES); glColor3f(0.0, 1.0, 0.0); drawkoch(0, 500, iter); drawkoch(-120, 500, iter); drawkoch(-240, 500, iter); glEnd(); glPopMatrix(); break; case 2: if (gDrawline) { oldx = anyline[0][0]; oldy = anyline[0][1]; point2d v; v[0] = (anyline[1][0] - anyline[0][0]); v[1] = (anyline[1][1] - anyline[0][1]); float len = sqrt(v[0] * v[0] + v[1] * v[1]); v[0] /= len; v[1] /= len; point2d n = { 1, 0 }; float cosTheta = v[0] * n[0] + v[1] * n[1]; float angle = acos(cosTheta) * 180.f / PI; if (anyline[1][1] < anyline[0][1]) { angle = -angle; } glBegin(GL_LINES); drawkoch(angle, len, iter); glEnd(); } break; default: break; } glBegin(GL_POINTS); glVertex2f(pp[0], pp[1]); glEnd(); glFlush(); wglMakeCurrent(pDC->m_hDC, NULL); }