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

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


Qt开发平台:5.8.0

编译器:Desktop Qt 5.8.0 MSVC2015_64bit


一,漫反射贴图

其实就是应用纹理,将纹理放进shader着色器中,用光照效果渲染。




项目组织如下:


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
struct Material{
  sampler2D diffuse;
  vec3 specular;
  float shininess;
};

struct Light{
  vec3 position;

  vec3 ambient;
  vec3 diffuse;
  vec3 specular;
};

out vec4 FragColor;
in vec2 TexCoords;

uniform Material material;
uniform Light light;

uniform vec3 viewPos;

in vec3 Normal;
in vec3 FragPos;

void main()
{
  //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 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.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 VBO, 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 vertices[] = {
      // positions          // normals           // texture coords
      -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  0.0f, 0.0f,
       0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  1.0f, 0.0f,
       0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  1.0f, 1.0f,
       0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  1.0f, 1.0f,
      -0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  0.0f, 1.0f,
      -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  0.0f, 0.0f,

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

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

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

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

      -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,  0.0f, 1.0f,
       0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,  1.0f, 1.0f,
       0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,  1.0f, 0.0f,
       0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,  1.0f, 0.0f,
      -0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,  0.0f, 0.0f,
      -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,  0.0f, 1.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, 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->glBindVertexArray(lightVAO);
    core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
    core->glEnableVertexAttribArray(0);
  core->glBindVertexArray(0);

  core->glDeleteBuffers(1, &VBO);

  ResourceManager::loadTexture("container2", ":/textures/res/textures/container2.png");
}

void Cube::drawCube(){
  core->glActiveTexture(GL_TEXTURE0);
  ResourceManager::getTexture("container2").bind();

  core->glBindVertexArray(cubeVAO);
  core->glDrawArrays(GL_TRIANGLES, 0, 36);
}

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

oglmanager.h

#ifndef OGLMANAGER_H
#define OGLMANAGER_H

#include <QOpenGLFunctions_3_3_Core>
#include "camera.h"
#include "shader.h"
#include <QOpenGLTexture>
#include "texture2d.h"

class OGLManager
{
public:
    OGLManager(GLuint w, GLuint h);
    ~OGLManager();

    void init();
    void processInput(GLfloat dt);
    void update(GLfloat dt);
    void resize(GLuint w, GLuint h);
    void draw(GLfloat dt);


    GLboolean keys[1024];
    Camera *camera;
private:
    QOpenGLFunctions_3_3_Core *core;
    GLuint width;
    GLuint height;


};

#endif // OGLMANAGER_H

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("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().setInteger("material.diffuse", 0);
  ResourceManager::getShader("cube").use().setVector3f("material.specular", QVector3D(0.5f, 0.5f, 0.5f));
  ResourceManager::getShader("cube").use().setFloat("material.shininess", 64.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.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);
}

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

二,镜面反射贴图

箱子纹理的木头材质按理说是不会反光的,上面的效果图看起来就不是很自然。所以为解决这个问题,仿照漫反射贴图,设置镜面反射贴图。

效果如下,左为镜面反射贴图,右为程序效果。



修改cube.frag,略微修改cube.cpp与oglmanager.cpp

cube.frag

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

struct Light{
  vec3 position;

  vec3 ambient;
  vec3 diffuse;
  vec3 specular;
};

out vec4 FragColor;
in vec2 TexCoords;

uniform Material material;
uniform Light light;

uniform vec3 viewPos;

in vec3 Normal;
in vec3 FragPos;

void main()
{
		//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 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 * vec3(texture2D(material.specular, TexCoords)));

		//all
  vec3 result =  ambient + diffuse + specular;
  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[] = {
      // positions          // normals           // texture coords
      -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  0.0f, 0.0f,
       0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  1.0f, 0.0f,
       0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  1.0f, 1.0f,
       0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  1.0f, 1.0f,
      -0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  0.0f, 1.0f,
      -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  0.0f, 0.0f,

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

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

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

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

      -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,  0.0f, 1.0f,
       0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,  1.0f, 1.0f,
       0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,  1.0f, 0.0f,
       0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,  1.0f, 0.0f,
      -0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,  0.0f, 0.0f,
      -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,  0.0f, 1.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, 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->glBindVertexArray(lightVAO);
    core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
    core->glEnableVertexAttribArray(0);
  core->glBindVertexArray(0);

  core->glDeleteBuffers(1, &VBO);

  ResourceManager::loadTexture("container2", ":/textures/res/textures/container2.png");
//  ResourceManager::loadTexture("container2_specular", ":/textures/res/textures/container2_specular.png");
  ResourceManager::loadTexture("container2_specular", ":/textures/res/textures/matrix.jpg");

}

void Cube::drawCube(){
  core->glActiveTexture(GL_TEXTURE0);
  ResourceManager::getTexture("container2").bind();
  core->glActiveTexture(GL_TEXTURE1);
  ResourceManager::getTexture("container2_specular").bind();


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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值