我学习了Java的基础知识,我想使用LWJGL。我找到了教程here。我复制了这段代码:
package simple;
import java.io.IOException;
import java.nio.IntBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.system.Configuration;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL;
public class Introduction {
/**
* This error callback will simply print the error to
* <code>System.err</code>.
*/
private static GLFWErrorCallback errorCallback
= GLFWErrorCallback.createPrint(System.err);
/**
* The main function will create a 640x480 window and renders a rotating
* triangle until the window gets closed.
*
*/
public void run() {
long window;
/* Set the error callback */
glfwSetErrorCallback(errorCallback);
/* Initialize GLFW */
if (!glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
/* Create window */
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (window == NULL) {
glfwTerminate();
throw new RuntimeException("Failed to create the GLFW window");
}
/* Center the window on screen */
GLFWVidMode vidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window,
(vidMode.width() - 640) / 2,
(vidMode.height() - 480) / 2
);
/* Create OpenGL context */
glfwMakeContextCurrent(window);
GL.createCapabilities();
/* Enable vertical synchronization */
glfwSwapInterval(1);
/* Declare buffers for using inside the loop */
IntBuffer width = BufferUtils.createIntBuffer(1);
IntBuffer height = BufferUtils.createIntBuffer(1);
/* Loop until window gets closed */
while (!glfwWindowShouldClose(window)) {
float ratio;
/* Get width and height to calcualte the ratio */
glfwGetFramebufferSize(window, width, height);
ratio = width.get() / (float) height.get();
/* Rewind buffers for next get */
width.rewind();
height.rewind();
/* Set viewport and clear screen */
glViewport(0, 0, width.get(), height.get());
glClear(GL_COLOR_BUFFER_BIT);
/* Set ortographic projection */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1f, 1f, 1f, -1f);
glMatrixMode(GL_MODELVIEW);
/* Rotate matrix */
glLoadIdentity();
glRotatef((float) glfwGetTime() * 50f, 0f, 0f, 1f);
/* Render triangle */
glBegin(GL_TRIANGLES);
glColor3f(1f, 0f, 0f);
glVertex3f(-0.6f, -0.4f, 0f);
glColor3f(0f, 1f, 0f);
glVertex3f(0.6f, -0.4f, 0f);
glColor3f(0f, 0f, 1f);
glVertex3f(0f, 0.6f, 0f);
glEnd();
/* Swap buffers and poll Events */
glfwSwapBuffers(window);
glfwPollEvents();
/* Flip buffers for next loop */
width.flip();
height.flip();
}
/* Release window and its callbacks */
glfwDestroyWindow(window);
/* Terminate GLFW and release the error callback */
glfwTerminate();
errorCallback.free();
}
public static void main(String[] args) {
try {
unpackNatives();
} catch (IOException ex) {
throw new IllegalStateException("Unable to initialize LWJGL", ex);
}
new Introduction().run();
}
/**
* This method loads the natives libraries required by LWJGL. The specified
* libraries will get extracted into the folder native.
*
* @throw IOException if an error occurs during extracting the libraries
*/
private static void unpackNatives() throws IOException {
Path libraryPath = Paths.get("native");
String os = System.getProperty("os.name").toLowerCase();
/* Mac OS X needs headless mode for AWT */
if (os.contains("mac")) {
System.setProperty("java.awt.headless", "true");
}
/* Only unpack if native folder doesn't exist */
if (!Files.exists(libraryPath)) {
/* Get architecture */
String arch = System.getProperty("os.arch").toLowerCase();
/* Check if JVM is 64 bit */
boolean is64bit = arch.equals("amd64") || arch.equals("x86_64");
/* Extract libraries */
if (!Files.isDirectory(libraryPath)) {
Files.createDirectory(libraryPath);
} else if (Files.exists(libraryPath)) {
return;
}
String[] libraries;
if (os.contains("win")) {
/* Windows */
if (is64bit) {
libraries = new String[]{
"lwjgl.dll",
"glfw.dll",
"OpenAL.dll",
"jemalloc.dll"
};
} else {
libraries = new String[]{
"lwjgl32.dll",
"glfw32.dll",
"OpenAL32.dll",
"jemalloc32.dll"
};
}
} else if (os.contains("mac")) {
/* Mac OS X */
libraries = new String[]{
"liblwjgl.dylib",
"libglfw.dylib",
"libopenal.dylib",
"libjemalloc.dylib"
};
} else if (os.contains("nix") || os.contains("nux") || os.indexOf("aix") > 0) {
/* Linux */
if (is64bit) {
libraries = new String[]{
"liblwjgl.so",
"libglfw.so",
"libopenal.so",
"libjemalloc.so"
};
} else {
libraries = new String[]{
"liblwjgl32.so",
"libglfw32.so",
"libopenal32.so",
"libjemalloc32.so"
};
}
} else {
/* Not supported */
throw new RuntimeException("Operating System "
+ System.getProperty("os.name") + " is not supported");
}
for (String library : libraries) {
Files.copy(Introduction.class.getResourceAsStream("/" + library), libraryPath.resolve(library),
StandardCopyOption.REPLACE_EXISTING);
}
}
/* Set LWJGL library path */
Configuration.LIBRARY_PATH.set(libraryPath.toString());
}
}
但是,当我运行我的应用程序时(使用gradlew run
)
Execution protection violation
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000, pid=10676, tid=4436
#
# JRE version: Java(TM) SE Runtime Environment (8.0_91-b14) (build 1.8.0_91-b14)
# Java VM: Java HotSpot(TM) Client VM (25.91-b14 mixed mode windows-x86 )
# Problematic frame:
# C 0x00000000
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\Yoann\IdeaProjects\simple.project\hs_err_pid10676.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#