如何在xcode上使用c ++使用opengl / glut创建简单的绘图应用程序

时间:2012-03-04 20:26:33

标签: c++ opengl draw paint glut

所以我可以注册鼠标事件。我唯一的问题是如何将绘图带到屏幕上。我已经发布了我目前正在使用的代码。它只是绘制一个窗口和一些彩色线条。我想要一种只是绘制到屏幕上的方法,而不是每次调用绘图功能时屏幕都没有完全清晰。

void myInit(void)
{
    glClearColor(1.0,1.0,1.0,0.0); // set white background color
    glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color
    glPointSize(4.0); // a ‘dot’ is 4 by 4 pixels
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the screen

    //First Line
    glLineWidth(8.0f);

    glBegin(GL_LINE_STRIP);
    glColor3ub(255, 0, 0);
    glVertex2i(40, 380);
    glColor3ub(0, 255, 0);
    glVertex2i(600, 380);

    glEnd();

    //Second Line
    glLineWidth(8.0f);

    glBegin(GL_LINE_STRIP);
    glColor3ub(0, 0, 255);
    glVertex2i(40, 250);
    glColor3ub(0, 255, 0);
    glVertex2i(600, 250);

    glEnd();

    //Third Line
    glLineWidth(8.0f);

    glBegin(GL_LINE_STRIP);
    glColor3ub(255, 0, 0);
    glVertex2i(40, 120);
    glColor3ub(0, 0, 255);
    glVertex2i(600, 120);

    glEnd();

    glFlush(); // send all output to display
}

int main(int argc, char** argv)
{
    //GLUT & OpenGL
    glutInit(&argc, argv); // initialize the toolkit
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
    glutInitWindowSize(640,480); // set window size
    glutInitWindowPosition(100, 150); // set window position on screen
    glutCreateWindow("my first attempt"); // open the screen window
    glutDisplayFunc(myDisplay); // register redraw function
    myInit();
    glutMainLoop(); // go into a perpetual loop

}

0 个答案:

没有答案