关于opengl绘制圆角矩形,大小圆角要用不同的绘制方法
我是相邻像素去绘制,这样虽然在大圆角的时候比较圆润,但是到个位数圆角就感觉比较差,是不是圆角到个位数了就要用角度 sin cos去绘制比较好?怎么做才比较完美
程序代码:
#define FREEGLUT_STATIC #include <stdio.h> #include <math.h> #include <GL/gl.h> #include <GL/glut.h> /** type defines **/ /** props defines **/ static int wwidth = 0; static int wheight = 0; /** funs defines **/ void opengl_resize(int w, int h); void opengl_mouse_event(int button,int state,int x,int y); void opengl_drawrect( int x, int y, int w, int h, int r); void opengl_fillrect(int x, int y, int w, int h, int r); void opengl_display(); float opengl_onelizedA(int a); float opengl_onelizedB(int b); int main(int argc, char ** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE); glutInitWindowPosition(300, 200); glutInitWindowSize(1000, 700); glutCreateWindow("simple"); glutDisplayFunc(opengl_display); glutReshapeFunc(opengl_resize); glutMouseFunc(opengl_mouse_event); glutMainLoop(); } /** funs impl **/ void opengl_resize(int w, int h) { glViewport(0, 0, w, h); wwidth = w; wheight = h; printf("window resize ... width=%d, height=%d\n", wwidth, wheight); } void opengl_mouse_event(int button,int state,int x,int y){ printf("mouse_event running... button=%d, state=%d, x=%d, y=%d\n", button, state, x, y); } void opengl_drawrect(int x, int y, int w, int h, int r) { } void opengl_fillrect(int x, int y, int w, int h, int r) { glColor4f(1.0f, 0.0f, 0.0f, 0.1f); if(0 == r){ glBegin(GL_POLYGON); glVertex2f(opengl_onelizedA(x), opengl_onelizedB(y+h)); glVertex2f(opengl_onelizedA(x), opengl_onelizedB(y)); glVertex2f(opengl_onelizedA(x+w), opengl_onelizedB(y)); glVertex2f(opengl_onelizedA(x+w), opengl_onelizedB(y+h)); glEnd(); }else{ glBegin(GL_POLYGON); glVertex2f(opengl_onelizedA(x+r), opengl_onelizedB(y+h)); glVertex2f(opengl_onelizedA(x+r), opengl_onelizedB(y)); glVertex2f(opengl_onelizedA(x+w-r), opengl_onelizedB(y)); glVertex2f(opengl_onelizedA(x+w-r), opengl_onelizedB(y+h)); glEnd(); glBegin(GL_POLYGON); glVertex2f(opengl_onelizedA(x), opengl_onelizedB(y+h-r)); glVertex2f(opengl_onelizedA(x), opengl_onelizedB(y+r)); glVertex2f(opengl_onelizedA(x+w), opengl_onelizedB(y+r)); glVertex2f(opengl_onelizedA(x+w), opengl_onelizedB(y+h-r)); glEnd(); for(int i=0; i<r; i++){ glBegin(GL_TRIANGLES); glVertex2f(opengl_onelizedA(x+r), opengl_onelizedB(y+r)); glVertex2f(opengl_onelizedA(x+i), opengl_onelizedB( y+(r-floor(sqrt(pow(r,2)-pow(r-i,2)))) )); glVertex2f(opengl_onelizedA(x+i+1), opengl_onelizedB( y+(r-floor(sqrt(pow(r,2)-pow(r-i-1,2)))) )); glEnd(); glBegin(GL_TRIANGLES); glVertex2f(opengl_onelizedA(x+w-r), opengl_onelizedB(y+r)); glVertex2f(opengl_onelizedA(x+w-r+i), opengl_onelizedB( y+r-ceil(sqrt(pow(r,2)-pow(i,2))) )); glVertex2f(opengl_onelizedA(x+w-r+i+1), opengl_onelizedB( y+r-ceil(sqrt(pow(r,2)-pow(i+1,2))) )); glEnd(); glBegin(GL_TRIANGLES); glVertex2f(opengl_onelizedA(x+w-r), opengl_onelizedB(y+h-r)); glVertex2f(opengl_onelizedA(x+w-r+i), opengl_onelizedB( y+h-r+ceil(sqrt(pow(r,2)-pow(i,2))) )); glVertex2f(opengl_onelizedA(x+w-r+i+1), opengl_onelizedB(y+h-r+ceil(sqrt(pow(r,2)-pow(i+1,2))) )); glEnd(); glBegin(GL_TRIANGLES); glVertex2f(opengl_onelizedA(x+r), opengl_onelizedB(y+h-r)); glVertex2f(opengl_onelizedA(x+i), opengl_onelizedB( y+h-r+floor(sqrt(pow(r,2)-pow(r-i,2))) )); glVertex2f(opengl_onelizedA(x+i+1), opengl_onelizedB( y+h-r+floor(sqrt(pow(r,2)-pow(r-i-1,2))) )); glEnd(); } } } void opengl_display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); opengl_fillrect(100, 100, 500, 500, 3); glFlush(); } GLfloat opengl_onelizedA(int a) { GLfloat m = a * 1.0; GLfloat n = wwidth * 1.0; return (2 * m / n) - 1; } GLfloat opengl_onelizedB(int b) { GLfloat m = b * 1.0; GLfloat n = wheight * 1.0; return 1 - (2 * m / n); }