Jump to content
• Advertisement

# OpenGL Opengl scaling using ortho projection not working correctly

This topic is 668 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

Hi there. I was trying to write a sprite renderer using opentk and C#. Everything was working fine till I've noticed zooming out using ortho projection changes sprites colors. I've created a small example of what I'm doing.

First of all as I use linear texture filtering I add half texel to top and left vertices texture coordinates, and subtract half texel from right and bottom vertices texture coordinates. As this ends to make mapped area of the texture one pixel smaller I subtract 0.5 from bottom and right vertices positions, and add 0.5 to left and top vertices positions.

float x = 0;
?float y = 64;
float w = 400;
?float h = 16;

VertexPositionTexColor[] vertices = new VertexPositionTexColor[] {
new VertexPositionTexColor(){Position = new Vector2(x + 0.5f, y + 0.5f),TexCoord = new Vector2(22.5f / bmp.Width,0.5f/bmp.Height) // added 0.5f to uv to map center of the texel. did the same thing for position
,Color =new Vector4(1f,1f,1f,1f)},
new VertexPositionTexColor(){Position = new Vector2(x + w - 0.5f, y + 0.5f),TexCoord = new Vector2(22.5f / bmp.Width,0.5f/bmp.Height) // subtracted u by 0.5f and added 0.5f to v to map center of the texel. did the same thing for position
,Color =new Vector4(1f,1f,1f,1f)},
new VertexPositionTexColor(){Position = new Vector2(x + w - 0.5f, y + h - 0.5f),TexCoord = new Vector2(22.5f / bmp.Width,15.5f/bmp.Height) // subtracted uv by 0.5f to map center of the texel. did the same thing for position
,Color =new Vector4(1f,1f,1f,1f)},
new VertexPositionTexColor(){Position = new Vector2(x + 0.5f, y + h - 0.5f),TexCoord = new Vector2(22.5f / bmp.Width,15.5f/bmp.Height) // subtracted v by 0.5f and added 0.5f to u to map center of the texel. did the same thing for position
,Color =new Vector4(1f,1f,1f,1f)},
};

The texture is setup as below and I'm using an atlas.

GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);


Is this approach correct?

Now I setup my viewport and projection matrix

float viewPortWorldX = 400;
float viewPortWorldY = 300;

.
.
.
.

Matrix4 ortho = Matrix4.CreateOrthographicOffCenter(0, viewPortWorldX,
viewPortWorldY, 0, -1, 1);

GL.Viewport(0, 0, 400, 300);
GL.UniformMatrix4(GL.GetUniformLocation(program, "projection"), false,
ref ortho);

The above code works fine because my ortho projection and viewport are the same size. Except one thing. The line is one pixel shorter than 400 while I've set w to 400. It is not surprising because I've subtracted 0.5 pixel from the right side.

The strange things start when I change ortho projection configuration.

float viewPortWorldX = 800;
float viewPortWorldY = 600;

.
.
.
.

Matrix4 ortho = Matrix4.CreateOrthographicOffCenter(0, viewPortWorldX,
viewPortWorldY, 0, -1, 1);

GL.Viewport(0, 0, 400, 300);
GL.UniformMatrix4(GL.GetUniformLocation(program, "projection"), false,
ref ortho);


The line color has changed! the middle of it was white but now it is blue!

if I change position of the line from 64 to 65 or any other odd number the result will be correct!

I've attached the visual studio 2013 project to this post:

[attachment=33274:OpenTKTest.zip]

Thanks in advance :)

#### Share this post

##### Share on other sites
Advertisement

The color change may happen due to the linear filtering, because by using linear filtering, you're averaging between the 4 closest texels, and, since most of the texture is blueish, the resulting color will also be blueish.

The odd line position thing, may also be somehow related to this (though I can't properly explain it).

Have you tried setting the minification filtering to nearest instead of linear, to see if the results improve? You could keep the magnification filter linear, so that, if you zoom in, the result will be linearly filtered.

Anyway, I'm not sure this is the case, but I hope it helps.

Edited by __SKYe

#### Share this post

##### Share on other sites

The color change may happen due to the linear filtering, because by using linear filtering, you're averaging between the 4 closest texels, and, since most of the texture is blueish, the resulting color will also be blueish.

The odd line position thing, may also be somehow related to this (though I can't properly explain it).

Have you tried setting the minification filtering to nearest instead of linear, to see if the results improve? You could keep the magnification filter linear, so that, if you zoom in, the result will be linearly filtered.

Anyway, I'm not sure this is the case, but I hope it helps.

Yes this solves the problem. But makes another problem in some viewport sizes the line is thicker. It seems that there is no real solution to this problem. Even if I solve this problem there is another problem. When zooming in or out at some viewport sizes the alignment of objects breaks. some objects may overlap. I tested this situation in gamemaker 8 and it has the alignment problem.

#### Share this post

##### Share on other sites

Yes this solves the problem. But makes another problem in some viewport sizes the line is thicker. It seems that there is no real solution to this problem

Yeah, but you can't really help that, if you view such a small graphic zoomed out, stuff like this will happen.

Have you tried mipmaps? You might get a better result with them.

Even if I solve this problem there is another problem. When zooming in or out at some viewport sizes the alignment of objects breaks. some objects may overlap. I tested this situation in gamemaker 8 and it has the alignment problem.

This, I can't really help, except you could enforce sizes for the viewport zooming, instead of allowing arbitrary zooming amounts.

What I mean is, allow zooming, but in 50% increments (or something along those lines), so that you can ensure it doesn't break the alignment.

Obviously, depending on what you're trying to do, this may not be possible (or wanted), but it's a thought.

Anyway, good luck.

#### Share this post

##### Share on other sites

Thanks. I just wanted to know if there is an out of the box way to solve this. I think using some fixed viewport sizes is a good way. I already force the aspect ration to be the same in all view sizes. I should choose some viewport sizes and use them.

I should take a look at mipmaps too.

Thank you

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5
• Advertisement

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631706
• Total Posts
3001833
×

## 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!