• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Meltac

Beginner Question: 3D to 2D projection of a point in pixel shader

11 posts in this topic

Hi everyone!

I've got a very basic question (I'm really bad in math, so please no introductions into vector geometry):

In my pixel shader I want to draw a point whose 3D world-space coordinates I have onto the screen. How do I calculate its correct 2D screen-space coordinates, given the camera/view direction and the field of view angle (83 degrees) of the screen?

Thanks in advance!
0

Share this post


Link to post
Share on other sites
You need to setup the projection matrix, that is all. Both, OpenGL(glut) and DirectX , provide according utility methods to calculate the projection matrix for you. Edited by Ashaman73
0

Share this post


Link to post
Share on other sites
[quote name='Ashaman73' timestamp='1342181474' post='4958764']
You need to setup the projection matrix, that is all. Both, OpenGL(glut) and DirectX , provide according utility methods to calculate the projection matrix for you.
[/quote]

Thanks. I'm using DirectX, so what utility methods would I need here and what parameters to feed them?

EDIT:
I'm in a pixel shader, so I can only use HLSL code, no D3d... functions! Edited by Meltac
0

Share this post


Link to post
Share on other sites
[url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb147302%28v=vs.85%29.aspx"]Setting up your projection matrix in DX9.[/url]
0

Share this post


Link to post
Share on other sites
I think my game already provides the world, view, and projection matrices:

[CODE]
uniform half3x4 m_W;
uniform half3x4 m_V;
uniform half4x4 m_P;
uniform half3x4 m_WV;
uniform half4x4 m_VP;
uniform half4x4 m_WVP;
[/CODE]

But which one do I have to use here, and how to apply it?
0

Share this post


Link to post
Share on other sites
If I understand you correctly, you are in a vertex shader that likely has a world space position as input and you are trying to output the appropriate position. To do this you will need to do something like the following:

float4 someVertexShaderFunc(float3 worldPos) : SV_POSITION
{
[indent=1]float4 outPos = mul(float4(worldPos, 1), m_WVP);[/indent]
[indent=1]...[/indent]
[indent=1]return outPos;[/indent]
}
0

Share this post


Link to post
Share on other sites
[quote name='JBurnside' timestamp='1342203655' post='4958858']
If I understand you correctly, you are in a vertex shader that likely has a world space position as input and you are trying to output the appropriate position. To do this you will need to do something like the following:

float4 someVertexShaderFunc(float3 worldPos) : SV_POSITION
{
[indent=1]float4 outPos = mul(float4(worldPos, 1), m_WVP);[/indent]
[indent=1]...[/indent]
[indent=1]return outPos;[/indent]
}
[/quote]

No, as mentioned in my first post I'm in a pixel shader, so how would I do it then?
0

Share this post


Link to post
Share on other sites
You can't do it in a pixel shader - the projection has already been done a coupla stages in the pipeline before that and can't be modifed. You need to use a vertex shader instead.
0

Share this post


Link to post
Share on other sites
[quote name='mhagain' timestamp='1342296217' post='4959123']
You can't do it in a pixel shader - the projection has already been done a coupla stages in the pipeline before that and can't be modifed. You need to use a vertex shader instead.
[/quote]

Sorry but I disagree. It might be true that I'm [b]not supposed[/b] to do this in a pixel shader, as it's normally part of the vertex shader's work. But that doesn't necessarily mean that it's [b]not possible[/b] to do in a pixel shader - only the required approach would be different.

I can do all math I want in my pixel shader, so what I just need here is the [b]correct geometric transformation formula[/b]. That's a purely mathematical issue, not one of any GPU pipeline thingy.

So are there any other ideas here? One alternate option that came into my mind was if I have the actual [b]angle between the view direction and the location vector of the spot[/b] in question (which can simply be calculated with the dot product as I've learned), then it might be possible to calculate the [b]horizontal distance from that spot to the screen center[/b] when its projected to screen space by some formula. However that formula is not linear, it's likely to involve some circle calculation or something, so I've not been able to figure it out so far.

Any help here?

EDIT:
I guess I'll need some like kind of Azimuthal Projection: See here: [url="http://www-history.mcs.st-and.ac.uk/Projects/Hoyer/S3.html"]http://www-history.m...s/Hoyer/S3.html[/url]

For a start it would already be great to know which kind of projection needs to be done here:[list]
[*]Gnomonic Projection, or
[*]Stereographic Projection, or
[*]Orthographic Projection?
[/list]


EDIT 2:
Apparently what I need is perspective projection, so probably none of the above. But still this is pure math, isn't it? So there must be some formula! Edited by Meltac
0

Share this post


Link to post
Share on other sites
You can certainly do whatever math you want in a pixel shader, but you can't change what pixel is being defined at that point in the pipeline. The math I posted for the vertex shader would work in a pixel shader if you have the right data, but the pixel being processed has already been defined.
0

Share this post


Link to post
Share on other sites
[quote name='JBurnside' timestamp='1342535337' post='4960009']
You can certainly do whatever math you want in a pixel shader, but you can't change what pixel is being defined at that point in the pipeline. The math I posted for the vertex shader would work in a pixel shader if you have the right data, but the pixel being processed has already been defined.
[/quote]

That wouldn't be the issue. I don't need to [i]change[/i] any pixel's position either in world nor in screen space. I just need want to do the following:
[list=1]
[*]Take the world space sun position and mirror that spot on the axis of the camera to get a virtual world point opposite to the sun
[*]Calculate the screen space coordinate of that point with perspective projection
[*]Check whether the currently processed pixel is within some screen-space distance (in 2D) range to that projected spot.
[*]If so, output some color blended into the original pixel color.
[/list]

When done this would result in a rainbow being drawn as some sort of post process effect from within my pixel shader.

So, is this possible with that code snippet above, and if so, why does the mul(float4(worldPos, 1), m_WVP) result in an incorrect screen position?


EDIT:
I have tested several different projection approaches and matrices. The overall result is always the same: When I turned the camera to the left or right, the calculated screen position is not accurate.

More precisely, I "moves" over the screen while turning in the correct manner as long as near the screen center. Then, when about 1/4 of the screen away from center, it starts moving "faster", meaning the calculated position is further away from the center than it should be. Then, when coming to some 3/4 of the screen (i.e. near the left or right border), the calculated point moves too slow, i.e. is not enough right/left positioned.

This happens not abruptly but fluently, like some roller coaster movement, first the calculated position wanders too much left/right, then too few left/right. Seems to depend on the circular movement of turning, but I couldn't figure out how to express this in a formula or what adjustments must take place to correct this. Edited by Meltac
0

Share this post


Link to post
Share on other sites
[quote name='Ashaman73' timestamp='1342181781' post='4958767']
[url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb147302%28v=vs.85%29.aspx"]Setting up your projection matrix in DX9.[/url]
[/quote]

I've tried building my own projection matrix both with the above approach and with this one:

[url="http://www.geeks3d.com/20090729/howto-perspective-projection-matrix-in-opengl/"]http://www.geeks3d.c...trix-in-opengl/[/url]

Both didn't help. Strange thing is, in both cases [b]only changing the value of FOV changes the result, while near and far plane values seem to be ignored at all.[/b]

Why this? Is this kind of matrix / projection the thing I need here at all? Looks to me very much as if I'm missing something incredibly basic, but somehow I couldn't figure out what's wrong here.


EDIT: typos. Edited by Meltac
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0