第七章 OGRE中的材质以及shader

#include "ExampleApplication.h"

class Example:public ExampleApplication
{
	void createScene()
	{
		//1 begin
		//Ogre::ManualObject* manual=mSceneMgr->createManualObject("Quad");
		//1.1 begin
		//manual->begin("BaseWhiteNoLighting",RenderOperation::OT_TRIANGLE_LIST);
		//1.1 end
		//2_1|1.1 begin
		//manual->begin("MyMaterial1",RenderOperation::OT_TRIANGLE_LIST);
		//2_1|1.1 end
		//3|2 begin
		//manual->begin("MyMaterial2",RenderOperation::OT_TRIANGLE_LIST);
		//3|2 end
		//5_4|2 begin
		//manual->begin("MyMaterial3",RenderOperation::OT_TRIANGLE_LIST);
		//5_4|2 end
		//6_4|5 begin
		//manual->begin("MyMaterial4",RenderOperation::OT_TRIANGLE_LIST);
		//6_4|5 end
		//7_4|6 begin
		//manual->begin("MyMaterial5",RenderOperation::OT_TRIANGLE_LIST);
		//7_4|6 end
		//8_4|7 begin
		//manual->begin("MyMaterial6",RenderOperation::OT_TRIANGLE_LIST);
		//8_4|7 end
		//9_4|8 begin
		//manual->begin("MyMaterial7",RenderOperation::OT_TRIANGLE_LIST);
		//9_4|8 end
		//10|9&4 begin
		//10.1 begin
		//manual->begin("MyMaterial8",RenderOperation::OT_TRIANGLE_LIST);
		//10.1 end
		//11_10|10.1 begin
/*
		
		manual->begin("MyMaterial9",RenderOperation::OT_TRIANGLE_LIST);
		//11_10|10.1 end
		manual->position(5.0,0.0,0.0);
		manual->textureCoord(0,0.2);
		manual->position(-5.0,10.0,0.0);
		manual->textureCoord(0.2,0);
		manual->position(-5.0,0.0,0.0);
		manual->textureCoord(0.2,0.2);
		manual->position(5.0,10.0,0.0);
		manual->textureCoord(0,0);*/

		//10|9&4 end
		//12_1.2|10 begin
		//manual->begin("MyMaterial11",RenderOperation::OT_TRIANGLE_LIST);
		//12_1.2|10 end
		//13_1.2|12 begin
		//manual->begin("MyMaterial12",RenderOperation::OT_TRIANGLE_LIST);
		//13_1.2|12 end
		//14_1.2|13 begin
		//manual->begin("MyMaterial13",RenderOperation::OT_TRIANGLE_LIST);
		//14_1.2|13 end
		//15_1.2|14 begin
		//manual->begin("MyMaterial14",RenderOperation::OT_TRIANGLE_LIST);
		//15_1.2|14 end
		//16|1.2&14 begin
/*
		manual->begin("MyMaterial15",RenderOperation::OT_TRIANGLE_LIST);
		manual->position(5.0,0.0,0.0);
		manual->colour(1,0,0);
		manual->position(-5.0,10.0,0.0);
		manual->colour(0,1,0);
		manual->position(-5.0,0.0,0.0);
		manual->colour(1,1,0);
		manual->position(5.0,10.0,0.0);
		manual->colour(0,0,1);*/

		//16|1.2&14 end


		//1.2 begin
/*
		manual->position(5.0,0.0,0.0);
		manual->textureCoord(0,1);
		manual->position(-5.0,10.0,0.0);
		manual->textureCoord(1,0);
		manual->position(-5.0,0.0,0.0);
		manual->textureCoord(1,1);
		manual->position(5.0,10.0,0.0);
		manual->textureCoord(0,0);*/



		//1.2 end
		//4_2|1.2 begin
		/*
		manual->position(5.0,0.0,0.0);
				manual->textureCoord(0,2);
				manual->position(-5.0,10.0,0.0);
				manual->textureCoord(2,0);
				manual->position(-5.0,0.0,0.0);
				manual->textureCoord(2,2);
				manual->position(5.0,10.0,0.0);
				manual->textureCoord(0,0);*/
		
		//4_2|1.2 end
/*
		manual->index(0);
		manual->index(1);
		manual->index(2);
		manual->index(0);
		manual->index(3);
		manual->index(1);

		manual->end();
		manual->convertToMesh("Quad");

		Ogre::Entity* ent=mSceneMgr->createEntity("Quad");
		Ogre::SceneNode* node=mSceneMgr->getRootSceneNode()->createChildSceneNode("Node1");
		node->attachObject(ent);*/

		//1 end
		//16|all begin
		Ogre::SceneNode* node=mSceneMgr->getRootSceneNode()->createChildSceneNode("Node1");
		Ogre::Entity* ent=mSceneMgr->createEntity("Entity1","sinbad.mesh");
		//16.1 begin
		ent->setMaterialName("MyMaterial14");
		//16.1 end
		//17_16|16.1 begin
		//ent->setMaterialName("MyMaterial17");
		//17_16|16.1 end
		node->attachObject(ent);
		//16|all end

	}
};
int main()
{
	Example app;
	app.go();
	return 0;
}


