Jump to content
  • Advertisement
Sign in to follow this  
Headkaze

Texel size in perspective projection

This topic is 2949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I have my projection matrix set as the following

gluPerspective(15, (float)320 / (float) 480, 0.1, 40);


When Z is zero how can I calculate the size of a texel?

Share this post


Link to post
Share on other sites
Advertisement
Do you mean texel(=texture element) or pixel(=screen element) ?


Specification:
gluPerspective( fovy,aspect,znear,zfar)

Determine height and width of near plane:
tan(fovy/2)=h/znear =>
h = tan(fovy/2)*znear
w = h*aspect

h=height of camera near-plane in "worldspace", w=width of camera near-plane in "worldspace"

Determine height and widht of single pixel:
Now we need the screen resolution sw and sh:
pw = w/sw
ph = h/sh

pw and ph is the size of a single pixel on the near-plane (z=0) in world space.

Share this post


Link to post
Share on other sites
Yes I want the texel size to match a pixel size. What I want to do is calculate the texel size and then draw a quad on the screen that will be 1 texel : 1 pixel.

So to draw a 64x64 pixel quad on the screen I want to do.

float quadWidth = 64.0f * pw;
float quadHeight = 64.0f * ph;

I believe with your code I have to convert the pw/ph coordinates to model space before I can draw a quad the correct size?

Share this post


Link to post
Share on other sites
Um... Why don't you simply set up an orthogonal projection which matches the screen resolution?

Share this post


Link to post
Share on other sites
Quote:
Original post by szecs
Um... Why don't you simply set up an orthogonal projection which matches the screen resolution?


Because it is a 2.5d game which has a bit of perspective (in this case a FOV of 15 which gives everything a very slight depth to it). For the game I'm making there are models mixed with 3d quads. Things are in different positions but Z=0 is where the main action is. What I wanted is to be able to have text and the size of quads to match 1:1 texels to pixels when Z=0. Everything still needs to be able to move around in Z so we don't want to use orthogonal projection.

Now that I have the formula to get the size of a texel in world space all I need to do is convert it to model space. I read that I need to apply the inverse of the model matrix. Is that correct?

Share this post


Link to post
Share on other sites
Quote:
Original post by Headkaze
Now that I have the formula to get the size of a texel in world space all I need to do is convert it to model space. I read that I need to apply the inverse of the model matrix. Is that correct?

If you don't scale the models, you can apply the size directly in model space, else just take the inverse of the model scaling into account, if you use uniform scaling.

Share this post


Link to post
Share on other sites
Here are the two functions I ended up with

static inline SizeF getTexelSize(GLfloat fovy, GLfloat zoom)
{
GLint viewPort[4];
glGetIntegerv(GL_VIEWPORT, viewPort);
GLfloat screenWidth = viewPort[2], screenHeight = viewPort[3];
GLfloat height = tanf((fovy * M_PI / 180.0f) / 2.0f) * zoom * 2.0f;
GLfloat width = height * (screenWidth / screenHeight);

SizeF texelSize = SizeFMake(width / screenWidth, height / screenHeight);

return texelSize;
}

static inline SizeF getScreenSize(GLfloat fovy, GLfloat zoom)
{
GLint viewPort[4];
glGetIntegerv(GL_VIEWPORT, viewPort);
GLfloat screenWidth = viewPort[2], screenHeight = viewPort[3];
GLfloat height = tanf((fovy * M_PI / 180.0f) / 2.0f) * zoom * 2.0f;
GLfloat width = height * (screenWidth / screenHeight);

return SizeFMake(width, height);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!