OpenGL不会让我画任何东西

OpenGL won't let me draw a thing

本文关键字:任何东 OpenGL      更新时间:2023-10-16

我在代码中遇到了一个奇怪的错误,它不允许我绘制任何东西。。。

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实际上是0,0,0…如果我错了,请纠正我,但屏幕的中心是,因为gluOrtho2D调用,x=0,y=0,对吧?对吗?:)

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

还要注意,您正在学习过时的OpenGL编程技术。

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

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

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

新的方法包括将坐标放入一个浮动数组中,并将其发送到图形卡中,以便一次绘制所有坐标。自从OpenGL 1.1(1997年发布)支持顶点阵列(尽管它们现在也被弃用,取而代之的是顶点缓冲区对象(或VBO),这些对象在功能上类似,并且自OpenGL 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

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