基于Qt的OpenGL编程(3.x以上GLSL可编程管线版)---(十四)多光源

Vries的原教程地址如下,https://learnopengl-cn.github.io/02%20Lighting/06%20Multiple%20lights/ 关于多光源的参数详情设置请看这个教程,本篇旨在对Vires基于visual studio的编程思想做Qt平台的移植,重在记录自身学习之用)

 

 

Qt开发平台:5.8.0

编译器:Desktop Qt 5.8.0 MSVC2015_64bit

 

本篇是对上篇三种投光物,平行光,点光源,手电筒的汇总,将三种类型的光源一同使用。

 

如下图所示:由一道平行光,四个点光源,一个手电筒光,叠加而成。

基于Qt的OpenGL编程(3.x以上GLSL可编程管线版)---(十四)多光源_第1张图片

项目组织管理如下:

基于Qt的OpenGL编程(3.x以上GLSL可编程管线版)---(十四)多光源_第2张图片

主要还是学习在shader中如何写函数,其实是与写c语言函数是一样的。

cube.vert    

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

out vec3 Normal;
out vec3 FragPos;
out vec2 TexCoords;

void main(){
  gl_Position = projection * view * model * vec4(aPos, 1.0f);
  Normal = mat3(transpose(inverse(model))) * aNormal;//预防scale对法线的干扰
  FragPos = vec3(model * vec4(aPos, 1.0f));
  TexCoords = aTexCoords;
}

 

cube.frag

 

#version 330 core
#define NR_POINT_LIGHTS 4
struct Material{
  sampler2D diffuse;
  sampler2D specular;
  float shininess;
};

struct DirLight{
  vec3 direction;
  vec3 ambient;
  vec3 diffuse;
  vec3 specular;
};

struct PointLight {
  vec3 position;

  float constant;
  float linear;
  float quadratic;

  vec3 ambient;
  vec3 diffuse;
  vec3 specular;
};

struct SpotLight{
  vec3 position;
  vec3 direction;//光源方向

  vec3 ambient;
  vec3 diffuse;
  vec3 specular;

  float constant;
  float linear;
  float quadratic;

  float cutOff;
  float outerCutOff;
};

vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);

uniform Material material;
uniform DirLight dirLight;
uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform SpotLight spotLight;
uniform vec3 viewPos;

out vec4 FragColor;
in vec2 TexCoords;
in vec3 Normal;
in vec3 FragPos;

void main(){
		// 属性
		vec3 norm = normalize(Normal);
		vec3 viewDir = normalize(viewPos - FragPos);

		// 第一阶段:定向光照
		vec3 result = CalcDirLight(dirLight, norm, viewDir);

		// 第二阶段:点光源
		for(int i = 0; i < NR_POINT_LIGHTS; i++)
				result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);

			//第三阶段:聚光
		result += CalcSpotLight(spotLight, norm, FragPos, viewDir);

		FragColor = vec4(result, 1.0);
}

vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir){
		vec3 lightDir = normalize(-light.direction);

		// 漫反射着色
		float diff = max(dot(normal, lightDir), 0.0);

		// 镜面光着色
		vec3 reflectDir = reflect(-lightDir, normal);
		float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);

		// 合并结果
		vec3 ambient  = light.ambient  * vec3(texture2D(material.diffuse, TexCoords));
		vec3 diffuse  = light.diffuse  * diff * vec3(texture2D(material.diffuse, TexCoords));
		vec3 specular = light.specular * spec * vec3(texture2D(material.specular, TexCoords));

		return (ambient + diffuse + specular);
}

vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir){
		vec3 lightDir = normalize(light.position - fragPos);

		// 漫反射着色
		float diff = max(dot(normal, lightDir), 0.0);

		// 镜面光着色
		vec3 reflectDir = reflect(-lightDir, normal);
		float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);

		// 衰减
		float distance = length(light.position - fragPos);
		float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));

		// 合并结果
		vec3 ambient  = light.ambient  * vec3(texture2D(material.diffuse, TexCoords));
		vec3 diffuse  = light.diffuse  * diff * vec3(texture2D(material.diffuse, TexCoords));
		vec3 specular = light.specular * spec * vec3(texture2D(material.specular, TexCoords));
		ambient  *= attenuation;
		diffuse  *= attenuation;
		specular *= attenuation;

		return (ambient + diffuse + specular);
}

vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir){
		//ambient
		vec3 ambient = light.ambient * vec3(texture2D(material.diffuse, TexCoords));

		//diffuse
		vec3 norm = normalize(Normal);
		vec3 lightDir = normalize(light.position - fragPos);//固定点光源
		float diff = max(dot(norm, lightDir), 0.0f);
		vec3 diffuse = light.diffuse * (diff * vec3(texture2D(material.diffuse, TexCoords)));

		//specular
		vec3 reflectDir = reflect(-lightDir, norm);
		float spec = pow(max(dot(viewDir, reflectDir), 0.0f), material.shininess);
		vec3 specular = light.specular * (spec * vec3(texture2D(material.specular, TexCoords)));

		//all
		vec3 result =  ambient + diffuse + specular;

		// 衰减
		float distance = length(light.position - fragPos);
		float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));

		//手电筒
		float theta = dot(lightDir, normalize(-light.direction));
		float epsilon = light.cutOff - light.outerCutOff;
		float intensity = clamp((theta - light.outerCutOff)/epsilon, 0.0f, 1.0f);

		return result * intensity * attenuation;
}

oglmanager.cpp

#include "oglmanager.h"
#include 
#include 
#include "resourcemanager.h"
#include "cube.h"
#include 

const QVector3D CAMERA_POSITION(0.0f, 0.0f, 3.0f);
const QVector3D LIGHT_POSITION(0.0f, 2.0f, 0.0f);

Cube *cube;

OGLManager::OGLManager(GLuint w, GLuint h){
  this->width = w;
  this->height = h;
  for(GLuint i = 0; i != 1024; ++i)
    keys[i] = GL_FALSE;

}

OGLManager::~OGLManager(){
  delete this->camera;
  ResourceManager::clear();
}

QVector3D pointLightPositions[] = {
    QVector3D( 0.7f,  0.2f,  2.0f),
    QVector3D( 2.3f, -3.3f, -4.0f),
    QVector3D(-4.0f,  2.0f, -12.0f),
    QVector3D( 0.0f,  0.0f, -3.0f)
};

void OGLManager::init(){
  cube = new Cube();

  this->camera = new Camera(CAMERA_POSITION);
  cube->init();
  core = QOpenGLContext::currentContext()->versionFunctions();

  ResourceManager::loadShader("cube", ":/shaders/res/shaders/cube.vert", ":/shaders/res/shaders/cube.frag");
  ResourceManager::loadShader("light", ":/shaders/res/shaders/light.vert", ":/shaders/res/shaders/light.frag");

  ResourceManager::getShader("cube").use().setInteger("material.diffuse", 0);
  ResourceManager::getShader("cube").use().setInteger("material.specular", 1);
  ResourceManager::getShader("cube").use().setFloat("material.shininess", 64.0f);

  //平行光
  ResourceManager::getShader("cube").use().setVector3f("dirLight.direction", QVector3D(-0.2f, -1.0f, -0.3f));
  ResourceManager::getShader("cube").use().setVector3f("dirLight.ambient", QVector3D(0.05f, 0.05f, 0.05f));
  ResourceManager::getShader("cube").use().setVector3f("dirLight.diffuse", QVector3D(0.05f, 0.05f, 0.05f));
  ResourceManager::getShader("cube").use().setVector3f("dirLight.specular", QVector3D(0.5f, 0.5f, 0.5f));

  //点光源 1
  ResourceManager::getShader("cube").use().setVector3f("pointLights[0].position", pointLightPositions[0]);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[0].ambient", QVector3D(0.05f, 0.05f, 0.05f));
  ResourceManager::getShader("cube").use().setVector3f("pointLights[0].diffuse", QVector3D(0.8f, 0.8f, 0.8f));
  ResourceManager::getShader("cube").use().setVector3f("pointLights[0].specular", QVector3D(1.0f, 1.0f, 1.0f));
  ResourceManager::getShader("cube").use().setFloat("pointLights[0].constant", 1.0f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[0].linear", 0.09f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[0].quadratic", 0.032f);


  //点光源 2
  ResourceManager::getShader("cube").use().setVector3f("pointLights[1].position", pointLightPositions[1]);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[1].ambient", QVector3D(0.05f, 0.05f, 0.05f));
  ResourceManager::getShader("cube").use().setVector3f("pointLights[1].diffuse", QVector3D(0.8f, 0.8f, 0.8f));
  ResourceManager::getShader("cube").use().setVector3f("pointLights[1].specular", QVector3D(1.0f, 1.0f, 1.0f));
  ResourceManager::getShader("cube").use().setFloat("pointLights[1].constant", 1.0f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[1].linear", 0.09f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[1].quadratic", 0.032f);

  //点光源 3
  ResourceManager::getShader("cube").use().setVector3f("pointLights[2].position", pointLightPositions[2]);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[2].ambient", QVector3D(0.05f, 0.05f, 0.05f));
  ResourceManager::getShader("cube").use().setVector3f("pointLights[2].diffuse", QVector3D(0.8f, 0.8f, 0.8f));
  ResourceManager::getShader("cube").use().setVector3f("pointLights[2].specular", QVector3D(1.0f, 1.0f, 1.0f));
  ResourceManager::getShader("cube").use().setFloat("pointLights[2].constant", 1.0f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[2].linear", 0.09f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[2].quadratic", 0.032f);

  //点光源 4
  ResourceManager::getShader("cube").use().setVector3f("pointLights[3].position", pointLightPositions[3]);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[3].ambient", QVector3D(0.05f, 0.05f, 0.05f));
  ResourceManager::getShader("cube").use().setVector3f("pointLights[3].diffuse", QVector3D(0.8f, 0.8f, 0.8f));
  ResourceManager::getShader("cube").use().setVector3f("pointLights[3].specular", QVector3D(1.0f, 1.0f, 1.0f));
  ResourceManager::getShader("cube").use().setFloat("pointLights[3].constant", 1.0f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[3].linear", 0.09f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[3].quadratic", 0.032f);

  //聚光灯
  ResourceManager::getShader("cube").use().setVector3f("spotLight.position", camera->position);
  ResourceManager::getShader("cube").use().setVector3f("spotLight.direction", camera->front);
  ResourceManager::getShader("cube").use().setVector3f("spotLight.ambient", QVector3D(0.0f, 0.0f, 0.0f));
  ResourceManager::getShader("cube").use().setVector3f("spotLight.diffuse", QVector3D(1.0f, 1.0f, 1.0f));
  ResourceManager::getShader("cube").use().setVector3f("spotLight.specular", QVector3D(1.0f, 1.0f, 1.0f));
  ResourceManager::getShader("cube").use().setFloat("spotLight.constant", 1.0f);
  ResourceManager::getShader("cube").use().setFloat("spotLight.linear", 0.09f);
  ResourceManager::getShader("cube").use().setFloat("spotLight.quadratic", 0.032f);
  ResourceManager::getShader("cube").use().setFloat("spotLight.cutOff", cos(12.5f/180.0f*3.14));
  ResourceManager::getShader("cube").use().setFloat("spotLight.outerCutOff", cos(15.5f/180.0f*3.14));

  QMatrix4x4 model;
  ResourceManager::getShader("cube").use().setMatrix4f("model", model);

  //开启状态
  core->glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
  core->glEnable(GL_DEPTH_TEST);
}

void OGLManager::processInput(GLfloat dt){
  if (keys[Qt::Key_W])
    camera->processKeyboard(FORWARD, dt);
  if (keys[Qt::Key_S])
    camera->processKeyboard(BACKWARD, dt);
  if (keys[Qt::Key_A])
    camera->processKeyboard(LEFT, dt);
  if (keys[Qt::Key_D])
    camera->processKeyboard(RIGHT, dt);
  if (keys[Qt::Key_E])
    camera->processKeyboard(UP, dt);
  if (keys[Qt::Key_Q])
    camera->processKeyboard(DOWN, dt);
}


void OGLManager::update(GLfloat dt){
  QMatrix4x4 projection, model;
  projection.perspective(camera->zoom, (GLfloat)width/(GLfloat)height, 0.1f, 200.f);

  ResourceManager::getShader("cube").use().setMatrix4f("projection", projection);
  ResourceManager::getShader("cube").use().setMatrix4f("view", camera->getViewMatrix());
  ResourceManager::getShader("cube").use().setVector3f("viewPos", camera->position);

  ResourceManager::getShader("light").use().setMatrix4f("projection", projection);
  ResourceManager::getShader("light").use().setMatrix4f("view", camera->getViewMatrix());
}

void OGLManager::resize(GLuint w, GLuint h){
  core->glViewport(0, 0, w, h);
}

QVector3D cubePositions[] = {
    QVector3D( 0.0f,  0.0f,  0.0f),
    QVector3D( 2.0f,  5.0f, -15.0f),
    QVector3D(-1.5f, -2.2f, -2.5f),
    QVector3D(-3.8f, -2.0f, -12.3f),
    QVector3D( 2.4f, -0.4f, -3.5f),
    QVector3D(-1.7f,  3.0f, -7.5f),
    QVector3D( 1.3f, -2.0f, -2.5f),
    QVector3D( 1.5f,  2.0f, -2.5f),
    QVector3D( 1.5f,  0.2f, -1.5f),
    QVector3D(-1.3f,  1.0f, -1.5f)
};

void OGLManager::draw(GLfloat dt)
{
  core->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  ResourceManager::getShader("light").use();
  for (unsigned int i = 0; i < 4; i++){
    QMatrix4x4 model;
    model.translate(pointLightPositions[i]);
    model.scale(0.2f);
    ResourceManager::getShader("light").use().setMatrix4f("model", model);

    cube->drawLight();
  }

  for (unsigned int i = 0; i < 10; i++){
    QMatrix4x4 model;
    model.translate(cubePositions[i]);
    GLfloat angle = 20.0f * i;
    model.rotate(angle, QVector3D(1.0f, 0.3f, 0.5f));
    ResourceManager::getShader("cube").use().setMatrix4f("model", model);

    cube->drawCube();
  }
}

 

