好的,所以我仍然在努力让这个工作.我的代码的重要部分是:
def __init__(self, vertices, normals, triangles):
self.bufferVertices = glGenBuffersARB(1)
glBindBufferARB(GL_ARRAY_BUFFER_ARB, self.bufferVertices)
glBufferDataARB(GL_ARRAY_BUFFER_ARB, ADT.arrayByteCount(vertices), ADT.voidDataPointer(vertices), GL_STATIC_DRAW_ARB)
self.vertices = vertices
self.bufferNormals = glGenBuffersARB(1)
glBindBufferARB(GL_ARRAY_BUFFER_ARB, self.bufferNormals)
glBufferDataARB(GL_ARRAY_BUFFER_ARB, ADT.arrayByteCount(normals), ADT.voidDataPointer(normals), GL_STATIC_DRAW_ARB)
self.normals = normals
self.bufferTriangles = glGenBuffersARB(1)
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, self.bufferTriangles)
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ADT.arrayByteCount(triangles), ADT.voidDataPointer(triangles), GL_STATIC_DRAW_ARB)
self.triangles = triangles
glDisableClientState(GL_VERTEX_ARRAY) **(Not sure if any of the following influence in any way)**
glDisableClientState(GL_NORMAL_ARRAY)
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0)
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0)
Run Code Online (Sandbox Code Playgroud)
从目前为止我读到的关于VBO的内容来看,我认为这里没有任何错误.所以现在我有我的顶点,法线(尚未使用)和三角形索引缓冲区.现在进行实际抽奖:
def draw(self, type):
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0)
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0)
**Again above line not sure if they have any use.**
glEnableClientState(GL_VERTEX_ARRAY) …Run Code Online (Sandbox Code Playgroud) 我试图在python中编写一个模块,它将绘制一个numpy数组数据(rgb)到屏幕.目前我正在使用这样的三维颜色数组:
numpy.ones((10,10,3),dtype=np.float32,order='F') # (for 10x10 pure white tiles)
Run Code Online (Sandbox Code Playgroud)
将它绑定到缓冲区并使用a glVertexAttribArray将数据广播到一个tile数组(点精灵)(在这种情况下是一个10x10数组),这适用于静态图像.
但我希望能够更改数组中的数据并让缓冲区反映此更改,而无需从头开始重建.
目前我已经构建了缓冲区:
glBufferData(GL_ARRAY_BUFFER, buffer_data.nbytes, buffer_data, GL_DYNAMIC_DRAW)
Run Code Online (Sandbox Code Playgroud)
其中buffer_data是numpy数组.我可以传递什么(如果有的话)(某些指针可能会进入内存?)
我试图在交错数组中批量处理一堆顶点和纹理坐标,然后再发送给pyOpengl的glInterleavedArrays/glDrawArrays.唯一的问题是我无法找到足够快的方法将数据附加到numpy数组中.
有一个更好的方法吗?我原本以为预先分配数组然后用数据填充它会更快,但生成一个python列表并将其转换为numpy数组是"更快".虽然4096个四边形的15毫秒似乎很慢.
我已经包含了一些示例代码及其时间.
#!/usr/bin/python
import timeit
import numpy
import ctypes
import random
USE_RANDOM=True
USE_STATIC_BUFFER=True
STATIC_BUFFER = numpy.empty(4096*20, dtype=numpy.float32)
def render(i):
# pretend these are different each time
if USE_RANDOM:
tex_left, tex_right, tex_top, tex_bottom = random.random(), random.random(), random.random(), random.random()
left, right, top, bottom = random.random(), random.random(), random.random(), random.random()
else:
tex_left, tex_right, tex_top, tex_bottom = 0.0, 1.0, 1.0, 0.0
left, right, top, bottom = -1.0, 1.0, 1.0, -1.0
ibuffer = (
tex_left, tex_bottom, left, bottom, 0.0, # Lower left corner
tex_right, tex_bottom, …Run Code Online (Sandbox Code Playgroud) 我在Python中寻找一个简单的现代OpenGL(3.2+)示例.
我尝试使用GLUT和freeGLUT,但我无法在OS X(Mavericks)上获得3.2上下文.(这似乎是GLUT/freeGLUT的已知问题).
GLFW似乎是GLUT的现代轻量级替代品,但它似乎没有官方的Python绑定,我找不到一个简单的例子,它使用OpenGL的3.2核心配置文件功能与GLFW和Python.
(我在解决这个问题时遇到了困难,因此它可能对其他人有用,我在下面按照SO指南回答.)
我需要渲染一些场景.我设法使用pyopengl和pygame在python中完成它.问题是它会在短时间内创建一个窗口.
我想渲染相同的图像并保存它,而不创建一个可见的窗口(或者可能根本没有创建一个窗口,甚至没有pygame).
import cv2
import numpy as np
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
def main():
DISPLAY_WIDTH = 900
DISPLAY_HEIGHT = 900
pygame.init()
pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT), DOUBLEBUF | OPENGL)
gluPerspective(90, (DISPLAY_WIDTH / DISPLAY_HEIGHT), 0.01, 12)
glEnable(GL_TEXTURE_2D)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glRotatef(-90, 1, 0, 0) # Straight rotation
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glRotatef(285, 0, 0, 1) # Rotate yaw
glTranslatef(-5, -3, -2) # Move to position
# Draw rectangle
glBegin(GL_QUADS)
glColor3f(1, 0, 0)
glVertex3f(2, 2, 0)
glVertex3f(2, …Run Code Online (Sandbox Code Playgroud) 上下文:我的Python代码将2D顶点数组传递给OpenGL.
我测试了两种方法,一种是ctypes,另一种是结构,后者是两倍以上.
from random import random
points = [(random(), random()) for _ in xrange(1000)]
from ctypes import c_float
def array_ctypes(points):
n = len(points)
return n, (c_float*(2*n))(*[u for point in points for u in point])
from struct import pack
def array_struct(points):
n = len(points)
return n, pack("f"*2*n, *[u for point in points for u in point])
Run Code Online (Sandbox Code Playgroud)
还有其他选择吗?有关如何加速此类代码的任何提示(是的,这是我的代码的一个瓶颈)?
我不是在写游戏,而是使用Pygame进行科学渲染.我希望控件能够像第一人称射击游戏一样工作,这样用户就可以使用熟悉的控件进行导航.
我试图编写代码,使其具有与"天际"或"半条命"中的"外观"功能相同的属性,但鼠标不会移动光标 - 它可以让您以无限的圆圈环顾四周.单击应该没有效果.
控件的第一次尝试:
(游戏循环中的代码)
delta_y, delta_x = pygame.mouse.get_rel()
rotation_direction.x = float(delta_x)
rotation_direction.y = float(delta_y)
Run Code Online (Sandbox Code Playgroud)
(不要问我为什么,但是y和x需要像这样反转以获得预期的外观方向;必须与相机变换实现有关,这不是我的.)
然而,这导致光标位于窗口顶部,当光标到达屏幕边缘时,窗口停止旋转; 即代码报告屏幕上的实际位置.
我尝试在每个游戏循环中"重置"鼠标位置(顺便提一下,隐藏鼠标):
pygame.mouse.set_pos([150, 150])
pygame.mouse.set_visible(False)
Run Code Online (Sandbox Code Playgroud)
但是这会在下一个循环中产生对称的"向后移动"三角形,这意味着你无法在任何地方'看'.
总结一下,我想:
使用Pygame或其他Python黑客攻击的最佳方法是什么?
我正在尝试使用Python和PyGame开始使用OpenGL.
我将使用PyGame而不是GLUT来完成所有初始化,窗口打开,输入处理等.
但是,我的着色器无法编译,除非我准确指定OpenGL和配置文件的版本.
他们使用书中的GLUT初始化进行编译:
glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(400, 400)
# this is what I need
glutInitContextVersion(3, 3)
glutInitContextProfile(GLUT_CORE_PROFILE)
glutCreateWindow("main")
Run Code Online (Sandbox Code Playgroud)
但是,像这样简单的PyGame初始化:
pygame.init()
display = (400, 400)
pygame.display.set_mode(display, pygame.DOUBLEBUF|pygame.OPENGL)
Run Code Online (Sandbox Code Playgroud)
它没有指定确切的OpenGL版本3.3和CORE_PROFILE,同样的程序在尝试编译着色器时会失败:
RuntimeError:('着色器编译失败(0):0:2(10):错误:不支持GLSL 3.30.支持的版本是:1.10,1.20,1.30,1.00 ES和3.00 ES \n',['\n #version 330 core \n vec4位置的布局(位置= 0); \n void main()\n {\n \n <br_Position = position; \n} \n'],GL_VERTEX_SHADER)
我的问题是:如何使用PyGame进行初始化?
我看到了同样的问题,但对我没有用。
pip install PyOpenGL.3.1.1-cp34-cp34m-win_amd64.whl
Run Code Online (Sandbox Code Playgroud)
我对Numpy也有同样的问题
pip install numpy-1.11.1+mkl-cp34-cp34m-win_amd64.whl
Run Code Online (Sandbox Code Playgroud)
然后我得到:
numpy-1.11.1 + mkl-cp34-cp34m-win_amd64.whl在此平台上不受支持。在C://Users/myUsername/pip/pip.log中存储故障调试日志
我正在使用64位和Python 3.4.0
怎么了?
我有一个 2d pygame 水模拟东西,我是按照教程制作的。我还找到了这个问题的答案来解决教程中的问题:Pygame water Physics not working as expected
从那以后,我一直在尝试将此程序转换为使用 pyopengl 来呈现内容。但是,我一直在努力: A:绘制水多边形 B:使用平铺纹理对水多边形进行纹理化
这是我(相当糟糕)将此代码转换为 pyopengl 的尝试。
import pygame, random
import math as m
from pygame import *
from OpenGL import *
from OpenGL.GLU import *
from OpenGL.GL import *
pygame.init()
WINDOW_SIZE = (854, 480)
screen = pygame.display.set_mode(WINDOW_SIZE,0,32,DOUBLEBUF|OPENGL) # initiate the window
clock = pygame.time.Clock()
def draw_polygon(polygon_points):
glBegin(GL_POLYGON);
for i in polygon_points:
glVertex3fv(i)
#glEnd()
class surface_water_particle():
def __init__(self, x,y):
self.x_pos = x
self.y_pos = y
self.target_y = y …Run Code Online (Sandbox Code Playgroud)