- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习一个在 pyOpenGL 上相当陈旧的教程系列,我正在做的和他做的完全一样。但是我遇到了延迟 - 我有 AMD FX-6300 和 8gb ram,GTX-1050ti,文件存储在闪存驱动器上。我读过一些使用 glBegin
和 glEnd
会导致问题的地方?我应该使用什么来代替以及如何在此代码中使用它:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import random
"""
- A Cube has 8 Nodes/Verticies
- 12 Lines/connections
- 6 Sides
"""
vertices = (
(1, -1, -1),
(1, 1, -1),
(-1, 1, -1),
(-1, -1, -1),
(1, -1, 1),
(1, 1, 1),
(-1, -1, 1),
(-1, 1, 1)
)
edges = ( #Contains vertexes/nodes
(0, 1),
(0, 3),
(0, 4),
(2, 1),
(2, 3),
(2, 7),
(6, 3),
(6, 4),
(6, 7),
(5, 1),
(5, 4),
(5, 7)
)
surfaces = (
(0,1,2,3),
(3,2,7,6),
(6,7,5,4),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6),
)
colors = (
(1,0,0),
(0,1,0),
(0,0,1),
(0,0,0,),
(1,1,1),
(0,1,1),
(1,0,0),
(0,1,0),
(0,0,1),
(0,0,0,),
(1,1,1),
(0,1,1),
)
def set_vertices(max_distance):
#Create change between each cube
x_value_change = random.randrange(-10, 10)
y_value_change = random.randrange(-10, 10)
z_value_change = random.randrange(-1 * max_distance, -20)
new_vertices = []
for vert in vertices:
new_vert = []
new_x = vert[0] + x_value_change
new_y = vert[1] + y_value_change
new_z = vert[2] + z_value_change
new_vert.append(new_x)
new_vert.append(new_y)
new_vert.append(new_z)
new_vertices.append(new_vert) #Appends (1, 1, 1)
return new_vertices
def Cube(veritces):
glBegin(GL_QUADS)
for surface in surfaces:
x = 0
for vertex in surface:
x += 1
glColor3fv((colors[x]))
glVertex3fv(vertices[vertex])
glEnd()
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(vertices[vertex]) #Draws vertex's in position given according to vertices array
glEnd()
def main():
pygame.init()
display = (1000, 800)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0) #FOV, aspect ratio. clipping plane min, max
glTranslatef(random.randrange(-5, 5), random.randrange(-5, 5), -40) #X,Y,Z -5 to zoom out on z axis
#glRotatef(25, 1, 20, 0) #Degrees, x,y,z
object_passed = False
max_distance = 300
cube_dict = {}
for x in range(75): #Draws 75 cubes
cube_dict[x] = set_vertices(max_distance) #Returns a new cube set of vertices
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_move = 0.3
if event.key == pygame.K_RIGHT:
x_move = -0.3
if event.key == pygame.K_UP:
y_move = -0.3
if event.key == pygame.K_DOWN:
y_move = 0.3
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_move = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_move = 0
"""
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
glTranslatef(0, 0, 1)
if event.button == 5:
glTranslatef(0, 0, -1)
"""
#glRotatef(1, 1, 1, 1)
x = glGetDoublev(GL_MODELVIEW_MATRIX)
camera_x = x[3][0] #Access camera cordinates
camera_y = x[3][1]
camera_z = x[3][2]
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) #Clears the screen
glTranslatef(x_move, y_move, 0.5)
for each_cube in cube_dict:
Cube(cube_dict[each_cube])
pygame.display.flip() #Cant use update
pygame.time.wait(10)
main()
pygame.quit()
quit()
教程是here
谢谢!!
最佳答案
I have read some places that using
glBegin
andglEnd
cause issues? What should I use instead ...
由 glBegin
和 glEnd
绘制在现代 OpenGL 中已弃用(参见 Fixed Function Pipeline 和 Legacy OpenGL ).在现代 OpenGL 中 Vertices are Specified经过 Vertex Buffer Objects和 Vertex Array Object一切都是使用 Shader program 绘制的.
作为这个方向的第一步,我建议使用 Vertex Buffer Objects和客户端能力
The commands
void VertexPointer( int size, enum type, sizei stride, const void *pointer );
void NormalPointer( enum type, sizei stride, const void *pointer );
void ColorPointer( int size, enum type, sizei stride, const void *pointer );
void SecondaryColorPointer( int size, enum type, sizei stride, const void *pointer );
void IndexPointer( enum type, sizei stride, const void *pointer );
void EdgeFlagPointer( sizei stride, const void *pointer );
void FogCoordPointer( enum type, sizei stride, const void *pointer );
void TexCoordPointer( int size, enum type, sizei stride, const void *pointer );specify the location and organization of arrays to store vertex coordinates, normals, colors, secondary colors, color indices, edge flags, fog coordinates.
[...]
An individual array is enabled or disabled by calling one of
void EnableClientState( enum array );
void DisableClientState( enum array );with array set to
VERTEX_ARRAY
,NORMAL_ARRAY
,COLOR_ARRAY
,SECONDARY_COLOR_ARRAY
,INDEX_ARRAY
,EDGE_FLAG_ARRAY
,FOG_COORD_ARRAY
, orTEXTURE_COORD_ARRAY
, for the vertex, normal, color, secondary color, color index, edge flag, fog coordinate, or texture coordinate array, respectively.
为此,您必须做好准备,并且必须包含 NumPy :
import numpy
为顶点缓冲区对象创建全局变量并为面创建属性集(颜色和顶点坐标对)并为面创建顶点缓冲区对象(顶点坐标和颜色)。最后为边的顶点坐标创建顶点缓冲对象:
def main():
global face_vbos, edge_vbo
.....
# define the vertex buffers vor the faces
vertex_array = []
color_array = []
for face in range(len(surfaces)):
for vertex in surfaces[face]:
vertex_array .append( vertices[vertex] )
color_array.append( colors[face] )
face_vbos = glGenBuffers(2)
glBindBuffer(GL_ARRAY_BUFFER, face_vbos[0])
glBufferData( GL_ARRAY_BUFFER, numpy.array( vertex_array, dtype=numpy.float32 ), GL_STATIC_DRAW )
glBindBuffer(GL_ARRAY_BUFFER, face_vbos[1])
glBufferData( GL_ARRAY_BUFFER, numpy.array( color_array, dtype=numpy.float32 ), GL_STATIC_DRAW )
glBindBuffer(GL_ARRAY_BUFFER, 0)
# define the vertex buffer for the edges
edge_vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, edge_vbo)
glBufferData( GL_ARRAY_BUFFER, numpy.array( vertices, dtype=numpy.float32 ), GL_STATIC_DRAW )
glBindBuffer(GL_ARRAY_BUFFER, 0)
while True:
# [...]
当您绘制面和边时,您已经定义了一个顶点数据数组 ( glVertexPointer
)并定义一组颜色 ( glColorPointer
)启用客户端功能 ( glEnableClientState
)。
这些面孔可以通过 glDrawArrays
绘制,因为颜色的所有坐标都存储在一个连续的数组(vertex_array
和 color_array
-> face_vbos
)。
边缘必须由 glDrawElements
绘制, 使用索引 edges
,因为顶点 (vertices
-> edge_vbo
) 必须被索引以形成线:
def Cube(veritces):
global face_vbos, edge_vbo
# draw faces
glBindBuffer(GL_ARRAY_BUFFER, face_vbos[0])
glVertexPointer( 3, GL_FLOAT, 0, None )
glEnableClientState( GL_VERTEX_ARRAY )
glBindBuffer(GL_ARRAY_BUFFER, face_vbos[1])
glColorPointer( 3, GL_FLOAT, 0, None )
glEnableClientState( GL_COLOR_ARRAY )
glBindBuffer(GL_ARRAY_BUFFER, 0)
glDrawArrays(GL_QUADS, 0, 6*4)
glDisableClientState( GL_VERTEX_ARRAY )
glDisableClientState( GL_COLOR_ARRAY )
#draw edges
glBindBuffer(GL_ARRAY_BUFFER, edge_vbo)
glVertexPointer( 3, GL_FLOAT, 0, None )
glEnableClientState( GL_VERTEX_ARRAY )
glBindBuffer(GL_ARRAY_BUFFER, 0)
glColor3f( 1, 1, 0 )
glDrawElements(GL_LINES, 2*12, GL_UNSIGNED_INT, numpy.array( edges, dtype=numpy.uint32 ))
glDisableClientState( GL_VERTEX_ARRAY )
这可以通过使用 Vertex Array Objects 进一步改进和一个 Index buffer对于边缘:
def main():
global face_vao, edge_vao
# [...]
# define the vertex buffers vor the faces
attribute_array = []
for face in range(len(surfaces)):
for vertex in surfaces[face ]:
attribute_array.append( vertices[vertex] )
attribute_array.append( colors[face] )
face_vbos = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, face_vbos)
glBufferData( GL_ARRAY_BUFFER, numpy.array( attribute_array, dtype=numpy.float32 ), GL_STATIC_DRAW )
glBindBuffer(GL_ARRAY_BUFFER, 0)
# define the vertex array object for the faces
face_vao = glGenVertexArrays( 1 )
glBindVertexArray( face_vao )
glBindBuffer(GL_ARRAY_BUFFER, face_vbos)
glVertexPointer( 3, GL_FLOAT, 6*4, None )
glEnableClientState( GL_VERTEX_ARRAY )
glColorPointer( 3, GL_FLOAT, 6*4, ctypes.cast(3*4, ctypes.c_void_p) )
glEnableClientState( GL_COLOR_ARRAY )
glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindVertexArray( 0 )
# define the vertex buffer for the edges
edge_vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, edge_vbo)
glBufferData( GL_ARRAY_BUFFER, numpy.array( vertices, dtype=numpy.float32 ), GL_STATIC_DRAW )
glBindBuffer(GL_ARRAY_BUFFER, 0)
# define the vertex array object for the edges
edge_vao = glGenVertexArrays( 1 )
glBindVertexArray( edge_vao )
glBindBuffer(GL_ARRAY_BUFFER, edge_vbo)
glVertexPointer( 3, GL_FLOAT, 0, None )
glEnableClientState( GL_VERTEX_ARRAY )
glBindBuffer(GL_ARRAY_BUFFER, 0)
edge_ibo = glGenBuffers(1)
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, edge_ibo )
glBufferData( GL_ELEMENT_ARRAY_BUFFER, numpy.array( edges, dtype=numpy.uint32 ), GL_STATIC_DRAW )
glBindVertexArray( 0 )
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 )
while True:
# [...]
def Cube(veritces):
global face_vao, edge_vao
# draw faces
glBindVertexArray( face_vao )
glDrawArrays( GL_QUADS, 0, 6*4 )
glBindVertexArray( 0 )
#draw edges
glColor3f( 1, 1, 0 )
glBindVertexArray( edge_vao )
glDrawElements( GL_LINES, 2*12, GL_UNSIGNED_INT, None )
glBindVertexArray( 0 )
您可以通过 Face Culling 获得进一步的性能改进并启用 Depth Test .深度测试应该 less 或 eauel,这样边缘就不会被面覆盖:
# enable depth test (less or equal)
glEnable( GL_DEPTH_TEST )
glDepthFunc( GL_LEQUAL )
# enable back face culling (front faces are drawn clockwise)
glEnable( GL_CULL_FACE )
glCullFace( GL_BACK )
glFrontFace( GL_CW )
请注意,在 OpenGL 中以“现代”方式绘制几何图形的最后一步是使用 Shader program和将 glEnableClientState
分别替换为 glEnableVertexAttribArray
和 glVertexPointer
glColorPointer
为 glVertexAttribPointer
(当然是通过使用适当的参数)。
另见 PyGame and OpenGL 4 .
关于python - Pygame OpenGL 3D 立方体滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54114325/
我有 3 个具有相同结构但数据不同的数据库,因为它们来自不同的客户端。 现在,我有一个现有的 SSAS 项目。其数据源 View 、多维数据集和维度只能使用或访问一个数据库。 我想要的是能够使用具有相
所以我的目标是将这些立方体放在一个网格上,并让它们排成一行,可以拖动和捕捉。我根据 this example 使立方体运行良好,但我没有完全理解某些机制,因此遇到了一些问题。 立方体开始时比旋转后大。
我正在解决一个问题,我需要使用 C# 处理内存中的多维数据。我的要求类似于 OLAP 多维数据集,但没有那么复杂。例如,我不需要计算或聚合或类似的东西。我基本上想使用多维键来引用数据。例如: var
我在 Cubical agda 工作,并试图为以后的证明建立一些通用的实用程序。其中之一是,对于任何类型 A,它与 Σ A (\_ -> Top) 类型“相同”,其中 Top是具有一个元素的类型。问题
我有这个在 WPF Viewport3D 中绘制立方体的代码:
以下代码是我目前写的使用三个js尝试移动或翻译 使用 WASD 键上下左右旋转立方体对象,并使用空格键重置到原始位置(屏幕中间)。我对三个 js 很陌生,我不知道如何让运动发挥作用。任何帮助将不胜感激
我想通过使用 opengl 来绘制体素,但它似乎不受支持。我制作了一个立方体绘制函数,它有 24 个顶点(每个面 4 个顶点),但是当你绘制 2500 个立方体时它会降低帧速率。我希望有更好的方法。理
我正在努力为盒子基元创建线框。尝试了颜色、不透明度和透明属性,但似乎都不起作用。这是代码 - 需要渲染这样的东西 - 最佳答案 您需要查看THREE.Material docs对于这个,有一点需要注
我有一个 opengl 立方体,我想对所有 6 个面进行纹理处理。 我需要多个纹理吗? 这是当前立方体的屏幕截图: 基本上我不知道如何将纹理包裹在整个立方体周围...... 这是我的 cube.h 头
我正在为《我的世界》编写一个模组,并遇到了一个令人困惑的数学问题。我想找到中心 block 周围所有 block 的 ID。为此,我想循环遍历 3x3 的方 block 并返回哪些是我想要的方 blo
这是我的问题:我一直在尝试让这个 CSS 立方体打开滚动。我做到了!这是它的工作原理:https://codepen.io/vaninoo/pen/BmyYQd 我很高兴。但是和 friend 测试过
我正在尝试创建 3 个具有相同视角的 3d 立方体,这些立方体将在悬停时旋转 90 度。它几乎适用于 chrome,但如果你仔细观察左侧立方体的底部边框,你会看到 1 条蓝色像素线。当您将鼠标悬停在右
我正在编写一个立方体,但无法使其正确旋转,有人可以帮我吗?我已经尝试了一切。我的代码链接如下: Codepen Link @keyframes spin { from { transform: r
我想创建一个 CSS 立方体,它有 4 个面(正面、背面、顶部、底部),并且它仅在 X 轴上不断向上(或向下)旋转。但出于某种原因,我无法对齐所有 4 个边,所以它看起来像一个立方体。这是我的标记:
啊哈!看来我的问题是我给 gluPerspective 的 zNear 值必须大于 0,而且我必须启用深度缓冲区才能使其工作。我更新了下面的代码以使其正常工作。 我尝试过很多次这样做,并且一直认为我以
我正在为学校开发一个使用 HTML5 和 CSS3 的元素。该元素的目标是教幼儿如何计算简单的方程式。学习这一点的第一步是教他们识别不同形状的数字。 第一个练习是:显示一个随机数并让 child 选择
我用 html 和 css 制作了一个旋转立方体。 当我按向右和向左箭头键时,立方体会按预期围绕其中心旋转。但是,当我按下向上和向下箭头键时,它会在更大的空间内旋转。 在这种情况下,我也希望它围绕其中
我正在努力让立方体在 opengl 中呈现。当我将已经计算出的 MVP 传递给顶点着色器时,它工作正常,但是当我传递模型、 View 和投影然后在顶点着色器中进行计算时,它不显示立方体。当我将顶点着色
大家好,我是 opengl 世界的新手,所以这就像一个星期我试图了解 opengl 是如何工作的。所以我放下我的代码使用不同的例子,我编译它没有问题。但是立方体没有出现,我不知道为什么。谁能向我解释我
我有一个应该绘制立方体的类。 它在主体中很好地绘制了我的立方体,但我使用默认的 x、y、z 值创建它,以便默认情况下将其置于屏幕中央。在通过调用 build 绘制立方体后,我想平移和缩放立方体,但我显
我是一名优秀的程序员,十分优秀!