Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Messed up Settings?

  1. #1
    Newbie Newbie
    Join Date
    Jul 2015
    Posts
    4

    Messed up Settings?

    So I've (very) recently started trying to develop with LWJGL, and I am having some really weird issues.

    For one thing, as soon as the quad I am rendering gets more than 2 units in front of the camera, it disappears.
    Additionally, when I tried to render a cube, each quad would overwrite the previous one rendered. (eg. The sides of the cube are visible through the bottom)

    This is my code: EDIT: Apparently, the code contains forbidden words? The code is in the attached file, sorry for the hassle.

    Also, on a side note, does anyone know why "GLContext.createFromCurrent();" was not included in the example code?

    Code java:
    import org.lwjgl.Sys;
    import org.lwjgl.glfw.*;
    import org.lwjgl.opengl.*;
    import util.RenderUtil;
     
    import java.nio.ByteBuffer;
     
    import static org.lwjgl.glfw.Callbacks.*;
    import static org.lwjgl.glfw.GLFW.*;
    import static org.lwjgl.opengl.GL11.*;
    import static org.lwjgl.system.MemoryUtil.*;
     
    public class Main {
     
        // We need to strongly reference callback instances.
        private GLFWErrorCallback errorCallback;
        private GLFWKeyCallback keyCallback;
        private GLFWCursorPosCallback cursorCallback;
     
        // The window handle
        private long window;
     
        private int mouseX, mouseY, mouseDX, mouseDY;
     
        public void run() {
            System.out.println("Hello LWJGL " + Sys.getVersion() + "!");
     
            try {
                init();
                loop();
     
                // Release window and window callbacks
                glfwDestroyWindow(window);
                keyCallback.release();
            } finally {
                // Terminate GLFW and release the GLFWerrorfun
                glfwTerminate();
                errorCallback.release();
            }
        }
     
        private void init() {
            // Setup an error callback. The default implementation
            // will print the error message in System.err.
            glfwSetErrorCallback(errorCallback = errorCallbackPrint(System.err));
     
            // Initialize GLFW. Most GLFW functions will not work before doing this.
            if ( glfwInit() != GL11.GL_TRUE )
                throw new IllegalStateException("Unable to initialize GLFW");
     
            // Configure our window
            glfwDefaultWindowHints(); // optional, the current window hints are already the default
            glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation
            glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // the window will be resizable
     
            int WIDTH = 600;
            int HEIGHT = 600;
     
            // Create the window
            window = glfwCreateWindow(WIDTH, HEIGHT, "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, 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, GL_TRUE); // We will detect this in our rendering loop
                }
            });
     
            mouseX = 0;
            mouseY = 0;
            mouseDX = 0;
            mouseDY = 0;
     
            // Setup a cursor callback. It will be called every time the cursor is moved.
            glfwSetCursorPosCallback(window, cursorCallback = new GLFWCursorPosCallback() {
                @Override
                public void invoke(long window, double xpos, double ypos) {
                    mouseDX = (int) (xpos - mouseX);
                    mouseDY = (int) (xpos - mouseY);
     
                    mouseX = (int) xpos;
                    mouseY = (int) ypos;
                }
            });
     
            // Get the resolution of the primary monitor
            ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
            // Center our window
            glfwSetWindowPos(
                    window,
                    (GLFWvidmode.width(vidmode) - WIDTH) / 2,
                    (GLFWvidmode.height(vidmode) - HEIGHT) / 2
            );
     
            // Make the OpenGL context current
            glfwMakeContextCurrent(window);
            GLContext.createFromCurrent();
            // Enable v-sync
            glfwSwapInterval(1);
     
            // Make the window visible
            glfwShowWindow(window);
        }
     
        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 ContextCapabilities instance and makes the OpenGL
            // bindings available for use.
            GL.createCapabilities(false);
     
            // Set the clear color
            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
     
            float rot = 0;
            float r = 0.5F;
     
            // Run the rendering loop until the user has attempted to close
            // the window or has pressed the ESCAPE key.
            while ( glfwWindowShouldClose(window) == GL_FALSE ) {
                rot = (rot + 0.01F) % 5;
     
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
     
                glLoadIdentity();
     
                glPushMatrix();
                //glFrustum(-1, -1, 1, 1, 0, 100);
                glTranslatef(0, 0, -2.01F);
                RenderUtil.drawTemplateRectPrism(-1, -1, -1, 1, 1, 1);
                glPopMatrix();
     
                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();
        }
     
    }

    Main.txt
    Last edited by Dark Photon; 08-14-2015 at 06:36 PM.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,474
    Quote Originally Posted by Arkathorn View Post
    For one thing, as soon as the quad I am rendering gets more than 2 units in front of the camera, it disappears.
    You aren't changing the projection matrix. The default is the identity matrix, which results in the near and far planes being at Z=1 and -1.

    Quote Originally Posted by Arkathorn View Post
    Additionally, when I tried to render a cube, each quad would overwrite the previous one rendered. (eg. The sides of the cube are visible through the bottom)
    You aren't enabling depth tests.

    You probably need a more thorough introduction to OpenGL before attempting to write your own code. The wiki has a list of tutorials.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •