opengl 如何构建一个自编译文件,将立方体顶点的像素着色为黄色?

gev0vcfq  于 2023-11-18  发布在  其他
关注(0)|答案(1)|浏览(124)

如何构建一个自编译文件,将立方体顶点的像素着色为黄色?
你能帮助我解决这个Python编译错误并提出有效的代码示例吗?

import pygame
import OpenGL
import OpenGL.GL as g1
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GL.shaders import compileProgram, compileShader
from OpenGL.GL import shaders

if bool(glCreateShader):
    shader = glCreateShader(GL_VERTEX_SHADER)
    # Continue with shader operations
else:
    print("glCreateShader is not available on this system.")

vertex_shader = """
#version 330 core
layout(location = 0) in vec3 in_position;
void main()
{
    gl_Position = vec4(in_position, 1.0);
}
"""

fragment_shader = """
#version 330 core
out vec4 frag_color;
void main()
{
    frag_color = vec4(1.0, 0.0, 0.0, 1.0);  // Red color
}
"""

vertices= (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1)
    )

edges = (
    (0,1),
    (0,3),
    (0,4),
    (2,1),
    (2,3),
    (2,7),
    (6,3),
    (6,4),
    (6,7),
    (5,1),
    (5,4),
    (5,7)
    )

def Cube():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex])
    glEnd()

shader = shaders.compileShader(vertex_shader  , g1.GL_VERTEX_SHADER)

# Define vertex and fragment shaders here
vertex_shader_source = """
#version 330 core
layout(location = 0) in vec2 in_position;
void main() {
    gl_Position = vec4(in_position, 0.0, 1.0);
}
"""

fragment_shader_source = """
#version 330 core
uniform vec3 pixelColor;
out vec4 fragColor;
void main() {
    fragColor = vec4(pixelColor, 1.0);
}
"""

if __name__ == "__main__":
    vertex_shader = compileShader(vertex_shader_source, GL_VERTEX_SHADER)
    fragment_shader = compileShader(fragment_shader_source, GL_FRAGMENT_SHADER)
    shader_program = compileProgram(vertex_shader, fragment_shader)
    glUniform3f(glGetUniformLocation(shader_program, "pixelColor"), 1.0, 0.0, 0.0)
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    
    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

    glTranslatef(0.0,0.0, -5)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        Cube()
        pygame.display.flip()
        pygame.time.wait(10)

字符串
尝试调用未定义的备用函数(glossary Shader,glossary ShaderObjectARB),请在调用之前检查bool(glossary Shader)

yeotifhr

yeotifhr1#

在创建OpenGL Context之前,您无法调用OpenGL函数。当使用pygame.display.set_mode创建OpenGL窗口并设置pyame.OPENGL标志时,Pygame会创建OpenGL上下文。因此,在调用pygame.display.set_mode之前调用OpenGL API函数将导致错误。
如果你想使用传统的OpenGL,你所要做的就是用glColor设置颜色。例如:

glColor4f(1, 1, 0, 1)
Cube()

字符串
基于您的代码的完整示例:

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

vertices= (
    (1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, -1),
    (1, -1, 1), (1, 1, 1), (-1, -1, 1), (-1, 1, 1))
edges = ((0,1), (0,3), (0,4), (2,1), (2,3), (2,7), (6,3), (6,4), (6,7), (5,1), (5,4), (5,7))

def Cube():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex])
    glEnd()

if __name__ == "__main__":
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    
    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
    glTranslatef(0.0,0.0, -5)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        glColor4f(1, 1, 0, 1)
        Cube()
        pygame.display.flip()
        pygame.time.wait(10)

相关问题