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: HLSL
View page
Source of version: 7
(current)
__HLSL__ - High Level Shader Language. This is a shader language developed by Microsoft for use with ((-DirectX|DirectX)) and is very similar to ((-Cg|Cg)). {WIKIPEDIA()/} !!Using HLSL in OGRE HLSL shaders are used inside Ogre much like the other shading languages as Cg and ((-GLSL|GLSL)), but there are some little differences that I will try to explain here. First, the material definition will be much like this: {CODE(wrap="1", colors="c++")}material Test13/RockWall { technique { pass { vertex_program_ref Deferred_nm_vs { } fragment_program_ref Deferred_nm_ps { } texture_unit { // sampler s0 // ... } texture_unit { // sampler s1 // ... } } } }{CODE} __vertex_program_ref__ refers to a vertex program definition and __fragment_program_ref__ refers to a fragment program definition. Those can be defined in __.program__ scripts or inside the __.material__ script itself. These define the name of the program text file, the destination profile (vs/ps version) and default parameters. {CODE(wrap="1", colors="c++")}vertex_program Deferred_vs hlsl { source Deferred_vs.hlsl target vs_1_1 entry_point main default_params { param_named_auto worldView worldview_matrix param_named_auto worldViewProj worldviewproj_matrix } } fragment_program Deferred_ps hlsl { source Deferred_ps.hlsl target ps_2_0 entry_point main default_params { param_named specularity float 0.0 } }{CODE} * __source__ defines the program source code name. * __entry_point__ is the name of the shader function that is called for each vertex/fragment. This is usually __main__. * __target__ signifies the Vertex Shaders or Pixel Shaders version to compile for. For fragment_program this can be ps_1_1, ps_1_4, ps_2_0, ps_2_x, ps_3_0 or ps_3_x (for more information have a look at [http://www.ogre3d.org/docs/manual/manual_18.html#SEC89|Declaring Vertex and Fragment Programs]. For vertex_program it can be vs_1_1, vs_2_0, vs_2_x or vs_3_0. Always try to use the lowest version that can do what you need, as this is usually faster and supports more cards. * __default_params__ define default parameters for this shader. These can be overidden in the __vertex_program_ref__ and __fragment_program_ref__ blocks. !!Parameters Named parameters can be defined at global scope in the HLSL program: {CODE(wrap="1", colors="c++")}float4x4 worldViewProj; float4x4 world; float4x4 worldView;{CODE} Ogre will make sure these parameters will contain the values assigned to them via __param_named__ or __param_named_auto__ in the program or material script. !!Samplers and texture units Inside .hlsl fragment programs, you can define samplers using a special syntax, so that Ogre knows which texture unit the sampler refers to: {CODE(wrap="1", colors="c++")}sampler Tex0: register(s0); sampler Tex1: register(s1);{CODE} __register(s0)__ refers to the first __texture_unit__ block, __register(s1)__ to the second one and so on. !!Vertex shaders A minimal vertex shader that receives the POSITION, NORMAL and TEXCOORD0 vertex attributes and outputs POSITION and TEXCOORD0 to the fragment shader would look like this: {CODE(wrap="1", colors="c++")}struct VS_OUTPUT { float4 pos: POSITION; float2 texCoord0: TEXCOORD0; }; float4x4 worldViewProj; VS_OUTPUT main( float4 Pos: POSITION, float3 normal: NORMAL, float2 texCoord0: TEXCOORD0 ){ VS_OUTPUT Out; Out.pos = mul(worldViewProj, Pos); Out.texCoord0 = texCoord0; return Out; }{CODE} !!Fragment shaders A minimal fragment shader that samples a texture at the incoming texture coordinate and outputs the sampled COLOR: {CODE(wrap="1", colors="c++")}sampler Tex0: register(s0); float4 main(float4 texCoord0: TEXCOORD0): COLOR0 { return tex2D(Tex0, texCoord0); }{CODE} !!Tangent vectors If you need tangent vectors in your HLSL shaders, you can ask OGRE to provide them. However, you will have to use the semantic TEXCOORD for that. So the input structure for your vertex shader may look like this: {CODE(wrap="1", colors="c++")}struct a2v { float4 position : POSITION0; float3 normal : NORMAL; float2 tex : TEXCOORD0; float3 tangent : TEXCOORD1; };{CODE} To make sure OGRE can give you these tangents, either tell the exporter you use for your models to generate them, or tell OGRE to generate them by using the following code: {CODE(wrap="1", colors="c++")}unsigned short src, dest; if (!pMesh->suggestTangentVectorBuildParams(Ogre::VES_TANGENT, src, dest)) { pMesh->buildTangentVectors(Ogre::VES_TANGENT, src, dest); }{CODE} __Note:__ * Since OGRE [http://1.6.0|1.6.0] this is done automatically. * OGRE does not generate any binormals. If you need them, you can calculate them yourself by simply taking the cross product of the tangent and the normal in the vertex shader. !!Links [http://www.ogre3d.org/docs/manual/manual_20.html#SEC76|OGRE manual: Declaring Vertex and Fragment programs] [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/hlslreference/hlslreference.asp|Microsoft HLSL reference]
Search by Tags
Search Wiki by Freetags
Latest Changes
Minimal Ogre Collision
Artifex Terra
OpenMB
Advanced Mogre Framework
MogreSocks
Critter AI
Mogre Add-ons
MOGRE
Mogre MyGUI wrapper
MOGRE Editable Terrain Manager
...more
Search
Find
Advanced
Search Help
Online Users
21 online users