Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6993369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:45:21+00:00 2026-05-27T19:45:21+00:00

I can’t quite figure this out. I’ve got an OpenGL application I’m converting from

  • 0

I can’t quite figure this out.
I’ve got an OpenGL application I’m converting from the old fixed function pipeline to the programmable pipeline, and getting rid of the deprecated functions I’ve been using…
I’m not the best when it comes to math, and matrixes are fairly new to me.

However, I’ve gotten my ModelView matrix working okay, but I can’t seem to get the Perspective matrix working right.

By which, I mean, I can use the fixed function deprecated functions, get the value of the matrix, and match it to the calculated values I’ve generated… ModelView? No problem. Perspective? It doesn’t match, and I don’t understand why.

The two methods can be toggled between by commenting out the preprocessor #define USEPROJMATRIX


typedef float vec_t;

const static vec_t identityMatrix[16] = { 1.0, 0.0, 0.0, 0.0, 
                                          0.0, 1.0, 0.0, 0.0, 
                                          0.0, 0.0, 1.0, 0.0, 
                                          0.0, 0.0, 0.0, 1.0 };

const static vec_t zeroMatrix[16] = { 0.0, 0.0, 0.0, 0.0,
                                      0.0, 0.0, 0.0, 0.0,
                                      0.0, 0.0, 0.0, 0.0,
                                      0.0, 0.0, 0.0, 0.0 };

const double pi = 3.1415926535897932384626433832795;

float Utils::Radians(const float& degrees)
{
    return degrees * (pi / 180);
}

vec_t* Utils::FrustumMatrix(vec_t* out_matrix, const vec_t& left, const vec_t& right, const vec_t& bottom, const vec_t& top, const vec_t& znear, const vec_t& zfar) // Replaced glFrustum()
{
    memcpy(out_matrix, zeroMatrix, 16*sizeof(vec_t));

    out_matrix[0] = (2.0 * znear) / (right - left);
    out_matrix[5] = (2.0 * znear) / (top - bottom);
    out_matrix[8] = (right + left) / (right - left);
    out_matrix[9] = (top + bottom) / (top - bottom);
    out_matrix[10] = -((zfar + znear) / (zfar - znear));
    out_matrix[11] = -1.0;
    out_matrix[14] = -((2.0 * zfar * znear) / (zfar - znear));

    return out_matrix;
}

vec_t* Utils::PerspectiveMatrix(vec_t* out_matrix, const vec_t& yFOVDegrees, const vec_t& aspectRatio, const vec_t& znear, const vec_t& zfar) // Replaces gluPerspective()
{
    vec_t range, left, right, bottom, top;

    range = tan(Radians(yFOVDegrees / 2)) * znear;
    left = -range * aspectRatio;
    right = range * aspectRatio;
    bottom = -range;
    top = range;

    return FrustumMatrix(out_matrix, left, right, bottom, top, znear, zfar);
}

#define USEPROJMATRIX
void GameLogic::OnResize(int width = 640, int height = 480)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
#ifndef USEPROJMATRIX
    glLoadMatrixf(identityMatrix);
    gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 1.0f, 1000.0f);
#else
    vec_t pMatrix[16];
    memcpy(pMatrix, identityMatrix, sizeof(vec_t) * 16);
    Utils::PerspectiveMatrix(pMatrix, 45.0f, (GLfloat)width / (GLfloat)height, 1.0f, 1000.0f);
    glLoadMatrixf(pMatrix);
#endif

    vec_t projectionmatrix[16];
    glGetFloatv(GL_PROJECTION_MATRIX, projectionmatrix);

    /* Matrix calculation results in projectionmatrix equalling this:
       1.8106601, 0.00000000, 0.00000000, 0.00000000, 
       0.00000000, 2.4142134, 0.00000000, 0.00000000, 
       0.00000000, 0.00000000, -1.0020020, -1.0000000,
       0.00000000, 0.00000000, -2.0020020, 0.0000000
    */
    /* GL calculation
       1.8106601, 0.00000000, 0.00000000, 0.00000000,
       0.00000000, 2.4142137, 0.00000000, 0.00000000,
       0.00000000, 0.00000000, -1.0020020, -1.0000000,
       0.00000000, 0.00000000, -2.0020020, 0.00000000
    */

    glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf(identityMatrix);
}

I’m checking my values both by direct observation and by comparing matrix values.
The resultant values are listed in the comments above, and I’ve no idea why they don’t match. The manually calculated matrix visually distorts the rendered scene(a simple cube offset to the lower-left of center) by not positioning it correctly, and I believe it’s rendered smaller as well.

edit
Fixed the stupid mistake as per Nicol Bolas’ answer. This still doesn’t work properly, though. Values in the comment have been updated as well.
edit2
Code above works fine now, question resolved. Stupid mistakes on my part.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:45:21+00:00Added an answer on May 27, 2026 at 7:45 pm

    This is what you use in your gluPerspective call:

    (GLfloat)width / (GLfloat)height
    

    This is what you use in your Utils::PerspectiveMatrix call:

    width / height
    

    The first one produces a floating-point value. The second produces an integer. I’m guessing you wanted the first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
Can someone please help me figure out why my accordion script at http://www.mincovlaw.com/services/copyright gets
Can't work out a way to make an array of buttons in android. This
Can anyone help me trying to find out why this doesn't work. The brushes
Does anyone know how can I replace this 2 symbol below from the string
can any one tell me how can change this java code into objective c.is
Can the Application Name passed to SQL Server (via the ADO Connection string) be
Can we do following in SQL Server 2005 query with convert function. MaxRegID =
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can I run this in a Windows command prompt like I can run it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.