Opengl黑屏

Pyopengl Blackscreen

虽然 运行 this sample code 在支持 OpenGL 3.1 的 GPU 上,Pyopengl 3.1.5(Python 3.8 和 Windows10 Pro-64 位)window 显示 黑屏

示例代码中缺少一些内容。
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

sample code 中,缺少 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*