Results 1 to 2 of 2

Thread: Can anyone help me with perspective projection?

  1. #1
    Newbie Newbie
    Join Date
    Apr 2017
    Posts
    2

    Question Can anyone help me with perspective projection?

    Hey all...

    I was wondering if anyone could tell me where I am going wrong when implementing a perspective matrix.
    The math for creating the matrix appears to be correct from what I can tell, however the end result is a stretched out distorted shape that takes up the entire screen, which is a far cry from the basic cube i had rendering before.
    This is purely an educational exercise, hence why I have laid out the matrix deceleration this way..

    Creation

    Code :
    static Matrix4 CreatePerspectiveMatrix(float fov, float aspect, float zNear, float zFar) {
    			Matrix4 r;
     
    			float tanThetaOver2 =(float)tanf(ToRadians(fov / 2));
    			float zRange = zNear - zFar;
    			float zTotal = zNear + zFar;
     
    			r.m[0][0] = 1.0f / (tanThetaOver2);			r.m[0][1] = 0;						r.m[0][2] = 0;					r.m[0][3] = 0;
    			r.m[1][0] = 0;								r.m[1][1] = aspect / tanThetaOver2;	r.m[1][2] = 0;					r.m[1][3] = 0;
    			r.m[2][0] = 0;								r.m[2][1] = 0;						r.m[2][2] = zTotal/zRange;	    r.m[2][3] = 2 * zFar * zNear / zRange;
    			r.m[3][0] = 0;								r.m[3][1] = 0;						r.m[3][2] = -1;					r.m[3][3] = 0;
     
    			return r;
    		}


    Implementation

    Code :
    	void Game::Draw()
    	{
    		m_display->SwapBuffer();
    		m_renderer.Draw();
    		m_shader.Bind();
    		Matrix4 mtxModel = m_transform.GetMatrix();
    		Matrix4 mtxProjection = Matrix4::CreatePerspectiveMatrix(60.0f, m_display->GetAspectRatio(), 10.0f, 1000);
    		Matrix4 mtxModelProjection = mtxProjection * mtxModel;
    		m_shader.SetSharderMatrixUniform("transform", mtxModelProjection);
    		m_renderer.DrawMesh(m_mesh);
    		m_shader.UnBind();
    		//assert(glGetError() == GL_NO_ERROR);
    	}


    End result
    Capture_1.jpg

    Thanks for reading,

    Any help available would be greatly appreciated .

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,100
    Your matrix looks fine, but it's in row-major order. OpenGL defaults to column-major order, so it needs to be either explicitly declared as being in row-major order, or transposed.

Similar Threads

  1. Replies: 3
    Last Post: 01-25-2015, 12:27 PM
  2. Perspective projection
    By ukopenglnewbie in forum OpenGL: General
    Replies: 5
    Last Post: 01-30-2009, 02:52 PM
  3. perspective projection
    By OGforever in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-27-2005, 08:24 PM
  4. perspective projection
    By pavankumar in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-23-2004, 06:25 AM
  5. Perspective Projection
    By mattias in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 06-23-2001, 06:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean