Pyglet OpenGL绘图抗锯齿

发布于 2024-08-31 14:27:26 字数 1392 浏览 5 评论 0原文

我一直在寻找一种在 OpenGL 中消除锯齿线的方法,但它们似乎都不起作用......这里有一些示例代码:

import pyglet                                                                      
from pyglet.gl import *                                                            

window = pyglet.window.Window(resizable=True)                                      

@window.event                                                                      
def on_draw():                                                                     
    window.clear()                                                                 
    pyglet.gl.glColor4f(1.0,0,0,1.0)                                               
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)                             
    glEnable (GL_BLEND)                                                            
    glEnable (GL_LINE_SMOOTH);                                                     
    glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE)                                     
    glLineWidth (3)                                                                
    pyglet.graphics.draw(2, pyglet.gl.GL_LINES,                                    
        ('v2i', (10, 15, 300, 305))                                                
    )                                                                              

pyglet.app.run() 

任何人都可以看到我做错了什么吗?

I've been looking around for a way to anti-alias lines in OpenGL, but none of them seem to work... here's some example code:

import pyglet                                                                      
from pyglet.gl import *                                                            

window = pyglet.window.Window(resizable=True)                                      

@window.event                                                                      
def on_draw():                                                                     
    window.clear()                                                                 
    pyglet.gl.glColor4f(1.0,0,0,1.0)                                               
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)                             
    glEnable (GL_BLEND)                                                            
    glEnable (GL_LINE_SMOOTH);                                                     
    glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE)                                     
    glLineWidth (3)                                                                
    pyglet.graphics.draw(2, pyglet.gl.GL_LINES,                                    
        ('v2i', (10, 15, 300, 305))                                                
    )                                                                              

pyglet.app.run() 

Can anyone see what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若言繁花未落 2024-09-07 14:27:26

允许 Pyglet 使用额外的样本缓冲区可能会有所帮助。将您的窗口线更改为:

config = pyglet.gl.Config(sample_buffers=1, samples=4)
window = pyglet.window.Window(config=config, resizable=True) 

这对我有用。

Allow Pyglet to use an extra sample buffer might help. Change your window line to this:

config = pyglet.gl.Config(sample_buffers=1, samples=4)
window = pyglet.window.Window(config=config, resizable=True) 

This works for me.

陌上青苔 2024-09-07 14:27:26

很难肯定地说。第一件事可能是将您的提示从 GL_DONT_CARE 更改为 GL_NICEST。对于大多数显卡来说,它可能不会有太大区别,但可能会有所帮助。

除此之外,就有点难说了。这里有一些代码(C++;抱歉):

void draw_line(float y_offset) { 
    glBegin(GL_LINES);
        glColor3f(1.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f, 0.0f+y_offset, -1.0f);
        glVertex3f(1.0f, 0.1f+y_offset, -1.0f);
    glEnd();
}

void draw() { 
    glClearColor(0.0f, 0.0f, 0.2f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glFrustum(-1.2f, 1.2f, -0.2f, 0.2f, 0.8f, 5.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glLineWidth(3.0f);
    glDisable(GL_LINE_SMOOTH);
    draw_line(0.0f);
    glEnable(GL_LINE_SMOOTH);
    glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
    draw_line(-0.1f);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
    draw_line(-0.2f);

    glFlush();
}

这是我得到的输出:

 alt text

打开线条平滑后的差异似乎非常明显。 GL_DONT_CARE 和 GL_NICEST 之间的差异(至多)要小很多。

It's a bit hard to say for sure. The first thing is probably to change your hint from GL_DONT_CARE to GL_NICEST. It probably won't make much difference with most graphics cards, but it might help a little.

Other than that, it's a bit hard to say. Here's a bit of code (in C++; sorry):

void draw_line(float y_offset) { 
    glBegin(GL_LINES);
        glColor3f(1.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f, 0.0f+y_offset, -1.0f);
        glVertex3f(1.0f, 0.1f+y_offset, -1.0f);
    glEnd();
}

void draw() { 
    glClearColor(0.0f, 0.0f, 0.2f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glFrustum(-1.2f, 1.2f, -0.2f, 0.2f, 0.8f, 5.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glLineWidth(3.0f);
    glDisable(GL_LINE_SMOOTH);
    draw_line(0.0f);
    glEnable(GL_LINE_SMOOTH);
    glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
    draw_line(-0.1f);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
    draw_line(-0.2f);

    glFlush();
}

And here's what I'm getting as output from that:

alt text

The difference when line-smoothing is turned on seems pretty obvious. The difference between GL_DONT_CARE and GL_NICEST is (at most) a lot less so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文