Cross-Platform Game Programming with gameplay3d/More about Materials

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Materials, Techniques, Passes and Effects[edit]

As you may recall, a Model in gameplay3d is a specific instance of a Mesh, which consists of one or more MeshParts.

In order to render a Model in gameplay3d, each of the MeshParts in that Model must have an associated Material. Since each MeshPart can have a different Material, this allows different parts of the Model to be rendered in a different way. For example, if you have a character that is wearing armor, you could apply a metallic material to that armor while applying simple texturing to the remaining parts of the Model.

A Material contains one or more Technique(s). The idea behind having Techniques is to permit the easy switching of render techniques at runtime, without setting an entirely new material on the MeshPart. As an example, you may have a Model to which you apply a bump mapping Technique when it is near the camera, but you switch to a simple texturing Technique when it is further away in order to improve performance.

Each Technique can have one or more Pass(es), each of which corresponds to a single OpenGL draw call. In order to use multipass rendering techniques, you would add more than one Pass to your Technique.

Passes contain:

  • an Effect, which basically represents an GLSL shader program (i.e. vertex and fragment shader);
  • various information regarding the render state (e.g. the blend function to be applied, whether depth test is enabled etc.);
  • a VertexAttributeBinding, which either encapsulates (or simulates, if you hardware doesn't support it) an OpenGL VAO (vertex attribute object), and sets out how the Mesh's data relates to the inputs in the GLSL shader program.

Phew! That may seem like a long list of things to remember, but you should find that this system provides a lot of flexibility for arranging your rendering behavior in a neat and orderly fashion.

Creating materials[edit]

Setting material parameters[edit]

Working with the built-in shaders[edit]

Help! I can't see my model...[edit]

Here are some common reasons why you may not be able to see your model (or it shows up all in black):

  • You have not set all the required uniforms used by your shader. Have you remembered to call one of the following functions for every uniform used in your shader?:
    • material->getParameter([uniformName])->setValue([uniformValue]);
    • material->getParameter([uniformName])->bindValue([insert arguments]); or
    • material->setParameterAutoBinding([uniformName], [autobinding name]).
  • The mesh used in your model does not contain all the attributes (i.e. per-vertex data) required by your shader. If your model was generated in a 3D modelling program, did the program export all necessary per-vertex data (e.g. tangents and binormal data for bump mapping) required by your shader? Note that the gameplay-encoder can generate tangent and binormal data for an FBX model that does not contain them by using the -tb option.
  • Your mesh data is corrupt. This may be a problem if you have created your mesh procedurally, or if there is a bug in the FBX exporter in your 3D modelling program.

Using your own shaders[edit]

Working with offscreen framebuffers[edit]