diff --git a/README.md b/README.md index 5384a69..0ffbd16 100644 --- a/README.md +++ b/README.md @@ -5,29 +5,17 @@ Make sure to have python3 running and the following libs are installed: `pip install pillow` -## What is special about this fork? +Blender generate .obj-file steps: -For the general idea please visit [https://github.com/ssloy/tinyrenderer](https://github.com/ssloy/tinyrenderer) +1. object mode -> Object -> Convert to -> Mesh from Curve/Meta/Surf/Text +1. remove all keyframes +1. delete rigs, cam, particles ... +1. move objects -This fork is intended to be a starting point of teaching programming in python3 to everyone as I consider the content to be tough but motivational for beginners. -With this in mind I tried to follow ssloys tutorial closely and chose some guidelines: +1. add Decimate modifier to one object +1. select all in viewpane +1. ctrl + l -> Modifier to add last selected modifier in 1. sidepane to all objects +1. set origin to 3d cursor -1. Simplicity over perfection -1. Readability over performance - -To accomplish these goals I included a special linear algebra module `geom.py` instead of using `numpy`. -Numpy is undoubtly more universal and elaborated but harder to read for beginners. -The `geom.py` module contains some basic vector and matrix classes and supports indexing via '.x', '.y' and '.z' notation based on namedtuples. - -After finishing the fork I am going to be working on course material and structured lessons to roll out this exciting course of 3D animation and programming sometime in the future. To motivate students even more I plan providing some short Blender tutorials on how to prepare custom models for this renderer. - -## Here are some examples generated by the renderer - -### Rendered image with shadows, ambient occlusion and specular lighting (current optimum) -Rendered image with shadows and specular lighting - -### Rendered wire mesh -Rendered wire mesh - -### Rendered randomly colored filled mesh triangles -Randomly colored filled mesh triangles +1. select all +1. export obj (triangulate, no obj groups, selected only, write normals) \ No newline at end of file diff --git a/docs/images/ao_map_shade.png b/docs/images/ao_map_shade.png deleted file mode 100644 index caa7149..0000000 Binary files a/docs/images/ao_map_shade.png and /dev/null differ diff --git a/docs/images/diffuse_gouraud_interpolation_fail_wrong_matmul.png b/docs/images/diffuse_gouraud_interpolation_fail_wrong_matmul.png deleted file mode 100644 index b1ea056..0000000 Binary files a/docs/images/diffuse_gouraud_interpolation_fail_wrong_matmul.png and /dev/null differ diff --git a/docs/images/diffuse_gouraud_shade.png b/docs/images/diffuse_gouraud_shade.png deleted file mode 100644 index 3192d45..0000000 Binary files a/docs/images/diffuse_gouraud_shade.png and /dev/null differ diff --git a/docs/images/e04_autumn_mesh.png b/docs/images/e04_autumn_mesh.png deleted file mode 100644 index dd8bf46..0000000 Binary files a/docs/images/e04_autumn_mesh.png and /dev/null differ diff --git a/docs/images/e06_autumn_filled.png b/docs/images/e06_autumn_filled.png deleted file mode 100644 index 172c8ab..0000000 Binary files a/docs/images/e06_autumn_filled.png and /dev/null differ diff --git a/docs/images/e08.1_autumn_flat_shaded.png b/docs/images/e08.1_autumn_flat_shaded.png deleted file mode 100644 index acddf38..0000000 Binary files a/docs/images/e08.1_autumn_flat_shaded.png and /dev/null differ diff --git a/docs/images/e08.2_autumn_zbuffered.png b/docs/images/e08.2_autumn_zbuffered.png deleted file mode 100644 index f24e352..0000000 Binary files a/docs/images/e08.2_autumn_zbuffered.png and /dev/null differ diff --git a/docs/images/e09.1_autumn_perspective.png b/docs/images/e09.1_autumn_perspective.png deleted file mode 100644 index e3029c8..0000000 Binary files a/docs/images/e09.1_autumn_perspective.png and /dev/null differ diff --git a/docs/images/e09.2_autumn_texture.png b/docs/images/e09.2_autumn_texture.png deleted file mode 100644 index cb5ad73..0000000 Binary files a/docs/images/e09.2_autumn_texture.png and /dev/null differ diff --git a/docs/images/estimated_ao_shade.png b/docs/images/estimated_ao_shade.png deleted file mode 100644 index 2fec283..0000000 Binary files a/docs/images/estimated_ao_shade.png and /dev/null differ diff --git a/docs/images/flat_shade.png b/docs/images/flat_shade.png deleted file mode 100644 index 3c7a3a1..0000000 Binary files a/docs/images/flat_shade.png and /dev/null differ diff --git a/docs/images/global_normal_map_shade.png b/docs/images/global_normal_map_shade.png deleted file mode 100644 index edaf768..0000000 Binary files a/docs/images/global_normal_map_shade.png and /dev/null differ diff --git a/docs/images/gouraud_segregated_shade.png b/docs/images/gouraud_segregated_shade.png deleted file mode 100644 index 9b94d30..0000000 Binary files a/docs/images/gouraud_segregated_shade.png and /dev/null differ diff --git a/docs/images/light_z_normalmap_shader.png b/docs/images/light_z_normalmap_shader.png deleted file mode 100644 index b367bf4..0000000 Binary files a/docs/images/light_z_normalmap_shader.png and /dev/null differ diff --git a/docs/images/light_z_normalmap_shader_diffuse.png b/docs/images/light_z_normalmap_shader_diffuse.png deleted file mode 100644 index ed91705..0000000 Binary files a/docs/images/light_z_normalmap_shader_diffuse.png and /dev/null differ diff --git a/docs/images/shadow_buffer.png b/docs/images/shadow_buffer.png deleted file mode 100644 index 0fa5268..0000000 Binary files a/docs/images/shadow_buffer.png and /dev/null differ diff --git a/docs/images/shadow_shade.png b/docs/images/shadow_shade.png deleted file mode 100644 index ee71ccd..0000000 Binary files a/docs/images/shadow_shade.png and /dev/null differ diff --git a/docs/images/specular_map_shading.png b/docs/images/specular_map_shading.png deleted file mode 100644 index 6721a80..0000000 Binary files a/docs/images/specular_map_shading.png and /dev/null differ diff --git a/docs/images/tiny_shader.png b/docs/images/tiny_shader.png deleted file mode 100644 index f3ceedb..0000000 Binary files a/docs/images/tiny_shader.png and /dev/null differ diff --git a/docs/images/wrong_normal_input_use_vertex_point_as_normal_direction.png b/docs/images/wrong_normal_input_use_vertex_point_as_normal_direction.png deleted file mode 100644 index 9cc1d8a..0000000 Binary files a/docs/images/wrong_normal_input_use_vertex_point_as_normal_direction.png and /dev/null differ diff --git a/main.py b/main.py index 708e0f8..839ecf1 100644 --- a/main.py +++ b/main.py @@ -6,29 +6,76 @@ from geom import ScreenCoords, Vector3D from model import ModelStorage, NormalMapType -from tiny_shaders import TinyShader, DepthShader +from tiny_shaders import FlatShader, GouraudShader, GouraudShaderSegregated, \ + DiffuseGouraudShader, GlobalNormalmapShader, SpecularmapShader, \ + TangentNormalmapShader, DepthShader, SpecularShadowShader, \ + ZShader, AmbientOcclusionShader, AmbientOcclusionMapShader if __name__ == "__main__": - # Model property definition - OBJ_FILENAME = "obj/autumn/autumn.obj" - DIFFUSE_FILENAME = "obj/autumn/TEX_autumn_body_color.tga" - NORMAL_MAP_FILENAME = "obj/autumn/TEX_autumn_body_normals_wrld.tga" - NORMAL_MAP_TYPE = NormalMapType.GLOBAL - SPECULAR_MAP_FILENAME = "obj/autumn/TEX_autumn_body_spec.tga" - AO_MAP_FILENAME = "obj/autumn/TEX_autumn_body_ao.tga" - OUTPUT_FILENAME = "renders/out.png" - - # Image property definition - (w, h) = (800, 800) + # Model property selection + MODEL_PROP_SET = 0 + if MODEL_PROP_SET == 0: + OBJ_FILENAME = "obj/autumn/autumn.obj" + DIFFUSE_FILENAME = "obj/autumn/TEX_autumn_body_color.tga" + NORMAL_MAP_FILENAME = "obj/autumn/TEX_autumn_body_normals_tngt.tga" + NORMAL_MAP_TYPE = NormalMapType.TANGENT + SPECULAR_MAP_FILENAME = "obj/autumn/TEX_autumn_body_spec.tga" + AO_MAP_FILENAME = "obj/autumn/TEX_autumn_body_ao.tga" + OUTPUT_FILENAME = "renders/out.png" + elif MODEL_PROP_SET == 1: + OBJ_FILENAME = "obj/autumn/autumn.obj" + DIFFUSE_FILENAME = "obj/autumn/TEX_autumn_body_color.tga" + NORMAL_MAP_FILENAME = "obj/autumn/TEX_autumn_body_normals_wrld.tga" + NORMAL_MAP_TYPE = NormalMapType.GLOBAL + SPECULAR_MAP_FILENAME = "obj/autumn/TEX_autumn_body_spec.tga" + AO_MAP_FILENAME = "obj/autumn/TEX_autumn_body_ao.tga" + OUTPUT_FILENAME = "renders/out.png" + elif MODEL_PROP_SET == 2: + OBJ_FILENAME = "obj/head/head.obj" + DIFFUSE_FILENAME = "obj/head/head_diffuse.tga" + NORMAL_MAP_FILENAME = "obj/head/head_nm_tangent.tga" + NORMAL_MAP_TYPE = NormalMapType.TANGENT + SPECULAR_MAP_FILENAME = "obj/head/head_spec.tga" + OUTPUT_FILENAME = "renders/out.png" + else: + OBJ_FILENAME = "obj/head/head.obj" + DIFFUSE_FILENAME = "obj/head/head_diffuse.tga" + NORMAL_MAP_FILENAME = "obj/head/head_nm.tga" + NORMAL_MAP_TYPE = NormalMapType.GLOBAL + SPECULAR_MAP_FILENAME = "obj/head/head_spec.tga" + OUTPUT_FILENAME = "renders/out.png" + + # Image property selection + IMG_PROP_SET = 1 + if IMG_PROP_SET == 0: + (w, h) = (200, 200) + elif IMG_PROP_SET == 1: + (w, h) = (800, 800) + elif IMG_PROP_SET == 2: + (w, h) = (2000, 2000) + else: + raise ValueError + image = TinyImage(w, h) - # View property definition + # View property selection VIEW_PROP_SET = 0 - EYE = Vector3D(0, 0, 4) # Lookat camera 'EYE' position - CENTER = Vector3D(0, 0, 0) # Lookat 'CENTER'. 'EYE' looks at CENTER - UP = Vector3D(0, 1, 0) # Camera 'UP' direction - SCALE = .8 # Viewport scaling - + if VIEW_PROP_SET == 0: + EYE = Vector3D(0, 0, 4) # Lookat camera 'EYE' position + CENTER = Vector3D(0, 0, 0) # Lookat 'CENTER'. 'EYE' looks at CENTER + UP = Vector3D(0, 1, 0) # Camera 'UP' direction + SCALE = .8 # Viewport scaling + elif VIEW_PROP_SET == 1: + EYE = Vector3D(2.828, 0, 2.828) + CENTER = Vector3D(0, 0, 0) + UP = Vector3D(0, 1, 0) + SCALE = .8 + else: + EYE = Vector3D(4, 0, 0) # Lookat camera 'EYE' position + CENTER = Vector3D(0, 0, 0) # Lookat 'CENTER'. 'EYE' looks at CENTER + UP = Vector3D(0, 1, 0) # Camera 'UP' direction + SCALE = .8 # Viewport scaling + # Light property LIGHT_DIR = Vector3D(1, 1, 1).normalize() @@ -76,9 +123,31 @@ PREPARE_AO_SHADER = False PREPARE_SHADOW_SHADER = False - # Shader definition - PREPARE_SHADOW_SHADER = True - shader = TinyShader(mdl, LIGHT_DIR, M_pe, M_sc, M_pe_IT, None, None) + SHADER_PROP_SET = 2 + if SHADER_PROP_SET == 0: + shader = FlatShader(mdl, LIGHT_DIR, M_sc) + elif SHADER_PROP_SET == 1: + shader = GouraudShader(mdl, LIGHT_DIR, M_sc) + elif SHADER_PROP_SET == 2: + shader = GouraudShaderSegregated(mdl, LIGHT_DIR, M_sc, 4) + elif SHADER_PROP_SET == 3: + shader = DiffuseGouraudShader(mdl, LIGHT_DIR, M_sc) + elif SHADER_PROP_SET == 4: + shader = GlobalNormalmapShader(mdl, LIGHT_DIR, M_pe, M_sc, M_pe_IT) + elif SHADER_PROP_SET == 5: + shader = SpecularmapShader(mdl, LIGHT_DIR, M_pe, M_sc, M_pe_IT) + elif SHADER_PROP_SET == 6: + shader = TangentNormalmapShader(mdl, LIGHT_DIR, M_pe, M_pe_IT, M_viewport) + elif SHADER_PROP_SET == 7: + PREPARE_SHADOW_SHADER = True + shader = SpecularShadowShader(mdl, LIGHT_DIR, M_pe, M_sc, M_pe_IT, None, None) + elif SHADER_PROP_SET == 8: + PREPARE_AO_SHADER = True + shader = AmbientOcclusionShader(mdl, M_sc, None, w, h) + elif SHADER_PROP_SET == 9: + shader = AmbientOcclusionMapShader(mdl, M_sc) + else: + raise ValueError if PREPARE_SHADOW_SHADER: # Fill shadow buffer and set data for final shader @@ -110,6 +179,31 @@ shadow_image.save_to_disk("renders/shadow_buffer.png") + if PREPARE_AO_SHADER: + # Fill zbuffer and set data for final shader + print("Precalculating zbuffer for ambient occlusion shader ...") + precalc_z_buffer = [[-float('Inf') for bx in range(w)] for y in range(h)] + z_image = TinyImage(w, h) + z_shader = ZShader(mdl, M_sc) + + for face_idx in progressbar.progressbar(range(mdl.get_face_count())): + for face_vert_idx in range(3): + # Get transformed vertex and prepare internal shader data + vert = z_shader.vertex(face_idx, face_vert_idx) + screen_coords = screen_coords.set_col(face_vert_idx, vert) + + # Rasterize triangle + image = gl.draw_triangle(screen_coords, z_shader, precalc_z_buffer, z_image) + + # Prepare and set data for the next shader + shader.uniform_precalc_zbuffer = precalc_z_buffer + + # Lower precalc z buffer a little bit to make next shader raster the top pixels but + # not all pixels since AO shader calls are expensive + for x_sc in range(w): + for y_sc in range(h): + zbuffer[x_sc][y_sc] = precalc_z_buffer[x_sc][y_sc] - 1e-10 + # Final shader run print("Drawing triangles ...") for face_idx in progressbar.progressbar(range(mdl.get_face_count())): @@ -121,4 +215,4 @@ # Rasterize triangle image = gl.draw_triangle(screen_coords, shader, zbuffer, image) - image.save_to_disk(OUTPUT_FILENAME) + image.save_to_disk(OUTPUT_FILENAME) \ No newline at end of file diff --git a/tiny_image.py b/tiny_image.py index 4aba6db..0310ff0 100644 --- a/tiny_image.py +++ b/tiny_image.py @@ -17,7 +17,7 @@ class TinyImage: def __init__(self, width = None, height = None): if not width is None and not height is None: - self._im = Image.new(size=(width, height), mode="RGB", color="lightgray") + self._im = Image.new(size=(width, height), mode="RGB") self._draw = ImageDraw.Draw(self._im) def load_image(self, ipath): diff --git a/tiny_shaders.py b/tiny_shaders.py index a3ca915..7f3e3dd 100644 --- a/tiny_shaders.py +++ b/tiny_shaders.py @@ -2,14 +2,377 @@ same structure maintained by an abstract base class in our_gl module.""" import math +import random import our_gl as gl from geom import Matrix4D, Matrix3D, MatrixUV, \ - Vector4DType, Vector3D, Barycentric, PointUV, \ + Vector4DType, Vector3D, Vector2D, Point2D, Barycentric, PointUV, \ transform_3D4D3D, transform_vertex_to_screen, \ - comp_min + cross_product, comp_min from model import ModelStorage, NormalMapType +class FlatShader(gl.Shader): + """Shader which creates a paperfold effect.""" + mdl: ModelStorage + + # Vertices are stored row-wise + varying_vert = Matrix3D(9*[0]) + + varying_n_tri: Vector3D + uniform_l_global: Vector3D + uniform_M: Matrix4D + + def __init__(self, mdl, light_dir, M): + self.mdl = mdl + self.uniform_l_global = light_dir + self.uniform_M = M # pylint: disable=invalid-name + + def vertex(self, face_idx: int, vert_idx: int): + vert = self.mdl.get_vertex(face_idx, vert_idx) # Read the vertex + self.varying_vert = self.varying_vert.set_row(vert_idx, vert) + + # Get normal vector of triangle; should only be done at last vertex + if vert_idx == 2: + v_0 = Vector3D(self.varying_vert.get_row(0)) + v_1 = Vector3D(self.varying_vert.get_row(1)) + v_2 = Vector3D(self.varying_vert.get_row(2)) + self.varying_n_tri = cross_product(v_2 - v_0, v_0 - v_1).normalize() + + return transform_vertex_to_screen(vert, self.uniform_M) # Transform it to screen coordinates + + def fragment(self, bary: Barycentric): + cos_phi = max(0, self.varying_n_tri.tr() * self.uniform_l_global) + + color = (Vector3D(255, 255, 255) * cos_phi) // 1 + return (False, color) # Do not discard pixel and return color + +class GouraudShader(gl.Shader): + """Shader which interpolates normals of triangle vertices.""" + mdl: ModelStorage + + # Written by vertex shader, read by fragment shader + varying_intensity = Vector3D(0, 0, 0, shape = (1,3)) + uniform_l_global: Vector3D + uniform_M: Matrix4D + + def __init__(self, mdl, light_dir, M): + self.mdl = mdl + self.uniform_l_global = light_dir + self.uniform_M = M # pylint: disable=invalid-name + + def vertex(self, face_idx: int, vert_idx: int): + n_tri = self.mdl.get_normal(face_idx, vert_idx) + + # Get diffuse lighting intensity + cos_phi = max(0, n_tri.tr() * self.uniform_l_global) + self.varying_intensity = self.varying_intensity.set_col(vert_idx, cos_phi) + + # Read the vertex data and return + vertex = self.mdl.get_vertex(face_idx, vert_idx) + + # Transform it to screen coordinates + return transform_vertex_to_screen(vertex, self.uniform_M) + + def fragment(self, bary: Barycentric): + intensity = self.varying_intensity * bary + + # Interpolate intensity for the current pixel + color = (Vector3D(255, 255, 255) * intensity) // 1 + + # Do not discard pixel and return color + return (False, color) + +class GouraudShaderSegregated(gl.Shader): + """Gouraud shader with distinct, segregated grey tones.""" + mdl: ModelStorage + varying_intensity = Vector3D(0, 0, 0, shape = (1,3)) + uniform_l_global: Vector3D + uniform_M: Matrix4D + segregate_count = 1 + + def __init__(self, mdl, light_dir, M, segregate_count): + self.mdl = mdl + self.uniform_l_global = light_dir + self.uniform_M = M # pylint: disable=invalid-name + self.segregate_count = segregate_count + + def vertex(self, face_idx: int, vert_idx: int): + n_tri = self.mdl.get_normal(face_idx, vert_idx) + + # Get diffuse lighting intensity + cos_phi = max(0, n_tri.tr() * self.uniform_l_global) + self.varying_intensity = self.varying_intensity.set_col(vert_idx, cos_phi) + + vert = self.mdl.get_vertex(face_idx, vert_idx) # Read the vertex + + # Transform it to screen coordinates + return transform_vertex_to_screen(vert, self.uniform_M) + + def fragment(self, bary: Barycentric): + # Interpolate intensity for current pixel + intensity = self.varying_intensity * bary + + # Segregates intensity values to n = 'segregate_count' distinct values + intensity = round(intensity * self.segregate_count) / self.segregate_count + + color = (Vector3D(255, 255, 255) * intensity) // 1 + + # Do not discard pixel and return color + return (False, color) + +class DiffuseGouraudShader(gl.Shader): + """Shader which combines Gouraud shading and diffuse texture color.""" + mdl: ModelStorage + + # Written by vertex shader, read by fragment shader: varying_data + varying_intensity = Vector3D(0, 0, 0, shape = (1,3)) + # Points in varying_uv are stacked row-wise, 3 rows x 2 columns + varying_uv = MatrixUV(6*[0]) + + uniform_l_global: Vector3D + uniform_M: Matrix4D + + def __init__(self, mdl, light_dir, M): + self.mdl = mdl + self.uniform_l_global = light_dir + self.uniform_M = M # pylint: disable=invalid-name + + def vertex(self, face_idx: int, vert_idx: int): + n_tri = self.mdl.get_normal(face_idx, vert_idx) + + # Get diffuse lighting intensity + cos_phi = max(0, n_tri.tr() * self.uniform_l_global) + self.varying_intensity = self.varying_intensity.set_col(vert_idx, cos_phi) + + # Read the vertex + vertex = self.mdl.get_vertex(face_idx, vert_idx) + + # Get uv map point for diffuse color interpolation and store it + self.varying_uv = \ + self.varying_uv.set_col(vert_idx, self.mdl.get_uv_map_point(face_idx, vert_idx)) + + # Transform it to screen coordinates + return transform_vertex_to_screen(vertex, self.uniform_M) + + def fragment(self, bary: Barycentric): + # Interpolate intensity for the current pixel + intensity = self.varying_intensity * bary + + # For interpolation with bary coordinates we need a 2 rows x 3 columns matrix + p_uv = PointUV(self.varying_uv * bary) + + color = self.mdl.get_diffuse_color(p_uv) + color = (color * intensity) // 1 + + # Do not discard pixel and return color + return (False, color) + +class GlobalNormalmapShader(gl.Shader): + """Shader reading a global space normal map to increase detail.""" + mdl: ModelStorage + + # Points in varying_uv are stacked row-wise, 3 rows x 2 columns + varying_uv = MatrixUV(6*[0]) + + uniform_light_dir: Vector3D + uniform_M_pe: Matrix4D + uniform_M_sc: Matrix4D + uniform_M_pe_IT: Matrix4D + + def __init__(self, mdl, light_dir, M_pe, M_sc, M_pe_IT): + self.mdl = mdl + if self.mdl.normal_map_type != NormalMapType.GLOBAL: + raise ValueError("Only use global space normalmaps with this shader") + + self.uniform_light_dir = light_dir + self.uniform_M_pe = M_pe # pylint: disable=invalid-name + self.uniform_M_sc = M_sc # pylint: disable=invalid-name + self.uniform_M_pe_IT = M_pe_IT # pylint: disable=invalid-name + + def vertex(self, face_idx: int, vert_idx: int): + # Read the vertex + vertex = self.mdl.get_vertex(face_idx, vert_idx) + + # Get uv map point for diffuse color interpolation and store it + self.varying_uv = \ + self.varying_uv.set_col(vert_idx, self.mdl.get_uv_map_point(face_idx, vert_idx)) + + # Transform it to screen coordinates + return transform_vertex_to_screen(vertex, self.uniform_M_sc) + + def fragment(self, bary: Barycentric): + # For interpolation with bary coordinates we need a 2 rows x 3 columns matrix + p_uv = PointUV(self.varying_uv * bary) + + n_global = self.mdl.get_normal_from_map(p_uv) + n_local = transform_3D4D3D(n_global, Vector4DType.DIRECTION, \ + self.uniform_M_pe_IT).normalize() + l_local = transform_3D4D3D(self.uniform_light_dir, Vector4DType.DIRECTION, + self.uniform_M_pe).normalize() + + # Get diffuse lighting intensity + cos_phi = max(0, n_local.tr() * l_local) + + color = self.mdl.get_diffuse_color(p_uv) + color = (color * cos_phi) // 1 + + # Do not discard pixel and return color + return (False, color) + +class SpecularmapShader(gl.Shader): + """Shader combining global normal map shading and specular lighting.""" + mdl: ModelStorage + + # Points in varying_uv are stacked row-wise, 3 rows x 2 columns + varying_uv = MatrixUV(6*[0]) + + uniform_light_dir: Vector3D + uniform_M_pe: Matrix4D + uniform_M_sc: Matrix4D + uniform_M_pe_IT: Matrix4D + + def __init__(self, mdl, light_dir, M_pe, M_sc, M_pe_IT): + self.mdl = mdl + if self.mdl.normal_map_type != NormalMapType.GLOBAL: + raise ValueError("Only use global space normalmaps with this shader") + + self.uniform_light_dir = light_dir + self.uniform_M_pe = M_pe # pylint: disable=invalid-name + self.uniform_M_sc = M_sc # pylint: disable=invalid-name + self.uniform_M_pe_IT = M_pe_IT # pylint: disable=invalid-name + + def vertex(self, face_idx: int, vert_idx: int): + # Read the vertex + vertex = self.mdl.get_vertex(face_idx, vert_idx) + + # Get uv map point for diffuse color interpolation and store it + self.varying_uv = \ + self.varying_uv.set_col(vert_idx, self.mdl.get_uv_map_point(face_idx, vert_idx)) + + # Transform it to screen coordinates + return transform_vertex_to_screen(vertex, self.uniform_M_sc) + + def fragment(self, bary: Barycentric): + # For interpolation with bary coordinates we need a 2 rows x 3 columns matrix + p_uv = PointUV(self.varying_uv * bary) + + n_global = self.mdl.get_normal_from_map(p_uv) + n_local = transform_3D4D3D(n_global, Vector4DType.DIRECTION, \ + self.uniform_M_pe_IT).normalize() + + l_local = transform_3D4D3D(self.uniform_light_dir, Vector4DType.DIRECTION, \ + self.uniform_M_pe).normalize() + cos_phi = n_local.tr() * l_local + + # Get diffuse lighting intensity + diffuse_intensity = max(0, cos_phi) + + # Reflected light direction (already transformed as n and l got transformed) + reflect = (2 * (cos_phi) * n_local - l_local).normalize() + cos_r_z = max(0, reflect.z) # equals: reflect.tr() * Vector3D(0, 0, 1) == reflect.z + specular_intensity = math.pow(cos_r_z, self.mdl.get_specular_power_from_map(p_uv)) + + color = self.mdl.get_diffuse_color(p_uv) + + # Combine base, diffuse and specular intensity + color = 10 * Vector3D(1, 1, 1) + (diffuse_intensity + 0.5 * specular_intensity) * color + color = comp_min(Vector3D(255, 255, 255), color) // 1 + + # Do not discard pixel and return color + return (False, color) + +class TangentNormalmapShader(gl.Shader): + """Shader equal to GlobalNormalmapShader but with tangent space normal map.""" + mdl: ModelStorage + + # Points in varying_uv are stacked row-wise, 3 rows x 2 columns + varying_uv = MatrixUV(6*[0]) + + # Contains precalculated info of varying_vert to save ops in fragment shader + varying_vert = Matrix3D(9*[0]) + varying_A = Matrix3D(9*[0]) + + varying_normal = Matrix3D(9*[0]) + varying_b_u: Vector3D + varying_b_v: Vector3D + + uniform_l_local: Vector3D + uniform_M_pe: Matrix4D + uniform_M_sc: Matrix4D + uniform_M_pe_IT: Matrix4D + + def __init__(self, mdl, light_dir, M_pe, M_pe_IT, M_viewport): + self.mdl = mdl + if self.mdl.normal_map_type != NormalMapType.TANGENT: + raise ValueError("Only use only tangent space normalmaps with this shader") + + # Transform light vector + l_local = transform_3D4D3D(light_dir, Vector4DType.DIRECTION, M_pe) + self.uniform_l_local = l_local.normalize() + + self.uniform_M_pe = M_pe # pylint: disable=invalid-name + self.uniform_M_pe_IT = M_pe_IT # pylint: disable=invalid-name + self.uniform_M_viewport = M_viewport # pylint: disable=invalid-name + + def vertex(self, face_idx: int, vert_idx: int): + # Store triangle vertex (after being transformed to perspective) + vert = self.mdl.get_vertex(face_idx, vert_idx) + vert = transform_3D4D3D(vert, Vector4DType.POINT, self.uniform_M_pe) + self.varying_vert = self.varying_vert.set_col(vert_idx, vert) + + n_tri_global = self.mdl.get_normal(face_idx, vert_idx) # Read normal of the vertex + n_tri_local = transform_3D4D3D(n_tri_global, Vector4DType.DIRECTION, \ + self.uniform_M_pe_IT).normalize() + + # Store triangle vertex normal (after being transformed to perspective) + self.varying_normal = self.varying_normal.set_col(vert_idx, n_tri_local) + + # Get uv map point for diffuse color interpolation and store it + self.varying_uv = \ + self.varying_uv.set_col(vert_idx, self.mdl.get_uv_map_point(face_idx, vert_idx)) + + if vert_idx == 2: + self.varying_b_u = Vector3D(self.varying_uv.u_2 - self.varying_uv.u_0, \ + self.varying_uv.u_1 - self.varying_uv.u_0, \ + 0) + + self.varying_b_v = Vector3D(self.varying_uv.v_2 - self.varying_uv.v_0, \ + self.varying_uv.v_1 - self.varying_uv.v_0, \ + 0) + + vd_0 = self.varying_vert.get_col(2) - self.varying_vert.get_col(0) + vd_1 = self.varying_vert.get_col(1) - self.varying_vert.get_col(0) + self.varying_A = (self.varying_A.set_row(0, vd_0)).set_row(1, vd_1) # pylint: disable=invalid-name + + # Transform it to screen coordinates + return transform_vertex_to_screen(vert, self.uniform_M_viewport) + + def fragment(self, bary: Barycentric): + p_uv = PointUV(self.varying_uv * bary) + + n_bary = (self.varying_normal * bary).normalize() + + A_inv = self.varying_A.set_row(2, n_bary).inv() # pylint: disable=invalid-name + + vect_i = (A_inv * self.varying_b_u).normalize() + vect_j = (A_inv * self.varying_b_v).normalize() + + B = Matrix3D([vect_i, # pylint: disable=invalid-name + vect_j, + n_bary]).tr() + + # Load normal of tangent space and transform to get global normal + n_local = (B * self.mdl.get_normal_from_map(p_uv)).normalize() + + # Get diffuse lighting intensity + cos_phi = max(0, n_local.tr() * self.uniform_l_local) + + color = self.mdl.get_diffuse_color(p_uv) + color = cos_phi * color // 1 + + # Do not discard pixel and return color + return (False, color) + class DepthShader(gl.Shader): """Shader used to save shadow buffer.""" mdl: ModelStorage @@ -35,8 +398,8 @@ def fragment(self, bary: Barycentric): color = (Vector3D(255, 255, 255) * v_bary.z / self.uniform_depth_res) // 1 return (False, color) # Do not discard pixel and return color -class TinyShader(gl.Shader): - """A tiny shader with all techniques applied.""" +class SpecularShadowShader(gl.Shader): + """Shader combining global normal map shading, specular lighting and shadows.""" mdl: ModelStorage # Points in varying_uv are stacked row-wise, 3 rows x 2 columns @@ -82,7 +445,7 @@ def fragment(self, bary: Barycentric): p_shadow = transform_vertex_to_screen(v_bary, self.uniform_M_sb) z_lit = self.shadow_buffer[p_shadow.x][p_shadow.y] - shadowed_intensity = .7 if p_shadow.z + .02 * 255 < z_lit else 1.0 + shadowed_intensity = .3 if p_shadow.z + .02 * 255 < z_lit else 1.0 n_global = self.mdl.get_normal_from_map(p_uv) n_local = transform_3D4D3D(n_global, Vector4DType.DIRECTION, \ @@ -100,14 +463,162 @@ def fragment(self, bary: Barycentric): cos_r_z = max(0, reflect.z) # equals: reflect.tr() * Vector3D(0, 0, 1) == reflect.z specular_intensity = math.pow(cos_r_z, self.mdl.get_specular_power_from_map(p_uv)) - # Get diffuse color and apply ambient occlusion intensity - ao_intensity = self.mdl.get_ao_intensity_from_map(p_uv) color = self.mdl.get_diffuse_color(p_uv) # Combine base, diffuse and specular intensity - color = 10 * Vector3D(1,1,1) + \ - color * shadowed_intensity * (2.8 * (.7 * diffuse_intensity + .3 * ao_intensity) + .6 * specular_intensity) + color = 20 * Vector3D(1,1,1) + \ + color * shadowed_intensity * (1.2 * diffuse_intensity + .6 * specular_intensity) color = comp_min(Vector3D(255, 255, 255), color) // 1 # Do not discard pixel and return color return (False, color) + +class ZShader(gl.Shader): + """Shader used to save shadow buffer.""" + mdl: ModelStorage + + uniform_M: Matrix4D + + def __init__(self, mdl, M): + self.mdl = mdl + self.uniform_M = M # pylint: disable=invalid-name + + def vertex(self, face_idx: int, vert_idx: int): + vert = self.mdl.get_vertex(face_idx, vert_idx) # Read the vertex + return transform_vertex_to_screen(vert, self.uniform_M) + + def fragment(self, bary: Barycentric): + # Do not return color. This is shader is used passively to create + # generate a screen space shadow buffer + return (True, None) + +class AmbientOcclusionShader(gl.Shader): + """Shader used to compute ambient occlusion local illumination.""" + mdl: ModelStorage + + # Vertices are stored col-wise + varying_vert = Matrix3D(9*[0]) + + uniform_M: Matrix4D + uniform_precalc_zbuffer: list + uniform_zbuffer_width: int + uniform_zbuffer_height: int + + varying_uv = MatrixUV(6*[0]) + + sweep_step: int + sweep_incr_fact: float + + def __init__(self, mdl, M, percalc_zbuffer, zbuffer_width, zbuffer_height): + self.mdl = mdl + self.uniform_M = M # pylint: disable=invalid-name + self.uniform_precalc_zbuffer = percalc_zbuffer + self.uniform_zbuffer_width = zbuffer_width + self.uniform_zbuffer_height = zbuffer_height + + # Performance is very dependent on sweep step of ray casting and number of rays + + # Increase factor to get faster renders but more inaccurate AO + self.sweep_incr_fact = 12.0 + assert self.sweep_incr_fact >= 1.0, "Factor must not be below 1.0. Infinite loop run ahead." + + # Decrease ray num to get faster renders but more inaccurate AO + self.ray_num = 12 + + def vertex(self, face_idx: int, vert_idx: int): + vert = self.mdl.get_vertex(face_idx, vert_idx) # Read the vertex + vert = transform_vertex_to_screen(vert, self.uniform_M) + self.varying_vert = self.varying_vert.set_col(vert_idx, vert) + + self.varying_uv = \ + self.varying_uv.set_col(vert_idx, self.mdl.get_uv_map_point(face_idx, vert_idx)) + + return vert + + def fragment(self, bary: Barycentric): + p_uv = PointUV(self.varying_uv * bary) + + if self.mdl.ao_map is None: + (x_sc, y_sc, _) = self.varying_vert * bary // 1 + + summed_ang = 0 + for ray_angle in self.get_ray_angles(randomized = True): + max_elevation = self.max_elevation_angle( + Point2D(x_sc, y_sc), + Vector2D(math.cos(ray_angle), math.sin(ray_angle)), + self.sweep_incr_fact) + + summed_ang = summed_ang + (math.pi / 2 - max_elevation) + + ao_intensity = summed_ang / (math.pi / 2 * self.ray_num) + ao_intensity = ao_intensity ** 5 + + else: + ao_intensity = self.mdl.get_ao_intensity_from_map(p_uv) + + color = Vector3D(255,255,255) * ao_intensity // 1 + + return (False, color) + + def max_elevation_angle(self, pt_amb: Point2D, sweep_dir: Vector2D, sweep_incr_fact: float): + """Returns max elevation angle ray-casted from starting point pt_amb.""" + + pt_amb_z = self.uniform_precalc_zbuffer[pt_amb.x][pt_amb.y] + sweep = Vector2D(pt_amb) + if abs(sweep_dir.x) > abs(sweep_dir.y): + max_sweep_comp = abs(sweep_dir.x) + else: + max_sweep_comp = abs(sweep_dir.y) + sweep_delta = 1.0 / max_sweep_comp * sweep_dir + max_tan = 0 + while True: + z_height = self.uniform_precalc_zbuffer[int(sweep.x)][int(sweep.y)] + if z_height > pt_amb_z: + elevation = z_height - pt_amb_z + tan_val = elevation / sweep.abs() + max_tan = max(tan_val, max_tan) + sweep_delta *= sweep_incr_fact + sweep += sweep_delta + if not 0 <= sweep.x < (self.uniform_zbuffer_width) or \ + not 0 <= sweep.y < (self.uniform_zbuffer_height): + break + return math.atan(max_tan) + + def get_ray_angles(self, randomized: bool = True): + """Return randomized or equal-spaced rays in between 0 and 2 * pi + for a given count of rays.""" + if randomized: + return [random.uniform(0, 2 * math.pi) for ray in range(self.ray_num)] + + return [2 * math.pi * ray / self.ray_num for ray in range(self.ray_num)] + + +class AmbientOcclusionMapShader(gl.Shader): + """Shader used to apply ambient occlusion from ao map.""" + mdl: ModelStorage + + uniform_M: Matrix4D + varying_uv = MatrixUV(6*[0]) + + def __init__(self, mdl, M): + self.mdl = mdl + self.uniform_M = M # pylint: disable=invalid-name + + def vertex(self, face_idx: int, vert_idx: int): + vert = self.mdl.get_vertex(face_idx, vert_idx) # Read the vertex + + self.varying_uv = \ + self.varying_uv.set_col(vert_idx, self.mdl.get_uv_map_point(face_idx, vert_idx)) + + return transform_vertex_to_screen(vert, self.uniform_M) + + def fragment(self, bary: Barycentric): + p_uv = PointUV(self.varying_uv * bary) + ao_intensity = self.mdl.get_ao_intensity_from_map(p_uv) + + color = Vector3D(255,255,255) * ao_intensity // 1 + + return (False, color) + +class TinyShader(gl.Shader): + pass \ No newline at end of file