最近开始学CG,在OGRE官网中的shader有一些教程。看完Getting Started With Ogre CG Materials后能简单的写一些CG程序。
首先设置材质
// CG Vertex shader definition vertex_program PlainTexture_VS cg { // Look in this source file for shader code source GameObjStandard.cg // Use this function for the vertex shader entry_point main_plain_texture_vp // Compile the shader to vs_1_1 format profiles vs_1_1 // This block saves us from manually setting parameters in code default_params { // Ogre will put the worldviewproj into our 'worldViewProj' parameter for us. param_named_auto worldViewProj worldviewproj_matrix // Note that 'worldViewProj' is a parameter in the cg code. } } // CG Pixel shader definition fragment_program PlainTexture_PS cg { // Look in this source file for shader code source GameObjStandard.cg // Use this function for the pixel shader entry_point main_plain_texture_fp // Compile to ps_1_1 format profiles ps_1_1 } material PlainTexture { // Material has one technique technique { // This technique has one pass pass { // Make this pass use the vertex shader defined above vertex_program_ref PlainTexture_VS { } // Make this pass use the pixel shader defined above fragment_program_ref PlainTexture_PS { } texture_unit { // This pass will use this 2D texture as its input texture Ten.png 2d } } } }
上面材质设完之后开始CG程序
void main_plain_texture_vp( // Vertex Inputs float4 position : POSITION, // Vertex position in model space float2 texCoord0 : TEXCOORD0, // Texture UV set 0 // Outputs out float4 oPosition : POSITION, // Transformed vertex position out float2 uv0 : TEXCOORD0, // UV0 // Model Level Inputs uniform float4x4 worldViewProj) { // Calculate output position oPosition = mul(worldViewProj, position); // Simply copy the input vertex UV to the output uv0 = texCoord0; } void main_plain_texture_fp( // Pixel Inputs float2 uv0 : TEXCOORD0, // UV interpolated for current pixel // Outputs out float4 color : COLOR, // Output color we want to write // Model Level Inputs uniform sampler2D texture) // Texture we're going to use { // Just sample texture using supplied UV color = tex2D(texture, uv0); }The keyword uniform identifies the parameters you will have to supply in the material file.
- Some parameters can be supplied by the Ogre Engine (param_named_auto), like a light position.
- Some parameters can be supplied by your material file (param_named) like a texture map.
- Some parameters can be supplied by your application (custom).
CG中 关键字 uniform 需要在材质中提供 或者在材质中设置param_named_auto colorModulate custom 1索引
然后在 ogre 中设置参数setCustomParameter(1, value)
编译CG程序
设置cgc的工作路径
cd "C:\Path\to\my\cg\file"
然后
cgc -entry yourMainFP -profile ps_2_0 yourFile.cg
or
cgc -entry yourMainVP -profile vs_1_1 yourFile.cg