opengl绘制长方体线框,OpenGL绘制矩形轮廓

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?

EDIT

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.

After fiddling with coordinates as suggested arrived at this interesting result (iOS simulator):

uobO7.png

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.

EDIT 2

OpenGL FAQ question 14.100 states that

OpenGL doesn't provide a mechanism to cleanly join lines that share common vertices nor to cleanly cap the endpoints.

解决方案

Shift your coordinate frame a bit so your lines fall on pixel centers:

b1cb0112272262a449eb420f3c771260.png

#include

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值