(Vries的原教程地址如下,https://learnopengl-cn.github.io/05%20Advanced%20Lighting/01%20Advanced%20Lighting/ 关于blinn光照详细内容看这个教程,本篇旨在对Vires基于visual studio的编程思想做Qt平台的移植,重在记录自身学习之用)
Qt开发平台:5.8.0
编译器:Desktop Qt 5.8.0 MSVC2015_64bit
本篇内容 简要介绍blinn光照。
blinn光照与phone式光照非常相似,只修改了phone式中的镜面光specular部分。
phone式光照 blinn光照
木头纹理
一.blinn原理
在phone式光照中,specular的计算,用的是light关于法线的反射光reflectDir与视线viewDir的点乘。
这样的话,当视线和灯光在一个方向时,如第二幅图所示,如果仍按原来方法,算出来的点乘就会使负的,为了解决这个问题,blinn提出一种新的计算方法。
blinn改用视线viewDir与光源方向lightDir的矢量和,与法线之间的夹角点乘作为phone式光照中specular的替换。解决了这个问题。
按“b”键进行phone式与blinn光照的替换
项目组织如下:
重点修改五个文件:
cube.vert
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
out VS_OUT{
vec3 Normal;
vec3 FragPos;
vec2 TexCoords;
}vs_out;
uniform mat4 view;
uniform mat4 projection;
void main(){
gl_Position = projection * view * vec4(aPos, 1.0f);
vs_out.FragPos = aPos;
vs_out.Normal = aNormal;
vs_out.TexCoords = aTexCoords;
}
cube.frag
#version 330 core
out vec4 FragColor;
in VS_OUT {
vec3 Normal;
vec3 FragPos;
vec2 TexCoords;
}fs_in;
uniform sampler2D floorTexture;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform bool blinn;
void main(){
vec3 color = texture2D(floorTexture, fs_in.TexCoords).rgb;
//ambient;
vec3 ambient = color * 0.05f;
//diffuse;
vec3 lightDir = normalize(lightPos - fs_in.FragPos);
vec3 normal = normalize(fs_in.Normal);
float diff = max(dot(lightDir, normal), 0.0f);
vec3 diffuse = color * diff * 0.1f;
//specular;
vec3 viewDir = normalize(viewPos - fs_in.FragPos);
float spec = 0.0f;
if(blinn){
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0f), 32.0f);
}else{
vec3 reflectDir = reflect(-lightDir, normal);
spec = pow(max(dot(viewDir, reflectDir), 0.0f), 8.0f);
}
vec3 specular = color * spec * 0.3f;
//all
FragColor = vec4(ambient + diffuse + specular, 1.0f);
}
cube.h
#ifndef CUBE_H
#define CUBE_H
#include <QOpenGLFunctions_3_3_Core>
class Cube
{
public:
Cube();
~Cube();
void init();
void drawLight();
void drawCube();
private:
QOpenGLFunctions_3_3_Core *core;
GLuint cubeVBO, lightVBO, cubeVAO, lightVAO;
};
#endif // CUBE_H
cube.cpp
#include "cube.h"
#include "resourcemanager.h"
Cube::Cube(){
}
Cube::~Cube(){
core->glDeleteVertexArrays(1, &cubeVAO);
core->glDeleteVertexArrays(1, &lightVAO);
}
void Cube::init(){
core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
float cubeVertices[] = {
// positions // normals // texture coords
-0.5f, 0.0f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.0f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.0f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
float lightVertices[] = {
// positions // normals // texture coords
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f
};
core->glGenVertexArrays(1, &cubeVAO);//两个参数,第一个为需要创建的缓存数量。第二个为用于存储单一ID或多个ID的GLuint变量或数组的地址
core->glGenVertexArrays(1, &lightVAO);
core->glGenBuffers(1, &cubeVBO);
core->glGenBuffers(1, &lightVBO);
core->glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
core->glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
core->glBindVertexArray(cubeVAO);
core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
core->glEnableVertexAttribArray(0);
core->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3*sizeof(float)));
core->glEnableVertexAttribArray(1);
core->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6*sizeof(float)));
core->glEnableVertexAttribArray(2);
core->glBindVertexArray(0);
core->glBindBuffer(GL_ARRAY_BUFFER, lightVBO);
core->glBufferData(GL_ARRAY_BUFFER, sizeof(lightVertices), lightVertices, GL_STATIC_DRAW);
core->glBindVertexArray(lightVAO);
core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
core->glEnableVertexAttribArray(0);
core->glBindVertexArray(0);
core->glDeleteBuffers(1, &cubeVBO);
core->glDeleteBuffers(1, &lightVBO);
ResourceManager::loadTexture("wood", ":/textures/res/textures/wood.jpg");
}
void Cube::drawCube(){
core->glActiveTexture(GL_TEXTURE0);
ResourceManager::getTexture("wood").bind();
core->glBindVertexArray(cubeVAO);
core->glDrawArrays(GL_TRIANGLES, 0, 6);
}
void Cube::drawLight(){
core->glBindVertexArray(lightVAO);
core->glDrawArrays(GL_TRIANGLES, 0, 36);
}
oglmanager.cpp
#include "oglmanager.h"
#include <QKeyEvent>
#include <QDebug>
#include "resourcemanager.h"
#include "cube.h"
const QVector3D CAMERA_POSITION(0.0f, 0.0f, 3.0f);
const QVector3D LIGHT_POSITION(0.0f, 0.4f, 0.0f);
GLboolean blinn = false;
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();
}
void OGLManager::init(){
cube = new Cube();
this->camera = new Camera(CAMERA_POSITION);
cube->init();
core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
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("floorTexture", 0);
ResourceManager::getShader("cube").use().setVector3f("lightPos", LIGHT_POSITION);
QMatrix4x4 model;
model.translate(LIGHT_POSITION);
model.scale(0.1f);
ResourceManager::getShader("light").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);
if (keys[Qt::Key_B])
blinn = !blinn;
}
//GLfloat time = 0.0f;
void OGLManager::update(GLfloat dt){
QMatrix4x4 projection;
projection.perspective(camera->zoom, (GLfloat)width/(GLfloat)height, 0.1f, 200.f);
ResourceManager::getShader("cube").use().setMatrix4f("projection", projection);
ResourceManager::getShader("cube").use().setVector3f("viewPos", camera->position);
ResourceManager::getShader("cube").use().setMatrix4f("view", camera->getViewMatrix());
ResourceManager::getShader("cube").use().setBool("blinn", blinn);
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);
}
void OGLManager::draw(GLfloat dt)
{
core->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ResourceManager::getShader("cube").use();
cube->drawCube();
ResourceManager::getShader("light").use();
cube->drawLight();
}