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 :)