未完成!!!!
(Vries的原教程地址如下,
https://learnopengl-cn.github.io/05%20Advanced%20Lighting/04%20Normal%20Mapping/ 关于法线贴图的详细知识请看这个教程,本篇旨在对Vires基于visual studio的编程思想做Qt平台的移植,重在记录自身学习之用)
一.法线贴图
普通法线 法线贴图
可以看出将在bline-phone式光照模型中的法线计算,由buffer赋值,改为法线贴图后,渲染效果有一个质的提升。
原图 法线贴图
发现贴图本质是将法线的xyz三坐标转换为纹理的rgb三元素,而多数图像的法线初始状态下是指向z轴的,所以大多数法线贴图显蓝色。
项目组织如下:
按“b”键进行法线计算方法的切换。
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 sampler2D normalMap;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform bool isFlag;
void main(){
vec3 normal;
if(isFlag){
normal = texture2D(normalMap, fs_in.TexCoords).rgb;
normal = normalize(normal * 2.0 - 1.0);
}else{
normal = normalize(fs_in.Normal);
}
vec3 color = texture2D(floorTexture, fs_in.TexCoords).rgb;
//ambient;
vec3 ambient = color * 0.05f;
//diffuse;
vec3 lightDir = normalize(lightPos - fs_in.FragPos);
float diff = max(dot(lightDir, normal), 0.0f);
vec3 diffuse = color * diff * 0.4f;
//specular;
vec3 viewDir = normalize(viewPos - fs_in.FragPos);
vec3 halfwayDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(normal, halfwayDir), 0.0f), 32.0f);
vec3 specular = color * spec * 0.8f;
//all
FragColor = vec4(ambient + diffuse + specular, 1.0f);
}
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.3f, 0.3f, 0.8f);
GLboolean isFlag = true;
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().setInteger("normalMap", 1);
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])
isFlag = !isFlag;
}
//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("isFlag", isFlag);
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();
}
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.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.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("brickwall", ":/textures/res/textures/brickwall.jpg");
ResourceManager::loadTexture("brickwall_normal", ":/textures/res/textures/brickwall_normal_mapping.png");
}
void Cube::drawCube(){
core->glActiveTexture(GL_TEXTURE0);
ResourceManager::getTexture("brickwall").bind();
core->glActiveTexture(GL_TEXTURE1);
ResourceManager::getTexture("brickwall_normal").bind();
core->glBindVertexArray(cubeVAO);
core->glDrawArrays(GL_TRIANGLES, 0, 6);
}
void Cube::drawLight(){
core->glBindVertexArray(lightVAO);
core->glDrawArrays(GL_TRIANGLES, 0, 36);
}
/*待补充*/