ManualPerspective.vert in Tut 04

Issue #27 resolved
Former user created an issue

First thank you so much for writing this book. I finally feel like there is a book written about opengl that I can understand.

On to the issue...

In that vertex shader, and in the tutorial page, the camera position is defined as:

vec4 cameraPos = position + vec4(offset.x, offset.y, 0.0, 0.0);

then there is code that does the calculations to get the clip position. And after that we set w as:

clipPos.w = -cameraPos.z;

Since cameraPos.z has it's z to be 0.0 than this would cause division by zero when the "perspective divide" happens, no?

Running the shader "as is" results in a blank screen. But changing the definition of camera pos to: vec4(offset.x, offset.y, -2.0, 0.0); and then running the shader results in the expected image that is shown on the tutorial page.

I should point out though, that I'm on a mac and converting the tutorials to plain c and opengl 2.1 with ogl extensions since that's what supported on mac. But there does seem to be any screw ups with the code conversion as far as I can see.

Also may I suggest adding a way for people to contact you for general information? This bug tracker is great for reporting bugs, but some people may just want to say "thank you".

Again thank you for spending the time and writing this book.

Comments (2)

  1. Jason McKesson repo owner

    "Since cameraPos.z has it's z to be 0.0 than this would cause division by zero when the "perspective divide" happens, no? "

    No. Remember: the "position" (the vertex shader input) has a z value; the offset is just there to shift it in the X,Y axes. And cameraPos is offset + position.

    I can't explain why putting a -2 there makes it work for you, but I don't have a Mac or your Mac-compliant shader. Perhaps the zNear/zFar values aren't being set properly in your version of the code?

  2. Anonymous


    I figured it out. The problem was that I was using the vertex data from the OrthoCube.cpp instead of the vertex data from the ShaderPerspective.cpp. And that data has different positions. Using the right data fixed the problem. Sorry about the previous email.

  3. Log in to comment