VoxelEngine/Lights/Lights.py

107 lines
4.1 KiB
Python
Raw Normal View History

2017-08-27 12:51:26 +02:00
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
2020-07-19 10:41:08 +02:00
def getDepthProgram(self, vertexshader=-1, geometryshader=-1):
if ((
vertexshader, geometryshader) not in self.programId.keys() and vertexshader != -1 and geometryshader != -1):
2017-08-27 12:51:26 +02:00
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:
2020-07-19 10:41:08 +02:00
if (vertexshader, geometryshader) not in self.programId.keys():
2017-08-27 12:51:26 +02:00
return -1
2020-07-19 10:41:08 +02:00
return self.programId[(vertexshader, geometryshader)]
2017-08-27 12:51:26 +02:00
def __init__(self):
self._ModelviewProjectionMatrix = np.identity(4)
2020-07-19 10:41:08 +02:00
self._pos = [0, 0, 0]
self._lightColor = [1, 1, 1]
2017-08-27 12:51:26 +02:00
self.FramebufferId = -1
self.DepthBuffer = -1
2020-07-19 10:41:08 +02:00
self.map_size = 1024
@property
def lightColor(self):
return self._lightColor
2020-07-19 10:41:08 +02:00
@lightColor.setter
2020-07-19 10:41:08 +02:00
def lightColor(self, value):
self._lightColor = value
@property
def ModelviewProjectionMatrix(self):
return self._ModelviewProjectionMatrix
@ModelviewProjectionMatrix.setter
def ModelviewProjectionMatrix(self, value):
self._ModelviewProjectionMatrix = np.matrix(value)
@property
def pos(self):
return self._pos
@pos.setter
2020-07-19 10:41:08 +02:00
def pos(self, value):
self._pos = value
2017-08-27 12:51:26 +02:00
def prepareForDepthMapping(self):
new = False
if self.FramebufferId == -1:
self.FramebufferId = glGenFramebuffers(1)
new = True
2020-07-19 10:41:08 +02:00
glClearColor(1.0, 1.0, 1.0, 1.0)
glBindFramebuffer(GL_FRAMEBUFFER, self.FramebufferId)
glCullFace(GL_FRONT)
glViewport(0, 0, self.map_size, self.map_size)
2017-08-27 12:51:26 +02:00
if new:
if self.DepthBuffer == -1:
self.DepthBuffer = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, self.DepthBuffer)
2020-07-19 10:41:08 +02:00
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, self.map_size, self.map_size, 0, GL_DEPTH_COMPONENT, GL_FLOAT, None)
2017-08-27 12:51:26 +02:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
2020-07-19 10:41:08 +02:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER)
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, np.array([0, 0, 0], dtype=np.float32))
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, self.FramebufferId, 0)
2017-08-27 12:51:26 +02:00
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)
2020-07-19 10:41:08 +02:00
glBindFramebuffer(GL_FRAMEBUFFER, 0)