PDA

View Full Version : Messed up Settings?



Arkathorn
08-14-2015, 12:26 PM
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?


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();
}

}


2044

GClements
08-14-2015, 02:03 PM
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.



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 (https://www.opengl.org/wiki/Getting_started#Tutorials_and_How_To_Guides).