问题描述
我正在尝试绘制一条4条线的矩形(左一条).但是无论我如何绘制(带状,环形,普通线),结果都是右侧的一个-它在拐角处缺少像素.
I am trying to draw a rectangle with 4 lines (left one). But no matter how I draw (strip, loop, plain lines) the result is the right side one - it is missing pixels at the corners.
||||||||||| ||||||||||
| | | |
| | | |
||||||||||| ||||||||||
有没有办法获得左侧结果?
Is there a way to get the left hand side result?
编辑
无法发布代码,因为它在很大程度上取决于基础框架,但是所有的绘制调用只不过是glDrawElements,其索引缓冲区指向4个顶点,没有alpha测试.
Can't post code since it depends a lot on the underlying framework, but all draw calls are nothing more than glDrawElements with index buffer pointing to 4 vertices, no alpha tests.
按照建议摆弄坐标后,得到了这个有趣的结果(iOS模拟器):
After fiddling with coordinates as suggested arrived at this interesting result (iOS simulator):
请注意,某些矩形仅左上角缺少像素...在单个绘制元素调用内,所有矩形均以GL_LINES模式绘制.没有其他东西绘制.
Notice only upper left corner is missing pixels for some rectangles...All rectangles are drawn in GL_LINES mode within single draw elements call. No other things are drawn.
编辑2
OpenGL FAQ 问题14.100指出
OpenGL FAQ question 14.100 states that
推荐答案
稍微移动坐标系,使线条落在像素中心:
Shift your coordinate frame a bit so your lines fall on pixel centers:
#include <GL/glut.h>
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( 0, w, 0, h, -1, 1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// important
glTranslatef( 0.5, 0.5, 0 );
float offset = 40;
glColor3ub( 255, 0, 0 );
glBegin(GL_LINE_LOOP);
glVertex2f( 0+offset, 0+offset );
glVertex2f( 0+offset, h-offset );
glVertex2f( w-offset, h-offset );
glVertex2f( w-offset, 0+offset );
glEnd();
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 320,240 );
glutCreateWindow( "Rect" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
这篇关于OpenGL绘制矩形轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!