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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:45:49+00:00 2026-05-12T18:45:49+00:00

From this site: http://www.toymaker.info/Games/html/vertex_shaders.html We have the following code snippet: // transformations provided by

  • 0

From this site: http://www.toymaker.info/Games/html/vertex_shaders.html

We have the following code snippet:

// transformations provided by the app, constant Uniform data
float4x4 matWorldViewProj: WORLDVIEWPROJECTION;

// the format of our vertex data
struct VS_OUTPUT
{
  float4 Pos  : POSITION;
};

// Simple Vertex Shader - carry out transformation
VS_OUTPUT VS(float4 Pos  : POSITION)
{
  VS_OUTPUT Out = (VS_OUTPUT)0;
  Out.Pos = mul(Pos,matWorldViewProj);
  return Out;
}

My question is: why does the struct VS_OUTPUT have a 4 dimensional vector as its position? Isn’t position just x, y and z?

  • 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-12T18:45:49+00:00Added an answer on May 12, 2026 at 6:45 pm

    Because you need the w coordinate for perspective calculation. After you output from the vertex shader than DirectX performs a perspective divide by dividing by w.

    Essentially if you have 32768, -32768, 32768, 65536 as your output vertex position then after w divide you get 0.5, -0.5, 0.5, 1. At this point the w can be discarded as it is no longer needed. This information is then passed through the viewport matrix which transforms it to usable 2D coordinates.

    Edit: If you look at how a matrix multiplication is performed using the projection matrix you can see how the values get placed in the correct places.

    Taking the projection matrix specified in D3DXMatrixPerspectiveLH

    2*zn/w  0       0              0
    0       2*zn/h  0              0
    0       0       zf/(zf-zn)     1
    0       0       zn*zf/(zn-zf)  0
    

    And applying it to a random x, y, z, 1 (Note for a vertex position w will always be 1) vertex input value you get the following

    x' = ((2*zn/w) * x) + (0 * y) + (0 * z) + (0 * w)
    y' = (0 * x) + ((2*zn/h) * y) + (0 * z) + (0 * w)
    z' = (0 * x) + (0 * y) + ((zf/(zf-zn)) * z) + ((zn*zf/(zn-zf)) * w)
    w' = (0 * x) + (0 * y) + (1 * z) + (0 * w)
    

    Instantly you can see that w and z are different. The w coord now just contains the z coordinate passed to the projection matrix. z contains something far more complicated.

    So .. assume we have an input position of (2, 1, 5, 1) we have a zn (Z-Near) of 1 and a zf (Z-Far of 10) and a w (width) of 1 and a h (height) of 1.

    Passing these values through we get

    x' = (((2 * 1)/1) * 2
    y' = (((2 * 1)/1) * 1
    z' = ((10/(10-1)  * 5 + ((10 * 1/(1-10)) * 1)
    w' = 5
    

    expanding that we then get

    x' = 4
    y' = 2
    z' = 4.4
    w' = 5
    

    We then perform final perspective divide and we get

    x'' = 0.8
    y'' = 0.4
    z'' = 0.88
    w'' = 1
    

    And now we have our final coordinate position. This assumes that x and y ranges from -1 to 1 and z ranges from 0 to 1. As you can see the vertex is on-screen.

    As a bizarre bonus you can see that if |x’| or |y’| or |z’| is larger than |w’| or z’ is less than 0 that the vertex is offscreen. This info is used for clipping the triangle to the screen.

    Anyway I think thats a pretty comprehensive answer 😀

    Edit2: Be warned i am using ROW major matrices. Column major matrices are transposed.

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

Sidebar

Related Questions

I have downloaded the sample code from this site for FTP Client http://www.lysesoft.com/products/andftp/index.html But
I am using code from this site: http://www.spacesimulator.net/tut4_3dsloader.html It works in their example project
How do I turn this regexp [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])? from this site: http://www.regular-expressions.info/email.html into a Perl
from this site there is header compress.h http://www.fredosaurus.com/notes-cpp/examples/compress/compress.html i am using visual c++ 2010
using code from this site: http://www.saltycrane.com/blog/2008/09/simplistic-python-thread-example/ The code is import time from threading import
I am following the tutorial for py2exe from this site http://www.py2exe.org/index.cgi/Tutorial this is the
I downloaded the font from this site: http://www.fontsquirrel.com/fonts/Hattori-Hanzo Here's my code: <style type=text/css> @font-face
I got this code from this site: http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx But I'm just a beginner so
I have a site http://www.special4you.co.uk and has a animation nav made from balloons. This
I have replicated a toggle functionality from this site: http://www.williamsprofessionalpainting.com/FAQ.php Here is the updated

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.