LWJGL的OpenGL无法正确绘制多边形

时间:2019-01-12 02:01:23

标签: opengl lwjgl

好的,我正在尝试使用lwjgl的OpenGL绘制多边形,但是,尽管我的代码对绘制功能正确,但是多边形仍未显示在屏幕上。

我的显示代码如下:

public class Display {
public static long window;
public boolean quit;

private Display() {
    quit = false;
}

public static void startDisplay() {
    Display d = new Display();
    d.init();
    while(!d.quit){
        d.loop();
    }
    glfwTerminate();
}

private void init() {
    GLFWErrorCallback.createPrint(System.err).set();
    if ( !glfwInit() )
        throw new IllegalStateException("Unable to initialize GLFW");

    window = glfwCreateWindow(800, 800, "Display", NULL, NULL);
    if ( window == NULL )
        throw new RuntimeException("Failed to create the GLFW window");
    glfwSetKeyCallback(window, new InputHandler());
    try ( MemoryStack stack = stackPush() ) {
        IntBuffer pWidth = stack.mallocInt(1); // int*
        IntBuffer pHeight = stack.mallocInt(1); // int*

        // Get the window size passed to glfwCreateWindow
        glfwGetWindowSize(window, pWidth, pHeight);

        // Get the resolution of the primary monitor
        GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

        // Center the window
        glfwSetWindowPos(
            window,
            (vidmode.width() - pWidth.get(0)) / 2,
            (vidmode.height() - pHeight.get(0)) / 2
        );

    }

    glfwMakeContextCurrent(window);
    glfwShowWindow(window);
    GL.createCapabilities();

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 800, 0, 600, 1, -1);
    glMatrixMode(GL_MODELVIEW);
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
}

private void loop() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
    Start.shapes[0].draw(0.5f,0.5f);

    glfwSwapBuffers(window); // swap the color buffers

    // Poll for window events. The key callback above will only be
    // invoked during this call.
    glfwPollEvents();

}

public static void setShapeColor() {
    glColor4d(0.0,0.0,0.0,0.0);
}


}

您可以在此处查看几何代码:

public class Geometry {
public final Point[] points;
final double area;
final Point center;

public Geometry(Point[] points){
    this.points = points;
    center = findCenter();
    area = findArea();

}   
public void draw(float x, float y){
    glBegin(GL_POLYGON);
    Display.setShapeColor();
    for(Point p : this.points) {
        glVertex2f((float)(x+p.x),(float)(y+p.y));
    }
    glEnd();
}
}

最后,点数据类型是一个具有2个双精度点的x和y成员的类。 所有几何都是具有2个以上点的2d多边形。

当前代码产生一个空白的白色屏幕,无论几何输入如何,上面都没有多边形。

我听说我必须稍微偏移一下相机才能正确获得渲染,但是我不完全确定是这种情况,因为大多数教程(包括LWJGL网站上的教程)都不会这样做。

0 个答案:

没有答案