基于Qt的OpenGL编程(3.x以上GLSL可编程管线版)---(十一)材质

Vries的原教程地址如下,https://learnopengl-cn.github.io/02%20Lighting/03%20Materials/ 关于OpenGL函数的详细解析及OpenGL关于光照材质的知识点详情描述请看这个教程,本篇旨在对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张图片

简单修改cube.frag与oglmanager.cpp中对shader的属性设置

cube.frag

添加两个struct,Material与Light,后期在oglmanager中修改较为方便

#version 330 core
struct Material{
  vec3 ambient;
  vec3 diffuse;
  vec3 specular;
  float shininess;
};

struct Light{
  vec3 position;

  vec3 ambient;
  vec3 diffuse;
  vec3 specular;
};

out vec4 FragColor;

uniform Material material;
uniform Light light;

uniform vec3 viewPos;

in vec3 Normal;
in vec3 FragPos;

void main()
{
  //ambient
  vec3 ambient = light.ambient * material.ambient;

  //diffuse
  vec3 norm = normalize(Normal);
  vec3 lightDir = normalize(light.position - FragPos);
  float diff = max(dot(norm, lightDir), 0.0f);
  vec3 diffuse = light.diffuse * (diff * material.diffuse);

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

  //all
  vec3 result =  ambient + diffuse + specular;
  FragColor = vec4(result, 1.0f);

}

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对法线的干扰
}

oglmanager.cpp

#include "oglmanager.h"
#include 
#include 
#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();

  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("material.ambient", QVector3D(1.0f, 0.5f, 0.31f));
  ResourceManager::getShader("cube").use().setVector3f("material.diffuse", QVector3D(1.0f, 0.5f, 0.31f));
  ResourceManager::getShader("cube").use().setVector3f("material.specular", QVector3D(0.5f, 0.5f, 0.5f));
  ResourceManager::getShader("cube").use().setFloat("material.shininess", 32.0f);

  ResourceManager::getShader("cube").use().setVector3f("light.ambient", QVector3D(0.2f, 0.2f, 0.2f));
  ResourceManager::getShader("cube").use().setVector3f("light.diffuse", QVector3D(0.5f, 0.5f, 0.5f));
  ResourceManager::getShader("cube").use().setVector3f("light.specular", QVector3D(1.0f, 1.0f, 1.0f));
  ResourceManager::getShader("cube").use().setVector3f("light.position", 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();
}

 

二.有意思的属性设置

 

基于Qt的OpenGL编程(3.x以上GLSL可编程管线版)---(十一)材质_第3张图片

一个颜色不停改变的小盒子,只要在循环里不断修改shader中的颜色属性,很容易就可以实现。

基于Qt的OpenGL编程(3.x以上GLSL可编程管线版)---(十一)材质_第4张图片

我选择在继承有QOpenGLWidget类的DeskTop中,用update()函数不断调用paintGL()函数来仿照visual studio的消息循环机制。

所以我将 不断修改shader颜色属性的过程,放在类oglmanager的update()函数完成。因为并没有往oglmanager类中传送当前程序运行时间,即

time.elapsed()

函数,所以在类中使用一个全局变量time来累计时间。

oglmanager.cpp

#include "oglmanager.h"
#include 
#include 
#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();

  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("material.ambient", QVector3D(1.0f, 0.5f, 0.31f));
  ResourceManager::getShader("cube").use().setVector3f("material.diffuse", QVector3D(1.0f, 0.5f, 0.31f));
  ResourceManager::getShader("cube").use().setVector3f("material.specular", QVector3D(0.5f, 0.5f, 0.5f));
  ResourceManager::getShader("cube").use().setFloat("material.shininess", 32.0f);

//  ResourceManager::getShader("cube").use().setVector3f("light.ambient", QVector3D(0.2f, 0.2f, 0.2f));
//  ResourceManager::getShader("cube").use().setVector3f("light.diffuse", QVector3D(0.5f, 0.5f, 0.5f));
  ResourceManager::getShader("cube").use().setVector3f("light.specular", QVector3D(1.0f, 1.0f, 1.0f));
  ResourceManager::getShader("cube").use().setVector3f("light.position", 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);
}

GLfloat time = 0.0f;

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

  QVector3D lightColor, ambientColor, diffuseColor;
  time += dt * 0.08f;
  lightColor.setX(sin(time * 2.0f));
  lightColor.setY(sin(time * 0.7f));
  lightColor.setZ(sin(time * 1.3f));

  diffuseColor = lightColor * 0.5f;
  ambientColor = diffuseColor * 0.3f;

  ResourceManager::getShader("cube").use().setVector3f("light.ambient", ambientColor);
  ResourceManager::getShader("cube").use().setVector3f("light.diffuse", diffuseColor);
}

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

 

 

 

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