-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1826c51
commit aaf5cc4
Showing
65 changed files
with
46,751 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"app_name": "Perlin Noise 3D Voxel Generator", | ||
"debug_mode": false, | ||
"fullscreen": false, | ||
"world_width": 40, | ||
"world_height": 64, | ||
"world_depth": 40, | ||
"window_width": 1280, | ||
"window_height": 720, | ||
"shadow_width": 1024, | ||
"shadow_height": 1024, | ||
"near_plane": 0.1, | ||
"far_plane": 1000, | ||
"sampling_level": 4 | ||
} |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import glm | ||
from engine.base.shader import Shader | ||
from OpenGL.GL import * | ||
from OpenGL.error import NullFunctionError | ||
|
||
class Program: | ||
def __init__(self): | ||
self.__programId = 0 | ||
self.shaders = [] | ||
|
||
def attachShader(self, shader): | ||
self.shaders.append(shader) | ||
|
||
def link(self): | ||
self.__programId = glCreateProgram() | ||
for shader in self.shaders: | ||
shader.compile() | ||
glAttachShader(self.__programId, shader.getId()) | ||
|
||
glLinkProgram(self.__programId) | ||
|
||
for shader in self.shaders: | ||
shader.delete() | ||
self.shaders.clear() | ||
|
||
if glGetProgramiv(self.__programId, GL_LINK_STATUS) != GL_TRUE: | ||
info = glGetProgramInfoLog(self.__programId) | ||
self.delete() | ||
raise RuntimeError(f'Error in program linking: {info}') | ||
|
||
def __del__(self): | ||
self.delete() | ||
|
||
def delete(self): | ||
try: | ||
glDeleteProgram(self.__programId) | ||
self.__programId = 0 | ||
except NullFunctionError: | ||
pass | ||
|
||
def use(self): | ||
glUseProgram(self.__programId) | ||
|
||
def getId(self): | ||
return self.__programId | ||
|
||
def getAttribLocation(self, name): | ||
return glGetAttribLocation(self.__programId, name) | ||
|
||
def getUniformLocation(self, name): | ||
return glGetUniformLocation(self.__programId, name) | ||
|
||
def setInt(self, name, value): | ||
glUniform1i(self.getUniformLocation(name), value) | ||
|
||
def setFloat(self, name, value): | ||
glUniform1f(self.getUniformLocation(name), value) | ||
|
||
def setVec2(self, name, vec): | ||
glUniform2fv(self.getUniformLocation(name), 1, glm.value_ptr(vec)) | ||
|
||
def setVec3(self, name, vec): | ||
glUniform3fv(self.getUniformLocation(name), 1, glm.value_ptr(vec)) | ||
|
||
def setVec4(self, name, vec): | ||
glUniform4fv(self.getUniformLocation(name), 1, glm.value_ptr(vec)) | ||
|
||
def setMat2(self, name, mat): | ||
glUniformMatrix2fv(self.getUniformLocation(name), 1, GL_FALSE, glm.value_ptr(mat)) | ||
|
||
def setMat3(self, name, mat): | ||
glUniformMatrix3fv(self.getUniformLocation(name), 1, GL_FALSE, glm.value_ptr(mat)) | ||
|
||
def setMat4(self, name, mat): | ||
glUniformMatrix4fv(self.getUniformLocation(name), 1, GL_FALSE, glm.value_ptr(mat)) | ||
|
||
def getLinkedProgram(vertPath, fragPath): | ||
program = Program() | ||
program.attachShader(Shader(vertPath, GL_VERTEX_SHADER)) | ||
program.attachShader(Shader(fragPath, GL_FRAGMENT_SHADER)) | ||
program.link() | ||
return program |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
from OpenGL.GL import * | ||
from OpenGL.error import NullFunctionError | ||
|
||
class Shader: | ||
def __init__(self, sourcePath, shaderType): | ||
self.__shaderId = 0 | ||
if not os.path.exists(sourcePath): | ||
raise RuntimeError(f'Shader source file {sourcePath} does not exists.') | ||
self.__sourcePath = sourcePath | ||
self.shaderType = shaderType | ||
|
||
def compile(self): | ||
self.__shaderId = glCreateShader(self.shaderType) | ||
glShaderSource(self.__shaderId, self.__loadSource()) | ||
glCompileShader(self.__shaderId) | ||
if glGetShaderiv(self.__shaderId, GL_COMPILE_STATUS) != GL_TRUE: | ||
info = glGetShaderInfoLog(self.__shaderId) | ||
raise RuntimeError(f'Shader compilation failed:\n{info}') | ||
|
||
def __loadSource(self): | ||
with open(self.__sourcePath) as file: | ||
source = file.read() | ||
return source | ||
|
||
def getId(self): | ||
return self.__shaderId | ||
|
||
def __del__(self): | ||
self.delete() | ||
|
||
def delete(self): | ||
try: | ||
glDeleteShader(self.__shaderId) | ||
self.__shaderId = 0 | ||
except NullFunctionError: | ||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from OpenGL.GL import * | ||
from OpenGL.error import NullFunctionError | ||
from engine.buffer.framebuffer import Framebuffer | ||
|
||
class Blurbuffer: | ||
def create(self, width, height): | ||
self.FBOs = glGenFramebuffers(2) | ||
self.colorBuffers = glGenTextures(2) | ||
for i in range(2): | ||
glBindFramebuffer(GL_FRAMEBUFFER, self.FBOs[i]) | ||
glBindTexture(GL_TEXTURE_2D, self.colorBuffers[i]) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) | ||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, None) | ||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.colorBuffers[i], 0) | ||
|
||
if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE: | ||
raise RuntimeError('Error when creating Blur Framebuffers.') | ||
glBindFramebuffer(GL_FRAMEBUFFER, 0) | ||
|
||
def __del__(self): | ||
self.delete() | ||
|
||
def delete(self): | ||
try: | ||
glDeleteFramebuffers(2, self.FBOs) | ||
glDeleteTextures(2, self.colorBuffers) | ||
self.colorBuffers = 0 | ||
except NullFunctionError: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from OpenGL.GL import * | ||
from OpenGL.error import NullFunctionError | ||
from engine.buffer.texture import Texture | ||
|
||
class Depthbuffer(Texture): | ||
def __init__(self): | ||
super().__init__(GL_TEXTURE_2D) | ||
|
||
def create(self, width, height): | ||
self.bind() | ||
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, None) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) | ||
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, [1.0, 1.0, 1.0, 1.0]) | ||
self.unbind() | ||
|
||
def attach(self): | ||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, self.getId(), 0) | ||
glDrawBuffer(GL_NONE) | ||
glReadBuffer(GL_NONE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from OpenGL.GL import * | ||
from OpenGL.error import NullFunctionError | ||
|
||
class Framebuffer: | ||
def __init__(self): | ||
self.FBO = glGenFramebuffers(1) | ||
|
||
def checkComplete(self): | ||
if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE: | ||
raise RuntimeError('Error when creating Framebuffer.') | ||
self.unbind() | ||
|
||
def getId(self): | ||
return self.FBO | ||
|
||
def bind(self): | ||
glBindFramebuffer(GL_FRAMEBUFFER, self.FBO) | ||
|
||
def unbind(self): | ||
glBindFramebuffer(GL_FRAMEBUFFER, 0) | ||
|
||
def __del__(self): | ||
self.delete() | ||
|
||
def delete(self): | ||
try: | ||
glDeleteFramebuffers(1, self.FBO) | ||
self.FBO = 0 | ||
except (NullFunctionError, TypeError): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from OpenGL.GL import * | ||
from OpenGL.error import NullFunctionError | ||
from engine.buffer.framebuffer import Framebuffer | ||
from engine.config import config | ||
|
||
class HDRbuffer: | ||
def create(self, width, height): | ||
self.width = width | ||
self.height = height | ||
self.__createFBO() | ||
self.__createMultisampleFBO() | ||
|
||
def __createFBO(self): | ||
self.hdrFBO = Framebuffer() | ||
self.hdrFBO.bind() | ||
self.colorBuffers = glGenTextures(2) | ||
for i in range(2): | ||
glBindTexture(GL_TEXTURE_2D, self.colorBuffers[i]) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) | ||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, self.width, self.height, 0, GL_RGB, GL_FLOAT, None) | ||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, self.colorBuffers[i], 0) | ||
|
||
self.rboDepth = glGenRenderbuffers(1) | ||
glBindRenderbuffer(GL_RENDERBUFFER, self.rboDepth) | ||
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, self.width, self.height) | ||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, self.rboDepth) | ||
glDrawBuffers(2, [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1]) | ||
self.hdrFBO.checkComplete() | ||
|
||
def __createMultisampleFBO(self): | ||
self.__hdrFBO_MS = Framebuffer() | ||
self.__hdrFBO_MS.bind() | ||
self.__colorBuffersMS = glGenTextures(2) | ||
for i in range(2): | ||
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, self.__colorBuffersMS[i]) | ||
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, config['sampling_level'], GL_RGB16F, self.width, self.height, GL_TRUE) | ||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D_MULTISAMPLE, self.__colorBuffersMS[i], 0) | ||
|
||
self.__rboDepthMS = glGenRenderbuffers(1) | ||
glBindRenderbuffer(GL_RENDERBUFFER, self.__rboDepthMS) | ||
glRenderbufferStorageMultisample(GL_RENDERBUFFER, config['sampling_level'], GL_DEPTH_COMPONENT, self.width, self.height) | ||
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, self.__rboDepthMS) | ||
glDrawBuffers(2, [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1]) | ||
self.__hdrFBO_MS.checkComplete() | ||
|
||
def bind(self): | ||
self.__hdrFBO_MS.bind() | ||
|
||
def finalize(self): | ||
glBindFramebuffer(GL_READ_FRAMEBUFFER, self.__hdrFBO_MS.getId()) | ||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, self.hdrFBO.getId()) | ||
for i in range(2): | ||
glReadBuffer(GL_COLOR_ATTACHMENT0 + i) | ||
glDrawBuffer(GL_COLOR_ATTACHMENT0 + i) | ||
glBlitFramebuffer(0, 0, self.width, self.height, 0, 0, self.width, self.height, GL_COLOR_BUFFER_BIT, GL_NEAREST) | ||
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0) | ||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0) | ||
glBindFramebuffer(GL_FRAMEBUFFER, 0) | ||
|
||
def unbind(self): | ||
self.__hdrFBO_MS.unbind() | ||
|
||
def __del__(self): | ||
self.delete() | ||
|
||
def delete(self): | ||
self.hdrFBO.delete() | ||
self.__hdrFBO_MS.delete() | ||
try: | ||
glDeleteRenderbuffers(1, self.rboDepth) | ||
glDeleteRenderbuffers(1, self.rboDepthMS) | ||
glDeleteTextures(2, self.colorBuffers) | ||
glDeleteTextures(2, self.__colorBuffersMS) | ||
self.colorBuffers, self.__colorBuffersMS = 0, 0 | ||
self.rboDepth, self.rboDepthMS = 0, 0 | ||
except (NullFunctionError, TypeError): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from OpenGL.GL import * | ||
from OpenGL.error import NullFunctionError | ||
from PIL import Image | ||
|
||
class Texture: | ||
def __init__(self, type): | ||
self.type = type | ||
self.texture = glGenTextures(1) | ||
|
||
def getId(self): | ||
return self.texture | ||
|
||
def bind(self): | ||
glBindTexture(self.type, self.texture) | ||
|
||
def unbind(self): | ||
glBindTexture(self.type, 0) | ||
|
||
def __del__(self): | ||
self.delete() | ||
|
||
def delete(self): | ||
try: | ||
glDeleteTextures(1, self.texture) | ||
self.texture = 0 | ||
except (NullFunctionError, TypeError): | ||
pass | ||
|
||
def loadTexture2D(path): | ||
texture = Texture(GL_TEXTURE_2D) | ||
texture.bind() | ||
|
||
image = Image.open(path) | ||
image = image.transpose(Image.FLIP_TOP_BOTTOM) | ||
imgData = image.convert('RGBA').tobytes() | ||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imgData) | ||
|
||
glGenerateMipmap(GL_TEXTURE_2D) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) | ||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) | ||
return texture |
Oops, something went wrong.