OGRE Wiki
Support and community documentation for Ogre3D
Ogre Forums
ogre3d.org
Log in
Username:
Password:
CapsLock is on.
Remember me (for 1 year)
Log in
Home
Tutorials
Tutorials Home
Basic Tutorials
Intermediate Tutorials
Mad Marx Tutorials
In Depth Tutorials
Older Tutorials
External Tutorials
Cookbook
Cookbook Home
CodeBank
Snippets
Experiences
Ogre Articles
Libraries
Libraries Home
Alternative Languages
Assembling A Toolset
Development Tools
OGRE Libraries
List of Libraries
Tools
Tools Home
DCC Tools
DCC Tutorials
DCC Articles
DCC Resources
Assembling a production pipeline
Development
Development Home
Roadmap
Building Ogre
Installing the Ogre SDK
Setting Up An Application
Ogre Wiki Tutorial Framework
Frequently Asked Questions
Google Summer Of Code
Help Requested
Ogre Core Articles
Community
Community Home
Projects Using Ogre
Recommended Reading
Contractors
Wiki
Immediate Wiki Tasklist
Wiki Ideas
Wiki Guidelines
Article Writing Guidelines
Wiki Styles
Wiki Page Tracker
Ogre Wiki Help
Ogre Wiki Help Overview
Help - Basic Syntax
Help - Images
Help - Pages and Structures
Help - Wiki Plugins
Toolbox
Freetags
Categories
List Pages
Structures
Trackers
Statistics
Rankings
List Galleries
Ogre Lexicon
Comments
History: Materials
View page
Source of version: 13
(current)
The best way to get information on materials is starting to read the manual. [http://www.ogre3d.org/docs/manual/manual_14.html#SEC23|Material Scripts] Attached is a collection of material templates that can be used as the launching pad for your own OGRE projects. Programmatic materials - e.g. materials created systematically via C++ code - can be found ((here)). Get ((Syntax highlighting for Material files)) using [http://www.crimsoneditor.com/|Crimson Editor]. {maketoc} !!Basic !!!Simple image map The simplest material - apply an image to a mesh. __Material Requirements__ * ''image.png'' - Texture __Material Template__ {CODE(wrap="1", colors="c++")} material Template/texture_map { technique { pass { texture_unit { texture image.png scale 0.1 0.1 // this means that the texture will be tiled 10 times (1/10 = 0.1) } } } } {CODE} !!!Solid colour Use a script like this to create a solid coloured material: __Material Template__ {CODE(wrap="1", colors="c++")} material Template/Red { technique { pass { texture_unit { colour_op_ex source1 src_manual src_current 1 0 0 } } } } {CODE} 'source1' uses the first source without modifications. In this case, the first source is the very next argument. 'src_manual' means to use the colour denoted by the three numbers at the end. The numbers represent RGB values and can be between 0 and 1. 'src_current' is the texture_unit that is being blended with 'src_manual', but in this case, since the operation is to use the first source unchanged, the resulting texture is simply the solid color as denoted by '1 0 0'. !!!Simple shaded colour Create a single colored material with shading: __Material Template__ {CODE(wrap="1", colors="c++")} material Template/Red { technique { pass { lighting on ambient 0.3 0.1 0.1 1 diffuse 0.8 0.05 0.05 1 emissive 0 0 0 1 } } } material Template/Blue { technique { pass { lighting on ambient 0.3 0.3 0.3 1 diffuse 0.1 0.2 0.7 1 emissive 0 0 0 1 } } } material Templates/RadioactiveGreen { technique { pass { lighting on ambient 0.1 0.3 0.1 1 diffuse 0.2 0.2 0.2 1 emissive 0.05 0.8 0.05 1 } } }{CODE} All colors are formed using three values between 0 and 1 that represent the RGB components. Some parameters take 4 values, with the fourth being the alpha value (also between 0 and 1). A value of 1 means white or maximum brightness, 0 means black or lowest brightness. Ambient determines the base color of the object, without any lighting applied to it. This means, since the color model is additive, that this is the minimal brightness of the object (when it is in absolute shadow or no light shines on it). Making this value too high (eg. completely white (1 1 1) ) will result in all your objects being completely white. Setting it to black (0 0 0) will give you completely black shadows. Diffuse color determines the base color of your material in the presence of lights. It determines how much of each color component is reflected, defining what color the material will have when a full white light shines on it. Note that the ambient color is added to the result of the diffuse, so if you set an ambient value you might have to reduce your diffuse value to compensate for that. Emissive term is the amount of light or the color that is emitted from the object without needing any light, and gives the effect as if the object is lit by its own personal ambient light. Note that emissive does not cast light on other objects in the scene, so if you want a glowing object that casts light on the rest of the scene you will have to add an additional light to the scene. If you want an ambient color in the absence of lights this is the parameter that you need, instead of ambient. Additionally, there is a 'specular' setting that allows to set the specular component of your material. By default it is disabled (0 0 0 0). For these settings (ambient, diffuse and emissive) to work, you need to have 'lighting' on. Also using 'colour_op replace' in a texture unit of your material will disable those settings. For more information about these parameters have a look at the [http://www.ogre3d.org/docs/manual/manual_16.html|material passes section of the manual]. !!!Transparent colour Use a script like this to create a transparent colour material: __Material Template__ {CODE(wrap="1", colors="c++")} material Template/Red50 { technique { pass { scene_blend alpha_blend depth_write off texture_unit { colour_op_ex source1 src_manual src_current 1 0 0 alpha_op_ex source1 src_manual src_current 0.5 } } } } {CODE} The 'colour_op_ex' line is explained above. The 'alpha_op_ex' has the same argument structure, the only difference is that the value at the end specifies the alpha value to use. Here, the colour is being set to 50% transparent. You can experiment with different arguments for scene_blend to give the material different kinds of transparency. !!!Simple texture transition Suppose you have three quads representing a ground plane. Quad A you want to be dirt, Quad B you want to be a combination of dirt and grass, and finally quad C is grass. Here is a simple material for Quad B that does not require external creation of a dirt/grass texture. __Material Requirements__ * the blending file must be in __8 bit__ format (24 bit doesn't work) * be sure that you really save the following images itself (and not the target page) * ''Material_grass.png'' - Source texture {img src="img/wiki_up/Material_grass.png" alt="Material_grass.png"} * ''Material_dirt.jpg'' - Desination texture {img src="img/wiki_up/Material_dirt.jpg" alt="Material_dirt.jpg"} * ''Material_alpha_blend.png'' - A PNG file that has the alpha mask we want to borrow for the transition. {img src="img/wiki_up/Material_alpha_blend.png" alt="Material_alpha_blend.png"} The Rule of blending image: The black (or color) area will be the source texture, and the transparent area will be destination texture __Material Template__ {CODE(wrap="1", colors="c++")} material Template/texture_blend { technique { pass { texture_unit { texture Material_grass.png } texture_unit { texture Material_alpha_blend.png colour_op alpha_blend } texture_unit { texture Material_dirt.jpg colour_op_ex blend_current_alpha src_texture src_current } } } } {CODE} The result {img src="img/wiki_up/Result.jpg" alt="Result.jpg"} __Pro__: Easy, no programming required __Con__: You have to build a new transition material for each transition you would like. __Advanced Tip__: See Alpha Splatting. !!!Dynamic texture transition You can programatically control the amount of grass/dirt shown with some minor adjustments to the above example. __Dynamic Material Template__ {CODE(wrap="1", colors="c++")}material dynamic_texture_blend { technique { pass { texture_unit { texture Material_grass.png } } pass { scene_blend alpha_blend texture_unit { texture Material_alpha_blend.png //The alpha_op for this texunit is programatically set, see below //alpha_op_ex modulate src_manual src_texture SOME_NUMBER } texture_unit { texture Material_dirt.jpg colour_op_ex blend_current_alpha src_texture src_current } } } }{CODE} Then in your source code: {CODE(wrap="1", colors="c++")}Ogre::MaterialPtr materialPtr = Ogre::MaterialManager::getSingleton().getByName("dynamic_texture_blend"); Ogre::TextureUnitState* ptus = materialPtr->getTechnique(0)->getPass(1)->getTextureUnitState(0); //2nd pass, first texture unit ptus->setAlphaOperation(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_TEXTURE, your_transparency_level_varaible_goes_here);{CODE} !!!Stained Glass Window Suppose you have a quad, and you want it to be partially transparent, much like a stained glass window. __Material Requirements__ * image.png - Texture you want to be transparent __Material Template__ {CODE(wrap="1", colors="c++")} material Template/TransparentTexture { technique { pass { lighting off scene_blend alpha_blend depth_write off texture_unit { texture image.png alpha_op_ex source1 src_manual src_texture 0.5 } } } } {CODE} !!!Transparent map Suppose you have a alpha-masked texture of a tree, and you want to apply this texture to a quad. __Material Requirements__ * image.png - An image with an alpha layer that dictates what part of the image should be visible (and by how much) __Material Template__ {CODE(wrap="1", colors="c++")} material cutout { technique { pass { lighting off ambient 1 1 1 1 diffuse 1 1 1 1 specular 0 0 0 0 emissive 0 0 0 scene_blend alpha_blend depth_write off texture_unit { texture image.png tex_coord_set 0 colour_op modulate } } } }{CODE} __Note:__ This material script was produced by the maya6.0e exporter. It may contain more information than is necessary. However, If we use this script for many object nearby each other, we will get problem like this [http://www.ogre3d.org/phpBB2/viewtopic.php?t=12690|Blending scene problem]. So, this is the script we can use if we want to make many object with transparent near each other like a forest. __Material Template__ {CODE(wrap="1", colors="c++")} material cutout { technique { pass { lighting off ambient 1 1 1 1 diffuse 1 1 1 1 specular 0 0 0 0 emissive 0 0 0 // by increase this number, we will get more transparent objects alpha_rejection greater 128 depth_write on texture_unit { texture image.png tex_coord_set 0 colour_op modulate } } } } {CODE} !!!Chroma Key Quoted from OGRE MVP Christian "DWORD" Larsen: "It's not possible really, unless you use shaders or modify your textures dynamically. You're better off saving your textures in a format containing an alpha channel." Quoted from OGRE Project Lead ((User:Sinbad| sinbad)): "Colour keying went out with palettised textures and other dinosaur techniques Wink Alpha channels are in." HAve a look at this[http://www.ogre3d.org/phpBB2/viewtopic.php?p=81973&highlight=#81973|forum thread]. Alternatively, you could use this function: ((Creating transparency based on a key colour in code)) Old Chroma key info below: Suppose you have a texture with no alpha-layer and you want to make all pixels of a given color (be it green, blue, etc) transparent in the texture. __Material Requirements__ * An RGB color - say (0,255,0) for green * image.png - An image with the RGB color applied as the alpha mask __Material Template__ !!!!TO DO !!!Porcelain and Steel shader This shader can be used to made porcelain or steel objects based on environment maps. First step, create an environment map. That is the map that your object will reflect. {img src="img/wiki_up/Boncau_env.jpg" alt="Boncau_env.jpg"} And this is the shader: {CODE(wrap="1", colors="c++")} material porcelainshad { technique { pass { ambient 1.0 1.0 1.0 diffuse 1.0 1.0 1.0 specular 1.0 1.0 1.0 9800.0 emissive 0.0 0.0 0.0 1 texture_unit { texture env_room.jpg color_op_ex blend_manual src_texture src_current 0.1 env_map spherical } } } } {CODE} env_room is the environment map. With {CODE(wrap="1", colors="c++")} specular 1.0 1.0 1.0 9800.0{CODE} we have the Porcelain shader --- And with steel shader, increase the specular {CODE(wrap="1", colors="c++")} specular 0.9 0.9 0.9 10000.0 {CODE} !!!Lightmap A lightmap is just a texture for blending in OGRE. Normally, the lightmap texture is a grayscale texture like this ''lightmap.jpg'' {img src="img/wiki_up/Lmaa.jpg" alt="Lmaa.jpg"} The black area will be the shadowed and in the white are, the obejct will be illuminated. And we have material: ''rock.jpg'' {img src="img/wiki_up/Aaa.jpg" alt="Aaa.jpg"} And we will blend them like this: {CODE(wrap="1", colors="c++")} material rock_lightmap { technique { pass { ambient 1.0 1.0 1.0 diffuse 1.0 1.0 1.0 specular 1.0 1.0 1.0 9800.0 emissive 0.0 0.0 0.0 1 texture_unit { // the detail texture texture rock.jpg // UV set for the detail map (if you have multi-UV) tex_coord_set 0 colour_op modulate } } pass { scene_blend modulate texture_unit { // the lightmap texture lightmap.jpg // UV set for the lightmap (if you have multi-UV) tex_coord_set 0 colour_op modulate } } } } {CODE} And the result of the blending: {img src="img/wiki_up/Untitled-1.jpg" alt="Untitled-1.jpg"} --- Some advanced points: If you create multi UV for model in 3D program, you have to set their UV set in tex_coord_set. You can check the .XML file for more information. ''vertexbuffer positions="true" normals="true" colours_diffuse="true" colours_specular="false" texture_coords="1"'' The .MESH file converted from .XML will contain these information too. !!Intermediate Materials !!!Detail Textures !!!!TO DO !!Advanced Materials !!!Alpha Splatting This technique is used to allow multiple detail textures to be blended over each other according to an alpha map, that determines for each texture, where it is to be put. This is most often used for terrain, where you have many different undergrounds like dirt, walkways, grass, stone etc. This cannot be done with one pre-rendered texture, because it would require too much texture memory or look too coarse grained otherwise. {img src="img/wiki_up/Alpha_blending_done.jpg" alt="Alpha_blending_done.jpg"} __Material Requirements__ * {img src="img/wiki_up/alpha_blending_mesh.gif" alt="alpha_blending_mesh.gif"} A mesh, for instance a landscape * {img src="img/wiki_up/alpha_blending_uvmap.gif" alt="alpha_blending_uvmap.gif"} A uvmap for the mesh * {img src="img/wiki_up/alpha_blending_alpha.gif" alt="alpha_blending_alpha.gif"} A texture containing the alpha map (this texture can contain colour channel informations, but it is ignored) * {img src="img/wiki_up/alpha_blending_texturen.gif" alt="alpha_blending_texturen.gif"} detail textures for each underground type __Material Template__ {CODE(wrap="1", colors="c++")}material Material/SOLID/erdboden.jpg { receive_shadows on technique { // base pass pass { // no lighting lighting off texture_unit { // we use the grass texture as the base, other textures are blended over it texture grass.jpg // scale the texture so, that it looks nice scale 0.1 0.1 // only blend colour // colour_op_ex source1 src_texture src_texture } } // dirt pass pass { // no lighting lighting off // blend with former pass scene_blend alpha_blend // only overwrite fragments with the same depth depth_func equal // alpha map for the dirt texture_unit { texture alpha_dirt.png // use alpha from this texture alpha_op_ex source1 src_texture src_texture // and colour from last pass colour_op_ex source2 src_texture src_texture } // detail texture texture_unit { texture dirt.jpg scale 0.15 0.15 // alpha blend colour with colour from last pass colour_op_ex blend_diffuse_alpha src_texture src_current } } // ... further detail passes like the former one ... // lighting pass pass { ambient 0.5 0.5 0.5 1 diffuse 0.3 0.3 0.3 depth_func equal scene_blend zero src_colour } } }{CODE} !!!((BumpMapping)) First of all, Dot3Bump is a perfect example for bump mapping. Here are my 3 examples files: {CODE(wrap="1", colors="c++")} SolPSDNet.png: color texture{CODE} {img src="img/wiki_up/SolPSDNet.png" alt="SolPSDNet.png"} {CODE(wrap="1", colors="c++")} SolPSDNetbump.png: bump texture{CODE} {img src="img/wiki_up/SolPSDNetbump.png" alt="SolPSDNetbump.png"} {CODE(wrap="1", colors="c++")} SolPSDNetb.png: normal map obtained with bump texture{CODE} {img src="img/wiki_up/SolPSDNetb.png" alt="SolPSDNetb.png"} --- __STEP 1 : Convert bump to normal map__ I only found this for photoshop users. You will need a plug-in for photoshop by Nvidia: 'NVIDIA Normal Map Filter' ([http://www.nvidia.com/|Go here(Nvidia site)] and search for: "Normal Map Filter", you'll find in first results). [http://nifelheim.dyndns.org/~cocidius/normalmap/|GIMP version available here] Load your bump map in photoshop and go to filter/nvidia tool and normal map. Play around with the scale parameter for stronger effects. After conversion, save your file as "SolPSDNetb.png" for example. --- __STEP 2 : Read example-advancedmaterial__ Find example-advanced.material and look for Examples/BumpMapping/MultiLight section. Copy and paste it to your .material file. Here is the code: {CODE(wrap="1", colors="c++")} <pre>material Examples/BumpMapping/MultiLight { // this is the preferred technique which uses both vertex and // fragment programs and supports coloured lights technique { // base ambient pass pass { // base colours, not needed for rendering, but as information // to lighting pass categorisation routine ambient 1 1 1 diffuse 0 0 0 specular 0 0 0 0 // really basic vertex program // NB we don't use fixed functions here because GL does not like // mixing fixed functions and vertex programs (depth fighting can // be an issue) vertex_program_ref Ogre/BasicVertexPrograms/AmbientOneTexture { param_named_auto worldViewProj worldviewproj_matrix param_named_auto ambient ambient_light_colour } } // wow do the lighting pass // NB we don't do decal texture here because this is repeated per light pass { // base colours, not needed for rendering, but as information // to lighting pass categorisation routine ambient 0 0 0 // do this for each light iteration once_per_light scene_blend add // vertex program reference vertex_program_ref Examples/BumpMapVP { param_named_auto lightPosition light_position_object_space 0 param_named_auto worldViewProj worldviewproj_matrix } // fragment program fragment_program_ref Examples/BumpMapFP { param_named_auto lightDiffuse light_diffuse_colour 0 } // base bump map texture_unit { texture NMBumpsOut.png colour_op replace } // normalisation cube map texture_unit { cubic_texture nm.png combinedUVW tex_coord_set 1 tex_address_mode clamp } } // decal pass pass { // base colours, not needed for rendering, but as information // to lighting pass categorisation routine lighting off // really basic vertex program // NB we don't use fixed function here because GL does not like // mixing fixed function and vertex programs (depth fighting can // be an issue) vertex_program_ref Ogre/BasicVertexPrograms/AmbientOneTexture { param_named_auto worldViewProj worldviewproj_matrix param_named ambient float4 1 1 1 1 } scene_blend dest_colour zero texture_unit { texture RustedMetal.jpg } } } // this is the fallback which cards which don't have fragment program // support will use // Note: this still requires vertex program support technique { // base ambient pass pass { // base colours, not needed for rendering, but as information // to lighting pass categorisation routine ambient 1 1 1 diffuse 0 0 0 specular 0 0 0 0 // really basic vertex program // NB we don't use fixed function here because GL does not like // mixing fixed function and vertex programs (depth fighting can // be an issue) vertex_program_ref Ogre/BasicVertexPrograms/AmbientOneTexture { param_named_auto worldViewProj worldviewproj_matrix param_named_auto ambient ambient_light_colour } } // now do the lighting pass // NB we don't do decal texture here because this is repeated per light pass { // base colours, not needed for rendering, but as information // to lighting pass categorisation routine ambient 0 0 0 // do this for each light iteration once_per_light scene_blend add // vertex program reference vertex_program_ref Examples/BumpMapVP { param_named_auto lightPosition light_position_object_space 0 param_named_auto worldViewProj worldviewproj_matrix } // base bump map texture_unit { texture NMBumpsOut.png colour_op replace } // normalisation cube map, with dot product on bump map texture_unit { cubic_texture nm.png combinedUVW tex_coord_set 1 tex_address_mode clamp colour_op_ex dotproduct src_texture src_current colour_op_multipass_fallback dest_colour zero } } // decal pass pass { lighting off // really basic vertex program // NB we don't use fixed function here because GL does not like // mixing fixed function and vertex programs, depth fighting can // be an issue vertex_program_ref Ogre/BasicVertexPrograms/AmbientOneTexture { param_named_auto worldViewProj worldviewproj_matrix param_named ambient float4 1 1 1 1 } scene_blend dest_colour zero texture_unit { texture RustedMetal.jpg } } } }</pre> {CODE} After that, you can find the "header" of CG program in the same example-advanced.material file (only copy CG program header for bump maping): {CODE(wrap="1", colors="c++")} // bump map vertex program, support for this is required vertex_program Examples/BumpMapVP cg { source Example_BumpMapping.cg entry_point main_vp profiles vs_1_1 arbvp1 } // bump map fragment program, support for this is optional fragment_program Examples/BumpMapFP cg { source Example_BumpMapping.cg entry_point main_fp profiles ps_1_1 arbfp1 fp20 } // bump map with specular vertex program, support for this is required vertex_program Examples/BumpMapVPSpecular cg { source Example_BumpMapping.cg entry_point specular_vp profiles vs_1_1 arbvp1 } // bump map fragment program, support for this is optional fragment_program Examples/BumpMapFPSpecular cg { source Example_BumpMapping.cg entry_point specular_fp profiles ps_1_1 arbfp1 fp20 } {CODE} Copy and paste the headers at the beginning of your .material file. --- __STEP 3 : Lat work, include your textures__ You have to replace the following in your .material : RustedMetal.jpg by your color map SolPSDNet.png NMBumpsOut.png by your NORMAL MAP (not bump) SolPSDNetb.png And thats all folks! __Important Hint:__ To use bumpmapping, we'll need tangent vectors for our meshes. OGRE can do this for you ahead of time if you use the '-t' parameter to the command line tools OgreXmlConverter or OgreMeshUpgrade. Otherwise, you can generate them in code like this: {CODE(wrap="1", colors="c++")} // load the meshes with non-default HBU options MeshPtr pMesh = MeshManager::getSingleton().load("Block02.mesh", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY, HardwareBuffer::HBU_STATIC_WRITE_ONLY, true, true); // so we can still read it // build tangent vectors, all our meshes use only one texture coordset // Note: we can build into VES_TANGENT now (SM2+) unsigned short src, dest; if (!pMesh->suggestTangentVectorBuildParams(VES_TANGENT, src, dest)) { pMesh->buildTangentVectors(VES_TANGENT, src, dest); } {CODE} Read also ((BumpMapping)) in this wiki. !!Manual Also have a look in the OGRE manual * [http://www.ogre3d.org/docs/manual/manual_14.html#SEC23|Material Scripts] * [http://www.ogre3d.org/docs/manual/manual_15.html#SEC31|Techniques] * [http://www.ogre3d.org/docs/manual/manual_16.html#SEC38|Passes] * [http://www.ogre3d.org/docs/manual/manual_17.html#SEC74|Texture Units] * [http://www.ogre3d.org/docs/manual/manual_18.html#SEC101|Declaring Vertex and Fragment Programs] * [http://www.ogre3d.org/docs/manual/manual_19.html#SEC110|Cg programs] * [http://www.ogre3d.org/docs/manual/manual_20.html#SEC111|DirectX9 HLSL] * [http://www.ogre3d.org/docs/manual/manual_21.html#SEC112|OpenGL GLSL] * [http://www.ogre3d.org/docs/manual/manual_22.html#SEC118|Unified High-level Programs] * [http://www.ogre3d.org/docs/manual/manual_23.html#SEC119|Using Vertex and Fragment Programs in a Pass] * [http://www.ogre3d.org/docs/manual/manual_24.html#SEC126|Vertex Texture Fetch] * [http://www.ogre3d.org/docs/manual/manual_25.html#SEC132|Script Inheritence] * [http://www.ogre3d.org/docs/manual/manual_26.html#SEC136|Texture Aliases] * [http://www.ogre3d.org/docs/manual/manual_27.html#SEC137|Script Variables] * [http://www.ogre3d.org/docs/manual/manual_28.html#SEC138|Script Import Directive]
Search by Tags
Search Wiki by Freetags
Latest Changes
IDE Eclipse
FMOD SoundManager
HDRlib
Building Ogre V2 with CMake
Ogre 2.1 FAQ
Minimal Ogre Collision
Artifex Terra
OpenMB
Advanced Mogre Framework
MogreSocks
...more
Search
Find
Advanced
Search Help
Online Users
38 online users