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 7888729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:54:29+00:00 2026-06-03T05:54:29+00:00

When using a perspective matrix in a vertex shader am I supposed to write

  • 0

When using a perspective matrix in a vertex shader am I supposed to write code to divide by w or is it done automatically in a later stage?

The reason for my question is that I have seen lots of vertex shaders using:

gl_Position = matrix * pos;

which makes sense if there is a later stage that divides the vector with its w component.

However I never got it to work until I used the following in my vertex shader:

gl_Position = matrix * pos;
gl_Position = gl_Position / gl_Position.w;

Is the second example the correct one or could some other setting be missing?

Put it another way: which of the steps shown in the OpenGL Vertex transformation(first image) do I have to write in the vertex shader?

I know for sure that ModelView and Projection matrix belongs there(or a merge of the two). The viewport transform is not a part of the vertex shader, but what about the divide by w?

My setup is some simple triangles having coordinates within [-1 1] for x/y/z.

The Perspective matrix is supposed to project coordinates from z=-1 to -10 onto z=-1, x=[-1,1], y=[-1,1].

-1.0   0.0   0.0   0.0
 0.0  -1.0   0.0   0.0
 0.0   0.0  -1.2  -2.2
 0.0   0.0   1.0   0.0

It was generated by:

x = 2.0f * zNear / (xMax - xMin);
y = 2.0f * zNear / (yMax - yMin);
a = -(xMax + xMin) / (xMax - xMin);
b = -(yMax + yMin) / (yMax - yMin);
c = (zFar + zNear) / (zNear - zFar);
d = -(2.0f * zFar * zNear) / (zNear - zFar);

To make the matrix P:

x, 0, a, 0
0, y, b, 0
0, 0, c, d
0, 0, 1, 0;

Finally I generate the final matrix by matrix = P * T where T is a translation (0,0,-2)

I have tried to do the math on the CPU and it appears to work generating expected results, however there I also do the divide by w manually.

Update: Solved but need understanding

I negated all components in the matrix (multiply by -1) and now it works.
The example above also had an issue with projecting both positive and negative z-coordinates onto the projection plane which also got solved by this change.

Any references or explanation why it got solved by this change is welcome.

  • 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-06-03T05:54:31+00:00Added an answer on June 3, 2026 at 5:54 am

    You should not do the perspective divide yourself in the vertex shader, it will be done automatically later in the pipeline.

    If that’s not working, can you show some code or describe the problem more? I’m surprised that it’s making a difference for you.

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

Sidebar

Related Questions

I have developed some code in Python using the eclipse perspective PyDev. The code
I'm using Eclipse IDE and notice an option in the Debug perspective in the
I'm currently using twisted's perspective broker on python and I have considered in the
I have a CALayer that I apply a perspective to using a CGTransform3D and
This is sort of a homework question, however no expectations for code or whatever
I am messing around in a program that performs all rendering using a matrix
It's possible using eclipse debug perspective to disconnect from a debug process. After this,
So I've set up the perspective for my view in the onSurfaceCreate method using
I'm using a Perspective View with gluPerspective and gluLookAt and I have the x,y
I want to use in my game effect of perspective view by using few

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.