(pyopengl)正常不起作用......帮助我

时间:2018-03-17 17:16:07

标签: python opengl fragment-shader vbo pyopengl

我正在使用Python构建一个多维数据集。

我将使用vbo和着色器。

但是,普通不起作用。

问题出在哪里?我该如何编码?

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

pygame.init ()
screen = pygame.display.set_mode (( 800, 800), pygame.OPENGL|pygame.DOUBLEBUF, 24)
glViewport (0, 0, 800, 800)
glClearColor (0.0, 0.5, 0.5, 1.0)

##### shader

def createAndCompileShader(type,source):
    shader=glCreateShader(type)
    glShaderSource(shader,[source])
    glCompileShader(shader)

    return shader

vertex_shader=createAndCompileShader(GL_VERTEX_SHADER,"""

out vec4 result;
vec3 light;
vec4 color;

void main(void)
{
  light = vec3(1.5,1.5,1.5);
  color = vec4(0.5,0.5,0.5,0.1);

  vec4 V = gl_ModelViewMatrix * gl_Vertex;
  vec3 N = gl_NormalMatrix * gl_Normal ;

  //vec3 V = vec3(gl_ModelViewMatrix * gl_Vertex );
  //vec3 N = vec3(gl_ModelViewMatrix * vec4(gl_Normal,0.0));

  vec3 L = normalize( light - V.xyz );
  float distance = length( light - V.xyz);

  //vec3 L = normalize( light - V);
  //float distance = length( light - V);

  float diffuse = max(dot(N, L),0.1);

  diffuse = diffuse * (1.0 /(1.0+(0.5*distance*distance)));

  result = color * (0.6+diffuse);

  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

}
""")

fragment_shader=createAndCompileShader(GL_FRAGMENT_SHADER,"""

in vec4 result;

void main(void)
{
  gl_FragColor = result;

}
""")


program=glCreateProgram()
glAttachShader(program,vertex_shader)
glAttachShader(program,fragment_shader)
glLinkProgram(program)
glUseProgram(program)
#####
'''
vertices = [
        0.0,0.0,0.0,0.1,0.0,0.0,0.0,0.0,0.1,
        0.1,0.0,0.0,0.1,0.0,0.1,0.0,0.0,0.1,
        0.1,0.0,0.0,0.1,0.1,0.0,0.1,0.1,0.1,
        0.1,0.1,0.1,0.1,0.0,0.1,0.1,0.0,0.0,
        0.1,0.1,0.0,0.0,0.1,0.0,0.0,0.1,0.1,
        0.0,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.0,
        0.0,0.1,0.1,0.0,0.0,0.1,0.0,0.0,0.0,
        0.0,0.0,0.0,0.0,0.1,0.0,0.0,0.1,0.1,
        0.0,0.0,0.1,0.1,0.0,0.1,0.1,0.1,0.1,
        0.1,0.1,0.1,0.0,0.1,0.1,0.0,0.0,0.1,
        0.0,0.0,0.0,0.1,0.0,0.0,0.1,0.1,0.0,
        0.0,0.0,0.0,0.0,0.1,0.0,0.1,0.1,0.0
]

normals = [
    0.0,-1.0,0.0,
    0.0,-1.0,0.0,
    1.0,0.0,0.0,
    1.0,0.0,0.0,
    0.0,1.0,0.0,
    0.0,1.0,0.0,
    -1.0,0.0,0.0,
    -1.0,0.0,0.0,
    0.0,0.0,1.0,
    0.0,0.0,1.0,
    0.0,0.0,-1.0,
    0.0,0.0,-1.0
]
'''