Seamanj_Ogre3DBeginnerGuide.material文件


material MyMaterial1
{
	technique
	{
		pass
		{
			texture_unit
			{
				texture leaf.png
			}
		}
	}
}
material MyMaterial2
{
	technique
	{
		pass
		{
			texture_unit
			{
				texture Water02.jpg
			}
		}
	}
}
material MyMaterial3
{
	technique
	{
		pass
		{
			texture_unit
			{
				texture terr_rock6.jpg
			}
		}
	}
}
material MyMaterial4
{
	technique
	{
		pass
		{
			texture_unit
			{
				texture terr_rock6.jpg
				tex_address_mode clamp
			}
		}
	}
}
material MyMaterial5
{
	technique
	{
		pass
		{
			texture_unit
			{
				texture leaf.png
				tex_address_mode mirror
			}
		}
	}
}
material MyMaterial6
{
	technique
	{
		pass
		{
			texture_unit
			{
				texture leaf.png
				tex_address_mode border
			}
		}
	}
}
material MyMaterial7
{
	technique
	{
		pass
		{
			texture_unit
			{
				texture leaf.png
				tex_address_mode border
				tex_border_colour 0.0 0.0 1.0
			}
		}
	}
}
material MyMaterial8
{
	technique
	{
		pass
		{
			texture_unit
			{
				texture terr_rock6.jpg
				//11 bgein
				//scroll 0.8 0.8
				//11 end
			}
		}
	}
}
material MyMaterial9
{
	technique
	{
		pass
		{
			texture_unit
			{
				texture terr_rock6.jpg
				scroll_anim 0.01 0.01
			}
		}
	}
}
material MyMaterial11
{
	technique
	{
		pass
		{
			texture_unit texture1
			{
				texture terr_rock6.jpg
				rotate_anim 0.1
			}
		}
		
	}
}
material MyMaterial12:MyMaterial11
{
	set_texture_alias texture1 Water02.jpg
}


/*
Ogre 3D needs five pieces of information about the shader:
1.The name of the shader
2.In which language it is written
3.In which source file it is stored
4.How the main function of this shader is called
5.In what profile we want the shader to be compiled

all this information should be in the material file
*/

fragment_program MyFragmentShader1 cg
{
	source Seamanj_Ogre3DBeginnersGuideShaders.cg
	entry_point MyFragmentShader1
	profiles ps_1_1 arbfp1
}
vertex_program MyVertexShader1 cg
{
	source Seamanj_Ogre3DBeginnersGuideShaders.cg
	entry_point MyVertexShader1
	profiles vs_1_1 arbvp1
	default_params
	{
		param_named_auto worldViewMatrix worldviewproj_matrix
	}
}
material MyMaterial13
{
	technique
	{
		pass
		{
			vertex_program_ref MyVertexShader1
			{
			
			}
			fragment_program_ref MyFragmentShader1
			{
			
			}
		}
	}
}

