glVertex3f doesn't care about z axis???

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?

Hi

Are you really sure that you want to modify the projection matrix?? glMatrixMode(GL_PROJECTION)?

Rather call glFrustum or gluPerspective somewhere when the projection matrix is active and then leave the model-view matrix active. Please learn the basic concepts of OpenGL before using any Java binding for OpenGL/OpenGL-ES. You shouldn’t create a new context at each frame. Your code is really very wrong. It would be better if someone else with a better understanding of the library you use could fix your code.

Edit.: Look at this example:
http://www.lwjgl.org/guide
In this example, loop() is called only once.

It would be easier to learn these basic opengl concepts if there were any guides that made any sense. There are always lines and lines of unexplained code that cannot be understood by fiddling, and the opengl documentation is poor. That’s why I’m forced to ask my questions here, you see… I haven’t found a book or guide anywhere that is down to earth enough to understand.

Also, I’d like to point out that that is the exact tutorial I’ve been looking at… If even that isn’t useful for me, then what do you propose I do?

try the OpenGL tutorials here, they are easy to read, use GLFW and include source code. Your above snippet of code is using jurassic OpenGL which should be avoided.

I’m well aware that I should be using VBO’s. I just wanted to get an image displayed before I tried anything else…

Sri Harsha Chilakapati (known by his initials SHC here and on the LWJGL forum) is taking on the journey of writing a tutorial series on LWJGL 3.

I highly recommend having a visit to his site http://goharsha.com/lwjgl-tutorial-series/.

Regards,
Kai

OpenGL is pretty low level, so it not as straight forward as high level libraries to draw an image.

To draw an image you would normally first need to set up your matrices on how to draw your vertices on the screen which you can pass to a shader, then draw a square (using two triangles, the vertices and texture coordinates of which would be in a VBO). Then you would load your image as an OpenGL texture and pass that to a shader which draws it on the triangles when rendering them.

That’s my biggest fear, actually. I’ve delved into VBO’s before, and there is so much setup, it makes me want to cry. I’m going to have to remember all of the c functions and constants required to make this work, and I don’t even want to look at shaders again. Shaders are another concept that seem to lack explanation, even in the most down-to-earth tutorials.

I think this thread has drifted away from it’s intended purpose, though.

Okay, so you need a perspective projection then.
Have a look at http://forum.lwjgl.org/index.php?topic=5586.0, which is a post on the LWJGL forum targeting LWJGL 3 with exactly that problem and a possible solution to it.
Normally I don’t like referencing my own posts but it fits perfectly to your problem.

Maybe you have to learn the basic concepts of 3D computer graphics too. You can look at the OpenGL Red Book and the Blue Book. OpenGL isn’t the kind of thing that you can learn in a few days.

The OpenGL SDK is very well documented, you should always look at the “man”:
http://www.opengl.org/sdk/docs/

You modified a simple example that you didn’t understand and you broke it. Just move the creation of the context (GLContext.createFromCurrent()) and the setup of the projection matrix somewhere else. I totally understand your position. I thought that someone else could fix your code despite the fact that it relies on some features of the fixed pipeline.

When there is no projection matrix defined, you are using Orthographic projection matrix. In orthographic projection, depth doesn’t change the size of the shape.

To see the depth working, you need to have a perspective projection, so make a call to either glFrustum or gluPerspective functions after changing the matrix mode to GL_PROJECTION, and then change the mode back to GL_MODELVIEW.

Hope this helps…

[quote]… so make a call to either glFrustum or gluPerspective …
[/quote]
Hey SHC,
sorry, but just to prevent some misunderstanding: There is no gluPerspective or GLU or GLUT in general in LWJGL 3 anymore.
That’s why for perspective projection the OP needs to build the matrix on his/her own.

Oops, I didn’t read the thread completely, didn’t notice that the OP is using LWJGL 3.

Bah, it’s still actually quite helpful that you mentioned that I need to set up a projection… not that I quite know what that means when it comes to matricies, but it gives me somewhere to start looking for a solution. I’ll keep looking at tutorials hoping that at some point, they’ll start making sense. For now, I’ll try copying and pasting code snippits and tinker with them.

You don’t even understand what you copy, you’re creating a context at each iteration which is absurd :clue: