Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw lines with custom thickness with Pyglet/OpenGL?

I've drawn a rectangle (4 line segments), but they're too thin. How can I change the thickness of each line?

The following example of mine gives me this:

enter image description here

if __name__ == '__main__':
    app = App()
    game_area = None

    labels = [pyglet.text.Label("0"*8,
              font_name = "Times New Roman",
              font_size=18,
              color = (255, 0, 0, 255),
              x = app.width // 2, 
              y = app.height // 2 - n,
              anchor_x = "center", 
              anchor_y = "center") for n in range(0, 100, 18)]

    @app.event
    def on_draw():
        app.clear()
        [label.draw() for label in labels]
        pyglet.graphics.draw(4, pyglet.gl.GL_LINES, 
            ("v2f", (0, 0, 0, app.height, app.width / 2, app.height, app.width / 2, 0))
        )

    pyglet.app.run()
like image 229
Ericson Willians Avatar asked Dec 19 '22 22:12

Ericson Willians


2 Answers

You can change the width of lines by calling glLineWidth. This is accomplished through pyglet like so:

pyglet.gl.glLineWidth(desired_line_size)

If you require lines of varying thickness you'll have to call the function again with the new thickness before drawing each line. Otherwise, you can just set the thickness in your initialization and leave it be.

like image 174
Josh Avatar answered Dec 27 '22 09:12

Josh


I've realized that actually I have to draw triangles in order to achieve "thickness":

def draw_game_area():
        pyglet.graphics.draw(24, pyglet.gl.GL_TRIANGLES, 
            ("v2f", (ORIGIN, ORIGIN, ORIGIN, app.height, LINE_SEGMENT_THICKNESS, ORIGIN,
                     ORIGIN, app.height, LINE_SEGMENT_THICKNESS, app.height, LINE_SEGMENT_THICKNESS, ORIGIN,
                     ORIGIN, ORIGIN, ORIGIN, LINE_SEGMENT_THICKNESS, app.width / 2, ORIGIN,
                     ORIGIN, LINE_SEGMENT_THICKNESS, app.width / 2, LINE_SEGMENT_THICKNESS, app.width / 2, ORIGIN,
                     app.width / 2 - LINE_SEGMENT_THICKNESS, app.height, app.width / 2, app.height, app.width / 2, ORIGIN,
                     app.width / 2 - LINE_SEGMENT_THICKNESS, app.height, app.width / 2, ORIGIN, app.width / 2 - LINE_SEGMENT_THICKNESS, ORIGIN,
                     ORIGIN, app.height, app.width / 2, app.height, app.width / 2, app.height - LINE_SEGMENT_THICKNESS,
                     ORIGIN, app.height, ORIGIN, app.height - LINE_SEGMENT_THICKNESS, app.width / 2, app.height - LINE_SEGMENT_THICKNESS))
        )

enter image description here

like image 26
Ericson Willians Avatar answered Dec 27 '22 09:12

Ericson Willians