Erm, I’m just trying to get used to LWJGL 3, and I’m following the newbie tutorials again. For some reason, when I call glVertex3f(#, #, #), altering the z argument doesn’t change the shape
in any way. Is there some projection function I failed to call properly? Here is my code.
package driver;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.system.MemoryUtil.*;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;
import static org.lwjgl.opengl.GL11.*;
/**
* Class containing main method
* @author William Andrew Cahill
*/
public class Driver
{
// Fields
private static long window; // Handle on window
private static final int width = 600, height = 400; // Size of window
private static GLFWKeyCallback callback; // Reference to callback so it doesn't get gced
/**
* Main method that fires the program up
* @param args Command-line arguments
*/
public static void main(String[] args)
{
// Initializes OpenGL
init();
// Runs loop until window closes
while(glfwWindowShouldClose(window) == GL11.GL_FALSE)
{
loop();
try
{
Thread.sleep(1000 / 60);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
// Terminates program, cleaning up resources
terminate();
}
/**
* Initializes window
*/
public static void init()
{
// Tries to initialize GLFW
int code = glfwInit();
if(code != GL11.GL_TRUE)
throw new IllegalStateException("Failed to initialize GLFW. glfwInit() code was " + code);
// Tries to initialize window
window = GLFW.glfwCreateWindow(width, height, "Experiment", NULL, NULL);
if(window == NULL)
throw new IllegalStateException("Failed to initialize window. glfwCreateWindow() code was " + window);
// Determines how key input is handled
GLFW.glfwSetKeyCallback(window, callback = new GLFWKeyCallback()
{
@Override
public void invoke(long window, int key, int scancode, int action, int mods)
{
if(action == GLFW_PRESS)
System.out.println("Pressed " + key + '!');
else if(action == GLFW_RELEASE)
System.out.println("Released " + key + '!');
}
});
// Sets window to current window
GLFW.glfwMakeContextCurrent(window);
}
/**
* Main loop of the program
*/
public static void loop()
{
// Header info
GLContext.createFromCurrent();
glViewport(0, 0, width, height);
GL11.glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glClearColor(0.5f, 0.5f, 0.5f, 1);
glClear(GL_COLOR_BUFFER_BIT);
// Flips view
GL11.glPushMatrix();
// Rendering
glBegin(GL_TRIANGLES);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
// Triangle vertices
// Vertex 1
float depth = 0;
glColor3f(1.f, -0.f, 0.f);
glVertex3f(-1, -1, depth);
// Vertex 2
glColor3f(0.f, 1.f, 0.f);
glVertex3f(0.6f, -0.4f, depth);
// Vertex 3
glColor3f(0.f, 0.f, 1.f);
glVertex3f(0.f, 0.6f, depth);
// Done rendering triangle
glEnd();
// Reverts matrix
GL11.glPopMatrix();
// Draws
glfwSwapBuffers(window);
glfwPollEvents();
}
/**
* Terminates the program
*/
public static void terminate()
{
glfwTerminate();
glfwDestroyWindow(window);
}
}
It doesn’t seem to matter what I set my depth value to in loop(). As long as it’s between -1 and 1, the colorful triangle looks the same. Help?