vertices = [
    -1.0,-1.0,-1.0, -1.0,-1.0, 1.0, -1.0, 1.0, 1.0,  # Left Side
    -1.0,-1.0,-1.0, -1.0, 1.0, 1.0, -1.0, 1.0,-1.0,  # Left Side
     1.0, 1.0,-1.0, -1.0,-1.0,-1.0, -1.0, 1.0,-1.0,  # Back Side
     1.0,-1.0, 1.0, -1.0,-1.0,-1.0,  1.0,-1.0,-1.0,  # Bottom Side
     1.0, 1.0,-1.0,  1.0,-1.0,-1.0, -1.0,-1.0,-1.0,  # Back Side
     1.0,-1.0, 1.0, -1.0,-1.0, 1.0, -1.0,-1.0,-1.0,  # Bottom Side
    -1.0, 1.0, 1.0, -1.0,-1.0, 1.0,  1.0,-1.0, 1.0,  # Front Side
     1.0, 1.0, 1.0,  1.0,-1.0,-1.0,  1.0, 1.0,-1.0,  # Right Side
     1.0,-1.0,-1.0,  1.0, 1.0, 1.0,  1.0,-1.0, 1.0,  # Right Side
     1.0, 1.0, 1.0,  1.0, 1.0,-1.0, -1.0, 1.0,-1.0,  # Top Side
     1.0, 1.0, 1.0, -1.0, 1.0,-1.0, -1.0, 1.0, 1.0,  # Top Side
     1.0, 1.0, 1.0, -1.0, 1.0, 1.0,  1.0,-1.0, 1.0   # Front Side
]

normals= [ 
    -1.0, 0.0, 0.0, # Left Side
    -1.0, 0.0, 0.0, # Left Side
     0.0, 0.0, -1.0, # Back Side
     0.0, -1.0, 0.0, # Bottom Side
     0.0, 0.0, -1.0, # Back Side
     0.0, -1.0, 0.0, # Bottom Side
     0.0, 0.0, 1.0, # front Side
     1.0, 0.0, 0.0, # right Side
     1.0, 0.0, 0.0, # right Side
     0.0, 1.0, 0.0, # top Side
     0.0, 1.0, 0.0, # top Side
     0.0, 0.0, 1.0, # front Side
]


vbo1 = glGenBuffers(1)
glBindBuffer (GL_ARRAY_BUFFER, vbo1)
glBufferData (GL_ARRAY_BUFFER, len(vertices)*4, (c_float*len(vertices))(*vertices), GL_STATIC_DRAW)
#glBindBuffer (GL_ARRAY_BUFFER, 0)
vbo2 = glGenBuffers(1)
glBindBuffer (GL_ARRAY_BUFFER, vbo2)
glBufferData (GL_ARRAY_BUFFER, len(normals)*4, (c_float*len(normals))(*normals), GL_STATIC_DRAW)

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(90,1,0.01,1000)
gluLookAt(1.5,1.5,1.5,0,0,0,0,1,0)



while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        elif event.type == KEYDOWN:
            if event.key == K_RIGHT:
                glRotatef(3,0,1,0)
            elif event.key == K_LEFT:
                glRotatef(3,0,-1,0)
            elif event.key == K_UP:
                glRotatef(3,1,0,0)
            elif event.key == K_DOWN:
                glRotatef(3,-1,0,0)



    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glShadeModel(GL_SMOOTH)
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);


    glMatrixMode(GL_MODELVIEW)

    glEnableClientState (GL_VERTEX_ARRAY)
    glBindBuffer (GL_ARRAY_BUFFER,vbo1)
    glVertexPointer (3, GL_FLOAT,0, None)

    glEnableClientState(GL_NORMAL_ARRAY)
    glBindBuffer (GL_ARRAY_BUFFER,vbo2)
    glNormalPointer (GL_FLOAT, 0 , 0)

    n = len(vertices)//3
    glDrawArrays (GL_TRIANGLES, 0, n)

    glDisableClientState(GL_NORMAL_ARRAY)
    glDisableClientState(GL_VERTEX_ARRAY)

    pygame.display.flip ()

enter image description here

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

必须为每个顶点定义OpenGL中的法线,而不是为每个面定义。如果你有一个包含36个顶点的立方体,你还必须提供36个法线。

vertices = [
    -1.0,-1.0,-1.0, -1.0,-1.0, 1.0, -1.0, 1.0, 1.0,  # Left Side
    ...

normals= [ 
    -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, # Left Side
    ...

答案 1 :(得分:0)

好像你没有启用深度测试

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

可能导致奇怪的结果