2015-03-30 154 views
0

我想用SDL2绘制平滑的填充椭圆。 我有以下代码SDL2 OpengGL消除锯齿没有效果

#include <iostream> 
#include <SDL2/SDL.h> 
#include <SDL2/SDL2_gfxPrimitives.h> 
#include <GL/gl.h> 

void init() 
{ 
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); 
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2); 
    glEnable(GL_MULTISAMPLE); 
} 

int main() 
{ 
    SDL_Init(SDL_INIT_EVERYTHING); 
    init(); 

SDL_Window* window = SDL_CreateWindow("Anti-aliasing test", 
             SDL_WINDOWPOS_CENTERED, 
             SDL_WINDOWPOS_CENTERED, 
             100, 100, 
             SDL_WINDOW_RESIZABLE); 
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); 

int Buffers, Samples; 
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &Buffers); 
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &Samples); 
std::cout << "buf = " << Buffers << ", samples = " << Samples; 
std::cout.flush(); 


SDL_Event e; 
int x, y; 
while(true) 
{ 
    SDL_WaitEvent(&e); 
    if (e.type == SDL_QUIT) 
     break; 

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 
    SDL_RenderClear(renderer); 
    SDL_GetWindowSize(window, &x, &y); 
    filledEllipseRGBA(renderer, x/2, y/2, 50, 50, 255, 0, 0, 255); 
    SDL_RenderPresent(renderer); 
} 
} 

我预计抗锯齿绘制,但实际上我没有从OpenGL的效果。

我们可以看到我的圈子不使用alpha通道,这对于AA是必需的。但是控制台输出是“缓冲区= 0,样本= 4”。

关于如何在SDL2中激活(修复)AA(带或不带OpenGL)的任何想法。

下一个截图显示我有什么,我想要什么。在Pinta软件中绘制底图。

enter image description here

+0

我找到了一些“解决方案”。在调用filledEllipseRGBA之前,使用相同的参数添加aaellipseRGBA使得圆形非常好。但我认为这不是好的方法。 – Dmitry 2015-03-30 22:13:46

+0

如果您获得了** 0 **返回样本缓冲区的数量,这是您应该期望的行为。这需要至少** 1 **为多重采样做任何事情。 – 2015-03-31 08:17:11

回答

1

您正在尝试使用OpenGL窗口提示对抗锯齿的,但是当你创建一个窗口,用OpenGL上下文,这将禁用所有SDL renderering这仅适用。

SDL2/SDL2_gfxPrimitives.h用于绘制填充椭圆的库有几个抗锯齿功能,但不是用于填充的蚀刻,可以在每次绘制时绘制未填充的抗锯齿椭圆和填充的蚀刻一个消除锯齿的椭圆。

void aafilledEllipseRGBA(...) { 
    aaellipseRGBA(...); 
    filledEllipseRGBA(...); 
} 

或写你自己的抗锯齿填充椭圆渲染代码。你可以通过在代码here上查看你使用的开源库的代码。