5
\$\begingroup\$

I was reading chapter 4 of the Arcsyntesis OpenGL tutorial when I came across this line in the GLSL shader.

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

About it, the tutorial says

The first statement simply applies the offset to get the camera space positions of the vertices.

I understand what cameraPos is but I don't get what the offset is. It's defined as a uniform and it's passed to the shader by the main program like this:

glUniform2f(offsetUniform, 1.5f, 0.5f);

So offset.x = 1.5f and offset.y = 0.5f. But I see no mention of that offset in the rest of the tutorial and its values seem quite random to me. I know that the offset from the projection plane to the eye is always -1 but that refers to a value of Z. Where do X and Y come from? Are they arbitrary values? What they mean, what they do? I understood everything up to the point but I can find no reference to said offset in the tutorial and I'd like to know if someone can explain it to me, please.

Edit: the last line I posted was probably forgotten by the author of the tutorial and can be find in the book's source distribution here.

\$\endgroup\$
1
  • \$\begingroup\$ Looks like they are just fixed values that they are using to locate the camera. However, you have said that these values are set in the main program as glUniform2f(offsetUniform, 1.5f, 0.5f);, but I cannot find this code in the link you provided. Where did you get it from? \$\endgroup\$
    – Dan
    Commented Nov 6, 2011 at 12:33

2 Answers 2

1
\$\begingroup\$

It's the same offset used in chapter 3 to move the vertices around in circular motion. The offset uniform is supplied in the display function

float fXOffset = 0.0f, fYOffset = 0.0f;
ComputePositionOffsets(fXOffset, fYOffset);

glUniform2f(offsetLocation, fXOffset, fYOffset);

and the position was updated identically in the shader:

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

Chapter 4 uses offset similarly:

The vertex shader is a combination of things we know. It passes a color through to the fragment stage, but it also takes a vec2 offset uniform that it adds an offset to the X and Y components of the position.

So what I think the tutorial means by

The first statement simply applies the offset to get the camera space positions of the vertices.

is that the vertices are initially defined in their own model space (around the origin), and by applying a transformation on each vertex we get a new position. This will become clearer in chapter 6, where you learn additional ways to transform vertices (to resize an object or rotate it for example) and a better way of implementing such a transformation. For now offset is used to make the examples more interesting without having to introduce you to more advanced concepts.

\$\endgroup\$
1
\$\begingroup\$

I understand what cameraPos is but I don't get what the offset is. It's defined as a uniform and it's passed to the shader by the main program like this:

It's the same offset used in the non-perspective version of the tutorial. This is the orthographic shader:

#version 330

layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;

smooth out vec4 theColor;

uniform vec2 offset;

void main()
{
    gl_Position = position + vec4(offset.x, offset.y, 0.0, 0.0);
    theColor = color;
}

This is the perspective version:

#version 330

layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;

smooth out vec4 theColor;

uniform vec2 offset;
uniform float zNear;
uniform float zFar;
uniform float frustumScale;

void main()
{
    vec4 cameraPos = position + vec4(offset.x, offset.y, 0.0, 0.0);
    vec4 clipPos;

    clipPos.xy = cameraPos.xy * frustumScale;

    clipPos.z = cameraPos.z * (zNear + zFar) / (zNear - zFar);
    clipPos.z += 2 * zNear * zFar / (zNear - zFar);

    clipPos.w = -cameraPos.z;

    gl_Position = clipPos;
    theColor = color;
}

See how they both start out the same way? position + offset. The important part is what is different about them. And since I'd covered the offsetting in Tutorial 3, I didn't feel that it was necessary to bring it up again in Tutorial 4.

However, I did add a clarification that points out that the offset is the same as previous offset values.

\$\endgroup\$
1
  • \$\begingroup\$ is the Offset same as Camera Center Shift ? \$\endgroup\$
    – Summit
    Commented Apr 13, 2023 at 6:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .