opengles实现卡通效果

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hb707934728/article/details/72883900



核心shader实现

			const char* vs =
			{
				"attribute vec3 inVertex;"
				"attribute vec3 inNormal;"//法线

				"uniform mat4 MVPMatrix;"
				"uniform vec3 LightDirection;"//光的方向
				"uniform vec3 EyePosition;"

				"varying vec2 TexCoord;"

				"void main()"
				"{"
				"gl_Position=MVPMatrix*vec4(inVertex,1.0);"
				"vec3 eyeDirection=normalize(EyePosition-inVertex);"

				"TexCoord.x=dot(LightDirection,inNormal);"//dot(x, y): 点积,各分量分别相乘 后 相加
				"TexCoord.y=dot(eyeDirection,inNormal);"
				"}"
			};

			const char* ps =
			{
				"precision lowp float;"
				"uniform sampler2D sTexture;"

				"varying vec2 TexCoord;"
				"const vec3 cBaseColor=vec3(1,1,1);"
				"void main()"
				"{"

				"gl_FragColor=vec4(cBaseColor*texture2D(sTexture, TexCoord).rgb,1.0);"

				"}"
			};
渲染函数

        virtual void    onRender(Program_CELL& shader,CELL3RDCamera& camera)
        {
			static float angle = 0;

			matrix4 matRot(1);
			matRot.rotateY(angle);

			matrix4 matTrans;
			matTrans.translate(0,5,0);

			matrix4 matScale(1);
			matScale.scale(0.3f,0.3f,0.3f);

			//总的模型变换矩阵
			matrix4 matModel = matTrans*matRot*matScale;

			angle += 1.0f;

			//观察点坐标  也就是眼睛的位置
			float4 vMsEyePos = float4(0, 0, 125, 1)*matModel;
			_device.setUniform3fv(shader.EyePosition,1,&vMsEyePos.x);

			//从世界空间转换方向光到模型空间
			float4 vMsLightDir = float4(1, 2, 1, 0)*matModel;//光的入射方向
			//归一化
			float3 vMsLightDirS = normalize(float3(vMsLightDir.x,vMsLightDir.y,vMsLightDir.z));
			_device.setUniform3fv(shader.LightDirection,1,&vMsLightDirS.x);

			//光源位置
			float4 vMsLightPos = float4(50,20,40,1);
			vMsLightPos = vMsLightPos*matModel;
			matrix4 MVP = camera._matProj*camera._matView*matModel;

			_device.setUniformMatrix4fv(shader.MVPMatrix,1,false,MVP.data());

			VertexBumpMap* vert = &_arVertexs.front();
			_device.attributePointer(shader.inVertex,3,GL_FLOAT,GL_FALSE,sizeof(VertexBumpMap),vert);
			_device.attributePointer(shader.inNormal, 3, GL_FLOAT, GL_FALSE, sizeof(VertexBumpMap), &vert[0]._normal);

			_device.drawElements(GL_TRIANGLES,_arFaces.size()*3,GL_UNSIGNED_SHORT,&_arFaces.front());
        }



展开阅读全文

没有更多推荐了,返回首页