Want more features on Pastebin? Sign Up, it's FREE!
Guest

depth buffer problems

By: a guest on Feb 24th, 2012  |  syntax: Python  |  size: 4.31 KB  |  views: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from __future__ import division
  2.  
  3. import numpy as np
  4. from OpenGL.GL import *
  5. from OpenGL.GLUT import *
  6.  
  7. vshade = '''
  8. #version 110
  9.  
  10. uniform mat4 p_matrix;
  11. uniform mat4 xfm;
  12.  
  13. attribute vec4 position;
  14.  
  15. void main(void)
  16. {
  17.    vec4 eye_position = xfm * position;
  18.    gl_Position = p_matrix * eye_position;
  19. }
  20. '''
  21.  
  22. fshade = '''
  23. #version 120
  24.  
  25. uniform vec4 basecolor;
  26.  
  27. void main() {
  28.    gl_FragColor = basecolor;
  29. }
  30. '''
  31.  
  32. def _make_shader(stype, src):
  33.     shader = glCreateShader(stype)
  34.     glShaderSource(shader, src)
  35.     glCompileShader(shader)
  36.  
  37.     if not glGetShaderiv(shader, GL_COMPILE_STATUS):
  38.         err = glGetShaderInfoLog(shader)
  39.         glDeleteShader(shader)
  40.         raise Exception(err)
  41.    
  42.     return shader
  43.  
  44. def _make_poly(verts, polys):
  45.     verts, polys = np.array(verts), np.array(polys)
  46.     vbuf = glGenBuffers(1)
  47.     ebuf = glGenBuffers(1)
  48.     glBindBuffer(GL_ARRAY_BUFFER, vbuf)
  49.     glBufferData(GL_ARRAY_BUFFER,
  50.         verts.astype(np.float32).ravel(), GL_STATIC_DRAW)
  51.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebuf)
  52.     glBufferData(GL_ELEMENT_ARRAY_BUFFER,
  53.         polys.astype(np.uint16).ravel(), GL_STATIC_DRAW)
  54.     return vbuf, ebuf
  55.  
  56. def _draw_plane(ploc, vbuf, ebuf):
  57.     glEnableVertexAttribArray(ploc)
  58.     glBindBuffer(GL_ARRAY_BUFFER, vbuf)
  59.     glVertexAttribPointer(ploc, 4, GL_FLOAT, GL_FALSE, 4*4, GLvoidp(0))
  60.  
  61.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebuf);
  62.     glDrawElements(GL_TRIANGLES, 2*3, GL_UNSIGNED_SHORT, GLvoidp(0))
  63.     glDisableVertexAttribArray(ploc)
  64.    
  65. def perspective(angle, aspect, near, far):
  66.     '''Generates a perspective transform matrix'''
  67.     f = 1./ np.tan(np.radians(angle))
  68.     fn, nfn = far + near, far - near
  69.     return np.array([[f/aspect, 0,    0,      0],
  70.                      [0,        f,    0,      0],
  71.                      [0,        0, fn/nfn, 2*far*near/nfn],
  72.                      [0,        0,   -1,      0]])
  73.  
  74. class Window(object):
  75.     window_size = (640, 480)
  76.  
  77.     background = (0,0,0,1)
  78.     fps = 60
  79.     fov = 60
  80.  
  81.     def __init__(self):
  82.         super(Window, self).__init__()
  83.         self.models = []
  84.         self.eyepos = [0,-2,0]
  85.  
  86.     def init(self):
  87.         glEnable(GL_BLEND)
  88.         glDepthFunc(GL_LEQUAL)
  89.         glEnable(GL_DEPTH_TEST)
  90.         glEnable(GL_TEXTURE_2D)
  91.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  92.         glClearColor(*self.background)
  93.         glClearDepth(1.0)
  94.        
  95.         self.vshade = _make_shader(GL_VERTEX_SHADER, vshade)
  96.         self.fshade = _make_shader(GL_FRAGMENT_SHADER, fshade)
  97.         self.program = glCreateProgram()
  98.         glAttachShader(self.program, self.vshade)
  99.         glAttachShader(self.program, self.fshade)
  100.         glLinkProgram(self.program)
  101.        
  102.         w, h = self.window_size
  103.         self.projection = perspective(self.fov/2, w/h, 0.0625, 256.).astype(np.float32).ravel()
  104.  
  105.         self.mv = np.eye(4)
  106.         self.mv[:3,-1] = [0,0,-5]
  107.         self.mv = self.mv.astype(np.float32).ravel()
  108.  
  109.         self.plane1 = [(0,0,1,1),(1,0,1,1),(1,1,1,1),(0,1,1,1)], [(0,1,3), (3,1,2)]
  110.         self.plane2 = [(0,0,-4,1),(1,0,-4,1),(1,1,-4,1),(0,1,-4,1)], [(0,1,3), (3,1,2)]
  111.         self.plane1 = _make_poly(*self.plane1)
  112.         self.plane2 = _make_poly(*self.plane2)
  113.  
  114.         self.ploc = glGetUniformLocation(self.program, "p_matrix")
  115.         self.mvloc = glGetUniformLocation(self.program, "xfm")
  116.         self.cloc = glGetUniformLocation(self.program, "basecolor")
  117.         self.vloc = glGetAttribLocation(self.program, "position")
  118.    
  119.     def render(self):
  120.         w, h = self.window_size
  121.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  122.         glViewport(0, 0, w, h)
  123.         glUseProgram(self.program)
  124.         glUniformMatrix4fv(self.ploc, 1, GL_TRUE, self.projection)
  125.         glUniformMatrix4fv(self.mvloc, 1, GL_TRUE, self.mv)
  126.  
  127.         glUniform4f(self.cloc, 1, 0, 0, 1)
  128.         _draw_plane(self.vloc, *self.plane1)
  129.         glUniform4f(self.cloc, 0, 0, 1, 1)
  130.         _draw_plane(self.vloc, *self.plane2)
  131.  
  132.         glutSwapBuffers()
  133.  
  134. def main():
  135.     win = Window()
  136.     glutInit();
  137.     glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  138.     glutInitWindowSize(*win.window_size);
  139.     glutCreateWindow("Test");
  140.     glutDisplayFunc(win.render);
  141.     win.init()
  142.  
  143.     glutMainLoop()
  144.  
  145. if __name__ == "__main__":
  146.     main()
clone this paste RAW Paste Data