python - Pyopengl 黑屏
问题描述
在支持 OpenGL 3.1 的 GPU 上运行此示例代码时,Pyopengl 3.1.5(Python 3.8 和 Windows10 Pro-64bit)窗口显示Black Screen。
示例代码中缺少一些内容。
1. fragcolor 没有指定颜色。
2. 提供整数值作为 的输入glVetex2f()
,而不是浮点数。
纠正上述问题并运行代码后,屏幕仍然是黑色的。
import OpenGL
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import io
print("Imports successful!")
w, h = 500,500
def poly_shape():
glBegin(GL_QUADS)
glColor4f(1.0,0.0,0.0,1.0)
glVertex2f(100.0, 100.0)
glVertex2f(200.0, 100.0)
glVertex2f(200.0, 200.0)
glVertex2f(100.0, 200.0)
glEnd()
def showScreen():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Remove everything from screen (i.e. displays all white)
glLoadIdentity() # Reset all graphic/shape's position
poly_shape() # Draw function call
glutSwapBuffers()
#------
glutInit()
glutInitDisplayMode(GLUT_RGBA) # Set the display mode to be colored
glutInitWindowSize(500, 500) # Set the w and h of your window
glutInitWindowPosition(0, 0) # Set the position at which this windows should appear
wind = glutCreateWindow("OpenGL Coding Practice") # Set a window title
glutDisplayFunc(showScreen)
glutIdleFunc(showScreen) # Keeps the window open
glutMainLoop() # Keeps the above created window displaying/running in a loop
解决方案
在 示例代码中,缺少 Mvp(模型-视图-投影)矩阵的代码。在 ShowScreen() 函数下添加它时,绘图调用(在本例中为正方形)呈现在视口中。
import OpenGL
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import io
print("Imports successful!")
w, h = 500,500
def draw_shape():
glBegin(GL_QUADS)
glColor4f(1.0,0.0,0.0,1.0)
glVertex2f(100.0, 100.0)
glVertex2f(200.0, 100.0)
glVertex2f(200.0, 200.0)
glVertex2f(100.0, 200.0)
glEnd()
glFlush()
def showScreen():
glViewport(0, 0, 500, 500)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, 500, 500, 0, -1, 1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Remove everything from screen (i.e. displays all white)
glLoadIdentity() # Reset all graphic/shape's position
draw_shape() # Draw function
glutSwapBuffers()
#---Section 3---
glutInit()
glutInitDisplayMode(GLUT_RGBA) # Set the display mode to be colored
glutInitWindowSize(500, 500) # Set the w and h of your window
glutInitWindowPosition(0, 0) # Set the position at which this windows should appear
wind = glutCreateWindow("OpenGL Coding Practice") # Set a window title
glutDisplayFunc(showScreen)
glutIdleFunc(showScreen) # Keeps the window open
glutMainLoop() # Keeps the above created window displaying/running in a loop*
推荐阅读
- python - 分组方式/枢轴
- java - 如何使用 Firebase ML Kit 创建用于条码扫描的模块化类
- javascript - 根据选择列表过滤数组并删除不起作用的字段
- python-3.x - 如何将 DataFrame 中的值存储在新变量中?
- bitbucket - 如何在 BitBucket 中强制执行(拉取请求)合并提交消息的格式
- javascript - 如何在一个 GQL 突变中建立多个连接?
- sql - 在列中查找选择序列
- javascript - 有没有办法以编程方式从 Google Drive 公共文件夹中读取图像的 URL?
- postgresql - 为什么我的脚本中的 psql 命令突然被 jenkins / hudson 杀死?
- swift - Swift & Firebase 无法分配“AuthDataResult”类型的值?键入“用户?