0
\$\begingroup\$

I thought that everything outside of [-1.0f, +1.0f] should not be renderable in Open GL. However, this code renders just fine with Silk.NET. I draw 3 axes and one of them is longer than the others.

private static readonly float[] AxisPoints =
{
  // X
  -2.0f, 0.0f, 0.0f,
  2.0f, 0.0f, 0.0f,

  // Y
  0.0f, -1.0f, 0.0f,
  0.0f, 1.0f, 0.0f,

  // Z
  0.0f, 0.0f, -1.0f,
  0.0f, 0.0f, 1.0f
};

private static readonly uint[] Indices =
{
  0, 1,
  2, 3,
  4, 5
};

var numberOfLines = AxisPoints.Length / 3 / 2;

gl.VertexAttributePointer(0, numberOfLines, VertexAttribPointerType.Float, 3, 0);
gl.DrawElements(GLEnum.Lines, Indices.Length, DrawElementsType.UnsignedInt, 0);

Fragment shader

#version 330 core
out vec4 FragColor;

void main()
{
    FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}

Vertex shader

#version 330 core
layout (location = 0) in vec3 Position;

uniform mat4 Model;
uniform mat4 View;
uniform mat4 Projection;

void main()
{
    gl_Position = Projection * View * Model * vec4(Position, 1.0);
}

Question

What is the minimum and maximum value that I can specify for a vertex?

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Vertex can have ANY coordinates (within allowed IEEE754 ranges). In the end, after all MVP transformations they get mapped to your display, which is indeed limited to -1..1 (NDC)) or amount of Viewport pixels in width / height depending how you look at it. What is the practical reason for the question though? \$\endgroup\$
    – Kromster
    Commented Oct 22, 2023 at 10:09
  • \$\begingroup\$ @Kromster that looks like an answer to me. \$\endgroup\$
    – DMGregory
    Commented Oct 22, 2023 at 11:46
  • \$\begingroup\$ @DMGregory dunno, this seems like a XY-problem question altogether. \$\endgroup\$
    – Kromster
    Commented Oct 22, 2023 at 14:08
  • \$\begingroup\$ @Kromster I am rendering a chart and need to know how to correctly map physical values to screen coordinates. For example, if I have values 0, 50, 100, should I convert them 0, 0.5, 1.0 or can pass as is? \$\endgroup\$
    – Anonymous
    Commented Oct 22, 2023 at 21:25
  • \$\begingroup\$ @DMGregory Yes, talked to Chat XXX that shell not be named and it said the same. The value can be anything and after all matrix transformations, it will be normalized to [-1, +1] anyway. \$\endgroup\$
    – Anonymous
    Commented Oct 22, 2023 at 21:27

1 Answer 1

3
\$\begingroup\$

I thought that everything outside of [-1.0f, +1.0f] should not be renderable in Open GL.

The [-1.0f, +1.0f] range is what you have to arrive at as the output of your vertex shader. Because anything outside of that range is outside of the player's screen rectangle. But during any of the steps that come before that, OpenGL lets you use the whole range of single-precision floating point values.

How your intermediate coordinates scale in relation to the screen coordinates is managed by the matrices. For example, if you have two 3d models, one using coordinates in the [-1.0f, +1.0f] range and the other in the [-1000.0f, +1000.0f] range, then you can use different model matrices with different scaling factors for them. That way you can make them have the same dimensions in world space. Which range is "normal" size? That's for you to decide. Because your projection matrix is then used to control the overall scaling factor between world space and screen space. So 100 world-space units can be huge or tiny, depending on what you want them to be in your game.

See also How to transform 2D world to screen coordinates OpenGL on Stackoverflow.

\$\endgroup\$

You must log in to answer this question.

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