聚光灯原理:(可想象成探照灯)
运行效果图
片元shader
uniform vec4 U_LightPos;//光源位置
uniform vec3 U_EyePos;//眼睛的位置
uniform vec4 U_LightDirection;// 聚光灯轴线也就是中心线的方向
uniform float U_Cutoff;//中心线和照射方向的最大夹角
uniform vec4 U_AmbientLightColor;//环境光颜色
uniform vec4 U_AmbientMaterial;//环境光材质
uniform vec4 U_DiffuseLightColor;//漫反射光颜色
uniform vec4 U_DiffuseMaterial;//漫反射光材质
uniform vec4 U_SpecularLightColor;//镜面光颜色
uniform vec4 U_SpecularMaterial;//镜面光材质
varying vec3 V_Normal; //转换到世界空间的法线坐标
varying vec3 V_WorldPos;//转换到世界空间的顶点坐标
void main()
{
//将角度转弧度
float radianCutoff=U_Cutoff*3.14/180.0;
//计算夹角的余弦值
float cosThta=cos(radianCutoff);
//将聚光灯的轴线向量归一化
vec3 spotLightDirection=normalize(U_LightDirection.xyz);
//--计算环境光
vec4 ambientColor=U_AmbientLightColor*U_AmbientMaterial;
//--计算漫反射光
vec3 L=vec3(0.0);
float distance=0.0;
float attenuation=1.0;
float constantFactor=0.5;
float linearFactor=0.3;
float expFactor=0.1;
if(U_LightPos.w==0.0)//如果是方向光
{
L=U_LightPos.xyz;
}
else
{
//计算点光源的入射方向,也就是光线的照射方向
//因为点光源光线是发散的,所以必须计算入射光线,这点有别于平行光
L=U_LightPos.xyz-V_WorldPos;
//计算被照射点到光源的距离,点光源会随着距离的增加发生衰减,所以距离的计算也是必须的
distance=length(L);
//根据公式计算点光源的衰减因子
attenuation=1.0/(constantFactor+linearFactor*distance+expFactor*distance*distance);
}
//入射光线归一化
L=normalize(-L);
//法向量归一化
vec3 n=normalize(V_Normal);
float diffuseIntensity=0.0;
//这步很重要,计算聚光灯的中心线和入射光线的夹角
float currentCosThta=max(0.0,dot(L,spotLightDirection));
if(currentCosThta>cosThta)//超过规定夹角,就不能被照射到
{
diffuseIntensity=0.8;
}
//计算最终漫反射光
vec4 diffuseColor=U_DiffuseLightColor*U_DiffuseMaterial*diffuseIntensity*attenuation;
//--根据blin-phone模型计算镜面反射光
//--光线的反射方向和眼睛观察物体的方向重合,物体的被观察点就会产生亮斑,这就是镜面光的原理
//因此必须计算眼睛到被观察点的向量
vec3 viewDir=U_EyePos-V_WorldPos;
viewDir=normalize(viewDir);
//计算反射光
vec3 halfVector=L+viewDir;
halfVector=normalize(halfVector);
//根据漫反射光的强度做过滤,减少镜面光的计算量
float specularIntensity=0.0;
if(diffuseIntensity==0.0)
{
specularIntensity=0.0;
}
else
{
specularIntensity=pow(max(0.0,dot(n,halfVector)),128.0);
}
vec4 specularColor=U_SpecularLightColor*U_SpecularMaterial*specularIntensity*attenuation;
gl_FragColor=ambientColor+diffuseColor;//+specularColor;
}
attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;
uniform mat4 M;
uniform mat4 P;
uniform mat4 V;
uniform mat4 NM;
varying vec3 V_Normal;
varying vec3 V_WorldPos;
void main()
{
V_Normal=mat3(NM)*normal;//将法线向量转换到世界空间
vec4 worldPos=M*vec4(pos,1.0);
V_WorldPos=worldPos.xyz;
gl_Position=P*V*worldPos;
}
#include
#include "glew.h"
#include
#include
#include "utils.h"
#include "GPUProgram.h"
#include "ObjModel.h"
#include "FBO.h"
#include "FullScreenQuad.h"
#include "Glm/glm.hpp"
#include "Glm/ext.hpp"
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glew32.lib")
LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
WNDCLASSEX wndClass;
wndClass.cbClsExtra = 0;
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.cbWndExtra = 0;
wndClass.hbrBackground = NULL;
wndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndClass.hIcon = NULL;
wndClass.hIconSm = NULL;
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc=GLWindowProc;
wndClass.lpszClassName = L"OpenGL";
wndClass.lpszMenuName = NULL;
wndClass.style = CS_VREDRAW | CS_HREDRAW;
ATOM atom = RegisterClassEx(&wndClass);
HWND hwnd = CreateWindowEx(NULL, L"OpenGL", L"RenderWindow", WS_OVERLAPPEDWINDOW, 100, 100, 800, 600, NULL, NULL, hInstance, NULL);
HDC dc = GetDC(hwnd);
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA | PFD_DOUBLEBUFFER;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
int pixelFormatID = ChoosePixelFormat(dc, &pfd);
SetPixelFormat(dc,pixelFormatID,&pfd);
HGLRC rc = wglCreateContext(dc);
wglMakeCurrent(dc, rc);
glewInit();
//init gpu program
GPUProgram gpuProgram;
gpuProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/SpotLight.vs");
gpuProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/SpotLight.fs");
gpuProgram.Link();
gpuProgram.DetectAttribute("pos");
gpuProgram.DetectAttribute("texcoord");
gpuProgram.DetectAttribute("normal");
gpuProgram.DetectUniform("M");
gpuProgram.DetectUniform("V");
gpuProgram.DetectUniform("P");
gpuProgram.DetectUniform("NM");
gpuProgram.DetectUniform("U_AmbientLightColor");
gpuProgram.DetectUniform("U_AmbientMaterial");
gpuProgram.DetectUniform("U_DiffuseLightColor");
gpuProgram.DetectUniform("U_DiffuseMaterial");
gpuProgram.DetectUniform("U_SpecularLightColor");
gpuProgram.DetectUniform("U_SpecularMaterial");
gpuProgram.DetectUniform("U_LightPos");
gpuProgram.DetectUniform("U_EyePos");
gpuProgram.DetectUniform("U_LightDirection");
gpuProgram.DetectUniform("U_Cutoff");
//init 3d model
ObjModel model;
model.Init("Debug/res/model/Quad.obj");
float identity[] = {
1.0f,0,0,0,
0,1.0f,0,0,
0,0,1.0f,0,
0,0,0,1.0f
};
float ambientLightColor[] = { 0.2f,0.2f,0.2f,1.0f };
float ambientMaterial[] = { 0.2f,0.2f,0.2f,1.0f };
float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
float diffuseMaterial[] = { 0.6f,0.6f,0.6f,1.0f };
float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
float specularMaterial[] = { 1.0f,1.0f,1.0f,1.0f };
float lightPos[] = { 0.0f,1.5f,-3.0f,1.0f };//光源位置
float spotLightDirection[] = { 0.0f,-1.0f,0.0f,1.0f };//聚光灯的照射方向,也就是聚光灯的轴线,和被照射面垂直
float spotLightCutoff = 15.0f;//聚光灯轴线和聚光灯照射方向的最大夹角,超过这个夹角就没有光线
float eyePos[] = { 0.0f,0.0f,0.0f };//眼睛的位置
glm::mat4 modelMatrix = glm::translate(0.0f, -0.5f, -3.0f)*glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f)*glm::scale(2.0f,2.0f,2.0f);
glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
glm::mat4 normalMatrix = glm::inverseTranspose(modelMatrix);
glClearColor(41.0f/255.0f, 71.0f/255.0f, 121.0f / 255.0f, 1.0f);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
MSG msg;
while (true)
{
if (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
{
if (msg.message==WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glUseProgram(gpuProgram.mProgram);
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1,GL_FALSE, glm::value_ptr(modelMatrix));
glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, identity);
glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
glUniform4fv(gpuProgram.GetLocation("U_AmbientLightColor"), 1, ambientLightColor);
glUniform4fv(gpuProgram.GetLocation("U_AmbientMaterial"), 1, ambientMaterial);
glUniform4fv(gpuProgram.GetLocation("U_DiffuseLightColor"), 1, diffuseLightColor);
glUniform4fv(gpuProgram.GetLocation("U_DiffuseMaterial"), 1, diffuseMaterial);
glUniform4fv(gpuProgram.GetLocation("U_SpecularLightColor"), 1, specularLightColor);
glUniform4fv(gpuProgram.GetLocation("U_SpecularMaterial"), 1, specularMaterial);
glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos);
glUniform4fv(gpuProgram.GetLocation("U_LightDirection"), 1, spotLightDirection);
glUniform1f(gpuProgram.GetLocation("U_Cutoff"),spotLightCutoff);
model.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
model.Draw();
glUseProgram(0);
glFinish();
SwapBuffers(dc);
}
return 0;
}