OpenGL 4.2 LookAt matrix only works with -z value for eye position

Started by
22 comments, last by DBisar 4 years, 7 months ago
19 minutes ago, GoliathForge said:

How about your model matrix uniform...is that in a garbage state because it's commented out client side? What does that log?

model:
(1, 0, 0, 0)
(0, 1, 0, 0)
(0, 0, 1, 0)
(0, 0, 0, 1)
 

20 minutes ago, Green_Baron said:

...'Nother thing, do you actually set the view projection matrix ? What is 'value' ?

value is any value used for the uniform. I'll try to setup a minimal project and post that...

Advertisement

Ok, i ask again ?

What is the value of 'value' and where and how do you set the shader's uniforms. Also, please tell us how you obtain 'Id'. Is that the location of the model matrix or the viewProjection matrix ?

 

 

3 minutes ago, Green_Baron said:

Ok, i ask again ?

What is the value of 'value' and where and how do you set the shader's uniforms. Also, please tell us how you obtain 'Id'. Is that the location of the model matrix or the viewProjection matrix ?

 

 

value is just the matrix you set (depends); this is a method of an uniform encapsulation:


Id = GL.GetUniformLocation(programId, name);

public void Set(ref Matrix4 value)
{
    GL.UniformMatrix4(Id, false, ref value);
}

this is inside a class; it is instanciated for each uniform in the shader.

So yes Id is the location of the corresponding uniform.

 

You're messing with me. Good luck solving the problem.

8 minutes ago, Green_Baron said:

You're messing with me. Good luck solving the problem.

No I am not... sorry if i said something wrong... See my next post for complete code (just was working on that...)


using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;

namespace OGL420_Matrices
{
    // OpenTK version 3.1.0
    internal class Program
    {
        public static void Main(string[] args)
        {
            var program = new Program();
            program.Run();
        }
        
        private GameWindow _gameWindow;
        private Matrix4 _projectionMatrix;
        private Matrix4 _viewMatrix;
        private Matrix4 _viewProjectionMatrix;
        private Matrix4 _modelMatrix;
        private int _vbaId, _programId, _viewProjectionUniformId, _modelMatrixUniformId;

        private void Run()
        {
            // 4, 2 is OpenGL 4.2
            using (_gameWindow = new GameWindow(800, 600, GraphicsMode.Default, "", GameWindowFlags.Default,
                DisplayDevice.Default, 4, 2, GraphicsContextFlags.Default))
            {
                _gameWindow.Load += OnLoad;
                _gameWindow.Resize += OnResize;
                _gameWindow.RenderFrame += OnRenderFrame;
                
                _gameWindow.Run();
            }
        }

        private void OnResize(object sender, EventArgs e)
        {
            var clientArea = _gameWindow.ClientRectangle;
            GL.Viewport(0, 0, clientArea.Width, clientArea.Height);
        }

        private void OnLoad(object sender, EventArgs e)
        {
            _projectionMatrix = Matrix4.CreateOrthographic(3, 3, 0.001f, 50);

            // change -1 to -2.1f you dont see anything
            // change -1 to 2f you still see the same
            // change -1 to >= 0 you dont see anything; of course 0 doesn't make sense but 1 would
            _viewMatrix = Matrix4.LookAt(
                new Vector3(0, 0, -1f),
                new Vector3(0, 0, 0),
                new Vector3(0, 1, 0));
            _modelMatrix = Matrix4.Identity;

            var data = new float[]
            {
                0, 0, 0,
                1, 0, 0,
                0, 1, 0
            };
            
            var vboId = GL.GenBuffer();

            GL.BindBuffer(BufferTarget.ArrayBuffer, vboId);
            GL.BufferData(BufferTarget.ArrayBuffer, data.Length * sizeof(float), data, BufferUsageHint.StaticDraw);
            
            _vbaId = GL.GenVertexArray();

            GL.BindVertexArray(_vbaId);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vboId);
            
            GL.EnableVertexAttribArray(0);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
            
