【问题标题】:OpenGL won't let me draw a thingOpenGL不会让我画东西
【发布时间】:2012-07-26 03:06:47
【问题描述】:

我的代码中遇到了这个奇怪的错误,它不会让我绘制任何东西......

void render(void) {
    glClear(GL_COLOR_BUFFER_BIT);

    glLineWidth(4.0);
    glPointSize(4.0);
    glColor3i(0, 1, 1);
    glBegin(GL_POINTS);
        glVertex2i(0, 0);
        glVertex2i(2, 2);
        glVertex2i(3, 3);
    glEnd();
    glutSwapBuffers();
}

int main(int argc, char **argv) {
    int Largura, Altura;

    //width
    Largura = (abs(Plano::MinX) * Plano::EspacoPix) + (abs(Plano::MaxX) * Plano::EspacoPix);
    //height
    Altura  = (abs(Plano::MinY) * Plano::EspacoPix) + (abs(Plano::MaxY) * Plano::EspacoPix);
    // these are cartesian coordinates,
    // at class Plano: enum Dimension {MinY=-50, MaxY=50, MinX=-50, MaxX=50, EspacoPix=5};

    glutInit(&argc, argv);
    glutInitWindowPosition(-1, -1);
    glutInitWindowSize(Largura, Altura);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glClearColor(1, 1, 1, 1);
    glutCreateWindow("Graphix");
    glMatrixMode(GL_PROJECTION);
    //glEnable(GL_POINT_SMOOTH);
    //glEnable(GL_LINE_SMOOTH);
    gluOrtho2D(Plano::MinX, Plano::MaxX, Plano::MinY, Plano::MaxY); // left, right, bottom, top

    glutDisplayFunc(render);
    glutIdleFunc(render);


    std::cout << "[Dbg] Dimensions:  Height=" << Altura << "px Width=" << Largura << "px " << std::endl;
    std::cout << "[Dbg] Ortho: Left=" << Plano::MinX << ", Right=" << Plano::MaxX << ", Bottom=" << Plano::MinY << ", Top=" << Plano::MaxY << std::endl;
    //glGet with argument GL_COLOR_CLEAR_VALUE
    float cores[4] = {-1, -1, -1, -1}; // defaults
    glGetFloatv(GL_COLOR_CLEAR_VALUE, cores);
    std::cout << "[Dbg] Color Buffer: R=" << cores[0] << " G=" << cores[1] << " B=" << cores[2] << " A=" << cores[3] << std::endl;


    glutMainLoop();
    return(0);
}

我得到的只是一个黑屏和颜色缓冲区,我认为它是 1、1、1、1 实际上是 0、0、0、0... 如果我错了,请纠正我,但中心屏幕是,因为 gluOrtho2D 调用,x=0,y=0,对吧?对? :)

【问题讨论】:

    标签: c++ windows opengl glut


    【解决方案1】:

    glClearColor 必须在 glutCreateWindow 之后调用,因为尚未创建 OpenGL 上下文。

    另外请注意,您正在学习过时的 OpenGL 编程技术。

    glColor、glBegin、glEnd、glVertex*、glColor* glMatrixMode、gluOrtho2D、glu* 现在都是不推荐使用的函数。

    有些人认为他们可能更容易学习,但我认为不会太多,这就是你必须忘掉的所有东西。

    glBegin() 和 glEnd() 导致您一次绘制对象 1 个顶点,这非常慢。

    新方法是将坐标放入浮点数组中,然后将其发送到显卡以一次绘制所有内容。自从 OpenGL 1.1(1997 年发布)支持顶点数组以来,没有理由将这些旧东西用作所有东西(尽管它们现在也已被弃用,而是被功能相似且具有从 1.5(2005 年发布)开始使用 OpenGL。

    我建议您查看 http://www.opengl-tutorial.org/ 以获得对初学者来说不难的新教程。

    我也在这里回复:https://gamedev.stackexchange.com/questions/32876/good-resources-for-learning-modern-opengl-3-0-or-later/32917#32917

    【讨论】:

    • 天哪,非常感谢,它成功了……我虽然这些是最新的技术!!我现在觉得自己已经过时了)-:非常感谢:')
    【解决方案2】:

    我之前没有做过太多的 OpenGL 2 编码,但我认为您需要在调用 glBegin 之前调用 glLoadIdentity(),并且需要在 glMatrixMode 之前调用 glLoadIdentity()。

    【讨论】:

    • 我打了电话,还是一样。
    • 尝试调用 glTranslate (0, 0, -5) 以使其不正确,这可能是您看不到任何东西的原因。此外,不要使用 GL_POINTS,而是使用更易于查看的内容,例如 GL_TRIANGLE。
    猜你喜欢
    • 2011-02-14
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    相关资源
    最近更新 更多