LWJGL3线条画不起作用

时间:2016-11-19 11:34:30

标签: java lwjgl

抱歉,我不擅长英语(这不是我的主要语言)。

我刚开始LWJGL3。我首先从互联网复制示例代码并对其进行修改以了解该概念。

我试图绘制一条线但它不起作用,它看起来像是黑色的窗口(它假设是带有白线的黑色窗口)。

我做错了什么?

LWJGL3:版本3.1.0

Java:Java 8

package me.Porama6400.kimkibAttack;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL;

import java.awt.DisplayMode;
import java.nio.ByteBuffer;

import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;

public class Main {
    // We need to strongly reference callback instances.
    private static GLFWErrorCallback errorCallback;
    private static GLFWKeyCallback keyCallback;
    static final int INIT_WIDTH = 300;
    static final int INIT_HEIGHT = 300;

    // The window handle
    private static long window;

    public static boolean verbose = true;

    public static void init() {
        final String WINDOW_TITLE = "WINDOW";

        vPrint("Initializing GLFW...");
        if (glfwInit() != true) {
            throw new IllegalStateException("Unable to initialize GLFW");
        }
        vPrint("Done!\n");

        //Window will be hidden after creation
        glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
        //Window will be resizable
        glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

        vPrint("Creating window...");
        window = glfwCreateWindow(INIT_WIDTH, INIT_HEIGHT, WINDOW_TITLE, NULL, NULL);
        if (window == NULL) {
            throw new RuntimeException("Failed to create the GLFW window");
        } else {
            vPrint("Done!\n");
        }

        //Setup a key callback. It will be called every time a key is pressed, repeated or released.
        glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
            @Override
            public void invoke(long window, int key, int scancode, int action, int mods) {
                if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
                    glfwSetWindowShouldClose(window, true); // We will detect this in our rendering loop
            }
        });

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

        // Make the OpenGL context current
        glfwMakeContextCurrent(window);
        GL.createCapabilities();
        // Enable v-sync
        glfwSwapInterval(1);

        // Make the window visible
        glfwShowWindow(window);

        //Set window clear color
        glClearColor(0f, 0f, 0f, 1f);

        initOpenGL();
    }

    public static void main(String[] args) {
        try {
            init();
            loop();
            glfwDestroyWindow(window);
        } finally {
            glfwTerminate();
        }
    }

    public static void loop() {
        // Run the rendering loop until the user has attempted to close
        // the window or has pressed the ESCAPE key.
        while ( glfwWindowShouldClose(window) == false ) {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            render();
            glfwSwapBuffers(window);
            glfwPollEvents();
        }
    }

    private static void vPrint(String str) {
        if (verbose) {
            System.out.print(str);
        }
    }

    private static void render() {
        glColor3d(0, 0, 1);
        glBegin(GL_LINE);
        glLineWidth(2f);
        glVertex2d(0, 0);
        glVertex2d(100, 100);
        glEnd();
    }

    private static void initOpenGL() {
        glViewport(0, 0, INIT_WIDTH, INIT_HEIGHT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();


        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.0f, 0.0f, -50.0f);
    }
}

0 个答案:

没有答案