OpenGL画曲线

void CShape::drawCurve() {
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_LINES);
    glPointSize(10.0f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex2f(-1.0f, 0.0f);//动态绘制x坐标
    glVertex2f(beginXpoint += 0.01f, 0.0f);
    glVertex2f(0.0f,1.0f);//动态绘制y坐标
    glVertex2f(0.0f,beginYpoint -= 0.01f);
    glEnd();
    //GL_LINE_STRIP:不闭合折线
    //GL_LINES:多组双顶点线段
    //GL_LINE_LOOP : 首尾坐标相连,闭合折线
    glBegin(GL_LINE_STRIP);
    glColor3f(1.0f, 0.0f, 0.0f);
    for(float fValue = -1.0f; fValue <= beginPoint_.x; fValue += 0.0001f) {
        glVertex2f(fValue, fValue * fValue);
    }
    beginPoint_.x += 0.01f;
    glEnd();
    glFlush();
    glutSwapBuffers();
}


void CShape::init() {
    beginPoint_.x = -1.0f;
    beginPoint_.y = 1.0f;
    endPoint_.x = 1.0f;
    endPoint_.y = 1.0f;
    beginXpoint = -1.0f;
    beginYpoint = 1.0f;

    glClearColor(0.0f, 1.0f, 0, 1.0f);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DOUBLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 300);
    glutCreateWindow("**");
    glMatrixMode(GL_TEXTURE);
    glLoadIdentity(); //Move to the Center
    //gluOrtho2D(-0.0f, 0.0f, -0.0f, 0.0f);
    glOrtho(-0.0f, 0.0f, -0.0f, 0.0f, 1.0f, 1.0f);
    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
    //glLineWidth(1.0f);
    glutDisplayFunc(&CShape::drawCurve);
    glutIdleFunc(&CShape::displayDrawShape);
    glutMainLoop();
}


你可能感兴趣的:(OpenGL学习)