Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyglet OpenGL drawing anti-aliasing

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?

like image 891
Jared Forsyth Avatar asked Apr 30 '10 21:04

Jared Forsyth


2 Answers

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.

like image 108
Xavier Ho Avatar answered Nov 15 '22 20:11

Xavier Ho


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.

like image 30
Jerry Coffin Avatar answered Nov 15 '22 20:11

Jerry Coffin