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

这章内容简要说明Phone式光照模型中的环境光照(ambient), 漫反射(diffuse),镜面反射(specular)如何在OpenGL中的世界实现

Vries的原教程地址如下,https://learnopengl-cn.github.io/02%20Lighting/02%20Basic%20Lighting/#_7 关于OpenGL函数的详细解析及OpenGL关于基础光照的知识点详情描述请看这个教程,本篇旨在对Vires基于visual studio的编程思想做Qt平台的移植,重在记录自身学习之用)


Qt开发平台:5.8.0

编译器:Desktop Qt 5.8.0 MSVC2015_64bit 


项目组织管理如下:


一,Ambient环境光


仅在上接教程的代码修改cube.frag即可,

cube.frag

#version 330 core
out vec4 FragColor;

uniform vec3 objectColor;
uniform vec3 lightColor;

void main()
{
  float ambientStrength = 0.1f;
  vec3 ambient = ambientStrength * lightColor;
  vec3 result = ambient * objectColor;
  FragColor = vec4(result, 1.0f);
}

二,Diffuse漫反射

主要还是要修改着色器,cube.vert与cube.frag。因为在着色器中引入了法线,所以同时略微修改cube.cpp中的数据顶点集VBO与oglmanager.cpp。

cube.vert

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

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

out vec3 Normal;
out vec3 FragPos;

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

cube.frag

#version 330 core
out vec4 FragColor;

uniform vec3 objectColor;
uniform vec3 lightColor;
uniform vec3 lightPos;

in vec3 Normal;
in vec3 FragPos;

void main()
{
  //ambient
  float ambient = 0.1f;

  //diffuse
  vec3 norm = normalize(Normal);
  vec3 lightDir = normalize(lightPos - FragPos);
  float diffuse = max(dot(norm, lightDir), 0.0f);

  //all
  vec3 result =  (ambient + diffuse) * lightColor * objectColor;
  FragColor = vec4(result, 1.0f);
}

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 vertices[] = {
      -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
       0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
       0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
       0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
      -0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
      -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,

      -0.5f, -0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
       0.5f, -0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
       0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
       0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
      -0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
      -0.5f, -0.5f,  0.5f,  0.0f,  0.0f,  1.0f,

      -0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
      -0.5f,  0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
      -0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
      -0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
      -0.5f, -0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
      -0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,

       0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
       0.5f,  0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
       0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
       0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
       0.5f, -0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
       0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,

      -0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
       0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
       0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
       0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
      -0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
      -0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,

      -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
       0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
       0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
       0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
      -0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
      -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f
  };

  core->glGenVertexArrays(1, &cubeVAO);//两个参数,第一个为需要创建的缓存数量。第二个为用于存储单一ID或多个ID的GLuint变量或数组的地址
  core->glGenVertexArrays(1, &lightVAO);
  core->glGenBuffers(1, &VBO);

  core->glBindBuffer(GL_ARRAY_BUFFER, VBO);
  core->glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

  core->glBindVertexArray(cubeVAO);
    core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
    core->glEnableVertexAttribArray(0);
    core->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float)));
    core->glEnableVertexAttribArray(1);
  core->glBindVertexArray(0);

  core->glBindVertexArray(lightVAO);
    core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
    core->glEnableVertexAttribArray(0);
  core->glBindVertexArray(0);

  core->glDeleteBuffers(1, &VBO);
}

void Cube::drawCube(){
  core->glBindVertexArray(cubeVAO);
  core->glDrawArrays(GL_TRIANGLES, 0, 36);
}

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(1.0f, 0.8f, 0.8f);

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().setVector3f("objectColor", QVector3D(1.0f, 0.5f, 0.31f));
  ResourceManager::getShader("cube").use().setVector3f("lightColor", QVector3D(1.0f, 1.0f, 1.0f));
  ResourceManager::getShader("cube").use().setVector3f("lightPos", LIGHT_POSITION);

  //开启状态
  core->glClearColor(0.2f, 0.3f, 0.3f, 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().setMatrix4f("model", model);

  model.translate(LIGHT_POSITION);
  model.scale(0.2f);
  ResourceManager::getShader("light").use().setMatrix4f("projection", projection);
  ResourceManager::getShader("light").use().setMatrix4f("view", camera->getViewMatrix());
  ResourceManager::getShader("light").use().setMatrix4f("model", model);
}

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();
}

三,specular镜面反射



修改cube.frag与oglmanager.cpp。cube.vert不作改变

cube.frag

#version 330 core
out vec4 FragColor;

uniform vec3 objectColor;
uniform vec3 lightColor;

uniform vec3 lightPos;
uniform vec3 viewPos;

in vec3 Normal;
in vec3 FragPos;

void main()
{
  //ambient
  float ambient = 0.1f;

  //diffuse
  vec3 norm = normalize(Normal);
  vec3 lightDir = normalize(lightPos - FragPos);
  float diffuse = max(dot(norm, lightDir), 0.0f);

  //specular
  vec3 viewDir = normalize(viewPos - FragPos);
  vec3 reflectDir = reflect(-lightDir, norm);
  float specularStrength = 0.5f;
  float specular = pow(max(dot(viewDir, reflectDir), 0.0f), 64) * specularStrength;

  //all
  vec3 result =  (ambient + diffuse + specular) * lightColor * objectColor;
  FragColor = vec4(result, 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(1.0f, 0.8f, 0.8f);

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().setVector3f("objectColor", QVector3D(1.0f, 0.5f, 0.31f));
  ResourceManager::getShader("cube").use().setVector3f("lightColor", QVector3D(1.0f, 1.0f, 1.0f));
  ResourceManager::getShader("cube").use().setVector3f("lightPos", LIGHT_POSITION);


  //开启状态
  core->glClearColor(0.2f, 0.3f, 0.3f, 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().setMatrix4f("model", model);
  ResourceManager::getShader("cube").use().setVector3f("viewPos", camera->position);

  model.translate(LIGHT_POSITION);
  model.scale(0.2f);
  ResourceManager::getShader("light").use().setMatrix4f("projection", projection);
  ResourceManager::getShader("light").use().setMatrix4f("view", camera->getViewMatrix());
  ResourceManager::getShader("light").use().setMatrix4f("model", model);
}

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();
}
如此,一个完整的有趣的phone式光照模型就在OpenGL中应用完成了。

猜你喜欢

转载自blog.csdn.net/z136411501/article/details/80112880