我是Python和PIL的新手.我试图关注如何通过PIL将图像加载到Python,然后使用openGL绘制其像素的代码示例.以下是一些代码行:
from Image import *
im = open("gloves200.bmp")
pBits = im.convert('RGBA').tostring()
Run Code Online (Sandbox Code Playgroud)
.....
glDrawPixels(200, 200, GL_RGBA, GL_UNSIGNED_BYTE, pBits)
Run Code Online (Sandbox Code Playgroud)
这将在画布上绘制一个200 x 200的像素块.但是,它不是预期的图像 - 看起来它是从随机存储器中绘制像素.随机记忆假设得到以下事实的支持:即使我试图绘制完全不同的图像,我也会得到相同的模式.有人可以帮助我吗?我在Windows XP上使用Python 2.7和2.7版本的pyopenGL和PIL.

到目前为止,我从来没有关心过这个问题,但现在我需要使用一些需要由PyOpenGL缓冲的大量顶点,而且似乎python迭代是瓶颈.情况就是这样.我有一个3D点阵列vertices,在每一步我必须为每个顶点计算一个4D颜色数组.到目前为止我的方法是:
upper_border = len(self.vertices) / 3
#Only generate at first step, otherwise use old one and replace values
if self.color_array is None:
self.color_array = numpy.empty(4 * upper_border)
for i in range(upper_border):
#Obtain a color between a start->end color
diff_activity = (activity[i] - self.min) / abs_diff
clr_idx = i * 4
self.color_array[clr_idx] = start_colors[0] + diff_activity * end_colors[0]
self.color_array[clr_idx + 1] = start_colors[1] + diff_activity * end_colors[1]
self.color_array[clr_idx + 2] = start_colors[2] + diff_activity * end_colors[2]
self.color_array[clr_idx + 3] …Run Code Online (Sandbox Code Playgroud) 我一直在研究有关现代OpenGL编程的出色教程,并且正在慢慢调整使其适用于PyOpenGL和pygame。但是,我最难获得与透视几何配合使用的“简单”示例。代码如下:
import pygame
import numpy as np
from OpenGL.GL import *
##########################################
# Define the simple pass-through shaders
##########################################
import cStringIO
vshade = cStringIO.StringIO("""
#version 110
uniform mat4 p_matrix;
uniform mat4 mv_matrix;
attribute vec4 position;
void main()
{
vec4 eye_position = mv_matrix * position;
gl_Position = p_matrix * eye_position;
}
""")
fshade = cStringIO.StringIO("""
#version 110
void main()
{
gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}
""")
###################################################
# Set up a simple square composed of two triangles
###################################################
verts = …Run Code Online (Sandbox Code Playgroud) 你如何在PyOpenGL python绑定到OpenGL中使用glBufferData()?
当我运行以下代码时
import sys
from OpenGL.GL import *
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtOpenGL import *
class SimpleTestWidget(QGLWidget):
def __init__(self):
QGLWidget.__init__(self)
def initializeGL(self):
self._vertexBuffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self._vertexBuffer)
vertices = [0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, -0.5]
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW) # Error
def paintGL(self):
glViewport(0, 0, self.width(), self.height())
glClearColor(0.0, 1.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT)
glEnableClientState(GL_VERTEX_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER, self._vertexBuffer)
glVertexPointer(2, GL_FLOAT, 0, 0)
glColor3f(1.0, 0.0, 0.0)
glDrawArrays(GL_QUADS, 0, 4)
if __name__ == '__main__':
app = QApplication(sys.argv)
w …Run Code Online (Sandbox Code Playgroud) 我正在按照本教程进行 3d 中的弧球导航:
https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Arcball
我设法执行了所有步骤并且导航有效,但我似乎无法理解教程中的最后一步:
一个额外的技巧是将旋转轴从相机坐标转换为对象坐标。当相机和对象的放置方式不同时,这很有用。例如,如果您在 Y 轴上将对象旋转 90°(“将其头部向右转”),然后用鼠标执行垂直移动,您将在相机 X 轴上旋转,但它应该变成一个对象的 Z 轴旋转(平面桶滚)。通过将轴转换为对象坐标,旋转将尊重用户在相机坐标 (WYSIWYG) 中的工作。为了从相机坐标转换为对象坐标,我们采用 MV 矩阵的逆矩阵(来自 MVP 矩阵三元组)。
问题是,当我在第一步旋转轴变换中转动模型时,它们与我的“相机视图”不对齐。当然,我想让我的旋转轴始终与我的相机视图对齐。
有人可以给我一个建议如何解决它吗?在教程中有一个代码,但没有太多解释它实际在做什么,而且我只会说 Python。
谢谢你,雅各布
我的代码:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import math
import os
import numpy as np
size = 30
speed = 500
amplitude_amplificator = 80
color_table = ((1,0,0),
(0,1,0),
(0,0,1),
(1,1,0),
(1,0,1),
(0,1,1),
(1,0.5,0),
(0.5,1,0),
(0.5,1,0.5),
(0,0.5,0)
)
locations = ((0,-975, 0),
(0, 975, 0),
(-1273,-975, 0),
(-1273, 975, 0), …Run Code Online (Sandbox Code Playgroud) 让我通过提供一些我们将用来玩弄的样板代码来开始这个问题:
mcve_framework.py
import time
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import glm
from glm import unProject
from glm import vec2
from glm import vec3
from glm import vec4
# -------- Camera --------
class BaseCamera():
def __init__(
self,
eye=None, target=None, up=None,
fov=None, near=0.1, far=100000
):
self.eye = eye or glm.vec3(0, 0, 1)
self.target = target or glm.vec3(0, 0, 0)
self.up = up or glm.vec3(0, 1, 0)
self.original_up = glm.vec3(self.up)
self.fov = fov or glm.radians(45)
self.near …Run Code Online (Sandbox Code Playgroud) 我想做的事:
我有一个脚本,用于分解给定范围内的素数:
# Python program to display all the prime numbers within an interval
lower = 900
upper = 1000
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
Run Code Online (Sandbox Code Playgroud)
我想使用 GPU 而不是 CPU 来运行这样的脚本,这样会更快
问题:
我的英特尔 NUC NUC8i7HVK上没有 NVIDIA GPU,而是“独立 GPU”
如果我运行此代码来检查我的 GPU 是什么:
import pyopencl as …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一些代码,将单个图像显示为wxGLCanvas容器内的简单2D纹理矩形.
这是我到目前为止所获得的一个可运行的例子:
import wx
from wxPython.glcanvas import wxGLCanvas
from OpenGL.GLU import *
from OpenGL.GL import *
import numpy as np
from scipy.misc import ascent
def run():
imarray = np.float32(ascent())
imarray = np.repeat(imarray[..., np.newaxis], 3, axis=2)
app = wx.PySimpleApp()
frame = wx.Frame(None, title='Simple 2D texture')
canvas = myGLCanvas(frame, imarray)
frame.Show(True)
app.MainLoop()
class myGLCanvas(wxGLCanvas):
def __init__(self, parent, image):
attribList = [wx.glcanvas.WX_GL_DOUBLEBUFFER]
wxGLCanvas.__init__(self, parent, -1, attribList=attribList)
wx.EVT_PAINT(self, self.OnPaint)
self.image = np.float32(image)
self.InitProjection()
self.InitTexture()
pass
def OnPaint(self, event):
"""Called whenever the window …Run Code Online (Sandbox Code Playgroud) 我一直在尝试在线跟踪教程,我已经跟踪了每一行,出于某种原因我收到以下错误:
Traceback (most recent call last):
File "C:/Users/User/Desktop/OpenGLContextTest.py", line 2, in <module>
from OpenGLContext import testingcontext
File "C:\Python27\lib\site-packages\openglcontext-2.2.0a2-py2.7.egg\OpenGLContext\testingcontext.py", line 10, in <module>
from OpenGLContext import plugins, context, contextdefinition
File "C:\Python27\lib\site-packages\openglcontext-2.2.0a2-py2.7.egg\OpenGLContext\context.py", line 32, in <module>
from OpenGLContext import visitor, texturecache,plugins
File "C:\Python27\lib\site-packages\openglcontext-2.2.0a2-py2.7.egg\OpenGLContext\visitor.py", line 3, in <module>
from OpenGLContext.scenegraph import nodepath
File "C:\Python27\lib\site-packages\openglcontext-2.2.0a2-py2.7.egg\OpenGLContext\scenegraph\nodepath.py", line 3, in <module>
from vrml.vrml97 import nodepath, nodetypes
File "C:\Python27\lib\site-packages\pyvrml97-2.3.0a2-py2.7.egg\vrml\vrml97\nodepath.py", line 4, in <module>
from vrml import nodepath
File "C:\Python27\lib\site-packages\pyvrml97-2.3.0a2-py2.7.egg\vrml\nodepath.py", line 3, in <module>
from vrml …Run Code Online (Sandbox Code Playgroud) 我正在编写一个简单的 PyOpenGL 代码,显示部分为:
glBegin(GL_LINE_LOOP)
glVertex2f(0,0)
glVertex2f(0,1)
glVertex2f(1,1)
glVertex2f(1,0)
glEnd()
Run Code Online (Sandbox Code Playgroud)
我想将此模型导出为 .OBJ 文件。我怎样才能做到这一点?
我搜索了一段时间,但只能获取在 OpenGL 中打开 .OBJ 模型的资源,反之亦然。