Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package xan_code;
- import org.lwjgl.*;
- import org.lwjgl.glfw.*;
- import org.lwjgl.opengl.*;
- import org.lwjgl.system.*;
- import com.jogamp.opengl.glu.GLU;
- import xan_code.entity.Player;
- import xan_code.input.Keyboard;
- import xan_code.input.Mouse;
- import xan_code.util.Vector3;
- import java.nio.*;
- 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.*;
- public class Main {
- // The window handle
- private long window;
- private Player player = new Player(); //This is a custom class. All it stores is a Position value, which is my custom Vector3 class (Which just stores an X, Y and Z float value)
- public void run() {
- 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();
- }
- private void init() {
- // Setup an error callback. The default implementation
- // will print the error message in System.err.
- GLFWErrorCallback.createPrint(System.err).set();
- // Initialize GLFW. Most GLFW functions will not work before doing this.
- 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(1024, 768, "Xan's game", 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
- }
- Keyboard.SetKey(window, key, scancode, action, mods);
- });
- // 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);
- }
- private void Render() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- GLU.gluPerspective(80, 4/3, 0.1, 10000); //Faulty line right here.
- glTranslatef(player.Position.X, player.Position.Y, player.Position.Z); //See line 27
- glRotatef(45f, 0, 1, 1);
- glBegin(GL11.GL_QUADS);
- glColor3f(10f,10f,0.0f);
- glVertex3f( 10f, 10f,-10f);
- glVertex3f(-10f, 10f,-10f);
- glVertex3f(-10f, 10f, 10f);
- glVertex3f( 10f, 10f, 10f);
- glColor3f(10f,10f,0.0f);
- glVertex3f( 10f,-10f, 10f);
- glVertex3f(-10f,-10f, 10f);
- glVertex3f(-10f,-10f,-10f);
- glVertex3f( 10f,-10f,-10f);
- glColor3f(10f,0.0f,0.0f);
- glVertex3f( 10f, 10f, 10f);
- glVertex3f(-10f, 10f, 10f);
- glVertex3f(-10f,-10f, 10f);
- glVertex3f( 10f,-10f, 10f);
- glColor3f(10f,10f,0.0f);
- glVertex3f( 10f,-10f,-10f);
- glVertex3f(-10f,-10f,-10f);
- glVertex3f(-10f, 10f,-10f);
- glVertex3f( 10f, 10f,-10f);
- glColor3f(0.0f,0.0f,10f);
- glVertex3f(-10f, 10f, 10f);
- glVertex3f(-10f, 10f,-10f);
- glVertex3f(-10f,-10f,-10f);
- glVertex3f(-10f,-10f, 10f);
- glColor3f(10f,0.0f,10f);
- glVertex3f( 10f, 10f,-10f);
- glVertex3f( 10f, 10f, 10f);
- glVertex3f( 10f,-10f, 10f);
- glVertex3f( 10f,-10f,-10f);
- glEnd();
- }
- private void loop() {
- // This line is critical for LWJGL's interoperation with GLFW's
- // OpenGL context, or any context that is managed externally.
- // LWJGL detects the context that is current in the current thread,
- // creates the GLCapabilities instance and makes the OpenGL
- // bindings available for use.
- GL.createCapabilities();
- // Set the clear color
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- // Run the rendering loop until the user has attempted to close
- // the window or has pressed the ESCAPE key.
- while ( !glfwWindowShouldClose(window) ) {
- Mouse.Update(window);
- if (Keyboard.IsKeyDown(GLFW.GLFW_KEY_W)) {
- player.Position = player.Position.add(new Vector3(0, 0, 0.1f));
- }
- if (Keyboard.IsKeyDown(GLFW.GLFW_KEY_S)) {
- player.Position = player.Position.subtract(new Vector3(0, 0, 0.1f));
- }
- if (Keyboard.IsKeyDown(GLFW.GLFW_KEY_A)) {
- player.Position = player.Position.add(new Vector3(0.1f, 0, 0));
- }
- if (Keyboard.IsKeyDown(GLFW.GLFW_KEY_D)) {
- player.Position = player.Position.subtract(new Vector3(0.1f, 0, 0));
- }
- if (Keyboard.IsKeyDown(GLFW.GLFW_KEY_Q)) {
- player.Position = player.Position.subtract(new Vector3(0, 0.1f, 0));
- }
- if (Keyboard.IsKeyDown(GLFW.GLFW_KEY_E)) {
- player.Position = player.Position.add(new Vector3(0, 0.1f, 0));
- }
- Render();
- 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 main(String[] args) {
- new Main().run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement