## GAIA LABORATORY FOR GRAPHICS

Department of Game and Multimedia Engineering

# openGL CG 系列教程4 - Lighting + Texture

*原创文章，转载请注明出处*

openGL CG 系列教程04 – Lighting + Texture

 float2 oTexCoord: TEXCOORD0

 struct output { float4 position  : POSITION; float2 TexCoord  : TEXCOORD0;     float3 objectPos : TEXCOORD1;    float3 normal    : TEXCOORD2; };

 uniform sampler2D Texture

C= Ct Cl

A= At Al

 Lighting + Texture = Lighting+Texture

04vs.cg

 struct output {       float4 position : POSITION;           float3 objectPos: TEXCOORD1;          float3 normal   : TEXCOORD2;       float2 TexCoord : TEXCOORD0; };   output vs_main( float4 position : POSITION,                   float3 normal   : NORMAL,                   float2 oTexCoord: TEXCOORD0,                   uniform float4x4 MV,                   uniform float4x4 MVP                  ) {       output OUT;         OUT.position = mul(MVP, position);       OUT.objectPos = mul(MV, position).xyz;       OUT.normal = mul(MV, float4(normal,0.0)).xyz;       OUT.TexCoord = oTexCoord;         return OUT; }

04fs.cg

 uniform float3 LightPosition; uniform float3 eyePosition; uniform float3 I; uniform float3 Ka; uniform float3 Kd; uniform float3 Ks; uniform float shininess;   struct input {       float3 objectPos: TEXCOORD1;          float3 normal   : TEXCOORD2;       float2 texCoord : TEXCOORD0; };   struct output {       float4 color     : COLOR; };   output fs_main( in input IN,                   uniform sampler2D Texture) {       output OUT;         float3 N = normalize(IN.normal);       float3 P = IN.objectPos;         float3 L = normalize(LightPosition - P);       float NdotL = max(dot(N,L),0);         float3 ambient = Ka * I;       float3 diffuse = Kd * I * NdotL;         float3 V = normalize(eyePosition - P);       float3 H = normalize(L+V);       float NdotH = pow(max(dot(N,H), 0), shininess);         if(NdotL<=0)            NdotH = 0.0;       float3 specular = Ks*I*NdotH;         float3 tex = tex2D(Texture, IN.texCoord).xyz;       float3 light = ambient + diffuse + specular;       OUT.color.xyz= light * tex;       OUT.color.w = 1.0;         return OUT; }

*原创文章，转载请注明出处*

#### OpenGL&CG技术之Render To Texture

2015-12-26 14:21:02

#### openGL CG 系列教程3-Pixel Lighting

2010-04-30 18:36:00

#### Cg per-vertex lighting with texture

2015-10-16 11:30:57

#### Cg per-pixel lighting with texture

2015-10-16 19:43:06

#### openGL CG 系列教程2 - Vertex Lighting

2010-04-28 22:54:00

#### 【转】OpenGL CG系列教程2,vertex lighting

2013-08-26 11:31:29

#### OpenGL学习：2D texture

2017-08-24 15:27:46

#### openGL CG4 - Lighting + Texture

2013-04-27 12:32:11

#### OpenGL系列教程之四：OpenGL 变换

2013-10-22 11:18:13

#### 【GPU编程】The Cg Runtime:OpenGL中调用Cg程序

2012-01-10 14:54:49