            var vertexShaderId = GL.CreateShader(ShaderType.VertexShader);
            GL.ShaderSource(vertexShaderId, @"#version 420

layout(location = 0) in vec3 position;

uniform mat4 viewProjection;
uniform mat4 model;

out vec3 outColor;

void main()
{
    gl_Position = viewProjection * model * vec4(position, 1);
    outColor = vec3(1,1,1);
}");

            GL.CompileShader(vertexShaderId);
            GL.GetShader(vertexShaderId, ShaderParameter.CompileStatus, out var result);

            if (result != 1)
                throw new Exception("compilation error: " + GL.GetShaderInfoLog(vertexShaderId));

            var fragShaderId = GL.CreateShader(ShaderType.FragmentShader);
            GL.ShaderSource(fragShaderId, @"#version 420

in vec3 outColor;
out vec4 fragmentColor;

void main()
{
    fragmentColor = vec4(outColor, 1);
}");

            GL.CompileShader(fragShaderId);
            GL.GetShader(fragShaderId, ShaderParameter.CompileStatus, out result);

            if (result != 1)
                throw new Exception("compilation error: " + GL.GetShaderInfoLog(fragShaderId));

            _programId = GL.CreateProgram();
            GL.AttachShader(_programId, vertexShaderId);
            GL.AttachShader(_programId, fragShaderId);

            GL.LinkProgram(_programId);
            GL.GetProgram(_programId, GetProgramParameterName.LinkStatus, out var linkStatus);

            if (linkStatus != 1) // 1 for true
                throw new Exception("Shader program compilation error: " + GL.GetProgramInfoLog(_programId));
            
            GL.DeleteShader(vertexShaderId);
            GL.DeleteShader(fragShaderId);

            _viewProjectionUniformId = GL.GetUniformLocation(_programId, "viewProjection");
            _modelMatrixUniformId = GL.GetUniformLocation(_programId, "model");
        }

        private void OnRenderFrame(object sender, FrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit);

            _viewProjectionMatrix = _projectionMatrix * _viewMatrix;
            
            GL.UniformMatrix4(_viewProjectionUniformId, false, ref _viewProjectionMatrix);
            GL.UniformMatrix4(_modelMatrixUniformId, false, ref _modelMatrix);

            GL.UseProgram(_programId);
            GL.BindVertexArray(_vbaId);
            GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
            
            _gameWindow.SwapBuffers();
        }
    }
}

 


private void OnResize(object sender, EventArgs e)
{
      var clientArea = _gameWindow.ClientRectangle;
      GL.Viewport(0, 0, clientArea.Width, clientArea.Height);
      _projectionMatrix = Matrix4.CreateOrthographic(clientArea.Width, clientArea.Height, -1, 1); 
}

private void OnLoad(object sender, EventArgs e)
{
      //_projectionMatrix = Matrix4.CreateOrthographic(3, 3, 0.001f, 50); // remove me
.
.
.
      // scale vertex array to be in pixel space

what about passing the actual screen size values to CreateOrthographic...does that change anything? (1pixel::1unit)

Not really. I see a small dot, conditions are the same for the eye position, no matter if zNear is positiv or negativ.

scrub

3 minutes ago, Green_Baron said:

Ok, i am back.

I asked you to give us numbers and you lectured me basic programming. I felt being messed with.

Now i see the problem: your Triangle is in x/z while your camera looks at x/y.

With these coordinates it'll work:

0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f

And do read the article i linked.

Ok sorry, guess i misunderstood your question...

Hm, how would I apply that coordinates you mentioned? The triangle is defined as:


var data = new float[]
            {
                0, 0, 0,
                1, 0, 0,
                0, 1, 0
            };

isn't that exactly what you wrote?

Ok, thanks to someone on Stackoverflow the problem is solved. The problem is OpenTK and how it handles matrices. The multiplication order is inverse compared to other libs. Means instead of writing: 

 


_viewProjectionMatrix = _projectionMatrix * _viewMatrix;

I need to write


_viewProjectionMatrix = _viewMatrix * _projectionMatrix;

And it works fine.

This topic is closed to new replies.

Advertisement