• Home
  • Textbooks
  • Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments
  • Customizing the Render Pipeline

Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments

Penny de Byl

Chapter 18

Customizing the Render Pipeline - all with Video Answers

Educators


Section 1

Coloring and texturing mesh faces

Problem 1

Make a copy of the Chapter_17 folder and name it Chapter_18.

Check back soon!

Problem 2

First, we need to modify LoadMesh. py to pass the vertex normal information that's being extracted from the OBJ file to the vertex shader. Modify the code like so:
class LoadMesh (Mesh3D) :
def_init_(self, vao_ref, material, draw_type,
model_filename, texture_file="",
back_face_cull=False) :
self. coordinates $=$
self.format_vertices (
self.vertices, self.triangles)
self.normals $=$
self.format_vertices (self.normals,
self.normal ind)
.
position = GraphicsData("vec3",
self.coordinates)
position.create_variable(material.program_id,
"position")
vertex_normals = GraphicsData("vec3",
self.normals)
vertex_normals.create_variable (
material.program_id,
"vertex_normal")
def format_vertices(self, coordinates, triangles):
.

Check back soon!

Problem 3

Next, we must modify vertexcolvert.vs so that it accepts the vertex normal value and passes it onto the fragment shader:
#version 330 core
in vec3 position;
in vec3 vertex_normal;
uniform mat4 projection_mat;
uniform mat4 view_mat;
uniform mat4 model_mat;
out vec3 normal;
void main()
{
gl_Position = projection_mat *
inverse(transpose(view_mat)) *
transpose(model_mat) *
vec4(position, 1);
normal = mat3(transpose(model_mat)) *
vertex_normal;
}

Check back soon!

Problem 4

Now, we can modify vertexcolfrag.vs so that it uses the normal color for coloring fragments:
#version 330 core
in vec3 normal;
out vec4 frag_color;
void main()
{
frag_color = vec4(normal, 1);
}

Check back soon!

Problem 5

A few tweaks in ShaderTeapot.py will make the starting render easier to examine. Modify this file like this:
mat = Material(“shaders/vertexcolvert.vs”,
“shaders/vertexcolfrag.vs”)
quat_teapot.add_component(LoadMesh(
quat_teapot.vao_ref, mat,GL_TRIANGLES,
“models/teapot.obj”))
quat_teapot.add_component(mat)
quat_trans: Transform =
quat_teapot.get_component(Transform)
quat_trans.update_position(pygame.Vector3(0, -1.5,
-5))
quat_trans.rotate_y(90, False)
objects_3d.append(quat_teapot)

Check back soon!
07:48

Problem 6

Run the ShaderTeapot.py file now to see the results.

Willis James
Willis James
Numerade Educator