init
This commit is contained in:
commit
2d257839c3
17 files changed
+13161
No files matched your search
@@ -0,0 +1,133 @@
|
||||
package org.example;
|
||||
|
||||
import org.lwjgl.*;
|
||||
import org.lwjgl.Version;
|
||||
import org.lwjgl.glfw.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
import org.lwjgl.system.*;
|
||||
|
||||
import java.nio.*;
|
||||
import java.io.*;
|
||||
import java.text.MessageFormat;
|
||||
|
||||
import static org.lwjgl.glfw.Callbacks.*;
|
||||
import static org.lwjgl.glfw.GLFW.*;
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
import static org.lwjgl.system.MemoryStack.*;
|
||||
import static org.lwjgl.system.MemoryUtil.*;
|
||||
|
||||
import com.mokiat.data.front.parser.*;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
|
||||
public class Game
|
||||
{
|
||||
private long window;
|
||||
|
||||
public void run()
|
||||
{
|
||||
System.out.println("Running LWJGL Version " + Version.getVersion() + ".");
|
||||
|
||||
init();
|
||||
loop();
|
||||
|
||||
// Free the window callbacks and destroy the window
|
||||
glfwFreeCallbacks(window);
|
||||
glfwDestroyWindow(window);
|
||||
|
||||
// Terminate GLFW and free the error callback
|
||||
glfwTerminate();
|
||||
glfwSetErrorCallback(null).free();
|
||||
}
|
||||
|
||||
public void init()
|
||||
{
|
||||
GLFWErrorCallback.createPrint(System.err).set();
|
||||
|
||||
if (!glfwInit())
|
||||
throw new IllegalStateException("Unable to Initialize GLFW");
|
||||
|
||||
// Configure GLFW
|
||||
glfwDefaultWindowHints(); // optional, the current window hints are already the default
|
||||
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
|
||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
|
||||
|
||||
// Create the window
|
||||
window = glfwCreateWindow(300, 300, "Hello World!", NULL, NULL);
|
||||
if ( window == NULL )
|
||||
throw new RuntimeException("Failed to create the GLFW window");
|
||||
|
||||
// Setup a key callback. It will be called every time a key is pressed, repeated or released.
|
||||
glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
|
||||
if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
|
||||
glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
|
||||
});
|
||||
|
||||
// Get the thread stack and push a new frame
|
||||
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
|
||||
);
|
||||
} // the stack frame is popped automatically
|
||||
|
||||
// Make the OpenGL context current
|
||||
glfwMakeContextCurrent(window);
|
||||
// Enable v-sync
|
||||
glfwSwapInterval(1);
|
||||
|
||||
// Make the window visible
|
||||
glfwShowWindow(window);
|
||||
|
||||
// Load Objects
|
||||
try (InputStream in = this.getClass().getResourceAsStream("/objs/cube.obj")) {
|
||||
// Create an OBJParser and parse the resource
|
||||
final IOBJParser parser = new OBJParser();
|
||||
final OBJModel model = parser.parse(in);
|
||||
|
||||
// Use the model representation to get some basic info
|
||||
System.out.println(MessageFormat.format(
|
||||
"OBJ model has {0} vertices, {1} normals, {2} texture coordinates, and {3} objects.",
|
||||
model.getVertices().size(),
|
||||
model.getNormals().size(),
|
||||
model.getTexCoords().size(),
|
||||
model.getObjects().size()));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private void loop()
|
||||
{
|
||||
GL.createCapabilities();
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
while ( !glfwWindowShouldClose(window) )
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
glfwPollEvents();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
new Game().run();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package org.example;
|
||||
|
||||
import static org.lwjgl.opengl.GL20.*;
|
||||
|
||||
public class ShaderProgram {
|
||||
|
||||
private final int programId;
|
||||
|
||||
private int vertexShaderId;
|
||||
|
||||
private int fragmentShaderId;
|
||||
|
||||
public ShaderProgram() throws Exception {
|
||||
programId = glCreateProgram();
|
||||
if (programId == 0) {
|
||||
throw new Exception("Could not create Shader");
|
||||
}
|
||||
}
|
||||
|
||||
public void createVertexShader(String shaderCode) throws Exception {
|
||||
vertexShaderId = createShader(shaderCode, GL_VERTEX_SHADER);
|
||||
}
|
||||
|
||||
public void createFragmentShader(String shaderCode) throws Exception {
|
||||
fragmentShaderId = createShader(shaderCode, GL_FRAGMENT_SHADER);
|
||||
}
|
||||
|
||||
protected int createShader(String shaderCode, int shaderType) throws Exception {
|
||||
int shaderId = glCreateShader(shaderType);
|
||||
if (shaderId == 0) {
|
||||
throw new Exception("Error creating shader. Type: " + shaderType);
|
||||
}
|
||||
|
||||
glShaderSource(shaderId, shaderCode);
|
||||
glCompileShader(shaderId);
|
||||
|
||||
if (glGetShaderi(shaderId, GL_COMPILE_STATUS) == 0) {
|
||||
throw new Exception("Error compiling Shader code: " + glGetShaderInfoLog(shaderId, 1024));
|
||||
}
|
||||
|
||||
glAttachShader(programId, shaderId);
|
||||
|
||||
return shaderId;
|
||||
}
|
||||
|
||||
public void link() throws Exception {
|
||||
glLinkProgram(programId);
|
||||
if (glGetProgrami(programId, GL_LINK_STATUS) == 0) {
|
||||
throw new Exception("Error linking Shader code: " + glGetProgramInfoLog(programId, 1024));
|
||||
}
|
||||
|
||||
if (vertexShaderId != 0) {
|
||||
glDetachShader(programId, vertexShaderId);
|
||||
}
|
||||
if (fragmentShaderId != 0) {
|
||||
glDetachShader(programId, fragmentShaderId);
|
||||
}
|
||||
|
||||
glValidateProgram(programId);
|
||||
if (glGetProgrami(programId, GL_VALIDATE_STATUS) == 0) {
|
||||
System.err.println("Warning validating Shader code: " + glGetProgramInfoLog(programId, 1024));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void bind() {
|
||||
glUseProgram(programId);
|
||||
}
|
||||
|
||||
public void unbind() {
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
unbind();
|
||||
if (programId != 0) {
|
||||
glDeleteProgram(programId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user