稍稍修改参数设置,就可以得到其他效果:

 

基于Qt的OpenGL编程(3.x以上GLSL可编程管线版)---(十四)多光源_第3张图片

这里注意,对于点光源发绿光,只要很简单的修改

light.frag

#version 330 core
out vec4 FragColor;

void main()
{
  FragColor = vec4(0.0f, 1.0f, 0.0f, 1.0f);
}

其余具体参数设置

oglmanager.cpp

....................
QVector3D pointLightColors[] = {
    QVector3D(0.4f, 0.7f, 0.1f),
    QVector3D(0.4f, 0.7f, 0.1f),
    QVector3D(0.4f, 0.7f, 0.1f),
    QVector3D(0.4f, 0.7f, 0.1f)
};

void OGLManager::init(){
.................................

  //平行光
  ResourceManager::getShader("cube").use().setVector3f("dirLight.direction", QVector3D(-0.2f, -1.0f, -0.3f));
  ResourceManager::getShader("cube").use().setVector3f("dirLight.ambient", QVector3D(0.5f, 0.5f, 0.5f));
  ResourceManager::getShader("cube").use().setVector3f("dirLight.diffuse", QVector3D(1.0f, 1.0f, 1.0f));
  ResourceManager::getShader("cube").use().setVector3f("dirLight.specular", QVector3D(1.0f, 1.0f, 1.0f));

  //点光源 1
  ResourceManager::getShader("cube").use().setVector3f("pointLights[0].position", pointLightPositions[0]);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[0].ambient", pointLightColors[0] * 0.1f);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[0].diffuse", pointLightColors[0]);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[0].specular", pointLightColors[0]);
  ResourceManager::getShader("cube").use().setFloat("pointLights[0].constant", 1.0f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[0].linear", 0.07f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[0].quadratic", 0.017f);


  //点光源 2
  ResourceManager::getShader("cube").use().setVector3f("pointLights[1].position", pointLightPositions[1]);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[1].ambient", pointLightColors[1] * 0.1f);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[1].diffuse", pointLightColors[1]);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[1].specular", pointLightColors[1]);
  ResourceManager::getShader("cube").use().setFloat("pointLights[1].constant", 1.0f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[1].linear", 0.07f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[1].quadratic", 0.017f);

  //点光源 3
  ResourceManager::getShader("cube").use().setVector3f("pointLights[2].position", pointLightPositions[2]);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[2].ambient", pointLightColors[2] * 0.1f);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[2].diffuse", pointLightColors[2]);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[2].specular", pointLightColors[2]);
  ResourceManager::getShader("cube").use().setFloat("pointLights[2].constant", 1.0f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[2].linear", 0.07f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[2].quadratic", 0.017f);

  //点光源 4
  ResourceManager::getShader("cube").use().setVector3f("pointLights[3].position", pointLightPositions[3]);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[3].ambient",  pointLightColors[3] * 0.1f);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[3].diffuse", pointLightColors[3]);
  ResourceManager::getShader("cube").use().setVector3f("pointLights[3].specular", pointLightColors[3]);
  ResourceManager::getShader("cube").use().setFloat("pointLights[3].constant", 1.0f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[3].linear", 0.07f);
  ResourceManager::getShader("cube").use().setFloat("pointLights[3].quadratic", 0.017f);

  //聚光灯
  ResourceManager::getShader("cube").use().setVector3f("spotLight.position", camera->position);
  ResourceManager::getShader("cube").use().setVector3f("spotLight.direction", camera->front);
  ResourceManager::getShader("cube").use().setVector3f("spotLight.ambient", QVector3D(0.0f, 0.0f, 0.0f));
  ResourceManager::getShader("cube").use().setVector3f("spotLight.diffuse", QVector3D(0.0f, 1.0f, 0.0f));
  ResourceManager::getShader("cube").use().setVector3f("spotLight.specular", QVector3D(0.0f, 1.0f, 0.0f));
  ResourceManager::getShader("cube").use().setFloat("spotLight.constant", 1.0f);
  ResourceManager::getShader("cube").use().setFloat("spotLight.linear", 0.07f);
  ResourceManager::getShader("cube").use().setFloat("spotLight.quadratic", 0.017f);
  ResourceManager::getShader("cube").use().setFloat("spotLight.cutOff", cos(7.0f/180.0f*3.14));
  ResourceManager::getShader("cube").use().setFloat("spotLight.outerCutOff", cos(10.0f/180.0f*3.14));

  core->glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
..............
}

 

你可能感兴趣的:(现代OpenGL学习教程)