Rendering on Z-axis not working?

Started by Peilot, January 23, 2017, 21:28:42

Previous topic - Next topic

Peilot

Hi! I've been trying to render a quad on the position (X = 0, Y = 0, Z = 1). It does nt seem to work and I'm kind of frustrated because I've been working on it for quite som time now(A couple of hours!) and I cant solve it! This is the code in my game loop which could be giving a better overview of what the problem is:
public Main() {
		if (glfwInit() == false) {
			System.out.println("GLFW failed to initialize!");
			System.exit(-1);
		}
		window = glfwCreateWindow(800, 600, "lil' Tanic", 0, 0);
		glfwShowWindow(window);
		glfwMakeContextCurrent(window);
		GL.createCapabilities();
		
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		
		glEnable(GL_DEPTH_TEST);
		glDepthFunc(GL_ALWAYS);
		
		glDepthRange(0.1, 100);
		glClearDepth(1.0);
		
		init();
		
		double lastTime = glfwGetTime();
		double currentTime = glfwGetTime();
		double frames = 0;
		
		float lastTimeSpent = (float) glfwGetTime();
		float timeSpent = (float) glfwGetTime();
		
		while (glfwWindowShouldClose(window) == false) {
			timeSpent = (float) glfwGetTime();
			deltatime = timeSpent - lastTimeSpent;
			lastTimeSpent = timeSpent;
			
			glfwPollEvents();
			glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
			
			input();
			update();
			render();
			
			glfwSwapBuffers(window);
			
			frames++;
			currentTime = glfwGetTime();
			if (currentTime - lastTime >= 1.0) {
				System.out.println("FPS: " + frames);
				frames = 0;
				lastTime = glfwGetTime();
			}
		}
		
		glfwTerminate();
	}


I am using shaders and VBOs to render. Rendering on the Z = 0 works fine but when i make that value higher it does not render at all!

Thanks in advance!
/Marcus

pdid

This is because openGL uses a right handed coordinate system which means a positive z value goes into the screen. Essentially you are trying to render something behind your camera.
Here is a good article on the topic. https://www.evl.uic.edu/ralph/508S98/coordinates.html

Peilot

Quote from: pdid on January 23, 2017, 21:40:31
This is because openGL uses a right handed coordinate system which means a positive z value goes into the screen. Essentially you are trying to render something behind your camera.
Here is a good article on the topic. https://www.evl.uic.edu/ralph/508S98/coordinates.html

I think I've tried that (Putting the block -1 on the Z-axis.) but neither did that work, but I'll try it as soon as I'm back home! But could it be something else that does not work?

Kai

Quote from: pdid on January 23, 2017, 21:40:31
This is because openGL uses a right handed coordinate system [...]
No. OpenGL's default coordinate system which you are using when:
- you do not modify the matrix stacks (when working with the fixed-function pipeline)
- or when you use the identity matrix/transformation or no transformation at all (when you use shaders)
is the left-handed clip space/coordinate system. As you said correctly, though, its +Z axis points into the screen, away from the user.

This clip space displays vertices for which their x, y and z coordinates divided by their w coordinate (in your case probably just 1) is >= -1 and <= +1. See the OpenGL specification at chapter 13.5. "Primitive Clipping".

You probably wonder why the quad does not show, when indeed the inequality is satisfied with z=+1. This seems to be because of floating-point rounding errors when using shaders. Rendering at z=+0.9999999f (which is just 1.0f - Math.ulp(1.0f), so the largest representable IEEE-754 32-bit floating-point value below 1.0f) does show the quad.
When you used the fixed-function pipeline, the quad at z=+1 would show. At least it does for me on Nvidia.

Also, glDepthRange(0.1, 100) does not do what you probably wanted it to do. It does not change the z-range of the clip space to be between 0.1 and 100, so that you could have rendered a quad at any z value within that range. No, this function only changes how the computed z value in the normalized device coordinate space - which is _always_ within [-1..+1] and is just the clip space after "perspective divide" - is translated to the window coordinates depth value written to the depth buffer.
And you should also read the documentation of that function, which states that any two arguments given to it will be clamped to [0..+1] before being effective. This is because the depth buffer can only store values within that range, max.

In order for you to render quads at different z values, you would need to change your coordinate system away from clip space into some other desired space, by applying some transformations in your shader, for example via a matrix.