私は2Dエンジンに取り組んでいます。すでにかなりうまく機能していますが、ピクセルエラーが発生し続けています。
たとえば、私のウィンドウは 960x540 ピクセルで、(0, 0) から (959, 0) までの線を引きます。スキャンライン 0 のすべてのピクセルが色に設定されることを期待しますが、そうではありません。右端のピクセルは描画されません。ピクセル 539 に垂直に描画すると、同じ問題が発生します。描画するには、(960, 0) または (0, 540) に描画する必要があります。
私はピクセル時代に生まれたので、これは正しい結果ではないと確信しています。画面のサイズが 320x200 ピクセルの場合、0 から 319 までと 0 から 199 までを描画でき、画面がいっぱいになりました。これで、右/下のピクセルが描画されていない画面になります。
これはさまざまなことが原因である可能性があります.openglラインプリミティブがピクセルからピクセルまで包括的に描画されると予想される場合、最後のピクセルは実際には排他的ですか? それですか?私の射影行列は間違っていますか? 私は、960x540 のバックバッファを持っている場合、実際には 1 ピクセル多いという誤った仮定をしていますか? 他の何か?
誰か助けてくれませんか?私はこの問題を長い間調べてきましたが、大丈夫だと思っていたときはいつでも、しばらくすると実際にはそうではないことがわかりました。
これが私のコードの一部です。可能な限り削除しようとしました。line-function を呼び出すと、すべての座標に 0.375、0.375 が追加され、ATI アダプターと nvidia アダプターの両方で正しくなります。
int width = resX();
int height = resY();
for (int i = 0; i < height; i += 2)
rm->line(0, i, width - 1, i, vec4f(1, 0, 0, 1));
for (int i = 1; i < height; i += 2)
rm->line(0, i, width - 1, i, vec4f(0, 1, 0, 1));
// when I do this, one pixel to the right remains undrawn
void rendermachine::line(int x1, int y1, int x2, int y2, const vec4f &color)
{
... some code to decide what std::vector the coordinates should be pushed into
// m_z is a z-coordinate, I use z-buffering to preserve correct drawing orders
// vec2f(0, 0) is a texture-coordinate, the line is drawn without texturing
target->push_back(vertex(vec3f((float)x1 + 0.375f, (float)y1 + 0.375f, m_z), color, vec2f(0, 0)));
target->push_back(vertex(vec3f((float)x2 + 0.375f, (float)y2 + 0.375f, m_z), color, vec2f(0, 0)));
}
void rendermachine::update(...)
{
... render target object is queried for width and height, in my test it is just the back buffer so the window client resolution is returned
mat4f mP;
mP.setOrthographic(0, (float)width, (float)height, 0, 0, 8000000);
... all vertices are copied to video memory
... drawing
if (there are lines to draw)
glDrawArrays(GL_LINES, (int)offset, (int)lines.size());
...
}
// And the (very simple) shader to draw these lines
// Vertex shader
#version 120
attribute vec3 aVertexPosition;
attribute vec4 aVertexColor;
uniform mat4 mP;
varying vec4 vColor;
void main(void) {
gl_Position = mP * vec4(aVertexPosition, 1.0);
vColor = aVertexColor;
}
// Fragment shader
#version 120
#ifdef GL_ES
precision highp float;
#endif
varying vec4 vColor;
void main(void) {
gl_FragColor = vColor.rgb;
}