Search code examples
pythonopenglpyopengl

How to connect multiple points with lines in opengl?


I couldn't draw multiple lines to connect my points, when it loops, it draws a single line.

in the list "pos" I have stored all my points positions, then I looped through the list to draw a line from one point in the list to the next, but what seems to happen is it draw one single line.

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

import numpy as np
import math as mt

pos_x = []
pos_y = []
pos= []
lol = 8

def point_pos(p, r):
    for i in range(p):
        t = 2 * mt.pi * i / p

        x = r * mt.cos(t)
        pos_x.append(x)

        y = r * mt.sin(t)
        pos_y.append(y)

        glBegin(GL_POINTS)
        glVertex2f(x, y)
        glEnd()

    pos = [[[a, b] for a in pos_x] for b in pos_y]


    for m in range(lol-1):
        glBegin(GL_LINES)
        glColor3f(0.0, 1.0, 0.0)
        glVertex2f(pos[0][m][0], pos[0][m][1])
        for n in range(lol-1):
            glVertex2f(pos[0][m+1][0], pos[0][m+1][1])
        glEnd()

def main():
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(90, (display[0]/display[1]), 0.1, 50.0)

    glTranslatef(0.0,0.0, -5)

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    point_pos(lol, 2)
    pygame.display.flip()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        #glRotatef(1, 1, 0, 1)
        pygame.time.wait(10)

main()

I think that the problem is with these lines of code

for m in range(lol-1):
     glBegin(GL_LINES)
     glColor3f(0.0, 1.0, 0.0)
     glVertex2f(pos[0][m][0], pos[0][m][1])
     for n in range(lol-1):
         glVertex2f(pos[0][m+1][0], pos[0][m+1][1])
     glEnd()

I expect to see all my points connected to each other with lines. like an orthoplex.


Solution

  • I recommend to create a list of points. Each element of the list is a tuple with the x and y coordinate of the point:

    points = []
    for i in range(p):
        t = 2 * mt.pi * i / p
    
        x = r * mt.cos(t)
        y = r * mt.sin(t)
    
        points.append((x, y))
    

    or

    points = [(r*mt.cos(t), r*mt.sin(t)) for t in [2*mt.pi * i/p for i in range(p)]]
    

    The lines from each point to each other point can be draw in 2 nested loops. Each loop traverse the entire list of points:

    glLineWidth(2.0)
    glBegin(GL_LINES)
    glColor3f(0.0, 1.0, 0.0)
    for pt1 in points:
        for pt2 in points:
            glVertex2f(*pt1)
            glVertex2f(*pt2)
    glEnd()
    

    Draw the points on top of the lines, in a single loop:

    glPointSize(7.0)
    glBegin(GL_POINTS)
    glColor3f(1.0, 0.0, 0.0)
    for pt in points:
        glVertex2f(*pt)
    glEnd()
    

    Note, glLineWidth specify the width of rasterized lines and glPointSize specify the diameter of rasterized points.