- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 pyopengl 创建一个简单的场景,但我不断收到运行时错误。我正在使用 glfw 来显示 opengl 场景。
我使用 python 是因为我想在其他一些 python 项目中包含 opengl。
我使用的是 macOS Mojave (10.14)、Python 3.7
错误:
Traceback (most recent call last):
File "main.py", line 109, in <module>
main()
File "main.py", line 64, in main
shader = OpenGL.GL.shaders.compileProgram(s.vertex(), s.fragment())
File "slib/python3.7/site-packages/OpenGL/GL/shaders.py", line 196, in
compileProgram
program.check_validate()
File "lib/python3.7/site-packages/OpenGL/GL/shaders.py", line 108, in
check_validate
glGetProgramInfoLog( self ),
RuntimeError: Validation failure (0): b'Validation Failed: No vertex
array object bound.\n'
代码:
import glfw
from OpenGL.GL import *
import OpenGL.GL.shaders
import numpy as np
def trikotnik():
trikotnik = np.array([
-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.0, 0.5, 0.0
], dtype=np.float32)
return trikotnik
class Shaders:
def __init__(self):
pass
def vertex(self):
v = """
#version 330
in vec4 position;
void main() {
gl_Position = position;
}
"""
return OpenGL.GL.shaders.compileShader(v, GL_VERTEX_SHADER)
def fragment(self):
f = """
#version 330
out vec4 fragColor;
void main() {
fragColor = vec4( 0, 1, 0, 1 );
}
"""
return OpenGL.GL.shaders.compileShader(f, GL_FRAGMENT_SHADER)
def main():
# Initialize the library
if not glfw.init():
return
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 2)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
# Create a windowed mode window and its OpenGL context
window = glfw.create_window(640, 480, "Hello World", None, None)
if not window:
glfw.terminate()
return
# Make the window's context current
glfw.make_context_current(window)
# Creating shaders
s = Shaders()
shader = OpenGL.GL.shaders.compileProgram(s.vertex(), s.fragment())
# Creating vertex buffer object on gpu
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
# Load data on array buffer...32 = size in bytes 9x4
# GL_STATIC_DRAW: The vertex data will be uploaded once and drawn many times.
glBufferData(GL_ARRAY_BUFFER, 32, trikotnik(), GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
position = glGetAttribLocation(shader, "position")
glVertexAttribPointer(
position, # attribute 0. No particular reason for 0, but must match the layout in the shader.
3, # size
GL_FLOAT, # type
GL_FALSE, # normalized
0, # stride
None # array buffer offset
)
glEnableVertexAttribArray(position)
glUseProgram(shader)
glClearColor(0, 107, 179, 1.0)
# Loop until the user closes the window
while not glfw.window_should_close(window):
# Render here, e.g. using pyOpenGL
# Clear color buffer
glClear(GL_COLOR_BUFFER_BIT)
# Draw
glDrawArrays(GL_TRIANGLES, 0, 3)
# Swap front and back buffers
glfw.swap_buffers(window)
# Poll for and process events
glfw.poll_events()
glfw.terminate()
if __name__ == "__main__":
main()
我在源代码中找到了这个:
validate (keyword only) -- if False, suppress automatic validation against current GL state. In advanced usage the validation can produce spurious errors. Note: this function is not really intended for advanced usage, if you're finding yourself specifying this flag you likely should be using your own shader management code.
我尝试将其设置为 False,但仍然收到错误。
最佳答案
要么使用 Vertex Array Object或者切换到兼容性配置文件上下文 glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_COMPAT_PROFILE)
。在核心配置文件中,您必须有一个命名的 VAO,它不是可选的。
在指定顶点数组数据数组之前创建并绑定(bind) VAO:
VAO = glGenVertexArrays(1)
glBindVertexArray(VAO)
glVertexAttribPointer(
position, # attribute 0. No particular reason for 0, but must match the layout in the shader.
3, # size
GL_FLOAT, # type
GL_FALSE, # normalized
0, # stride
None # array buffer offset
)
glEnableVertexAttribArray(position)
关于python - PyOpenGl 验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53279015/
我有一个在 blender 中创建的 3-D 对象。我想从 blender 中导出它并能够将它导入到 OpenGL 中。是否有为此过程推荐的方法或特定的文件扩展名? 最佳答案 来自 Here: 使用
我正在尝试使用 pillow 加载图像在 OpenGL 中绘制二维图像,但是当我在 OpenGL 中渲染它时,图像是倾斜的。 这是原始图像: 加载.png 这是代码: # -*- coding: ut
我正在尝试使用 pyopengl 创建一个简单的场景,但我不断收到运行时错误。我正在使用 glfw 来显示 opengl 场景。 我使用 python 是因为我想在其他一些 python 项目中包含
我遇到一个奇怪的问题,我试图使用两个不同的 VBO 并使用 glDrawArrays()。然而,尽管 VBO 数据不同,但 glDrawArrays() 绘制的图形完全相同 import ctypes
我从特定的角度(使用 View 和投影矩阵)绘制了一个特定的场景。我使用了三角形的 VBO 等等。我可以使用以下方法获取图像的 RGB: data = glReadPixels(0, 0, width
所以我只是测试一下我的引擎中的工作原理,并尝试简单地使用以下命令来旋转对象。 rot_y = Matrix44.from_y_rotation(glfw.get_time() * 0.5) 但是...
我有一个需要快速渲染的场景。所以我成功地将三角形的顶点生成为 numpy,上传到 GPU 并以非常快的速度渲染它。到目前为止一切顺利。 当场景中的某些元素需要从场景中消失时,问题就开始了。我所做的就是
所以我有一个问题,我已经追了几天了,但找不到任何帮助。我刚刚开始尝试学习 pyOpenGL 并很快遇到了这个问题。我希望我已经包含了足够的信息,感谢您的帮助。 问题: 当我绘制一个立方体时,后面的一些
我正在进行 PyQt5 PyOpenGL 项目。我正在尝试用一堆彩色实心立方体绘制一个白色线框立方体。线框立方体是从元组点列表以及对这些点的元组引用列表中绘制的。实心立方体是从对点的元组引用列表中绘制
我尝试了一切,但仍然没有得到我的错误。我正在尝试在我的球体对象上放置纹理。 """ Minimal texture on sphere demo This is demo for showing ho
我对 PyOpenGL 比较陌生,并开始学习转换以及模型、 View 和投影矩阵。一切都很好,直到我编译并运行我的代码,发现我的对象突然消失了,说实话,我不知道 OpenGL 是否正在裁剪我的对象,或
经过一些建议,我决定用困难的方式学习一点 OpenGL。我尝试将一个 (float - 名为 myAttrib) 变量传递给顶点着色器,它似乎有效(第 33 行),但显然不起作用't。稍后我想将代码分
好吧,感谢你们,我对OpenGL在(几乎)现代工作中的了解有了更深入的了解。但有件事我想不通。 即使我错过了正确设置的片段着色器如何对我的纹理进行采样?我认为采样器会以某种方式自动获取纹理。但如何呢?
我的问题是我的程序显示一个具有 120-fps 限制的简单立方体只能以 25 fps 运行。我发现所有性能损失都在 OpenGL 部分,但我无法弄清楚具体位置。 我的问题是: OpenGL 在 Pyt
这个问题在这里已经有了答案: What is the proper way to modify OpenGL vertex buffer? (3 个答案) 关闭 2 年前。 我想在 PyOpenGL
所以我一直在绞尽脑汁地不停地试错。我觉得我总是带着关于同一件事的问题回来,这令人沮丧。 我正在按照教程查看 C++ 代码,并跟随 Python 尝试复制结果。我注意到通过反复试验存在细微差别,并且我在
出现包含GLUT图形的窗口后,我想在终端输入: user@computer: python woop.py # Now displaying a beautiful landscape (cmd):
我刚刚开始学习 pyOpenGL 并遇到了一个问题,我的第一个项目非常简单:我试图打开一个窗口并使用着色器绘制一个简单的三角形。我使用 glfw 创建窗口,所有内容都正确编译,但未绘制三角形。 我的主
我使用了Python Imaging Library来加载.ttf字体。这是代码: self.rect = Image.new("RGBA", (600,100), (255,255,255)) se
我正在尝试用 Pygame 创建一个 2D python 游戏。 但我意识到我可以使用 PyOpengl,所以我尝试学习如何使用它。我可以在我的屏幕上用顶点创建一个三角形 strip 。但是,我想使用
我是一名优秀的程序员,十分优秀!