标签:cg cg程序
本人学习NVIDIA的C for Graphic (CG)的理论知识已经有2个月了,关于为什么选择CG,这就不说了.目前在国内<Cg教程_可编程实时图形权威指南(扫描清晰版)> , <GPU编程与CG语言之阳春白雪下里巴人>及<[GPU精粹3].(GPU.Gems.3).(美)Hubert.Nguyen.扫描版>都需要看看,也只有这些了,由于国内人对图形编程的误解,国内的GPU编程发展要晚于国外.不过,对于通用高级编程熟悉的我,再去写GPU上的程序确实有些淡淡的忧伤,还好大学的时候学过C,而CG和C很像,至少NVIDIA宣传说很像.
对于高级的语言,一般以main作 为入口,或者设置一个入口.但是很明显,CG程序一般有2个入口,顶点和片段.在C++中可以设.
另外 , 我学习的时候用的是FX Composer 2.5 ,里面也可以设.但是,这里仅仅够学习,测试效果及调试.
/* % Description of my shader. % Second line of description for my shader. keywords: material classic date: YYMMDD */ /* void main_v( float4 position : POSITION , float4 normal : NORMAL, out float4 oPosition : POSITION, out float4 color : COLOR, uniform float4x4 modelViewProj, uniform float4x4 worldMatrix, uniform float4x4 worldMatrix_IT, uniform float3 globalAmbient, uniform float3 lightPosition, uniform float3 lightColor, uniform float3 Kd){ oPosition = mul(modelViewProj , position ); float3 worldPos = mul( worldMatrix , position).xyz; float3 N = mul( worldMatrix_IT,normal).xyz; N = normalize( N ); //计算入射光方向 float3 L = lightPosition - worldPos ; L = normalize( L ); //计算方向光漫反射光强 float3 diffuseColor = Kd * lightColor*max(dot(N,L),0); //计算环境光漫发射光强 float3 ambientColor = Kd * globalAmbient; color = diffuseColor+ambientColor; color.w = 1; } */ float4x4 WorldViewProj : WorldViewProjection; texture diffuse < string ResourceName = "";//Optional default file name string UIName = "diffuse Texture"; string ResourceType = "2D"; >; sampler2D diffuseSampler = sampler_state { Texture = <diffuse>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = Wrap; AddressV = Wrap; }; struct VS_OUTPUT { float4 pos : POSITION; float2 tex : TEXCOORD0; }; VS_OUTPUT mainVS(float4 pos : POSITION,float2 tex : TEXCOORD0){ VS_OUTPUT vsOut; vsOut.pos = mul(pos,WorldViewProj); vsOut.tex = tex; return vsOut; } float4 mainPS(float2 tex : TEXCOORD0) : COLOR { return tex2D(diffuseSampler, tex); } technique technique0 { pass p0 { VertexShader = compile vs_3_0 mainVS(); PixelShader = compile ps_3_0 mainPS(); } } /** struct VertexIn { float4 position : POSITION; float4 normal : NORMAL; }; struct VertexScreen { float4 oPosition : POSITION; float4 color : COLOR; }; void main_v( VertexIn posIn , out VertexScreen posOut, uniform float4x4 modelViewProj, uniform float4x4 worldMatrix, uniform float4x4 worldMatrix_IT, uniform float3 globalAmbient, uniform float3 lightPosition, uniform float3 lightColor, uniform float3 Kd) { posOut.oPosition = mul( modelViewProj , posIn.position); float3 worldPos = mul( worldMatrix , posIn.position).xyz; float3 N = mul( worldMatrix_IT,posIn.normal).xyz; N = normalize(N); float3 L = lightPosition - worldPos; L = normalize(L); float3 diffuseColor = Kd * lightColor*max( dot(N,L) , 0 ); float3 ambientColor = Kd*globalAmbient; posOut.color.xyz = diffuseColor + ambientColor; posOut.color.w = 1; } */
注 : 程序内容出自 : <GPU编程与CG语言之阳春白雪下里巴人> 及 http://www.shangxueba.com/jingyan/85012.html.本人目前菜鸟,自然写不出如此NB的代码.
另外 : Material用的是FX Composer自带的
供各位CG爱好者交流,希望各位大神给予学习建议.
本文出自 “Better_Power_Wisdom” 博客,请务必保留此出处http://aonaufly.blog.51cto.com/3554853/1547532
标签:cg cg程序
原文地址:http://aonaufly.blog.51cto.com/3554853/1547532