jp-sdt's picture

Matrix.LookAt doesn't work!

Hi,
I tried simply to change the eye/target position. But the Final-view is ever the same position. What I forgot?

using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System.Drawing;
using System.Reflection;
using OpenTK.Input;
using Riv3D_Editor;
using Riv3D_Editor.ModelClasses;
 
namespace Nehe_Tutorials
{
    class Game : GameWindow
    {
        #region Main
        static void Main()
        {
            using (Game game = new Game())
            {
                game.Run(60.0);
            }
 
        }
 
        public Game()
            : base(1024, 768, new GraphicsMode(new ColorFormat()), "Test")
        {
            VSync = VSyncMode.On;
        }
        #endregion
        #region Defenitions
        bool fullscreen;
        Matrix4d Cam;
        #endregion
        #region OnLoad
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
 
            GL.ShadeModel(ShadingModel.Smooth);
            GL.ClearColor(0.5f, 0.8f, 0.9f, 0);
 
            GL.ClearDepth(1.0);
            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Lequal);
 
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
        }
        #endregion
        #region OnResize
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
 
            GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
 
            Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 0.1f, 2000.0f);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref projection);
        }
        #endregion
        #region Update
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
            if (Keyboard[Key.Escape])
                Exit();
 
            if (Keyboard[Key.F1])
            {
                fullscreen = !fullscreen;
                if (fullscreen)
                    WindowState = WindowState.Fullscreen;
                else
                    WindowState = WindowState.Normal;
            }
        }
        #endregion
        #region Draw
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
 
 
            Cam = Matrix4d.LookAt(100.0, 0, 0, 100.0, 20.0, 0, 0, 1.0, 0); //silly example to transform the view
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref Cam);
            GL.LoadIdentity();
 
            Grid grid = new Grid();
 
            SwapBuffers();
        }
        #endregion
    }
}

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
c2woody's picture
Quote:

GL.LoadMatrix(ref Cam);
GL.LoadIdentity();

The second call overwrites your Cam matrix.

jp-sdt's picture

Hi,
I deledet it but the problem is the same.
thank you for your try^^

best reguards
jp-sdt

jp-sdt's picture

Hi,
I deledet it but the problem is the same.
thank you for your try^^

best reguards
jp-sdt

c2woody's picture

And you're sure that Grid doesn't overwrite again your modelview matrix? As it's hard to guess that code.

jp-sdt's picture

Sorry ^^"
but Im Sure there aren't any overwrites for matrixes.
Maybe I forgot somewhat important here?

class Grid
    {
        public Grid()
        {
            GL.Begin(BeginMode.Lines);
            for (int i = 5; i < 105; i = i + 5)                
            {
                GL.Vertex3(-150.0, 0, i); GL.Vertex3(150.0, 0, i);       //X-Lines of Grid (back)
                GL.Vertex3(i, 0, -150.0); GL.Vertex3(i, 0, 150.0);       //Z-Lines of Grid(right)
            }
            for (int i = -5; i > -105; i = i - 5)            
            {
                GL.Vertex3(-150.0, 0, i); GL.Vertex3(150.0, 0, i);      //X-Lines of Grid (front)
                GL.Vertex3(i, 0, -150.0); GL.Vertex3(i, 0, 150.0);     //Z-Lines of Grid(left)
            }   
            GL.End();
        }
    }
jp-sdt's picture

Ok.
When I posted the code I saw the problem ^^"
I just forgot to use GL.PopMatrix() and GL.PushMatrix()

But thank you c2woody :)