fragment_program MyFragmentShader2 cg
{
	source Seamanj_Ogre3DBeginnersGuideShaders.cg
	entry_point MyFragmentShader2
	profiles ps_2_x arbfp1				//一个x的大小写浪费了我N个小时啊!~~

}
vertex_program MyVertexShader2 cg
{
	source Seamanj_Ogre3DBeginnersGuideShaders.cg
	entry_point MyVertexShader2
	profiles vs_1_1 arbvp1
	default_params
	{
		param_named_auto worldViewMatrix worldviewproj_matrix
	}
}

material MyMaterial14
{
	technique
	{
		pass
		{

			vertex_program_ref MyVertexShader2
			{
				
			}
			fragment_program_ref MyFragmentShader2
			{
			
			}
			texture_unit
			{
				texture terr_rock6.jpg//第0纹理单元
			}
			texture_unit
			{
				texture leaf.png//第1纹理单元
			}
		}
	}
}

vertex_program MyVertexShader5 cg
{
	source Seamanj_Ogre3DBeginnersGuideShaders.cg
	entry_point MyVertexShader5
	profiles vs_1_1 arbvp1
	default_params
	{
		param_named_auto worldViewMatrix worldviewproj_matrix
		param_named_auto pulseTime time
	}
}

material MyMaterial17
{
	technique
	{
		pass
		{
			vertex_program_ref MyVertexShader5
			{
			
			}
			fragment_program_ref MyFragmentShader2
			{
				
			}
			texture_unit
			{
				texture terr_rock6.jpg
			}
		}
	}
}

Seamanj_OgreBeginnersGuideShader.cg文件

void MyFragmentShader1(out float4 color:COLOR)
{
	color=float4(0,0,1,0);
}
void MyVertexShader1(float4 position:POSITION,out float4 oPosition:POSITION,uniform float4x4 worldViewMatrix)
{
	oPosition=mul(worldViewMatrix,position);
}



//
//sampler2D texture : TEXUNIT1;//这是第六种方法
//sampler2D texture ;//第七种,不指定默认为第0纹理

//sampler texture: register(s1);//这是第一种方法传入sampler
//sampler2D texture:register(s1);//这是第二种方法
void MyFragmentShader2(
float2 uv			: TEXCOORD0,
out float4 color		: COLOR

//,uniform sampler2D texture:register(s1)//这是第三种方法
//,uniform sampler texture:register(s1)//这是第四种方法
,uniform sampler texture//这是第五种方法,默认取第0纹理,第0纹理和第1纹理的指定通过material文件指定多个texture_unit
//,uniform sampler texture:TEXUNIT1//第八种
)
{
	color=tex2D(texture,uv);
}

/*另外也可以通过GL程序来指定


glBindTexture(GL_TEXTURE_2D, 666);
...

myCgFragmentParam_decal =
    cgGetNamedParameter(myCgFragmentProgram, "decal");
  checkForCgError("getting decal parameter");

  cgGLSetTextureParameter(myCgFragmentParam_decal, 666);
  checkForCgError("setting decal 2D texture");
  



C3E3f_Output C3E3f_texture(float2 texCoord : TEXCOORD0,
                           uniform sampler2D decal : TEX0)
{
  C3E3f_Output OUT;
  OUT.color = tex2D(decal,texCoord);
  return OUT;
}  
 
但是好像不能通过在脚本
material MyMaterial14
{
	technique
	{
		pass
		{

			vertex_program_ref MyVertexShader2
			{
				
			}
			fragment_program_ref MyFragmentShader2
			{
				param_named texture int 1

			}
来实现


 貌似glsl里面是这样的,以后再研究吧
 
*/

/


void MyVertexShader2(float4 position:POSITION,out float4 oPosition:POSITION,float2 uv:TEXCOORD0,out float2 

oUv:TEXCOORD0,uniform float4x4 worldViewMatrix)
{
	oPosition=mul(worldViewMatrix,position);
	oUv=uv;
}


void MyVertexShader5(uniform float pulseTime,
	float4 position:POSITION,
	out float4 oPosition:POSITION,
	float2 uv:TEXCOORD0,
	out float2 oUv:TEXCOORD0,
	uniform float4x4 worldViewMatrix)
{
	oPosition=mul(worldViewMatrix,position);
	oPosition.x*=(2+sin(pulseTime));
	oUv=uv;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值