# Texel size in perspective projection

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

## 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 on other sites
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 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 on other sites
Um... Why don't you simply set up an orthogonal projection which matches the screen resolution?

##### Share on other sites
Quote:
 Original post by szecsUm... 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 on other sites
Quote:
 Original post by HeadkazeNow 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 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);}

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633270
• Total Posts
3011158
• ### Who's Online (See full list)

There are no registered users currently online

×