74 lines
3 KiB
Python
74 lines
3 KiB
Python
|
from OpenGL.GL import *
|
||
|
import numpy as np
|
||
|
from OpenGL.GL.ARB.vertex_array_object import glDeleteVertexArrays
|
||
|
from OpenGL.GL.framebufferobjects import glBindRenderbuffer
|
||
|
from OpenGL.GLUT import *
|
||
|
import OpenGL.GLUT.freeglut
|
||
|
from OpenGL.GLU import *
|
||
|
from OpenGL.GL import *
|
||
|
from ctypes import sizeof, c_float, c_void_p, c_uint
|
||
|
from MatrixStuff.Transformations import *
|
||
|
|
||
|
|
||
|
class Light:
|
||
|
programId = {}
|
||
|
depthshaderId = -1
|
||
|
|
||
|
def getDepthProgram(self,vertexshader=-1,geometryshader=-1):
|
||
|
if ((vertexshader,geometryshader) not in self.programId.keys() and vertexshader != -1 and geometryshader != -1):
|
||
|
if self.depthshaderId == -1:
|
||
|
with open('./Lights/depthfragment.glsl', 'r') as f:
|
||
|
fragment_shader_string = f.read()
|
||
|
fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER)
|
||
|
glShaderSource(fragment_shader_id, fragment_shader_string)
|
||
|
glCompileShader(fragment_shader_id)
|
||
|
if glGetShaderiv(fragment_shader_id, GL_COMPILE_STATUS) != GL_TRUE:
|
||
|
raise RuntimeError(glGetShaderInfoLog(fragment_shader_id))
|
||
|
|
||
|
program_id = glCreateProgram()
|
||
|
glAttachShader(program_id, vertexshader)
|
||
|
glAttachShader(program_id, geometryshader)
|
||
|
glAttachShader(program_id, fragment_shader_id)
|
||
|
glLinkProgram(program_id)
|
||
|
self.programId[(vertexshader, geometryshader)] = program_id
|
||
|
return program_id
|
||
|
else:
|
||
|
if (vertexshader,geometryshader) not in self.programId.keys():
|
||
|
return -1
|
||
|
return self.programId[(vertexshader,geometryshader)]
|
||
|
|
||
|
|
||
|
|
||
|
def __init__(self):
|
||
|
self.ModelviewProjectionMatrix = np.identity(4)
|
||
|
self.FramebufferId = -1
|
||
|
self.DepthBuffer = -1
|
||
|
def prepareForDepthMapping(self):
|
||
|
new = False
|
||
|
if self.FramebufferId == -1:
|
||
|
self.FramebufferId = glGenFramebuffers(1)
|
||
|
new = True
|
||
|
glClearColor(1.0,1.0,1.0,1.0)
|
||
|
glBindFramebuffer(GL_FRAMEBUFFER,self.FramebufferId)
|
||
|
glCullFace(GL_FRONT)
|
||
|
glViewport(0, 0, 512, 512)
|
||
|
if new:
|
||
|
if self.DepthBuffer == -1:
|
||
|
self.DepthBuffer = glGenTextures(1)
|
||
|
glBindTexture(GL_TEXTURE_2D, self.DepthBuffer)
|
||
|
glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT, 512, 512, 0,GL_DEPTH_COMPONENT, GL_FLOAT, None)
|
||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
|
||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
|
||
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D, self.FramebufferId, 0)
|
||
|
|
||
|
DrawBuffers = [GL_NONE]
|
||
|
glDrawBuffers(DrawBuffers)
|
||
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE):
|
||
|
return False
|
||
|
|
||
|
def finishDepthMapping(self):
|
||
|
glCullFace(GL_BACK)
|
||
|
DrawBuffers = [GL_COLOR_ATTACHMENT0]
|
||
|
glDrawBuffers(DrawBuffers)
|
||
|
glClearColor(0.0, 0.0, 0.0, 1.0)
|
||
|
glBindFramebuffer(GL_FRAMEBUFFER,0)
|