我正在使用2D引擎。它已经相当不错了,但我一直在获取像素错误。Opengl像素完美2D绘图
例如,我的窗口是960x540像素,我画了一条从(0,0)到(959,0)的直线。我期望扫描线0上的每个像素都将被设置为一种颜色,但不会:最右边的像素不会被绘制。当我垂直绘制像素539时,同样的问题。我真的需要绘制(960,0)或(0,540)绘制它。
由于我出生在像素时代,我相信这不是正确的结果。当我的屏幕大小为320x200像素时,我可以从0到319和从0到199绘制,而且我的屏幕会显示满。现在我结束了一个右/底部像素未画的屏幕。
这可能是由于不同的事情: 在那里我预计opengl线原语是从一个像素绘制到一个像素,其中最后一个像素实际上是独占的?是吗? 我的投影矩阵不正确? 我在一个错误的假设下,当我有一个960x540的后置缓冲器时,实际上有一个像素更多? 还有别的吗?
有人可以帮我吗?我现在一直在研究这个问题很长一段时间,每次当我认为它没问题时,我看了一段时间后确实没有。
这是我的一些代码,我试图尽可能地去掉它。当我打电话给我的行功能时,每个坐标都添加了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;
}
我开始认为这是我所说的“点独家”的事情。如果您使用QT或C++ Builder/Delphi甚至Windows API绘制线条,则绘制像素不包括端点。所以有可能在opengl中完成同样的工作,也可能在绘制三角形的时候呢?当你用QT,builder,...绘制一个矩形时,你也会排除右下角的...... – scippie 2012-04-06 08:48:37
以下测试显示了与我上面所说的一致:当我使用GL_POINT来填充窗口所有角落处的像素时,这些坐标与我预期的一样:(0,0),(width-1,height -1)。这表明我的绘图方式是正确的,并且该线(也可能是三角形)将不包含终点。有人可以同意吗? – scippie 2012-04-06 09:11:02
相关http://stackoverflow.com/questions/5467218/opengl-2d-hud-over-3d – 2016-04-13 15:03:33