在具有OpenGL 3.1(#version140)支持的GPU上运行this sample code时,Pyopengl 3.1.5(Python 3.8和Windows10 Pro-64bit)窗口显示黑屏。
示例代码中缺少一些内容。1.没有为fragcolor指定颜色。2.提供整数值作为GlVetex2f()的输入,而不是浮点数。在纠正上述问题并运行代码后,屏幕仍为黑色。
因此,为了让GPU识别OpenGL版本,预处理器指令#version 140已添加到Glsl脚本中。但是,脚本输出黑屏仍然。如何解决这个问题?
Frag.glsl脚本
#version 140
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()
Python主脚本
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():
frag = open("frag.glsl","r")
frag.readlines()
print(frag.readlines())
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
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
对于Shader程序,您需要Vertex Shader和Fragment Shader。由于要使用Legacy OpenGL和立即模式顶点属性规范,因此必须降级到OpenGL Shading Language 1.30 Specification。
例如,一个着色器程序以绿色绘制Primitives:
顶点着色器
#version 130
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
片段着色器
#version 130
void main()
{
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
着色器必须进行编译和链接。 PyOpenGL为此提供了OpenGL.GL.shaders
。例如:
OpenGL.GL.shaders
from OpenGL.GL.shaders import *
在通过shader_program = compileProgram(
compileShader(verteex_shader_glsl, GL_VERTEX_SHADER),
compileShader(fragment_shader_glsl, GL_FRAGMENT_SHADER),
)
绘制几何图形之前,必须先安装着色器。
请参见示例:
glUseProgram
在glUseProgram
中,缺少Mvp(模型-视图-投影)矩阵的代码。将其添加到ShowScreen()函数下后,视口中将进行绘制调用(在这种情况下为正方形)render。
import OpenGL
from OpenGL.GL import *
from OpenGL.GL.shaders import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import io
print("Imports successful!")
w, h = 500,500
vertex_shader_glsl = """
#version 130
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
"""
fragment_shader_glsl = """
#version 130
void main()
{
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
"""
def poly_shape():
glUseProgram(shader_program)
glBegin(GL_QUADS)
glVertex2f(100.0, 100.0)
glVertex2f(200.0, 100.0)
glVertex2f(200.0, 200.0)
glVertex2f(100.0, 200.0)
glEnd()
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)
poly_shape()
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
shader_program = compileProgram(
compileShader(vertex_shader_glsl, GL_VERTEX_SHADER),
compileShader(fragment_shader_glsl, GL_FRAGMENT_SHADER),
)
glutMainLoop()
sample code