LWJGL / OpenGL - 不绘制基本四边形

时间:2016-11-30 07:54:07

标签: lwjgl opengl-4

我到处都是,我似乎无法让OpenGL绘制一个简单的四边形。 窗口显示正确的颜色背景,但OpenGL不会绘制框。 我使用的是OpenGL 4.4.0 - Build 20.19.15.4463 窗口大小为1920x1080

这是我目前的代码:

Main.java中的

public void init(){
    if(glfwInit() != true){
        System.err.println("GLFW failed to initialize");
    }
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
    //TODO change name.
    window = glfwCreateWindow(width, height, "GameName", NULL, NULL);
    if(window == NULL){
        System.err.println("Window failed to be created");
    }

    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, 100, 100);
    glfwMakeContextCurrent(window);
    glfwShowWindow(window);
    GL.createCapabilities();

    glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
    glEnable(GL_DEPTH_TEST);


    //set up projection matrix; allows us to draw.
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width, height, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);


    testBox = new EntityBox(3, Color.RED);//just to test right now

    System.out.println("OpenGL: "+ glGetString(GL_VERSION));

}
public void update(){
    glfwPollEvents();

}
public void render(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glfwSwapBuffers(window);
    testBox.draw();
}

在EntityBox中:

public void draw(){
    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_QUADS);
        glVertex2f(0,0);
        glVertex2f(0,100);
        glVertex2f(100,0);
        glVertex2f(100,100);
    glEnd();
}

1 个答案:

答案 0 :(得分:0)

我修好了: glClear需要

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

不知道为什么,因为如果你想要3d,我会使用depth_buffer。 和

glfwSwapBuffers(window);

需要在绘图后调用。