如何在opengl中用线连接多个点?
How to connect multiple points with lines in opengl?
我无法绘制多条线来连接我的点,当它循环时,它只画一条线。
在列表中 "pos" 我已经存储了我所有的点位置,然后我遍历列表以从列表中的一个点到下一个点画一条线,但似乎发生的是它画了一个单行。
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()
我认为问题出在这些代码行上
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()
我希望看到我所有的点都用线连接起来。像正交。
我建议创建一个点列表。列表的每个元素都是一个元组,带有点的 x 和 y 坐标:
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))
或
points = [(r*mt.cos(t), r*mt.sin(t)) for t in [2*mt.pi * i/p for i in range(p)]]
从每个点到另一个点的线可以绘制在 2 个嵌套循环中。每个循环遍历整个点列表:
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()
在单个循环中在线的顶部绘制点:
glPointSize(7.0)
glBegin(GL_POINTS)
glColor3f(1.0, 0.0, 0.0)
for pt in points:
glVertex2f(*pt)
glEnd()
注意,glLineWidth
specify the width of rasterized lines and glPointSize
指定光栅化点的直径。
我无法绘制多条线来连接我的点,当它循环时,它只画一条线。
在列表中 "pos" 我已经存储了我所有的点位置,然后我遍历列表以从列表中的一个点到下一个点画一条线,但似乎发生的是它画了一个单行。
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()
我认为问题出在这些代码行上
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()
我希望看到我所有的点都用线连接起来。像正交。
我建议创建一个点列表。列表的每个元素都是一个元组,带有点的 x 和 y 坐标:
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))
或
points = [(r*mt.cos(t), r*mt.sin(t)) for t in [2*mt.pi * i/p for i in range(p)]]
从每个点到另一个点的线可以绘制在 2 个嵌套循环中。每个循环遍历整个点列表:
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()
在单个循环中在线的顶部绘制点:
glPointSize(7.0)
glBegin(GL_POINTS)
glColor3f(1.0, 0.0, 0.0)
for pt in points:
glVertex2f(*pt)
glEnd()
注意,glLineWidth
specify the width of rasterized lines and glPointSize
指定光栅化点的直径。